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.
pull/54/head
Eldon Stegall 4 years ago
parent 212f060876
commit 3c236a7d6e

@ -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(&current)))) * d.currentLSB
power, err := d.m.ReadUint16(powerRegister)
if err != nil {

Loading…
Cancel
Save