mirror of
https://github.com/stianeikeland/go-rpio.git
synced 2025-01-23 10:41:03 +01:00
Minor change in edge event detection api
Also other minor changes and gofmt
This commit is contained in:
parent
9e67063fd5
commit
4ae1cfd964
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
An example of edge event handling by @Drahoslav7, using the go-rpio library
|
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.
|
Connect a button between pin 22 and some GND pin.
|
||||||
|
|
||||||
|
@ -12,8 +12,9 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
|
||||||
"os"
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/stianeikeland/go-rpio"
|
"github.com/stianeikeland/go-rpio"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -37,12 +38,12 @@ func main() {
|
||||||
|
|
||||||
fmt.Println("press a button")
|
fmt.Println("press a button")
|
||||||
|
|
||||||
for {
|
for i := 0; i < 2; {
|
||||||
if pin.EdgeDetected() { // check if event occured
|
if pin.EdgeDetected() { // check if event occured
|
||||||
fmt.Println("button pressed less than a second ago")
|
fmt.Println("button pressed")
|
||||||
break
|
i++
|
||||||
}
|
}
|
||||||
time.Sleep(time.Second)
|
time.Sleep(time.Second / 2)
|
||||||
}
|
}
|
||||||
pin.Detect(rpio.NoEdge) // disable edge event detection
|
pin.Detect(rpio.NoEdge) // disable edge event detection
|
||||||
}
|
}
|
||||||
|
|
33
rpio.go
33
rpio.go
|
@ -123,7 +123,7 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
// Edge events
|
// Edge events
|
||||||
const (
|
const (
|
||||||
NoEdge Edge = iota
|
NoEdge Edge = iota
|
||||||
RiseEdge
|
RiseEdge
|
||||||
FallEdge
|
FallEdge
|
||||||
|
@ -327,28 +327,33 @@ func TogglePin(pin Pin) {
|
||||||
//
|
//
|
||||||
// It also clears previously detected event 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,
|
// Note that call with RiseEdge will disable previously set FallEdge detection and vice versa
|
||||||
// to disable previously enabled event call it with NoEdge.
|
// 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) {
|
func DetectEdge(pin Pin, edge Edge) {
|
||||||
p := uint8(pin)
|
p := uint8(pin)
|
||||||
|
|
||||||
// Rising edge detect enable register (19/20 depending on bank)
|
// Rising edge detect enable register (19/20 depending on bank)
|
||||||
// Falling edge detect enable register (22/23 depending on bank)
|
// Falling edge detect enable register (22/23 depending on bank)
|
||||||
|
// Event detect status register (16/17)
|
||||||
renReg := p/32 + 19
|
renReg := p/32 + 19
|
||||||
fenReg := p/32 + 22
|
fenReg := p/32 + 22
|
||||||
|
edsReg := p/32 + 16
|
||||||
|
|
||||||
if edge == NoEdge { // clear bits
|
bit := uint32(1 << (p & 31))
|
||||||
gpioMem[renReg] = gpioMem[renReg] &^ (1 << (p&31))
|
|
||||||
gpioMem[fenReg] = gpioMem[fenReg] &^ (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
|
if edge&FallEdge > 0 { // set bit
|
||||||
gpioMem[renReg] = gpioMem[renReg] | (1 << (p&31))
|
gpioMem[fenReg] = gpioMem[fenReg] | bit
|
||||||
}
|
} else { // clear bit
|
||||||
if edge & FallEdge > 0 { // set bit
|
gpioMem[fenReg] = gpioMem[fenReg] &^ bit
|
||||||
gpioMem[fenReg] = gpioMem[fenReg] | (1 << (p&31))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
EdgeDetected(pin) // to clear outdated detection
|
gpioMem[edsReg] = bit // to clear outdated detection
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check whether edge event occured
|
// Check whether edge event occured
|
||||||
|
@ -360,7 +365,7 @@ func EdgeDetected(pin Pin) bool {
|
||||||
// Event detect status register (16/17)
|
// Event detect status register (16/17)
|
||||||
edsReg := p/32 + 16
|
edsReg := p/32 + 16
|
||||||
|
|
||||||
test := gpioMem[edsReg] & (1 << (p&31))
|
test := gpioMem[edsReg] & (1 << (p & 31))
|
||||||
gpioMem[edsReg] = test // set bit to clear it
|
gpioMem[edsReg] = test // set bit to clear it
|
||||||
return test != 0
|
return test != 0
|
||||||
}
|
}
|
||||||
|
@ -376,7 +381,7 @@ func PullMode(pin Pin, pull Pull) {
|
||||||
|
|
||||||
switch pull {
|
switch pull {
|
||||||
case PullDown, PullUp:
|
case PullDown, PullUp:
|
||||||
gpioMem[pullReg] = gpioMem[pullReg] &^ 3 | uint32(pull)
|
gpioMem[pullReg] = gpioMem[pullReg]&^3 | uint32(pull)
|
||||||
case PullOff:
|
case PullOff:
|
||||||
gpioMem[pullReg] = gpioMem[pullReg] &^ 3
|
gpioMem[pullReg] = gpioMem[pullReg] &^ 3
|
||||||
}
|
}
|
||||||
|
|
50
rpio_test.go
50
rpio_test.go
|
@ -1,20 +1,21 @@
|
||||||
package rpio
|
package rpio
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
"os"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestMain(m *testing.M) {
|
func TestMain(m *testing.M) {
|
||||||
|
println("Note: bcm pins 2 and 3 has to be directly connected")
|
||||||
if err := Open(); err != nil {
|
if err := Open(); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
defer Close()
|
||||||
os.Exit(m.Run())
|
os.Exit(m.Run())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRisingEdgeEvent(t *testing.T) {
|
func TestRisingEdgeEvent(t *testing.T) {
|
||||||
// bcm pins 2 and 3 has to be directly connected
|
|
||||||
src := Pin(3)
|
src := Pin(3)
|
||||||
src.Mode(Output)
|
src.Mode(Output)
|
||||||
src.Low()
|
src.Low()
|
||||||
|
@ -25,19 +26,20 @@ func TestRisingEdgeEvent(t *testing.T) {
|
||||||
pin.Detect(RiseEdge)
|
pin.Detect(RiseEdge)
|
||||||
|
|
||||||
timeout := time.After(time.Second)
|
timeout := time.After(time.Second)
|
||||||
loop: for {
|
loop:
|
||||||
|
for {
|
||||||
src.High()
|
src.High()
|
||||||
|
|
||||||
time.Sleep(time.Second/5)
|
time.Sleep(time.Second / 5)
|
||||||
if pin.EdgeDetected() {
|
if pin.EdgeDetected() {
|
||||||
t.Log("edge rised")
|
t.Log("edge rised")
|
||||||
} else {
|
} else {
|
||||||
t.Errorf("Raise event should be detected")
|
t.Errorf("Rise event should be detected")
|
||||||
}
|
}
|
||||||
select {
|
select {
|
||||||
case <- timeout:
|
case <-timeout:
|
||||||
break loop
|
break loop
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
|
||||||
src.Low()
|
src.Low()
|
||||||
|
@ -48,12 +50,11 @@ func TestRisingEdgeEvent(t *testing.T) {
|
||||||
pin.Detect(NoEdge)
|
pin.Detect(NoEdge)
|
||||||
src.High()
|
src.High()
|
||||||
if pin.EdgeDetected() {
|
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) {
|
func TestFallingEdgeEvent(t *testing.T) {
|
||||||
// bcm pins 2 and 3 has to be directly connected
|
|
||||||
src := Pin(3)
|
src := Pin(3)
|
||||||
src.Mode(Output)
|
src.Mode(Output)
|
||||||
src.High()
|
src.High()
|
||||||
|
@ -64,23 +65,24 @@ func TestFallingEdgeEvent(t *testing.T) {
|
||||||
pin.Detect(FallEdge)
|
pin.Detect(FallEdge)
|
||||||
|
|
||||||
timeout := time.After(time.Second)
|
timeout := time.After(time.Second)
|
||||||
loop: for {
|
loop:
|
||||||
src.Low()
|
for {
|
||||||
|
src.Low()
|
||||||
|
|
||||||
time.Sleep(time.Second/5)
|
time.Sleep(time.Second / 5)
|
||||||
if pin.EdgeDetected() {
|
if pin.EdgeDetected() {
|
||||||
t.Log("edge fallen")
|
t.Log("edge fallen")
|
||||||
} else {
|
} else {
|
||||||
t.Errorf("Fall event should be detected")
|
t.Errorf("Fall event should be detected")
|
||||||
}
|
}
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <- timeout:
|
case <-timeout:
|
||||||
break loop
|
break loop
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
|
||||||
src.High()
|
src.High()
|
||||||
}
|
}
|
||||||
if pin.EdgeDetected() {
|
if pin.EdgeDetected() {
|
||||||
t.Error("Fall should not be detected, no change since last call")
|
t.Error("Fall should not be detected, no change since last call")
|
||||||
|
|
Loading…
Reference in New Issue
Block a user