|
| 1 | +//go:build example |
| 2 | +// +build example |
| 3 | + |
| 4 | +// |
| 5 | +// Do not build by default. |
| 6 | + |
| 7 | +package main |
| 8 | + |
| 9 | +import ( |
| 10 | + "fmt" |
| 11 | + "time" |
| 12 | + |
| 13 | + "gobot.io/x/gobot/v2" |
| 14 | + "gobot.io/x/gobot/v2/drivers/gpio" |
| 15 | + "gobot.io/x/gobot/v2/platforms/adaptors" |
| 16 | + "gobot.io/x/gobot/v2/platforms/radxa/zero" |
| 17 | +) |
| 18 | + |
| 19 | +// Wiring |
| 20 | +// PWR : 1, 17 (+3.3V, VCC), 2, 4 (+5V), 6, 9, 14, 20, 25, 30, 34, 39 (GND) |
| 21 | +// GPIO : header pin 24 is input, pin 32 used as normal output, pin 36 used as inverted output |
| 22 | +// Button: the input pin is wired with a button to GND, the internal pull up resistor is used |
| 23 | +// LED's: the output pins are wired to the cathode of the LED, the anode is wired with a resistor (70-130Ohm for 20mA) |
| 24 | +// to VCC |
| 25 | +// Expected behavior: always one LED is on, the other in opposite state, if button is pressed the state changes |
| 26 | +func main() { |
| 27 | + const ( |
| 28 | + inPinNum = "24" |
| 29 | + outPinNum = "32" |
| 30 | + outPinInvertedNum = "36" |
| 31 | + ) |
| 32 | + // note: WithGpiosOpenDrain() is optional, if using WithGpiosOpenSource() the LED's will not light up |
| 33 | + board := zero.NewAdaptor(adaptors.WithGpiosActiveLow(outPinInvertedNum), |
| 34 | + adaptors.WithGpiosOpenDrain(outPinNum, outPinInvertedNum), adaptors.WithGpiosPullUp(inPinNum)) |
| 35 | + |
| 36 | + inPin := gpio.NewDirectPinDriver(board, inPinNum) |
| 37 | + outPin := gpio.NewDirectPinDriver(board, outPinNum) |
| 38 | + outPinInverted := gpio.NewDirectPinDriver(board, outPinInvertedNum) |
| 39 | + |
| 40 | + work := func() { |
| 41 | + level := byte(1) |
| 42 | + |
| 43 | + gobot.Every(500*time.Millisecond, func() { |
| 44 | + read, err := inPin.DigitalRead() |
| 45 | + fmt.Printf("pin %s state is %d\n", inPinNum, read) |
| 46 | + if err != nil { |
| 47 | + fmt.Println(err) |
| 48 | + if level == 1 { |
| 49 | + level = 0 |
| 50 | + } else { |
| 51 | + level = 1 |
| 52 | + } |
| 53 | + } else { |
| 54 | + level = byte(read) |
| 55 | + } |
| 56 | + |
| 57 | + err = outPin.DigitalWrite(level) |
| 58 | + fmt.Printf("pin %s is now %d\n", outPinNum, level) |
| 59 | + if err != nil { |
| 60 | + fmt.Println(err) |
| 61 | + } |
| 62 | + |
| 63 | + err = outPinInverted.DigitalWrite(level) |
| 64 | + fmt.Printf("pin %s is now not %d\n", outPinInvertedNum, level) |
| 65 | + if err != nil { |
| 66 | + fmt.Println(err) |
| 67 | + } |
| 68 | + }) |
| 69 | + } |
| 70 | + |
| 71 | + robot := gobot.NewRobot("pinBot", |
| 72 | + []gobot.Connection{board}, |
| 73 | + []gobot.Device{inPin, outPin, outPinInverted}, |
| 74 | + work, |
| 75 | + ) |
| 76 | + |
| 77 | + if err := robot.Start(); err != nil { |
| 78 | + panic(err) |
| 79 | + } |
| 80 | +} |
0 commit comments