gobot.io/x/gobot/v2@v2.1.0/examples/tinkerboard_mfcrc522gpio.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/spi" 15 "gobot.io/x/gobot/v2/platforms/adaptors" 16 "gobot.io/x/gobot/v2/platforms/tinkerboard" 17 ) 18 19 // Wiring 20 // PWR Tinkerboard: 1 (+3.3V, VCC), 2(+5V), 6, 9, 14, 20 (GND) 21 // GPIO-SPI Tinkerboard (same as SPI2): 23 (CLK), 19 (TXD), 21 (RXD), 24 (CSN0) 22 // MFRC522 plate: VCC, GND, SCK (CLK), MOSI (->TXD), MISO (->RXD), NSS/SDA (CSN0/CSN1?) 23 const ( 24 sclk = "23" 25 nss = "24" 26 mosi = "19" 27 miso = "21" 28 speedHz = 5000 // more than 15kHz is not possible with GPIO's, so we choose 5kHz 29 ) 30 31 func main() { 32 a := tinkerboard.NewAdaptor(adaptors.WithSpiGpioAccess(sclk, nss, mosi, miso)) 33 d := spi.NewMFRC522Driver(a, spi.WithSpeed(speedHz)) 34 35 wasCardDetected := false 36 const textToCard = "Hello RFID user!\nHey, we use GPIO's for SPI." 37 38 work := func() { 39 if err := d.PrintReaderVersion(); err != nil { 40 fmt.Println("get version err:", err) 41 } 42 43 gobot.Every(2*time.Second, func() { 44 45 if !wasCardDetected { 46 fmt.Println("\n+++ poll for card +++") 47 if err := d.IsCardPresent(); err != nil { 48 fmt.Println("no card found") 49 } else { 50 fmt.Println("\n+++ write card +++") 51 err := d.WriteText(textToCard) 52 if err != nil { 53 fmt.Println("write err:", err) 54 } 55 wasCardDetected = true 56 } 57 } else { 58 fmt.Println("\n+++ read card +++") 59 text, err := d.ReadText() 60 if err != nil { 61 fmt.Println("read err:", err) 62 wasCardDetected = false 63 } else { 64 fmt.Printf("-- start text --\n%s\n-- end text --\n", text) 65 } 66 } 67 68 }) 69 } 70 71 robot := gobot.NewRobot("gpioSpiBot", 72 []gobot.Connection{a}, 73 []gobot.Device{d}, 74 work, 75 ) 76 77 robot.Start() 78 }