1
0
mirror of https://github.com/kidoman/embd synced 2024-11-14 05:28:54 +01:00
embd/samples/us020.go
Kunal Powar 779096e668 samples: enable flag parsing
this will allow people to use glog options (like -v=3) to increase
verbosity of the log output
2014-04-05 01:43:16 +05:30

55 lines
785 B
Go

// +build ignore
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"time"
"github.com/kidoman/embd"
"github.com/kidoman/embd/sensor/us020"
)
func main() {
flag.Parse()
if err := embd.InitGPIO(); err != nil {
panic(err)
}
defer embd.CloseGPIO()
echoPin, err := embd.NewDigitalPin("P9_21")
if err != nil {
panic(err)
}
triggerPin, err := embd.NewDigitalPin("P9_22")
if err != nil {
panic(err)
}
rf := us020.New(echoPin, triggerPin, nil)
defer rf.Close()
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt, os.Kill)
for {
select {
default:
distance, err := rf.Distance()
if err != nil {
panic(err)
}
fmt.Printf("Distance is %v\n", distance)
time.Sleep(500 * time.Millisecond)
case <-quit:
return
}
}
}