mirror of
https://github.com/kidoman/embd
synced 2024-12-22 04:40:04 +01:00
samples: universal blinker example
This commit is contained in:
parent
e73b70b296
commit
fdd1d6b5c3
1
samples/.gitignore
vendored
1
samples/.gitignore
vendored
@ -20,5 +20,6 @@ servobbb
|
||||
servoblaster
|
||||
simpleblinker
|
||||
tmp006
|
||||
universalblinker
|
||||
us020
|
||||
watersensor
|
||||
|
59
samples/universalblinker.go
Normal file
59
samples/universalblinker.go
Normal file
@ -0,0 +1,59 @@
|
||||
// +build ignore
|
||||
|
||||
// Universal LED blinker, works OOTB on a RPi / BBB.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"os"
|
||||
"os/signal"
|
||||
"time"
|
||||
|
||||
"github.com/kidoman/embd"
|
||||
_ "github.com/kidoman/embd/host/all"
|
||||
)
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
panicIf(embd.InitLED())
|
||||
defer embd.CloseLED()
|
||||
|
||||
led, err := embd.NewLED(ledToBlink())
|
||||
panicIf(err)
|
||||
defer led.Off()
|
||||
|
||||
quit := make(chan os.Signal, 1)
|
||||
signal.Notify(quit, os.Interrupt, os.Kill)
|
||||
defer signal.Stop(quit)
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-time.After(250 * time.Millisecond):
|
||||
panicIf(led.Toggle())
|
||||
case <-quit:
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func ledToBlink() string {
|
||||
host, _, err := embd.DetectHost()
|
||||
panicIf(err)
|
||||
|
||||
switch host {
|
||||
case embd.HostRPi:
|
||||
return "LED0"
|
||||
case embd.HostBBB:
|
||||
return "USR3"
|
||||
}
|
||||
|
||||
panic("Unsupported host")
|
||||
}
|
||||
|
||||
func panicIf(err error) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user