From 3c236a7d6ee75e9f920238787a2112d3c678d64a Mon Sep 17 00:00:00 2001 From: Eldon Stegall Date: Mon, 14 Nov 2022 14:42:25 -0500 Subject: [PATCH] ina219: Interpret signed output register values The current and shunt voltage register values are signed, twos complement format. In my tests interpreting them directly in the way implemented here gave output consistent with other ina219 libraries. --- ina219/ina219.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ina219/ina219.go b/ina219/ina219.go index 0226ba1..c327c55 100644 --- a/ina219/ina219.go +++ b/ina219/ina219.go @@ -5,6 +5,7 @@ package ina219 import ( + "unsafe" "encoding/binary" "errors" "fmt" @@ -110,7 +111,7 @@ func (d *Dev) Sense() (PowerMonitor, error) { return PowerMonitor{}, errReadShunt } // Least significant bit is 10µV. - pm.Shunt = physic.ElectricPotential(shunt) * 10 * physic.MicroVolt + pm.Shunt = physic.ElectricPotential(*((*int16)(unsafe.Pointer(&shunt)))) * 10 * physic.MicroVolt bus, err := d.m.ReadUint16(busVoltageRegister) if err != nil { @@ -128,7 +129,7 @@ func (d *Dev) Sense() (PowerMonitor, error) { if err != nil { return PowerMonitor{}, errReadCurrent } - pm.Current = physic.ElectricCurrent(current) * d.currentLSB + pm.Current = physic.ElectricCurrent(*((*int16)(unsafe.Pointer(¤t)))) * d.currentLSB power, err := d.m.ReadUint16(powerRegister) if err != nil {