Use psm StateChangeEvent to cache isCharging.
This commit is contained in:
parent
35f48a59d0
commit
2695d48ba7
@ -2,42 +2,71 @@
|
||||
#include "../common/common.h"
|
||||
|
||||
static bool psmInitialized;
|
||||
static bool psmCacheInitialized;
|
||||
static uint32_t psmCacheCharge;
|
||||
static bool psmCacheIsCharging;
|
||||
|
||||
bool powerGetDetails(uint32_t *batteryCharge, bool *isCharging) {
|
||||
ChargerType charger = ChargerType_None;
|
||||
bool hwReadsSucceeded = false;
|
||||
bool use_cache = false;
|
||||
Result rc = 0;
|
||||
|
||||
*isCharging = false;
|
||||
*batteryCharge = 0;
|
||||
|
||||
if (psmInitialized)
|
||||
{
|
||||
if (psmInitialized) {
|
||||
if (psmCacheInitialized) {
|
||||
rc = psmWaitStateChangeEvent(0);
|
||||
|
||||
if (R_FAILED(rc)) use_cache = true;
|
||||
}
|
||||
|
||||
rc = psmGetBatteryChargePercentage(batteryCharge);
|
||||
hwReadsSucceeded = R_SUCCEEDED(rc);
|
||||
if (use_cache) {
|
||||
*isCharging = psmCacheIsCharging;
|
||||
}
|
||||
else {
|
||||
rc = psmGetChargerType(&charger);
|
||||
hwReadsSucceeded &= R_SUCCEEDED(rc);
|
||||
*isCharging = (charger > ChargerType_None);
|
||||
}
|
||||
|
||||
psmCacheCharge = *batteryCharge;
|
||||
psmCacheIsCharging = *isCharging;
|
||||
psmCacheInitialized = true;
|
||||
}
|
||||
|
||||
return hwReadsSucceeded;
|
||||
}
|
||||
|
||||
void powerInit(void) {
|
||||
if (!psmInitialized)
|
||||
{
|
||||
uint32_t charge=0;
|
||||
bool isCharging=0;
|
||||
|
||||
psmCacheInitialized = false;
|
||||
psmCacheCharge = 0;
|
||||
psmCacheIsCharging = false;
|
||||
|
||||
if (!psmInitialized) {
|
||||
Result rc = psmInitialize();
|
||||
if (R_SUCCEEDED(rc))
|
||||
{
|
||||
if (R_SUCCEEDED(rc)) {
|
||||
rc = psmBindStateChangeEvent(1, 1, 1);
|
||||
|
||||
if (R_FAILED(rc)) psmExit();
|
||||
if (R_SUCCEEDED(rc)) {
|
||||
psmInitialized = true;
|
||||
powerGetDetails(&charge, &isCharging);//Init the cache.
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void powerExit(void) {
|
||||
if (psmInitialized)
|
||||
{
|
||||
if (psmInitialized) {
|
||||
psmExit();
|
||||
psmInitialized = false;
|
||||
psmCacheInitialized = false;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user