2014-03-02 08:06:34 +01:00
|
|
|
// +build ignore
|
|
|
|
|
2013-12-13 01:21:22 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2014-04-04 22:13:16 +02:00
|
|
|
"flag"
|
2014-03-31 15:16:04 +02:00
|
|
|
"fmt"
|
2014-04-04 22:13:16 +02:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2013-12-13 01:21:22 +01:00
|
|
|
"time"
|
|
|
|
|
2014-03-02 20:21:23 +01:00
|
|
|
"github.com/kidoman/embd"
|
2014-02-10 00:35:41 +01:00
|
|
|
"github.com/kidoman/embd/sensor/us020"
|
2013-12-13 01:21:22 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2014-04-04 22:13:16 +02:00
|
|
|
flag.Parse()
|
|
|
|
|
2014-03-02 20:21:23 +01:00
|
|
|
if err := embd.InitGPIO(); err != nil {
|
2014-02-26 23:54:53 +01:00
|
|
|
panic(err)
|
|
|
|
}
|
2014-03-02 20:21:23 +01:00
|
|
|
defer embd.CloseGPIO()
|
2014-02-26 23:54:53 +01:00
|
|
|
|
2014-04-04 22:13:16 +02:00
|
|
|
echoPin, err := embd.NewDigitalPin("P9_21")
|
2014-02-26 23:54:53 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2014-04-04 22:13:16 +02:00
|
|
|
|
|
|
|
triggerPin, err := embd.NewDigitalPin("P9_22")
|
2014-02-26 23:54:53 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2014-02-07 21:51:01 +01:00
|
|
|
|
2014-02-26 23:54:53 +01:00
|
|
|
rf := us020.New(echoPin, triggerPin, nil)
|
2014-01-08 23:09:06 +01:00
|
|
|
defer rf.Close()
|
2013-12-13 01:21:22 +01:00
|
|
|
|
2014-04-04 22:13:16 +02:00
|
|
|
quit := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(quit, os.Interrupt, os.Kill)
|
|
|
|
|
2013-12-13 01:21:22 +01:00
|
|
|
for {
|
2014-04-04 22:13:16 +02:00
|
|
|
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
|
2013-12-13 01:21:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|