Skip to content

Commit

Permalink
fix: joypad battery values
Browse files Browse the repository at this point in the history
  • Loading branch information
ABeltramo committed Jul 10, 2024
1 parent dcb906a commit 40b0c5c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
16 changes: 8 additions & 8 deletions src/moonlight-protocol/moonlight/control.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ enum CONTROLLER_CAPABILITIES : uint8_t {
RGB_LED = 0x80
};

constexpr uint8_t BATTERY_PERCENTAGE_UNKNOWN = 0xFF;

enum CONTROLLER_BTN : unsigned int {
DPAD_UP = 0x0001,
DPAD_DOWN = 0x0002,
Expand Down Expand Up @@ -250,13 +248,15 @@ enum MOTION_TYPE : uint8_t {
GYROSCOPE = 0x02
};

constexpr uint8_t BATTERY_PERCENTAGE_UNKNOWN = 0xFF;

enum BATTERY_STATE : unsigned short {
BATTERY_DISCHARGING = 0x0,
BATTERY_CHARGHING = 0x1,
BATTERY_FULL = 0x2,
VOLTAGE_OR_TEMPERATURE_OUT_OF_RANGE = 0xA,
TEMPERATURE_ERROR = 0xB,
CHARGHING_ERROR = 0xF
BATTERY_STATE_UNKNOWN = 0x0,
BATTERY_NOT_PRESENT = 0x1,
BATTERY_DISCHARGHING = 0x2,
BATTERY_CHARGING = 0x3,
BATTERY_NOT_CHARGING = 0x4,
BATTERY_FULL = 0x05
};

struct CONTROLLER_MOTION_PACKET : INPUT_PKT {
Expand Down
25 changes: 21 additions & 4 deletions src/moonlight-server/control/input_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -516,10 +516,27 @@ void controller_battery(const CONTROLLER_BATTERY_PACKET &pkt, state::StreamSessi
if (auto joypad = joypads->find(pkt.controller_number)) {
selected_pad = std::move(*joypad);
if (std::holds_alternative<PS5Joypad>(*selected_pad)) {
// Battery values in Moonlight are in the range [0, 0xFF (255)]
// Inputtino expects them as a percentage [0, 100]
std::get<PS5Joypad>(*selected_pad)
.set_battery(inputtino::PS5Joypad::BATTERY_STATE(pkt.battery_state), pkt.battery_percentage / 2.55);
inputtino::PS5Joypad::BATTERY_STATE state;
switch (pkt.battery_state) {
case BATTERY_STATE_UNKNOWN:
case BATTERY_NOT_PRESENT:
return; // We can't set it, let's return
case BATTERY_DISCHARGHING:
state = inputtino::PS5Joypad::BATTERY_DISCHARGING;
break;
case BATTERY_CHARGING:
state = inputtino::PS5Joypad::BATTERY_CHARGHING;
break;
case BATTERY_NOT_CHARGING:
state = inputtino::PS5Joypad::CHARGHING_ERROR;
break;
case BATTERY_FULL:
state = inputtino::PS5Joypad::BATTERY_FULL;
break;
}
if (pkt.battery_percentage != BATTERY_PERCENTAGE_UNKNOWN) {
std::get<PS5Joypad>(*selected_pad).set_battery(state, pkt.battery_percentage);
}
}
}
}
Expand Down

0 comments on commit 40b0c5c

Please sign in to comment.