-
Notifications
You must be signed in to change notification settings - Fork 400
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add INA219 battery sensor (#2380)
* specify specific pip version wich works with zmq install * add INA219 sensor * average measurement for more accurate results. Use supply voltage for measurement. * Revert "specify specific pip version wich works with zmq install" This reverts commit 48dd1bf. * correct format * Update src/jukebox/components/battery_monitor/batt_mon_i2c_ina219/__init__.py Co-authored-by: s-martin <[email protected]> * Update copyright notice * Update license * Update license * Document the INA219 * add error handling and type safety * Update batterymonitor.md * Update __init__.py * fix markdown lint for batterymonitor.md * fix markdown lint batterymonitor.md * Update documentation/builders/components/power/batterymonitor.md Co-authored-by: s-martin <[email protected]> * Update documentation/builders/components/power/batterymonitor.md Co-authored-by: s-martin <[email protected]> --------- Co-authored-by: Timm <[email protected]> Co-authored-by: s-martin <[email protected]>
- Loading branch information
1 parent
d93f071
commit f2a1730
Showing
2 changed files
with
110 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/jukebox/components/battery_monitor/batt_mon_i2c_ina219/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# RPi-Jukebox-RFID Version 3 | ||
# Copyright (c) See file LICENSE in project root folder | ||
|
||
import logging | ||
import jukebox.plugs as plugs | ||
import jukebox.cfghandler | ||
from ina219 import INA219 | ||
from ina219 import DeviceRangeError | ||
from components.battery_monitor import BatteryMonitorBase | ||
|
||
logger = logging.getLogger('jb.battmon.ina219') | ||
|
||
batt_mon = None | ||
|
||
|
||
class battmon_ina219(BatteryMonitorBase.BattmonBase): | ||
'''Battery Monitor based on a INA219 | ||
See [Battery Monitor documentation](../../builders/components/power/batterymonitor.md) | ||
''' | ||
|
||
def __init__(self, cfg): | ||
super().__init__(cfg, logger) | ||
|
||
def init_batt_mon_hw(self, num: float, denom: float) -> None: | ||
try: | ||
self.adc = INA219(float(num) / 1000, busnum=1) | ||
self.adc.configure(self.adc.RANGE_16V, self.adc.GAIN_AUTO, self.adc.ADC_32SAMP, self.adc.ADC_32SAMP) | ||
except DeviceRangeError as e: | ||
logger.error(f"Device range error: {e}") | ||
raise | ||
except Exception as e: | ||
logger.error(f"Failed to initialize INA219: {e}") | ||
raise | ||
|
||
def get_batt_voltage(self) -> int: | ||
try: | ||
batt_voltage_mV = self.adc.supply_voltage() * 1000.0 | ||
return int(batt_voltage_mV) | ||
except Exception as e: | ||
logger.error(f"Failed to get supply voltage from INA219: {e}") | ||
raise | ||
|
||
|
||
@plugs.finalize | ||
def finalize(): | ||
global batt_mon | ||
cfg = jukebox.cfghandler.get_handler('jukebox') | ||
batt_mon = battmon_ina219(cfg) | ||
plugs.register(batt_mon, name='batt_mon') | ||
|
||
|
||
@plugs.atexit | ||
def atexit(**ignored_kwargs): | ||
global batt_mon | ||
batt_mon.status_thread.cancel() |