mirror of
https://github.com/stianeikeland/go-rpio.git
synced 2025-02-02 15:30:36 +01:00
Test edge events
This commit is contained in:
parent
2ec057fd07
commit
46ba7f5934
8
rpio.go
8
rpio.go
|
@ -323,12 +323,12 @@ func TogglePin(pin Pin) {
|
||||||
|
|
||||||
// Enable edge event detection on pin
|
// Enable edge event detection on pin
|
||||||
//
|
//
|
||||||
// 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.
|
|
||||||
//
|
|
||||||
// Combine with pin.EdgeDetected() to check whether event occured.
|
// Combine with pin.EdgeDetected() to check whether event occured.
|
||||||
//
|
//
|
||||||
// It also clears previously detected events of this pin if any.
|
// 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.
|
||||||
func DetectEdge(pin Pin, edge Edge) {
|
func DetectEdge(pin Pin, edge Edge) {
|
||||||
p := uint8(pin)
|
p := uint8(pin)
|
||||||
|
|
||||||
|
|
93
rpio_test.go
Normal file
93
rpio_test.go
Normal file
|
@ -0,0 +1,93 @@
|
||||||
|
package rpio
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestMain(m *testing.M) {
|
||||||
|
if err := Open(); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
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()
|
||||||
|
|
||||||
|
pin := Pin(2)
|
||||||
|
pin.Mode(Input)
|
||||||
|
pin.PullDown()
|
||||||
|
pin.Detect(RiseEdge)
|
||||||
|
|
||||||
|
timeout := time.After(time.Second)
|
||||||
|
loop: for {
|
||||||
|
src.High()
|
||||||
|
|
||||||
|
time.Sleep(time.Second/5)
|
||||||
|
if pin.EdgeDetected() {
|
||||||
|
t.Log("edge rised")
|
||||||
|
} else {
|
||||||
|
t.Errorf("Raise event should be detected")
|
||||||
|
}
|
||||||
|
select {
|
||||||
|
case <- timeout:
|
||||||
|
break loop
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
|
||||||
|
src.Low()
|
||||||
|
}
|
||||||
|
if pin.EdgeDetected() {
|
||||||
|
t.Error("Rise should not be detected, no change since last call")
|
||||||
|
}
|
||||||
|
pin.Detect(NoEdge)
|
||||||
|
src.High()
|
||||||
|
if pin.EdgeDetected() {
|
||||||
|
t.Error("Fall 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()
|
||||||
|
|
||||||
|
pin := Pin(2)
|
||||||
|
pin.Mode(Input)
|
||||||
|
pin.PullDown()
|
||||||
|
pin.Detect(FallEdge)
|
||||||
|
|
||||||
|
timeout := time.After(time.Second)
|
||||||
|
loop: for {
|
||||||
|
src.Low()
|
||||||
|
|
||||||
|
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:
|
||||||
|
}
|
||||||
|
|
||||||
|
src.High()
|
||||||
|
}
|
||||||
|
if pin.EdgeDetected() {
|
||||||
|
t.Error("Fall should not be detected, no change since last call")
|
||||||
|
}
|
||||||
|
pin.Detect(NoEdge)
|
||||||
|
src.Low()
|
||||||
|
if pin.EdgeDetected() {
|
||||||
|
t.Error("Fall should not be detected, events disabled")
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user