diff --git a/platform/switch/power_switch.cpp b/platform/switch/power_switch.cpp index 51b40a9ac300..949baac9a6e6 100644 --- a/platform/switch/power_switch.cpp +++ b/platform/switch/power_switch.cpp @@ -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; } diff --git a/platform/switch/power_switch.h b/platform/switch/power_switch.h index be83c6fc40e9..48767a72bc10 100644 --- a/platform/switch/power_switch.h +++ b/platform/switch/power_switch.h @@ -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