Skip to content

Commit

Permalink
feat: implemented DualSense battery support
Browse files Browse the repository at this point in the history
  • Loading branch information
ABeltramo committed Mar 14, 2024
1 parent 24ae4cb commit 29825e2
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
12 changes: 6 additions & 6 deletions include/inputtino/input.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -377,12 +377,12 @@ class PS5Joypad : public Joypad {
void set_motion(MOTION_TYPE type, float x, float y, float z);

enum BATTERY_STATE : uint8_t {
NOT_KNOWN = 0x00,
NOT_PRESENT = 0x01,
DISCHARGING = 0x02,
CHARGHING = 0x03,
NOT_CHARGING = 0x04,
FULL = 0x05
BATTERY_DISCHARGING = 0x0,
BATTERY_CHARGHING = 0x1,
BATTERY_FULL = 0x2,
VOLTAGE_OR_TEMPERATURE_OUT_OF_RANGE = 0xA,
TEMPERATURE_ERROR = 0xB,
CHARGHING_ERROR = 0xF
};

void set_battery(BATTERY_STATE state, int percentage);
Expand Down
8 changes: 5 additions & 3 deletions src/uhid/include/uhid/ps5.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,9 +369,11 @@ struct dualsense_input_report_usb {
uint8_t r2_adaptive_trigger = 0;
uint8_t l2_adaptive_trigger = 0;
uint8_t reserved4[9] = {};
/* Battery status */
uint8_t status = 0;
uint8_t reserved5[10] = {};

uint8_t battery_charge : 4;
uint8_t battery_status : 4;
uint8_t battery2 = 0x0c;
uint8_t reserved6[9] = {};
};

enum FLAG0 : uint8_t {
Expand Down
8 changes: 7 additions & 1 deletion src/uhid/joypad_ps5.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,13 @@ void PS5Joypad::set_motion(PS5Joypad::MOTION_TYPE type, float x, float y, float
}

void PS5Joypad::set_battery(PS5Joypad::BATTERY_STATE state, int percentage) {
// TODO
/*
* Each unit of battery data corresponds to 10%
* 0 = 0-9%, 1 = 10-19%, .. and 10 = 100%
*/
this->_state->current_state.battery_charge = std::lround((percentage / 10));
this->_state->current_state.battery_status = state;
send_report(*this->_state);
}

void PS5Joypad::set_on_led(const std::function<void(int, int, int)> &callback) {
Expand Down
9 changes: 8 additions & 1 deletion tests/testJoypads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,14 @@ TEST_CASE_METHOD(SDLTestsFixture, "PS Joypad", "[SDL]") {
joypad.release_finger(0);
joypad.release_finger(1);
}
// TODO: test battery

{ // Test battery
joypad.set_battery(inputtino::PS5Joypad::BATTERY_CHARGHING, 80);
auto joy = SDL_GameControllerGetJoystick(gc);
auto level = SDL_JoystickCurrentPowerLevel(joy);
if (level != SDL_JOYSTICK_POWER_UNKNOWN) // TODO: SDL doesn't seem to pick it up..
REQUIRE(level == SDL_JOYSTICK_POWER_MEDIUM);
}

// Adaptive triggers aren't supported by SDL
// see:https://github.com/libsdl-org/SDL/issues/5125#issuecomment-1204261666
Expand Down

0 comments on commit 29825e2

Please sign in to comment.