@@ -90,15 +90,16 @@ class SwitchProGamepadInputReport : public hid::report::base<hid::report::type::
90
90
// Byte 0: Counter which increments with each packet
91
91
uint8_t counter;
92
92
// 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.
94
96
// 8=full, 6=medium, 4=low, 2=critical, 0=empty. Bit 0 is charging status.
95
97
// low = 10%,
96
98
// medium = 50%,
97
99
// full = 100%
98
- // - Connection info.
99
- // Bit 0 is USB powered, bits 1-2 are connection info.
100
- uint8_t battery_level : 4 ;
101
100
uint8_t connection_info : 4 ;
101
+ uint8_t battery_charging : 1 ;
102
+ uint8_t battery_level : 3 ;
102
103
// Byte 2, 3, 4: Button status
103
104
bool btn_y : 1 ;
104
105
bool btn_x : 1 ;
@@ -334,37 +335,23 @@ class SwitchProGamepadInputReport : public hid::report::base<hid::report::type::
334
335
// / Set the battery level
335
336
// / @param level battery level, in the range [0, 100]
336
337
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 ;
354
345
} 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 ;
360
349
}
361
350
}
362
351
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; }
368
355
369
356
// / Set the connection info
370
357
// / @param info connection info
0 commit comments