From 0f78eb6ce67962a4147dce37cc4f8538d44b36d7 Mon Sep 17 00:00:00 2001 From: Peter Wu Date: Mon, 24 Mar 2014 13:25:49 +0100 Subject: unifying: implement BatteryStatus Battery percentage is fuzzy... ok. Signed-off-by: Peter Wu --- hw/usb/hid-logitech-hidpp20.c | 47 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) (limited to 'hw/usb/hid-logitech-hidpp20.c') diff --git a/hw/usb/hid-logitech-hidpp20.c b/hw/usb/hid-logitech-hidpp20.c index 3c664cc089..026dd587be 100644 --- a/hw/usb/hid-logitech-hidpp20.c +++ b/hw/usb/hid-logitech-hidpp20.c @@ -186,11 +186,58 @@ static HIDPP20_FEATURE(feat_devicename) } } +static uint8_t calc_battery_percent(LHidDevice *hd, uint8_t level) +{ + uint8_t nlevels = hd->battery.nlevels; + + assert(nlevels >= 2); + assert(level >= 1 && level <= nlevels); + if (nlevels == 2) { + switch (level) { + case 2: return 90; + default: return 5; + } + } else if (nlevels < 10 || !(hd->battery.flags & BAT_FLAG_ENABLE_MILEAGE)) { + switch (4 * level / nlevels) { + case 4: return 90; + case 3: return 50; + case 2: return 20; + default: return 5; + } + } + + return 100 * level / nlevels; +} + +static HIDPP20_FEATURE(feat_batterystatus) +{ + switch (fn) { + case 0: /* BatteryLevel = GetBatteryLevelStatus() */ + params[0] = calc_battery_percent(hd, hd->battery.level); + if (hd->battery.level > 0 && hd->battery.status == BAT_STS_DISCHARGING) { + params[1] = calc_battery_percent(hd, hd->battery.level - 1); + } else { + params[1] = 0; + } + params[2] = hd->battery.status; + return 2; + case 1: /* LevelList[] = GetBatteryCapacity() */ + params[0] = hd->battery.nlevels; + params[1] = hd->battery.flags; + params[2] = params[3] = 0; /* TODO: nominal battery life */ + params[4] = hd->battery.critical_perc; + return 5; + default: + return -HIDPP20_ERR_CODE_INVALID_FUNCTION_ID; + } +} + /* root feature not included! */ static const HidppFeature features_m525[] = { { 0x0001, 0, feat_featureset }, { 0x0003, 0, feat_devicefwversion }, { 0x0005, 0, feat_devicename }, + { 0x1000, 0, feat_batterystatus }, }; void hidpp20_init_features(LHidDevice *hd) { -- cgit v1.2.1