-
Notifications
You must be signed in to change notification settings - Fork 223
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from Drahoslav7/feature/edge-event
Edge detection, closes #21
- Loading branch information
Showing
3 changed files
with
245 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
An example of edge event handling by @Drahoslav7, using the go-rpio library | ||
Waits for button to be pressed twice before exit. | ||
Connect a button between pin 22 and some GND pin. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"time" | ||
|
||
"github.com/stianeikeland/go-rpio" | ||
) | ||
|
||
var ( | ||
// Use mcu pin 22, corresponds to GPIO 3 on the pi | ||
pin = rpio.Pin(22) | ||
) | ||
|
||
func main() { | ||
// Open and map memory to access gpio, check for errors | ||
if err := rpio.Open(); err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
// Unmap gpio memory when done | ||
defer rpio.Close() | ||
|
||
pin.Input() | ||
pin.PullUp() | ||
pin.Detect(rpio.FallEdge) // enable falling edge event detection | ||
|
||
fmt.Println("press a button") | ||
|
||
for i := 0; i < 2; { | ||
if pin.EdgeDetected() { // check if event occured | ||
fmt.Println("button pressed") | ||
i++ | ||
} | ||
time.Sleep(time.Second / 2) | ||
} | ||
pin.Detect(rpio.NoEdge) // disable edge event detection | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package rpio | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
"time" | ||
) | ||
|
||
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 TestEvent(t *testing.T) { | ||
src := Pin(3) | ||
src.Mode(Output) | ||
|
||
pin := Pin(2) | ||
pin.Mode(Input) | ||
pin.PullDown() | ||
|
||
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() | ||
} | ||
|
||
time.Sleep(time.Second / 10) | ||
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("Rise should not be detected, events disabled") | ||
} | ||
|
||
}) | ||
|
||
t.Run("falling edge", func(t *testing.T) { | ||
pin.Detect(FallEdge) | ||
src.High() | ||
|
||
for i := 0; ; i++ { | ||
src.Low() | ||
|
||
time.Sleep(time.Second / 10) | ||
if pin.EdgeDetected() { | ||
t.Log("edge fallen") | ||
} else { | ||
t.Errorf("Fall event should be detected") | ||
} | ||
|
||
if i == 5 { | ||
break | ||
} | ||
|
||
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() { | ||
t.Error("Fall should not be detected, events disabled") | ||
} | ||
}) | ||
|
||
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") | ||
} | ||
|
||
src.Low() | ||
|
||
if pin.EdgeDetected() { | ||
t.Log("edge detected") | ||
} else { | ||
t.Errorf("Fall edge should be detected") | ||
} | ||
} | ||
|
||
pin.Detect(NoEdge) | ||
src.High() | ||
src.Low() | ||
|
||
if pin.EdgeDetected() { | ||
t.Errorf("No edge should be detected, events disabled") | ||
} | ||
|
||
}) | ||
} |