From a1aec9a8705af5e6b4810bbf0add0ce412c14e33 Mon Sep 17 00:00:00 2001 From: lifajucejo Date: Thu, 27 Sep 2018 23:46:40 -0400 Subject: [PATCH] Add battery percentage (psm) to switch --- frontend/drivers/platform_switch.c | 46 ++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/frontend/drivers/platform_switch.c b/frontend/drivers/platform_switch.c index 1ad014c407..9157f71366 100644 --- a/frontend/drivers/platform_switch.c +++ b/frontend/drivers/platform_switch.c @@ -59,6 +59,8 @@ static uint64_t frontend_switch_get_mem_used(void); // Splash static uint32_t *splashData = NULL; +static bool psmInitialized = false; + #endif // HAVE_LIBNX static void get_first_valid_core(char *path_return) @@ -163,6 +165,9 @@ static void frontend_switch_deinit(void *data) splashData = NULL; } + if (psmInitialized) + psmExit(); + #ifndef HAVE_OPENGL gfxExit(); #endif @@ -582,6 +587,17 @@ static void frontend_switch_init(void *data) #endif // IS_SALAMANDER #endif // NXLINK + Result rc; + rc = psmInitialize(); + if (R_SUCCEEDED(rc)) + { + psmInitialized = true; + } + else + { + RARCH_WARN("Error initializing psm\n"); + } + rarch_system_info_t *sys_info = runloop_get_system_info(); retro_get_system_info(sys_info); @@ -681,8 +697,34 @@ static uint64_t frontend_switch_get_mem_used(void) static enum frontend_powerstate frontend_switch_get_powerstate(int *seconds, int *percent) { - // This is fine monkaS - return FRONTEND_POWERSTATE_CHARGED; + if (!psmInitialized) + return FRONTEND_POWERSTATE_NONE; + + uint32_t pct; + ChargerType ct; + Result rc; + + rc = psmGetBatteryChargePercentage(&pct); + if (!R_SUCCEEDED(rc)) + return FRONTEND_POWERSTATE_NONE; + + rc = psmGetChargerType(&ct); + if (!R_SUCCEEDED(rc)) + return FRONTEND_POWERSTATE_NONE; + + *percent = (int)pct; + + if (*percent >= 100) + return FRONTEND_POWERSTATE_CHARGED; + + switch (ct) + { + case ChargerType_Charger: + case ChargerType_Usb: + return FRONTEND_POWERSTATE_CHARGING; + } + + return FRONTEND_POWERSTATE_NO_SOURCE; } static void frontend_switch_get_os(char *s, size_t len, int *major, int *minor)