From 4c126f417d386fa7ba829d99ea885a0777073e84 Mon Sep 17 00:00:00 2001 From: "Peter F. Patel-Schneider" Date: Tue, 28 Jun 2022 19:18:36 -0400 Subject: [PATCH] device: add support for ADC MEASUREMENT battery feature --- lib/logitech_receiver/hidpp20.py | 18 ++++++++++++++++++ lib/logitech_receiver/notifications.py | 7 +++++++ 2 files changed, 25 insertions(+) diff --git a/lib/logitech_receiver/hidpp20.py b/lib/logitech_receiver/hidpp20.py index 6a6d2f6d..3799bb59 100644 --- a/lib/logitech_receiver/hidpp20.py +++ b/lib/logitech_receiver/hidpp20.py @@ -1310,10 +1310,28 @@ def decipher_battery_voltage(report): return FEATURE.BATTERY_VOLTAGE, charge_lvl, None, status, voltage +def get_adc_measurement(device): + try: # this feature call has been known to produce errors so be extra cautious + report = feature_request(device, FEATURE.ADC_MEASUREMENT) + if report is not None: + return decipher_adc_measurement(report) + except FeatureCallError: + return None + + +def decipher_adc_measurement(report): + # partial implementation - needs mapping to levels + adc, flags = _unpack('!HB', report[:3]) + if flags & 0x01: + status = BATTERY_STATUS.recharging if flags & 0x02 else BATTERY_STATUS.discharging + return FEATURE.ADC_MEASUREMENT, None, None, status, adc + + battery_functions = { FEATURE.BATTERY_STATUS: get_battery_status, FEATURE.BATTERY_VOLTAGE: get_battery_unified, FEATURE.UNIFIED_BATTERY: get_battery_voltage, + FEATURE.ADC_MEASUREMENT: get_adc_measurement, } diff --git a/lib/logitech_receiver/notifications.py b/lib/logitech_receiver/notifications.py index b666afda..1732f83c 100644 --- a/lib/logitech_receiver/notifications.py +++ b/lib/logitech_receiver/notifications.py @@ -337,6 +337,13 @@ def _process_feature_notification(device, status, n, feature): else: _log.warn('%s: unknown UNIFIED BATTERY %s', device, n) + elif feature == _F.ADC_MEASUREMENT: + if n.address == 0x00: + _ignore, level, next, status, voltage = _hidpp20.decipher_adc_measurement(n.data) + status.set_battery_info(level, next, status, voltage) + else: + _log.warn('%s: unknown ADC MEASUREMENT %s', device, n) + elif feature == _F.SOLAR_DASHBOARD: if n.data[5:9] == b'GOOD': charge, lux, adc = _unpack('!BHH', n.data[:5])