hidpp20: features: add BATTERY_VOLTAGE (0x1001) support

Signed-off-by: Filipe Laíns <lains@archlinux.org>

Co-authored-by: Filipe Laíns <lains@archlinux.org>
This commit is contained in:
effective-light 2020-01-01 23:31:02 -05:00 committed by Filipe Laíns
parent 82915eea4b
commit 430b70711b
2 changed files with 51 additions and 0 deletions

View File

@ -57,6 +57,7 @@ FEATURE = _NamedInts(
DFUCONTROL_2=0x00C1,
DFU=0x00D0,
BATTERY_STATUS=0x1000,
BATTERY_VOLTAGE=0x1001,
LED_CONTROL=0x1300,
CHANGE_HOST=0x1814,
BACKLIGHT=0x1981,
@ -141,6 +142,22 @@ BATTERY_STATUS = _NamedInts(
invalid_battery=0x05,
thermal_error=0x06)
CHARGE_STATUS = _NamedInts(
charging=0x00,
full=0x01,
not_charging=0x02,
error=0x07)
CHARGE_LEVEL = _NamedInts(
average=0x00,
full=0x01,
critical=0x02)
CHARGE_TYPE = _NamedInts(
standard=0x00,
fast=0x01,
slow=0x02)
ERROR = _NamedInts(
unknown=0x01,
invalid_argument=0x02,
@ -463,6 +480,37 @@ def get_battery(device):
return discharge, BATTERY_STATUS[status]
def get_voltage(device):
battery_voltage = feature_request(device, FEATURE.BATTERY_VOLTAGE)
if battery_voltage:
voltage, flags = _unpack('>HB', battery_voltage[:3])
charging = False
charge_sts = ERROR.unknown
charge_lvl = CHARGE_LEVEL.average
charge_type = CHARGE_TYPE.standard
if flags & (1 << 7):
charging = True
charge_sts = CHARGE_STATUS[flags & 0x03]
if charge_sts is None:
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):
# TODO: add here additional variants for other REPROG_CONTROLS
count = feature_request(device, FEATURE.REPROG_CONTROLS)

View File

@ -166,6 +166,7 @@ def _print_device(dev):
print (' %s, pos:%d, group:%1d, gmask:%d' % ( ', '.join(flags), k.pos, k.group, k.group_mask))
if dev.online:
battery = _hidpp20.get_battery(dev)
(voltage, charging, charge_sts, charge_lvl, charge_type) = _hidpp20.get_voltage(dev)
if battery is None:
battery = _hidpp10.get_battery(dev)
if battery is not None:
@ -179,6 +180,8 @@ def _print_device(dev):
else:
text = 'N/A'
print (' Battery: %s, %s.' % (text, status))
elif voltage:
print (' Battery: %smV, %s.' % (voltage, 'Charging' if charging else 'Discharging'))
else:
print (' Battery status unavailable.')
else: