Minor change in edge event detection api

Also other minor changes and gofmt
This commit is contained in:
Drahoslav 2018-06-05 21:51:36 +02:00
parent 9e67063fd5
commit 4ae1cfd964
3 changed files with 55 additions and 47 deletions

View File

@ -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
}

23
rpio.go
View File

@ -327,28 +327,33 @@ func TogglePin(pin Pin) {
//
// 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
bit := uint32(1 << (p & 31))
if edge == NoEdge { // clear bits
gpioMem[renReg] = gpioMem[renReg] &^ (1 << (p&31))
gpioMem[fenReg] = gpioMem[fenReg] &^ (1 << (p&31))
}
if edge&RiseEdge > 0 { // set bit
gpioMem[renReg] = gpioMem[renReg] | (1 << (p&31))
gpioMem[renReg] = gpioMem[renReg] | bit
} else { // clear bit
gpioMem[renReg] = gpioMem[renReg] &^ bit
}
if edge&FallEdge > 0 { // set bit
gpioMem[fenReg] = gpioMem[fenReg] | (1 << (p&31))
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

View File

@ -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,14 +26,15 @@ func TestRisingEdgeEvent(t *testing.T) {
pin.Detect(RiseEdge)
timeout := time.After(time.Second)
loop: for {
loop:
for {
src.High()
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:
@ -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,7 +65,8 @@ func TestFallingEdgeEvent(t *testing.T) {
pin.Detect(FallEdge)
timeout := time.After(time.Second)
loop: for {
loop:
for {
src.Low()
time.Sleep(time.Second / 5)