diff --git a/ina3221.go b/ina3221.go index 5ba5cc3..30217d8 100644 --- a/ina3221.go +++ b/ina3221.go @@ -29,7 +29,7 @@ var ( i2c_dev i2c.Dev ) -func start(slave_addr uint16) error { +func Start(slave_addr uint16) error { logger.SetPrefix("ina3112: ") logger.Println("Starting") @@ -53,10 +53,33 @@ func start(slave_addr uint16) error { return err } -func stop() error { +func Stop() error { return bus.Close() } +func Current(channel Channel) (float64, error) { + res, err := shunt_voltage(channel) + if err != nil { + logger.Println(err) + return 0.0, err + } + + return res * current_factor, err +} + +func Voltage(channel Channel) (float64, error) { + res, err := bus_voltage(channel) + if err != nil { + logger.Println(err) + return 0.0, err + } + return res * voltage_factor, err +} + +func ManufacturerID() (uint16, error) { + return read_register(0xfe) +} + func read_register(address uint8) (uint16, error) { return mem_dev.ReadUint16(address) } @@ -80,26 +103,3 @@ func bus_voltage(channel Channel) (float64, error) { res >>= 3 return float64(res), err } - -func Current(channel Channel) (float64, error) { - res, err := shunt_voltage(channel) - if err != nil { - logger.Println(err) - return 0.0, err - } - - return res * current_factor, err -} - -func Voltage(channel Channel) (float64, error) { - res, err := bus_voltage(channel) - if err != nil { - logger.Println(err) - return 0.0, err - } - return res * voltage_factor, err -} - -func ManufacturerID() (uint16, error) { - return read_register(0xfe) -} diff --git a/ina3221_test.go b/ina3221_test.go index b626118..ae06925 100644 --- a/ina3221_test.go +++ b/ina3221_test.go @@ -12,7 +12,7 @@ const ( ) func TestMain(t *testing.T) { - err := start(i2c_slave_addr) + err := Start(i2c_slave_addr) assert.NilError(t, err) var id uint16 @@ -27,6 +27,6 @@ func TestMain(t *testing.T) { assert.NilError(t, err) } - err = stop() + err = Stop() assert.NilError(t, err) }