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