gobot.io/x/gobot/v2@v2.1.0/examples/tinkerboard_pcf8583_counter.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 "log" 12 "time" 13 14 "gobot.io/x/gobot/v2" 15 "gobot.io/x/gobot/v2/drivers/i2c" 16 "gobot.io/x/gobot/v2/platforms/tinkerboard" 17 ) 18 19 // Wiring 20 // PWR Tinkerboard: 1 (+3.3V, VCC), 6, 9, 14, 20 (GND) 21 // I2C1 Tinkerboard: 3 (SDA), 5 (SCL) 22 // PCF8583 DIP package: 1 (OSCI, event), 2 (OSCO, nc), 3 (A0 - GND), 4 (VSS, GND), 5 (SDA), 6 (SCL), 7 (/INT, nc), 8 (VDD, +3.3V) 23 // Note: event can be created by e.g. an debounced button 24 func main() { 25 board := tinkerboard.NewAdaptor() 26 pcf := i2c.NewPCF8583Driver(board, i2c.WithBus(1), i2c.WithPCF8583Mode(i2c.PCF8583CtrlModeCounter)) 27 28 work := func() { 29 lastCnt := int32(1234) 30 31 if err := pcf.WriteCounter(lastCnt); err != nil { 32 fmt.Println(err) 33 } 34 35 gobot.Every(1000*time.Millisecond, func() { 36 if val, err := pcf.ReadCounter(); err != nil { 37 fmt.Println(err) 38 } else { 39 log.Printf("read Counter: %d, diff [Hz]: %d", val, val-lastCnt) 40 lastCnt = val 41 } 42 43 ramVal, err := pcf.ReadRAM(uint8(0)) 44 if err != nil { 45 fmt.Println(err) 46 } else { 47 log.Printf("read RAM: %d", ramVal) 48 ramVal++ 49 } 50 if err := pcf.WriteRAM(uint8(0), ramVal); err != nil { 51 fmt.Println(err) 52 } 53 }) 54 } 55 56 robot := gobot.NewRobot("pcfBot", 57 []gobot.Connection{board}, 58 []gobot.Device{pcf}, 59 work, 60 ) 61 62 robot.Start() 63 }