1
0
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:
Karan Misra 2014-04-25 02:28:01 +05:30
parent e73b70b296
commit fdd1d6b5c3
2 changed files with 60 additions and 0 deletions

1
samples/.gitignore vendored
View File

@ -20,5 +20,6 @@ servobbb
servoblaster
simpleblinker
tmp006
universalblinker
us020
watersensor

View 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)
}
}