Skip to content

Commit f54c709

Browse files
committed
rosalina: add extra battery info
- Battery percentage now has a resolution of 0.1% - Battery temperature and battery voltage are now displayed
1 parent 168647e commit f54c709

File tree

1 file changed

+74
-16
lines changed
  • sysmodules/rosalina/source

1 file changed

+74
-16
lines changed

sysmodules/rosalina/source/menu.c

Lines changed: 74 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "minisoc.h"
3838

3939
bool isHidInitialized = false;
40+
u32 mcuFwVersion = 0;
4041

4142
// libctru redefinition:
4243

@@ -142,7 +143,59 @@ u32 waitCombo(void)
142143

143144
static MyThread menuThread;
144145
static u8 ALIGN(8) menuThreadStack[0x1000];
145-
static u8 batteryLevel = 255;
146+
147+
static float batteryPercentage;
148+
static float batteryVoltage;
149+
static u8 batteryTemperature;
150+
151+
static Result menuUpdateMcuInfo(void)
152+
{
153+
Result res = 0;
154+
u8 data[4];
155+
156+
if (!isServiceUsable("mcu::HWC"))
157+
return -1;
158+
159+
res = mcuHwcInit();
160+
if (R_FAILED(res))
161+
return res;
162+
163+
// Read mcu regs directly
164+
for (u32 i = 0; i < 4; i++)
165+
{
166+
res = MCUHWC_ReadRegister(0xA + i, &data[i], 1);
167+
if (R_FAILED(res))
168+
break;
169+
}
170+
171+
if (R_SUCCEEDED(res))
172+
{
173+
batteryTemperature = data[0];
174+
175+
// The battery percentage isn't very precise... its precision ranges from 0.09% to 0.14% approx
176+
// Round to 0.1%
177+
batteryPercentage = data[1] + data[2] / 256.0f;
178+
batteryPercentage = (u32)((batteryPercentage + 0.05f) * 10.0f) / 10.0f;
179+
180+
// Round battery voltage to 0.01V
181+
batteryVoltage = (5u * data[3]) / 256.0f;
182+
batteryVoltage = (u32)((batteryVoltage + 0.005f) * 100.0f) / 100.0f;
183+
}
184+
185+
// Read mcu fw version if not already done
186+
if (mcuFwVersion == 0)
187+
{
188+
u8 minor = 0, major = 0;
189+
MCUHWC_GetFwVerHigh(&major);
190+
MCUHWC_GetFwVerLow(&minor);
191+
192+
// If it has failed, mcuFwVersion will be set to 0 again
193+
mcuFwVersion = SYSTEM_VERSION(major - 0x10, minor, 0);
194+
}
195+
196+
mcuHwcExit();
197+
return res;
198+
}
146199

147200
static inline u32 menuAdvanceCursor(u32 pos, u32 numItems, s32 displ)
148201
{
@@ -246,16 +299,8 @@ static void menuDraw(Menu *menu, u32 selected)
246299
s64 out;
247300
u32 version, commitHash;
248301
bool isRelease;
249-
bool isMcuHwcRegistered;
250302

251-
if(R_SUCCEEDED(srvIsServiceRegistered(&isMcuHwcRegistered, "mcu::HWC")) && isMcuHwcRegistered && R_SUCCEEDED(mcuHwcInit()))
252-
{
253-
if(R_FAILED(MCUHWC_GetBatteryLevel(&batteryLevel)))
254-
batteryLevel = 255;
255-
mcuHwcExit();
256-
}
257-
else
258-
batteryLevel = 255;
303+
Result mcuInfoRes = menuUpdateMcuInfo();
259304

260305
svcGetSystemInfo(&out, 0x10000, 0);
261306
version = (u32)out;
@@ -285,6 +330,10 @@ static void menuDraw(Menu *menu, u32 selected)
285330
dispY += SPACING_Y;
286331
}
287332

333+
// Clear lines that are prone to change
334+
Draw_DrawFormattedString(SCREEN_BOT_WIDTH - 10 - SPACING_X * 15, 10, COLOR_WHITE, "%15s", "");
335+
Draw_DrawFormattedString(SCREEN_BOT_WIDTH - 10 - SPACING_X * 19, SCREEN_BOT_HEIGHT - 20, COLOR_WHITE, "%19s", "");
336+
288337
if(miniSocEnabled)
289338
{
290339
char ipBuffer[17];
@@ -294,12 +343,21 @@ static void menuDraw(Menu *menu, u32 selected)
294343
Draw_DrawString(SCREEN_BOT_WIDTH - 10 - SPACING_X * n, 10, COLOR_WHITE, ipBuffer);
295344
}
296345

297-
Draw_DrawFormattedString(SCREEN_BOT_WIDTH - 10 - 4 * SPACING_X, SCREEN_BOT_HEIGHT - 20, COLOR_WHITE, " ");
298-
299-
if(batteryLevel != 255)
300-
Draw_DrawFormattedString(SCREEN_BOT_WIDTH - 10 - 4 * SPACING_X, SCREEN_BOT_HEIGHT - 20, COLOR_WHITE, "%02hhu%%", batteryLevel);
301-
else
302-
Draw_DrawString(SCREEN_BOT_WIDTH - 10 - 4 * SPACING_X, SCREEN_BOT_HEIGHT - 20, COLOR_WHITE, " ");
346+
if(R_SUCCEEDED(mcuInfoRes))
347+
{
348+
u32 voltageInt = (u32)batteryVoltage;
349+
u32 voltageFrac = (u32)(batteryVoltage * 100.0f) % 100u;
350+
u32 percentageInt = (u32)batteryPercentage;
351+
u32 percentageFrac = (u32)(batteryPercentage * 10.0f) % 10u;
352+
353+
char buf[32];
354+
int n = sprintf(
355+
buf, "%hhu\xF8""C %lu.%02luV %lu.%lu%%", batteryTemperature, // CP437
356+
voltageInt, voltageFrac,
357+
percentageInt, percentageFrac
358+
);
359+
Draw_DrawString(SCREEN_BOT_WIDTH - 10 - SPACING_X * n, SCREEN_BOT_HEIGHT - 20, COLOR_WHITE, buf);
360+
}
303361

304362
if(isRelease)
305363
Draw_DrawFormattedString(10, SCREEN_BOT_HEIGHT - 20, COLOR_TITLE, "Luma3DS %s", versionString);

0 commit comments

Comments
 (0)