Issue #73 - restructure so voltage can be returned even if power/current is meaningless. (#87)

pull/89/head
gsexton 1 year ago committed by GitHub
parent 0fba034629
commit c46abe928c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -99,44 +99,48 @@ const (
) )
// Sense reads the power values from the ina219 sensor. // Sense reads the power values from the ina219 sensor.
func (d *Dev) Sense() (PowerMonitor, error) { func (d *Dev) Sense() (pm PowerMonitor, err error) {
d.mu.Lock() d.mu.Lock()
defer d.mu.Unlock() defer d.mu.Unlock()
var pm PowerMonitor
shunt, err := d.m.ReadUint16(shuntVoltageRegister) shunt, err := d.m.ReadUint16(shuntVoltageRegister)
if err != nil { if err != nil {
return PowerMonitor{}, errReadShunt err = errReadShunt
return
} }
// Least significant bit is 10µV. // Least significant bit is 10µV.
pm.Shunt = physic.ElectricPotential(int16(shunt)) * 10 * physic.MicroVolt pm.Shunt = physic.ElectricPotential(int16(shunt)) * 10 * physic.MicroVolt
bus, err := d.m.ReadUint16(busVoltageRegister) bus, err := d.m.ReadUint16(busVoltageRegister)
if err != nil { if err != nil {
return PowerMonitor{}, errReadBus err = errReadBus
} return
// Check if bit zero is set, if set the ADC has overflowed.
if bus&1 > 0 {
return PowerMonitor{}, errRegisterOverflow
} }
// Least significant bit is 4mV. // Least significant bit is 4mV.
pm.Voltage = physic.ElectricPotential(bus>>3) * 4 * physic.MilliVolt pm.Voltage = physic.ElectricPotential(bus>>3) * 4 * physic.MilliVolt
// Check if bit zero is set, if set the ADC has overflowed.
if bus&1 > 0 {
err = errRegisterOverflow
return
}
current, err := d.m.ReadUint16(currentRegister) current, err := d.m.ReadUint16(currentRegister)
if err != nil { if err != nil {
return PowerMonitor{}, errReadCurrent err = errReadCurrent
return
} }
pm.Current = physic.ElectricCurrent(int16(current)) * d.currentLSB pm.Current = physic.ElectricCurrent(int16(current)) * d.currentLSB
power, err := d.m.ReadUint16(powerRegister) power, err := d.m.ReadUint16(powerRegister)
if err != nil { if err != nil {
return PowerMonitor{}, errReadPower err = errReadPower
return
} }
pm.Power = physic.Power(power) * d.powerLSB pm.Power = physic.Power(power) * d.powerLSB
return pm, nil return
} }
// Since physic electrical is in nano units we need to scale taking care to not // Since physic electrical is in nano units we need to scale taking care to not

Loading…
Cancel
Save