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