2022-11-13 23:26:26 +08:00
|
|
|
// +build example
|
|
|
|
//
|
|
|
|
// Do not build by default.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"gobot.io/x/gobot"
|
|
|
|
"gobot.io/x/gobot/drivers/gpio"
|
2023-01-28 19:22:32 +08:00
|
|
|
"gobot.io/x/gobot/platforms/adaptors"
|
2022-11-13 23:26:26 +08:00
|
|
|
"gobot.io/x/gobot/platforms/tinkerboard"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Wiring
|
|
|
|
// PWR Tinkerboard: 1 (+3.3V, VCC), 2(+5V), 6, 9, 14, 20 (GND)
|
2023-01-28 19:22:32 +08:00
|
|
|
// GPIO Tinkerboard: header pin 21 is input, pin 24 used as normal output, pin 26 used as inverted output
|
|
|
|
// Button: the input pin is wired with a button to GND, an external pull up resistor is needed (e.g. 1K)
|
|
|
|
// LED's: the output pins are wired to the cathode of a 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, on button press the state changes
|
2022-11-13 23:26:26 +08:00
|
|
|
func main() {
|
2023-01-28 19:22:32 +08:00
|
|
|
const (
|
|
|
|
inPinNum = "21"
|
|
|
|
outPinNum = "24"
|
|
|
|
outPinInvertedNum = "26"
|
|
|
|
)
|
|
|
|
board := tinkerboard.NewAdaptor(adaptors.WithGpiosActiveLow(outPinInvertedNum))
|
|
|
|
inPin := gpio.NewDirectPinDriver(board, inPinNum)
|
|
|
|
outPin := gpio.NewDirectPinDriver(board, outPinNum)
|
|
|
|
outPinInverted := gpio.NewDirectPinDriver(board, outPinInvertedNum)
|
2022-11-13 23:26:26 +08:00
|
|
|
|
|
|
|
work := func() {
|
|
|
|
level := byte(1)
|
|
|
|
|
|
|
|
gobot.Every(500*time.Millisecond, func() {
|
2023-01-28 19:22:32 +08:00
|
|
|
read, err := inPin.DigitalRead()
|
|
|
|
fmt.Printf("pin %s state is %d\n", inPinNum, read)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
} else {
|
|
|
|
level = byte(read)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = outPin.DigitalWrite(level)
|
|
|
|
fmt.Printf("pin %s is now %d\n", outPinNum, level)
|
2022-11-13 23:26:26 +08:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2023-01-28 19:22:32 +08:00
|
|
|
|
|
|
|
err = outPinInverted.DigitalWrite(level)
|
|
|
|
fmt.Printf("pin %s is now not %d\n", outPinInvertedNum, level)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
|
2022-11-13 23:26:26 +08:00
|
|
|
if level == 1 {
|
|
|
|
level = 0
|
|
|
|
} else {
|
|
|
|
level = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
robot := gobot.NewRobot("pinBot",
|
|
|
|
[]gobot.Connection{board},
|
2023-01-28 19:22:32 +08:00
|
|
|
[]gobot.Device{inPin, outPin, outPinInverted},
|
2022-11-13 23:26:26 +08:00
|
|
|
work,
|
|
|
|
)
|
|
|
|
|
|
|
|
robot.Start()
|
|
|
|
}
|