From 4ae1cfd964390dfc9e041d9d3e4a747119aa89ed Mon Sep 17 00:00:00 2001 From: Drahoslav Date: Tue, 5 Jun 2018 21:51:36 +0200 Subject: [PATCH] Minor change in edge event detection api Also other minor changes and gofmt --- examples/event/event.go | 13 ++++++----- rpio.go | 37 ++++++++++++++++------------- rpio_test.go | 52 +++++++++++++++++++++-------------------- 3 files changed, 55 insertions(+), 47 deletions(-) diff --git a/examples/event/event.go b/examples/event/event.go index 5d24403..66288c8 100644 --- a/examples/event/event.go +++ b/examples/event/event.go @@ -2,7 +2,7 @@ An example of edge event handling by @Drahoslav7, using the go-rpio library -Waits for button to be pressed before exit. +Waits for button to be pressed twice before exit. Connect a button between pin 22 and some GND pin. @@ -12,8 +12,9 @@ package main import ( "fmt" - "time" "os" + "time" + "github.com/stianeikeland/go-rpio" ) @@ -37,12 +38,12 @@ func main() { fmt.Println("press a button") - for { + for i := 0; i < 2; { if pin.EdgeDetected() { // check if event occured - fmt.Println("button pressed less than a second ago") - break + fmt.Println("button pressed") + i++ } - time.Sleep(time.Second) + time.Sleep(time.Second / 2) } pin.Detect(rpio.NoEdge) // disable edge event detection } diff --git a/rpio.go b/rpio.go index 2425a8b..d28e220 100644 --- a/rpio.go +++ b/rpio.go @@ -123,7 +123,7 @@ const ( ) // Edge events -const ( +const ( NoEdge Edge = iota RiseEdge FallEdge @@ -229,7 +229,7 @@ func (pin Pin) Detect(edge Edge) { // Check edge event on pin func (pin Pin) EdgeDetected() bool { return EdgeDetected(pin) -} +} // PinMode sets the mode (direction) of a given pin (Input, Output, Clock or Pwm) // @@ -322,33 +322,38 @@ func TogglePin(pin Pin) { } // Enable edge event detection on pin -// +// // Combine with pin.EdgeDetected() to check whether event occured. // // It also clears previously detected event of this pin if any. // -// Note that call with RiseEdge followed by call with FallEdge has the same effect as call with AnyEdge, -// to disable previously enabled event call it with NoEdge. +// Note that call with RiseEdge will disable previously set FallEdge detection and vice versa +// You have to call with AnyEdge, to enable detection for both edges +// To disable previously enabled detection call it with NoEdge func DetectEdge(pin Pin, edge Edge) { p := uint8(pin) // Rising edge detect enable register (19/20 depending on bank) // Falling edge detect enable register (22/23 depending on bank) + // Event detect status register (16/17) renReg := p/32 + 19 fenReg := p/32 + 22 + edsReg := p/32 + 16 - if edge == NoEdge { // clear bits - gpioMem[renReg] = gpioMem[renReg] &^ (1 << (p&31)) - gpioMem[fenReg] = gpioMem[fenReg] &^ (1 << (p&31)) + bit := uint32(1 << (p & 31)) + + if edge&RiseEdge > 0 { // set bit + gpioMem[renReg] = gpioMem[renReg] | bit + } else { // clear bit + gpioMem[renReg] = gpioMem[renReg] &^ bit } - if edge & RiseEdge > 0 { // set bit - gpioMem[renReg] = gpioMem[renReg] | (1 << (p&31)) - } - if edge & FallEdge > 0 { // set bit - gpioMem[fenReg] = gpioMem[fenReg] | (1 << (p&31)) + if edge&FallEdge > 0 { // set bit + gpioMem[fenReg] = gpioMem[fenReg] | bit + } else { // clear bit + gpioMem[fenReg] = gpioMem[fenReg] &^ bit } - EdgeDetected(pin) // to clear outdated detection + gpioMem[edsReg] = bit // to clear outdated detection } // Check whether edge event occured @@ -360,7 +365,7 @@ func EdgeDetected(pin Pin) bool { // Event detect status register (16/17) edsReg := p/32 + 16 - test := gpioMem[edsReg] & (1 << (p&31)) + test := gpioMem[edsReg] & (1 << (p & 31)) gpioMem[edsReg] = test // set bit to clear it return test != 0 } @@ -376,7 +381,7 @@ func PullMode(pin Pin, pull Pull) { switch pull { case PullDown, PullUp: - gpioMem[pullReg] = gpioMem[pullReg] &^ 3 | uint32(pull) + gpioMem[pullReg] = gpioMem[pullReg]&^3 | uint32(pull) case PullOff: gpioMem[pullReg] = gpioMem[pullReg] &^ 3 } diff --git a/rpio_test.go b/rpio_test.go index da18321..aed0312 100644 --- a/rpio_test.go +++ b/rpio_test.go @@ -1,20 +1,21 @@ package rpio import ( + "os" "testing" "time" - "os" ) func TestMain(m *testing.M) { + println("Note: bcm pins 2 and 3 has to be directly connected") if err := Open(); err != nil { panic(err) } + defer Close() os.Exit(m.Run()) } func TestRisingEdgeEvent(t *testing.T) { - // bcm pins 2 and 3 has to be directly connected src := Pin(3) src.Mode(Output) src.Low() @@ -25,19 +26,20 @@ func TestRisingEdgeEvent(t *testing.T) { pin.Detect(RiseEdge) timeout := time.After(time.Second) - loop: for { +loop: + for { src.High() - time.Sleep(time.Second/5) + time.Sleep(time.Second / 5) if pin.EdgeDetected() { t.Log("edge rised") } else { - t.Errorf("Raise event should be detected") + t.Errorf("Rise event should be detected") } select { - case <- timeout: - break loop - default: + case <-timeout: + break loop + default: } src.Low() @@ -48,12 +50,11 @@ func TestRisingEdgeEvent(t *testing.T) { pin.Detect(NoEdge) src.High() if pin.EdgeDetected() { - t.Error("Fall should not be detected, events disabled") + t.Error("Rise should not be detected, events disabled") } } func TestFallingEdgeEvent(t *testing.T) { - // bcm pins 2 and 3 has to be directly connected src := Pin(3) src.Mode(Output) src.High() @@ -64,23 +65,24 @@ func TestFallingEdgeEvent(t *testing.T) { pin.Detect(FallEdge) timeout := time.After(time.Second) - loop: for { - src.Low() +loop: + for { + src.Low() - time.Sleep(time.Second/5) - if pin.EdgeDetected() { - t.Log("edge fallen") - } else { - t.Errorf("Fall event should be detected") - } + time.Sleep(time.Second / 5) + if pin.EdgeDetected() { + t.Log("edge fallen") + } else { + t.Errorf("Fall event should be detected") + } - select { - case <- timeout: - break loop - default: - } + select { + case <-timeout: + break loop + default: + } - src.High() + src.High() } if pin.EdgeDetected() { t.Error("Fall should not be detected, no change since last call") @@ -90,4 +92,4 @@ func TestFallingEdgeEvent(t *testing.T) { if pin.EdgeDetected() { t.Error("Fall should not be detected, events disabled") } -} \ No newline at end of file +}