tinygo.org/x/drivers@v0.27.1-0.20240509133757-7dbca2a54349/ina260/ina260_test.go (about) 1 package ina260 2 3 import ( 4 "testing" 5 6 qt "github.com/frankban/quicktest" 7 "tinygo.org/x/drivers/tester" 8 ) 9 10 func TestDefaultI2CAddress(t *testing.T) { 11 c := qt.New(t) 12 bus := tester.NewI2CBus(c) 13 dev := New(bus) 14 c.Assert(dev.Address, qt.Equals, uint16(Address)) 15 } 16 17 func TestConnected(t *testing.T) { 18 c := qt.New(t) 19 bus := tester.NewI2CBus(c) 20 fake := tester.NewI2CDevice16(c, Address) 21 fake.Registers = defaultRegisters() 22 bus.AddDevice(fake) 23 24 dev := New(bus) 25 c.Assert(dev.Connected(), qt.Equals, true) 26 } 27 28 func TestVoltage(t *testing.T) { 29 c := qt.New(t) 30 bus := tester.NewI2CBus(c) 31 fake := tester.NewI2CDevice16(c, Address) 32 fake.Registers = defaultRegisters() 33 fake.Registers[REG_BUSVOLTAGE] = 0x2570 34 bus.AddDevice(fake) 35 36 dev := New(bus) 37 // Datasheet: 2570h = 11.98V = 11980mV = 11980000uV 38 c.Assert(dev.Voltage(), qt.Equals, int32(11980000)) 39 } 40 41 func TestCurrent(t *testing.T) { 42 c := qt.New(t) 43 bus := tester.NewI2CBus(c) 44 fake := tester.NewI2CDevice16(c, Address) 45 fake.Registers = defaultRegisters() 46 fake.Registers[REG_CURRENT] = 0x2710 47 bus.AddDevice(fake) 48 49 dev := New(bus) 50 // Datasheet: 2710h = 12.5A = 12500mA = 12500000uA 51 c.Assert(dev.Current(), qt.Equals, int32(12500000)) 52 } 53 54 func TestPower(t *testing.T) { 55 c := qt.New(t) 56 bus := tester.NewI2CBus(c) 57 fake := tester.NewI2CDevice16(c, Address) 58 fake.Registers = defaultRegisters() 59 fake.Registers[REG_POWER] = 0x3A7F 60 bus.AddDevice(fake) 61 62 dev := New(bus) 63 // 3A7Fh = 149.75W = 149750mW = 149750000uW 64 c.Assert(dev.Power(), qt.Equals, int32(149750000)) 65 } 66 67 // defaultRegisters returns the default values for all of the device's registers. 68 // set TI INA260 datasheet for power-on defaults 69 func defaultRegisters() map[uint8]uint16 { 70 return map[uint8]uint16{ 71 REG_CONFIG: 0x6127, 72 REG_CURRENT: 0x0000, 73 REG_BUSVOLTAGE: 0x0000, 74 REG_POWER: 0x0000, 75 REG_MASKENABLE: 0x0000, 76 REG_ALERTLIMIT: 0x0000, 77 REG_MANF_ID: 0x5449, 78 REG_DIE_ID: 0x2270, 79 } 80 }