Export Current and Voltage function

This commit is contained in:
Thomas Klaehn 2023-08-21 06:01:51 +02:00
parent 124e954d3a
commit b39caa074d
2 changed files with 27 additions and 27 deletions

View File

@ -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)
}

View File

@ -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)
}