2018-06-04 15:46:49 +02:00
|
|
|
package rpio
|
|
|
|
|
|
|
|
import (
|
2018-06-05 21:51:36 +02:00
|
|
|
"os"
|
2018-06-04 15:46:49 +02:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
2018-06-05 21:51:36 +02:00
|
|
|
println("Note: bcm pins 2 and 3 has to be directly connected")
|
2018-06-04 15:46:49 +02:00
|
|
|
if err := Open(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-06-05 21:51:36 +02:00
|
|
|
defer Close()
|
2018-06-04 15:46:49 +02:00
|
|
|
os.Exit(m.Run())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRisingEdgeEvent(t *testing.T) {
|
|
|
|
src := Pin(3)
|
|
|
|
src.Mode(Output)
|
|
|
|
src.Low()
|
|
|
|
|
|
|
|
pin := Pin(2)
|
|
|
|
pin.Mode(Input)
|
|
|
|
pin.PullDown()
|
|
|
|
pin.Detect(RiseEdge)
|
|
|
|
|
|
|
|
timeout := time.After(time.Second)
|
2018-06-05 21:51:36 +02:00
|
|
|
loop:
|
|
|
|
for {
|
2018-06-04 15:46:49 +02:00
|
|
|
src.High()
|
|
|
|
|
2018-06-05 21:51:36 +02:00
|
|
|
time.Sleep(time.Second / 5)
|
2018-06-04 15:46:49 +02:00
|
|
|
if pin.EdgeDetected() {
|
|
|
|
t.Log("edge rised")
|
|
|
|
} else {
|
2018-06-05 21:51:36 +02:00
|
|
|
t.Errorf("Rise event should be detected")
|
2018-06-04 15:46:49 +02:00
|
|
|
}
|
|
|
|
select {
|
2018-06-05 21:51:36 +02:00
|
|
|
case <-timeout:
|
|
|
|
break loop
|
|
|
|
default:
|
2018-06-04 15:46:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
2018-06-05 21:51:36 +02:00
|
|
|
t.Error("Rise should not be detected, events disabled")
|
2018-06-04 15:46:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFallingEdgeEvent(t *testing.T) {
|
|
|
|
src := Pin(3)
|
|
|
|
src.Mode(Output)
|
|
|
|
src.High()
|
|
|
|
|
|
|
|
pin := Pin(2)
|
|
|
|
pin.Mode(Input)
|
|
|
|
pin.PullDown()
|
|
|
|
pin.Detect(FallEdge)
|
|
|
|
|
|
|
|
timeout := time.After(time.Second)
|
2018-06-05 21:51:36 +02:00
|
|
|
loop:
|
|
|
|
for {
|
|
|
|
src.Low()
|
2018-06-04 15:46:49 +02:00
|
|
|
|
2018-06-05 21:51:36 +02:00
|
|
|
time.Sleep(time.Second / 5)
|
|
|
|
if pin.EdgeDetected() {
|
|
|
|
t.Log("edge fallen")
|
|
|
|
} else {
|
|
|
|
t.Errorf("Fall event should be detected")
|
|
|
|
}
|
2018-06-04 15:46:49 +02:00
|
|
|
|
2018-06-05 21:51:36 +02:00
|
|
|
select {
|
|
|
|
case <-timeout:
|
|
|
|
break loop
|
|
|
|
default:
|
|
|
|
}
|
2018-06-04 15:46:49 +02:00
|
|
|
|
2018-06-05 21:51:36 +02:00
|
|
|
src.High()
|
2018-06-04 15:46:49 +02:00
|
|
|
}
|
|
|
|
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")
|
|
|
|
}
|
2018-06-05 21:51:36 +02:00
|
|
|
}
|