Skip to content

Commit 6d102d8

Browse files
authored
fix(hid-rp): Fix how switch pro reports battery/connection (#412)
* Fix incorrect ordering of battery and connection info nibbles in switch pro input report. Ensures the battery is properly reported to the switch Build and run as part of [esp-usb-ble-hid](http://github.com/finger563/esp-usb-ble-hid).
1 parent 38497fe commit 6d102d8

File tree

1 file changed

+18
-31
lines changed

1 file changed

+18
-31
lines changed

components/hid-rp/include/hid-rp-switch-pro.hpp

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,16 @@ class SwitchProGamepadInputReport : public hid::report::base<hid::report::type::
9090
// Byte 0: Counter which increments with each packet
9191
uint8_t counter;
9292
// Byte 1:
93-
// - Battery level.
93+
// - low nibble: Connection info.
94+
// Bit 0 is USB powered, bits 1-2 are connection info.
95+
// - high nibble: Battery level.
9496
// 8=full, 6=medium, 4=low, 2=critical, 0=empty. Bit 0 is charging status.
9597
// low = 10%,
9698
// medium = 50%,
9799
// full = 100%
98-
// - Connection info.
99-
// Bit 0 is USB powered, bits 1-2 are connection info.
100-
uint8_t battery_level : 4;
101100
uint8_t connection_info : 4;
101+
uint8_t battery_charging : 1;
102+
uint8_t battery_level : 3;
102103
// Byte 2, 3, 4: Button status
103104
bool btn_y : 1;
104105
bool btn_x : 1;
@@ -334,37 +335,23 @@ class SwitchProGamepadInputReport : public hid::report::base<hid::report::type::
334335
/// Set the battery level
335336
/// @param level battery level, in the range [0, 100]
336337
constexpr void set_battery_level(float level) {
337-
// BATT_EMPTY = 0, // 0000
338-
// BATT_CHARGING = 1, // 0001
339-
// BATT_CRITICAL = 2, // 0010
340-
// BATT_LOW = 4, // 0100
341-
// BATT_MEDIUM = 6, // 0110
342-
// BATT_FULL = 8, // 1000
343-
344-
// unset all level bits, leaving only the charging bit unchanged.
345-
battery_level = battery_level & 0x1;
346-
347-
// now set the level bits
348-
if (level > 75) {
349-
// set full bit
350-
battery_level |= 8;
351-
} else if (level > 50) {
352-
// set medium bits
353-
battery_level |= 6;
338+
// now set the level bits (3 bits)
339+
if (level > 90) {
340+
battery_level = 4;
341+
} else if (level > 60) {
342+
battery_level = 3;
343+
} else if (level > 30) {
344+
battery_level = 2;
354345
} else if (level > 10) {
355-
// set low bit
356-
battery_level |= 4;
357-
} else if (level > 0) {
358-
// set critical bit
359-
battery_level |= 2;
346+
battery_level = 1;
347+
} else {
348+
battery_level = 0;
360349
}
361350
}
362351

363-
constexpr void set_battery_charging(bool charging) {
364-
// charging is the least significant bit of the battery level
365-
// so we just need to set the least significant bit of the battery level
366-
battery_level = (battery_level & 0xE) | (charging ? 1 : 0);
367-
}
352+
/// Set the battery charging status
353+
/// @param charging whether the battery is charging or not
354+
constexpr void set_battery_charging(bool charging) { battery_charging = charging; }
368355

369356
/// Set the connection info
370357
/// @param info connection info

0 commit comments

Comments
 (0)