-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.go
65 lines (57 loc) · 1.09 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"fmt"
"log"
"time"
"github.com/shanghuiyang/rpi-devices/dev"
)
const (
trig = 21
echo = 26
us100Dev = "/dev/ttyAMA0"
baud = 9600
)
func main() {
var mode int
fmt.Printf("choose interface type: 1: GPIO, 2: UART\n")
fmt.Printf("mode: ")
if n, err := fmt.Scanf("%d", &mode); n != 1 || err != nil {
log.Printf("invalid operator, error: %v", err)
return
}
if mode != 1 && mode != 2 {
log.Printf("invalid input")
return
}
// gpio interface
if mode == 1 {
u, err := dev.NewUS100GPIO(trig, echo)
if err != nil {
log.Fatalf("new us100 error: %v", err)
}
for {
dist, err := u.Dist()
if err != nil {
log.Printf("failed to get distance")
continue
}
log.Printf("%.2f cm\n", dist)
time.Sleep(50 * time.Millisecond)
}
}
// uart interface
u, err := dev.NewUS100UART(us100Dev, baud)
if err != nil {
log.Fatalf("new us100 error: %v", err)
}
defer u.Close()
for {
dist, err := u.Dist()
if err != nil {
log.Printf("failed to get distance")
continue
}
log.Printf("%.2f cm\n", dist)
time.Sleep(50 * time.Millisecond)
}
}