diff options
author | Peter Wu <peter@lekensteyn.nl> | 2014-03-24 13:25:49 +0100 |
---|---|---|
committer | Peter Wu <peter@lekensteyn.nl> | 2014-03-24 13:25:49 +0100 |
commit | 0f78eb6ce67962a4147dce37cc4f8538d44b36d7 (patch) | |
tree | db7b627eaa37e767e2a08aad19683fc79a6477e8 /hw/usb/hid-logitech-hidpp20.c | |
parent | 4215730f0ac21c625effe50ba14fbf050dc991a8 (diff) | |
download | qemu-0f78eb6ce67962a4147dce37cc4f8538d44b36d7.tar.gz |
unifying: implement BatteryStatus
Battery percentage is fuzzy... ok.
Signed-off-by: Peter Wu <peter@lekensteyn.nl>
Diffstat (limited to 'hw/usb/hid-logitech-hidpp20.c')
-rw-r--r-- | hw/usb/hid-logitech-hidpp20.c | 47 |
1 files changed, 47 insertions, 0 deletions
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) { |