-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathbeaglepocket_direct_pin.go
80 lines (67 loc) · 2.01 KB
/
beaglepocket_direct_pin.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//go:build example
// +build example
//
// Do not build by default.
package main
import (
"fmt"
"time"
"gobot.io/x/gobot/v2"
"gobot.io/x/gobot/v2/drivers/gpio"
"gobot.io/x/gobot/v2/platforms/adaptors"
"gobot.io/x/gobot/v2/platforms/beagleboard/pocketbeagle"
)
// Wiring
// PWR Pocket: P1.14, P2.23 (+3.3V, VCC); P1.15, P1.16, P1.22, P2.15, P2.21 (GND)
// GPIO Pocket: header pin P1.34 is input, pin P1.35 is normal output, pin P1.36 is inverted output
// Button: the input pin is wired with a button to GND, an external pull up resistor is needed (e.g. 2kOhm to VCC)
// LED's: the output pins are wired to the cathode of the LED, the anode is wired with a resistor (70-130Ohm for 20mA)
// to VCC
// Expected behavior: always one LED is on, the other in opposite state, if button is pressed the state changes
// note: you can also use user LEDs, e.g. "usr0", "usr3"
func main() {
const (
inPinNum = "P1_34"
outPinNum = "P1_35"
outPinInvertedNum = "P1_36"
)
board := pocketbeagle.NewAdaptor(adaptors.WithGpiosActiveLow(outPinInvertedNum))
inPin := gpio.NewDirectPinDriver(board, inPinNum)
outPin := gpio.NewDirectPinDriver(board, outPinNum)
outPinInverted := gpio.NewDirectPinDriver(board, outPinInvertedNum)
work := func() {
level := byte(1)
gobot.Every(500*time.Millisecond, func() {
read, err := inPin.DigitalRead()
fmt.Printf("pin %s state is %d\n", inPinNum, read)
if err != nil {
fmt.Println(err)
if level == 1 {
level = 0
} else {
level = 1
}
} else {
level = byte(read)
}
err = outPin.DigitalWrite(level)
fmt.Printf("pin %s is now %d\n", outPinNum, level)
if err != nil {
fmt.Println(err)
}
err = outPinInverted.DigitalWrite(level)
fmt.Printf("pin %s is now not %d\n", outPinInvertedNum, level)
if err != nil {
fmt.Println(err)
}
})
}
robot := gobot.NewRobot("pinBot",
[]gobot.Connection{board},
[]gobot.Device{inPin, outPin, outPinInverted},
work,
)
if err := robot.Start(); err != nil {
panic(err)
}
}