tinygo.org/x/drivers@v0.27.1-0.20240509133757-7dbca2a54349/examples/ina260/main.go (about) 1 package main 2 3 import ( 4 "machine" 5 "time" 6 7 "tinygo.org/x/drivers/ina260" 8 ) 9 10 func main() { 11 machine.I2C0.Configure(machine.I2CConfig{}) 12 13 dev := ina260.New(machine.I2C0) 14 dev.Configure(ina260.Config{ 15 AverageMode: ina260.AVGMODE_16, 16 VoltConvTime: ina260.CONVTIME_140USEC, 17 CurrentConvTime: ina260.CONVTIME_140USEC, 18 Mode: ina260.MODE_CONTINUOUS | ina260.MODE_VOLTAGE | ina260.MODE_CURRENT, 19 }) 20 21 if dev.Connected() { 22 println("INA260 detected") 23 } else { 24 println("INA260 NOT detected") 25 return 26 } 27 28 for { 29 microvolts := dev.Voltage() 30 microamps := dev.Current() 31 microwatts := dev.Power() 32 33 println(fmtD(microvolts, 4, 3), "mV,", fmtD(microamps, 4, 3), "mA,", fmtD(microwatts, 4, 3), "mW") 34 35 time.Sleep(10 * time.Millisecond) 36 } 37 } 38 39 func fmtD(val int32, i int, f int) string { 40 result := make([]byte, i+f+1) 41 neg := false 42 43 if val < 0 { 44 val = -val 45 neg = true 46 } 47 48 for p := len(result) - 1; p >= 0; p-- { 49 result[p] = byte(int32('0') + (val % 10)) 50 val = val / 10 51 52 if p == i+1 && p > 0 { 53 p-- 54 result[p] = '.' 55 } 56 } 57 58 if neg { 59 result[0] = '-' 60 } 61 62 return string(result) 63 }