all repos — mgba @ 3b784485f81a6eada120a99b9efc09d20a8a75ed

mGBA Game Boy Advance Emulator

mGUI: Show battery percentage
Vicki Pfau vi@endrift.com
Sun, 09 Aug 2020 17:56:26 -0700
commit

3b784485f81a6eada120a99b9efc09d20a8a75ed

parent

d746cb16d6350c587a4bca70e80be6aaae8c659b

M CHANGESCHANGES

@@ -47,6 +47,7 @@ - GB: Allow pausing event loop while CPU is blocked

- GBA: Allow pausing event loop while CPU is blocked - Debugger: Keep track of global cycle count - FFmpeg: Add looping option for GIF/APNG + - mGUI: Show battery percentage - Qt: Renderer can be changed while a game is running - Qt: Add hex index to palette view - Qt: Add transformation matrix info to sprite view
M include/mgba-util/gui.hinclude/mgba-util/gui.h

@@ -42,13 +42,15 @@ };

enum { BATTERY_EMPTY = 0, - BATTERY_LOW = 1, - BATTERY_HALF = 2, - BATTERY_HIGH = 3, - BATTERY_FULL = 4, + BATTERY_LOW = 25, + BATTERY_HALF = 50, + BATTERY_HIGH = 75, + BATTERY_FULL = 100, + BATTERY_VALUE = 0x7F, + BATTERY_PERCENTAGE_VALID = 0x80, - BATTERY_CHARGING = 8, - BATTERY_NOT_PRESENT = 16 + BATTERY_CHARGING = 0x100, + BATTERY_NOT_PRESENT = 0x200, }; struct GUIBackground {
M src/platform/3ds/cia.rsf.insrc/platform/3ds/cia.rsf.in

@@ -154,14 +154,13 @@ - boss:U

- cam:u - cecd:u - cfg:u - - dlp:FKCL - - dlp:SRVR - dsp::DSP - frd:u - fs:USER - gsp::Gpu - hid:USER - http:C + - mcu::HWC - mic:u - ndm:u - news:u
M src/platform/3ds/main.csrc/platform/3ds/main.c

@@ -187,6 +187,7 @@

camExit(); ndspExit(); ptmuExit(); + mcuHwcExit(); } static void _map3DSKey(struct mInputMap* map, int ctrKey, enum GBAKey key) {

@@ -237,14 +238,19 @@

static int _batteryState(void) { u8 charge; u8 adapter; - PTMU_GetBatteryLevel(&charge); PTMU_GetBatteryChargeState(&adapter); + int state = 0; + if (R_SUCCEEDED(MCUHWC_GetBatteryLevel(&charge))) { + charge |= BATTERY_PERCENTAGE_VALID; + } else { + PTMU_GetBatteryLevel(&charge); + if (charge > 0) { + --charge; + } + } if (adapter) { state |= BATTERY_CHARGING; - } - if (charge > 0) { - --charge; } return state | charge; }

@@ -814,6 +820,7 @@ camera.bufferSize = 0;

camera.cam = SELECT_IN1; ptmuInit(); + mcuHwcInit(); camInit(); hasSound = NO_SOUND;
M src/platform/psp2/main.csrc/platform/psp2/main.c

@@ -72,13 +72,12 @@ return GUI_CURSOR_DOWN;

} static int _batteryState(void) { - int charge = scePowerGetBatteryLifePercent(); + int charge = scePowerGetBatteryLifePercent() | BATTERY_PERCENTAGE_VALID; int adapter = scePowerIsPowerOnline(); int state = 0; if (adapter) { state |= BATTERY_CHARGING; } - charge /= 25; return state | charge; }
M src/platform/switch/main.csrc/platform/switch/main.c

@@ -607,7 +607,7 @@ static int _batteryState(void) {

u32 charge; int state = 0; if (R_SUCCEEDED(psmGetBatteryChargePercentage(&charge))) { - state = (charge + 12) / 25; + state = charge | BATTERY_PERCENTAGE_VALID; } else { return BATTERY_NOT_PRESENT; }
M src/util/gui/font-metrics.csrc/util/gui/font-metrics.c

@@ -137,11 +137,11 @@ { 0, 0, { 0, 0, 0, 0 }}, // 0x7F

}; const struct GUIIconMetric defaultIconMetrics[] = { - [GUI_ICON_BATTERY_FULL] = { 0, 0, 32, 16 }, - [GUI_ICON_BATTERY_HIGH] = { 32, 0, 32, 16 }, - [GUI_ICON_BATTERY_HALF] = { 64, 0, 32, 16 }, - [GUI_ICON_BATTERY_LOW] = { 96, 0, 32, 16 }, - [GUI_ICON_BATTERY_EMPTY] = { 128, 0, 32, 16 }, + [GUI_ICON_BATTERY_FULL] = { 0, 2, 32, 12 }, + [GUI_ICON_BATTERY_HIGH] = { 32, 2, 32, 12 }, + [GUI_ICON_BATTERY_HALF] = { 64, 2, 32, 12 }, + [GUI_ICON_BATTERY_LOW] = { 96, 2, 32, 12 }, + [GUI_ICON_BATTERY_EMPTY] = { 128, 2, 32, 12 }, [GUI_ICON_SCROLLBAR_BUTTON] = { 6, 16, 4, 5 }, [GUI_ICON_SCROLLBAR_TRACK] = { 23, 16, 2, 16 }, [GUI_ICON_SCROLLBAR_THUMB] = { 38, 16, 4, 16 },
M src/util/gui/font.csrc/util/gui/font.c

@@ -43,7 +43,7 @@ uint32_t c = utf8Char(&text, &len);

if (c == '\1') { c = utf8Char(&text, &len); if (c < GUI_ICON_MAX) { - GUIFontDrawIcon(font, x, y, GUI_ALIGN_BOTTOM, GUI_ORIENT_0, color, c); + GUIFontDrawIcon(font, x, y, (align & GUI_ALIGN_HCENTER) | GUI_ALIGN_BOTTOM, GUI_ORIENT_0, color, c); unsigned w; GUIFontIconMetrics(font, c, &w, 0); x += w;
M src/util/gui/menu.csrc/util/gui/menu.c

@@ -264,20 +264,20 @@ if (state == BATTERY_NOT_PRESENT) {

return; } uint32_t color = 0xFF000000; - if (state == (BATTERY_CHARGING | BATTERY_FULL)) { + if ((state & (BATTERY_CHARGING | BATTERY_FULL)) == (BATTERY_CHARGING | BATTERY_FULL)) { color |= 0xFFC060; } else if (state & BATTERY_CHARGING) { color |= 0x60FF60; - } else if (state >= BATTERY_HALF) { + } else if ((state & BATTERY_VALUE) >= BATTERY_HALF) { color |= 0xFFFFFF; - } else if (state == BATTERY_LOW) { + } else if ((state & BATTERY_VALUE) >= BATTERY_LOW) { color |= 0x30FFFF; } else { color |= 0x3030FF; } enum GUIIcon batteryIcon; - switch (state & ~BATTERY_CHARGING) { + switch ((state & BATTERY_VALUE) - (state & BATTERY_VALUE) % 25) { case BATTERY_EMPTY: batteryIcon = GUI_ICON_BATTERY_EMPTY; break;

@@ -298,7 +298,12 @@ batteryIcon = GUI_ICON_BATTERY_EMPTY;

break; } - GUIFontDrawIcon(params->font, params->width, 0, GUI_ALIGN_RIGHT, GUI_ORIENT_0, color, batteryIcon); + GUIFontDrawIcon(params->font, params->width, GUIFontHeight(params->font) + 2, GUI_ALIGN_RIGHT | GUI_ALIGN_BOTTOM, GUI_ORIENT_0, color, batteryIcon); + if (state & BATTERY_PERCENTAGE_VALID) { + unsigned width; + GUIFontIconMetrics(params->font, batteryIcon, &width, NULL); + GUIFontPrintf(params->font, params->width - width, GUIFontHeight(params->font), GUI_ALIGN_RIGHT, color, "%u%%", state & BATTERY_VALUE); + } } void GUIDrawClock(struct GUIParams* params) {