Skip to content

Commit

Permalink
[Switch] Implement LibNX psm.h for power_switch.cpp
Browse files Browse the repository at this point in the history
It handles everything but power_seconds_left.
  • Loading branch information
halotroop2288 committed Jan 24, 2024
1 parent 8e30da2 commit 1b815f9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
39 changes: 31 additions & 8 deletions platform/switch/power_switch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,47 @@
/**************************************************************************/

#include "power_switch.h"

#include "switch_wrapper.h"

PowerSwitch::PowerSwitch() {
nsecs_left = 0;
percent_left = 0;
power_state = OS::PowerState::POWERSTATE_ON_BATTERY;
if (R_SUCCEEDED(psmInitialize())) {
initialized = true;
}
}

PowerSwitch::~PowerSwitch() {}

OS::PowerState PowerSwitch::get_power_state() {
return power_state;
if (!initialized) {
return OS::POWERSTATE_UNKNOWN;
}

bool enough_power;
psmIsEnoughPowerSupplied(&enough_power);

if (!enough_power) {
return OS::PowerState::POWERSTATE_ON_BATTERY;
}

int percentage = PowerSwitch::get_power_percent_left();

if (percentage == 100) {
return OS::PowerState::POWERSTATE_CHARGED;
}

return OS::PowerState::POWERSTATE_CHARGING;
}

int PowerSwitch::get_power_seconds_left() {
return nsecs_left / 1000000;
WARN_PRINT("power_seconds_left is not implemented on this platform, defaulting to -1");
return -1;
}

int PowerSwitch::get_power_percent_left() {
return percent_left;
if (!initialized) {
return -1;
}

u32 voltage_percentage;
psmGetBatteryChargePercentage(&voltage_percentage);
return (int)voltage_percentage;
}
15 changes: 9 additions & 6 deletions platform/switch/power_switch.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,24 @@
/**************************************************************************/

#pragma once

#ifndef POWER_SWITCH_H
#define POWER_SWITCH_H

#include "core/os/os.h"

class PowerSwitch {
private:
int nsecs_left;
int percent_left;
OS::PowerState power_state;
bool initialized = false;

bool GetPowerInfo_Switch();

public:
PowerSwitch();
virtual ~PowerSwitch();

OS::PowerState get_power_state();
int get_power_seconds_left();
int get_power_percent_left();

PowerSwitch();
};

#endif // POWER_SWITCH_H

0 comments on commit 1b815f9

Please sign in to comment.