receiver: process battery voltage notifications
This commit is contained in:
parent
013f383bc8
commit
579b09619b
|
@ -527,32 +527,38 @@ def get_battery(device):
|
||||||
def get_voltage(device):
|
def get_voltage(device):
|
||||||
battery_voltage = feature_request(device, FEATURE.BATTERY_VOLTAGE)
|
battery_voltage = feature_request(device, FEATURE.BATTERY_VOLTAGE)
|
||||||
if battery_voltage:
|
if battery_voltage:
|
||||||
voltage, flags = _unpack('>HB', battery_voltage[:3])
|
return decipher_voltage(battery_voltage)
|
||||||
charging = False
|
|
||||||
|
|
||||||
|
# modified to be much closer to battery reports
|
||||||
|
def decipher_voltage(voltage_report):
|
||||||
|
voltage, flags = _unpack('>HB', voltage_report[:3])
|
||||||
|
status = BATTERY_STATUS.discharging
|
||||||
|
charge_sts = ERROR.unknown
|
||||||
|
charge_lvl = CHARGE_LEVEL.average
|
||||||
|
charge_type = CHARGE_TYPE.standard
|
||||||
|
|
||||||
|
if flags & (1 << 7):
|
||||||
|
status = BATTERY_STATUS.recharging
|
||||||
|
charge_sts = CHARGE_STATUS[flags & 0x03]
|
||||||
|
if charge_sts is None:
|
||||||
charge_sts = ERROR.unknown
|
charge_sts = ERROR.unknown
|
||||||
charge_lvl = CHARGE_LEVEL.average
|
elif charge_sts == CHARGE_STATUS.full:
|
||||||
charge_type = CHARGE_TYPE.standard
|
charge_lvl = CHARGE_LEVEL.full
|
||||||
|
status = BATTERY_STATUS.full
|
||||||
|
if (flags & (1 << 3)):
|
||||||
|
charge_type = CHARGE_TYPE.fast
|
||||||
|
elif (flags & (1 << 4)):
|
||||||
|
charge_type = CHARGE_TYPE.slow
|
||||||
|
status = BATTERY_STATUS.slow_recharge
|
||||||
|
elif (flags & (1 << 5)):
|
||||||
|
charge_lvl = CHARGE_LEVEL.critical
|
||||||
|
|
||||||
if flags & (1 << 7):
|
if _log.isEnabledFor(_DEBUG):
|
||||||
charging = True
|
_log.debug("device %d, battery voltage %d mV, charging = %s, charge status %d = %s, charge level %s, charge type %s",
|
||||||
charge_sts = CHARGE_STATUS[flags & 0x03]
|
device.number, voltage, status, (flags & 0x03), charge_sts, charge_lvl, charge_type)
|
||||||
|
|
||||||
if charge_sts is None:
|
return charge_lvl, status, voltage, charge_sts, charge_type
|
||||||
charge_sts = ERROR.unknown
|
|
||||||
elif charge_sts == CHARGE_STATUS.full:
|
|
||||||
charge_lvl = CHARGE_LEVEL.full
|
|
||||||
|
|
||||||
if (flags & (1 << 3)):
|
|
||||||
charge_type = CHARGE_TYPE.fast
|
|
||||||
elif (flags & (1 << 4)):
|
|
||||||
charge_type = CHARGE_TYPE.slow
|
|
||||||
elif (flags & (1 << 5)):
|
|
||||||
charge_lvl = CHARGE_LEVEL.critical
|
|
||||||
|
|
||||||
if _log.isEnabledFor(_DEBUG):
|
|
||||||
_log.debug("device %d, battery voltage %d mV, charging = %d, charge status %d = %s, charge level %s, charge type %s",
|
|
||||||
device.number, voltage, charging, (flags & 0x03), charge_sts, charge_lvl, charge_type)
|
|
||||||
return voltage, charging, charge_sts, charge_lvl, charge_type
|
|
||||||
|
|
||||||
|
|
||||||
def get_keys(device):
|
def get_keys(device):
|
||||||
|
|
|
@ -247,6 +247,14 @@ def _process_feature_notification(device, status, n, feature):
|
||||||
_log.warn("%s: unknown BATTERY %s", device, n)
|
_log.warn("%s: unknown BATTERY %s", device, n)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
if feature == _F.BATTERY_VOLTAGE:
|
||||||
|
if n.address == 0x00:
|
||||||
|
level, status, voltage, _ignore, _ignore =_hidpp20.decipher_voltage(n.data)
|
||||||
|
status.set_battery_info(level, status, None, voltage)
|
||||||
|
else:
|
||||||
|
_log.warn("%s: unknown VOLTAGE %s", device, n)
|
||||||
|
return True
|
||||||
|
|
||||||
# TODO: what are REPROG_CONTROLS_V{2,3}?
|
# TODO: what are REPROG_CONTROLS_V{2,3}?
|
||||||
if feature == _F.REPROG_CONTROLS:
|
if feature == _F.REPROG_CONTROLS:
|
||||||
if n.address == 0x00:
|
if n.address == 0x00:
|
||||||
|
|
|
@ -238,8 +238,7 @@ class DeviceStatus(dict):
|
||||||
if battery is None:
|
if battery is None:
|
||||||
v = _hidpp20.get_voltage(d)
|
v = _hidpp20.get_voltage(d)
|
||||||
if v is not None:
|
if v is not None:
|
||||||
voltage, charging, _ignore, level, _ignore = v
|
level, status, voltage, _ignore, _ignore = v
|
||||||
status = _hidpp20.BATTERY_STATUS.recharging if charging else _hidpp20.BATTERY_STATUS.discharging
|
|
||||||
self.set_battery_keys( (level, status, None), voltage)
|
self.set_battery_keys( (level, status, None), voltage)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
|
@ -188,8 +188,8 @@ def _print_device(dev):
|
||||||
else:
|
else:
|
||||||
battery_voltage = _hidpp20.get_voltage(dev)
|
battery_voltage = _hidpp20.get_voltage(dev)
|
||||||
if battery_voltage :
|
if battery_voltage :
|
||||||
(voltage, charging, charge_sts, charge_lvl, charge_type) = battery_voltage
|
(level, status, voltage, charge_sts, charge_type) = battery_voltage
|
||||||
print (' Battery: %smV, %s.' % (voltage, 'Charging' if charging else 'Discharging'))
|
print (' Battery: %smV, %s, %s.' % (voltage, status, level))
|
||||||
else:
|
else:
|
||||||
print (' Battery status unavailable.')
|
print (' Battery status unavailable.')
|
||||||
else:
|
else:
|
||||||
|
|
Loading…
Reference in New Issue