go-rpio/rpio_test.go

194 lines
3.1 KiB
Go
Raw Permalink Normal View History

2018-06-04 15:46:49 +02:00
package rpio
import (
"os"
2018-06-04 15:46:49 +02:00
"testing"
"time"
)
func TestMain(m *testing.M) {
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)
}
defer Close()
2018-06-04 15:46:49 +02:00
os.Exit(m.Run())
}
2018-06-06 17:51:04 +02:00
func TestEvent(t *testing.T) {
2018-06-04 15:46:49 +02:00
src := Pin(3)
src.Mode(Output)
pin := Pin(2)
pin.Mode(Input)
pin.PullDown()
2018-06-06 17:51:04 +02:00
t.Run("rising edge", func(t *testing.T) {
pin.Detect(RiseEdge)
src.Low()
for i := 0; ; i++ {
src.High()
time.Sleep(time.Second / 10)
if pin.EdgeDetected() {
t.Log("edge rised")
} else {
t.Errorf("Rise event should be detected")
}
if i == 5 {
break
}
src.Low()
}
2018-06-04 15:46:49 +02:00
2018-06-06 17:51:04 +02:00
time.Sleep(time.Second / 10)
2018-06-04 15:46:49 +02:00
if pin.EdgeDetected() {
2018-06-06 17:51:04 +02:00
t.Error("Rise should not be detected, no change since last call")
2018-06-04 15:46:49 +02:00
}
2018-06-06 17:51:04 +02:00
pin.Detect(NoEdge)
src.High()
if pin.EdgeDetected() {
t.Error("Rise should not be detected, events disabled")
2018-06-04 15:46:49 +02:00
}
2018-06-06 17:51:04 +02:00
})
2018-06-04 15:46:49 +02:00
2018-06-06 17:51:04 +02:00
t.Run("falling edge", func(t *testing.T) {
pin.Detect(FallEdge)
src.High()
2018-06-04 15:46:49 +02:00
2018-06-06 17:51:04 +02:00
for i := 0; ; i++ {
src.Low()
2018-06-04 15:46:49 +02:00
2018-06-06 17:51:04 +02:00
time.Sleep(time.Second / 10)
if pin.EdgeDetected() {
t.Log("edge fallen")
} else {
t.Errorf("Fall event should be detected")
}
if i == 5 {
break
}
2018-06-04 15:46:49 +02:00
2018-06-06 17:51:04 +02:00
src.High()
}
time.Sleep(time.Second / 10)
if pin.EdgeDetected() {
t.Error("Fall should not be detected, no change since last call")
}
pin.Detect(NoEdge)
src.Low()
if pin.EdgeDetected() {
2018-06-06 17:51:04 +02:00
t.Error("Fall should not be detected, events disabled")
}
2018-06-06 17:51:04 +02:00
})
t.Run("both edges", func(t *testing.T) {
pin.Detect(AnyEdge)
src.Low()
for i := 0; i < 5; i++ {
src.High()
if pin.EdgeDetected() {
t.Log("edge detected")
} else {
t.Errorf("Rise event shoud be detected")
}
2018-06-04 15:46:49 +02:00
2018-06-06 17:51:04 +02:00
src.Low()
if pin.EdgeDetected() {
t.Log("edge detected")
} else {
t.Errorf("Fall edge should be detected")
}
}
2018-06-04 15:46:49 +02:00
2018-06-06 17:51:04 +02:00
pin.Detect(NoEdge)
src.High()
2018-06-06 17:51:04 +02:00
src.Low()
if pin.EdgeDetected() {
t.Errorf("No edge should be detected, events disabled")
}
})
}
2018-06-05 23:18:07 +02:00
2018-06-06 00:41:50 +02:00
func BenchmarkGpio(b *testing.B) {
2018-06-05 23:18:07 +02:00
src := Pin(3)
src.Mode(Output)
src.Low()
pin := Pin(2)
pin.Mode(Input)
pin.PullDown()
2018-06-06 00:41:50 +02:00
oldWrite := func(pin Pin, state State) {
p := uint8(pin)
2018-06-06 00:28:17 +02:00
2018-06-06 00:41:50 +02:00
setReg := p/32 + 7
clearReg := p/32 + 10
2018-06-06 00:28:17 +02:00
2018-06-06 00:41:50 +02:00
memlock.Lock()
defer memlock.Unlock()
2018-06-06 00:28:17 +02:00
2018-06-06 00:41:50 +02:00
if state == Low {
gpioMem[clearReg] = 1 << (p & 31)
} else {
gpioMem[setReg] = 1 << (p & 31)
2018-06-06 00:28:17 +02:00
}
2018-06-06 00:41:50 +02:00
}
2018-06-06 00:28:17 +02:00
2018-06-06 00:41:50 +02:00
oldToggle := func(pin Pin) {
switch ReadPin(pin) {
case Low:
oldWrite(pin, High)
case High:
oldWrite(pin, Low)
2018-06-06 00:28:17 +02:00
}
}
2018-06-06 00:41:50 +02:00
b.Run("write", func(b *testing.B) {
b.Run("old", func(b *testing.B) {
for i := 0; i < b.N; i++ {
if i%2 == 0 {
oldWrite(src, High)
} else {
oldWrite(src, Low)
}
}
})
b.Run("new", func(b *testing.B) {
for i := 0; i < b.N; i++ {
if i%2 == 0 {
WritePin(src, High)
} else {
WritePin(src, Low)
}
}
})
})
2018-06-06 00:28:17 +02:00
2018-06-06 00:41:50 +02:00
b.Run("toggle", func(b *testing.B) {
b.Run("old", func(b *testing.B) {
for i := 0; i < b.N; i++ {
oldToggle(src)
}
})
2018-06-06 00:28:17 +02:00
2018-06-06 00:41:50 +02:00
b.Run("new", func(b *testing.B) {
for i := 0; i < b.N; i++ {
TogglePin(src)
}
})
})
2018-06-06 00:28:17 +02:00
}