Add support to display current charge, and indicate whether or not device is charging (#67)

* Add support to display current charge, and indicate whether or not device is charging.
Updated Makefile.pc to incorporate new icon binaries

* Refactored power-related code, added drawIcon

Added common power interface

* Add battery icon and shift charge text as required
This commit is contained in:
Daniel Bernard 2018-10-09 17:27:43 -05:00 committed by yellows8
parent afa9bd49d0
commit 8b86b36393
11 changed files with 121 additions and 0 deletions

View File

@ -26,6 +26,7 @@ test : pc_main/main.cpp pc_main/pc_launch.c \
build_pc/invalid_icon.bin.o build_pc/folder_icon.bin.o \ build_pc/invalid_icon.bin.o build_pc/folder_icon.bin.o \
build_pc/hbmenu_logo_light.bin.o build_pc/hbmenu_logo_dark.bin.o \ build_pc/hbmenu_logo_light.bin.o build_pc/hbmenu_logo_dark.bin.o \
build_pc/theme_icon_dark.bin.o build_pc/theme_icon_light.bin.o \ build_pc/theme_icon_dark.bin.o build_pc/theme_icon_light.bin.o \
build_pc/charging_icon.bin.o build_pc/battery_icon.bin.o \
#build_pc/tahoma24.o build_pc/tahoma12.o build_pc/interuimedium20.o build_pc/interuimedium30.o build_pc/interuiregular14.o build_pc/interuiregular18.o #build_pc/tahoma24.o build_pc/tahoma12.o build_pc/interuimedium20.o build_pc/interuimedium30.o build_pc/interuiregular14.o build_pc/interuiregular18.o
gcc -Wall -O2 -g -DVERSION=\"v$(APP_VERSION)\" $(EXTRA_CFLAGS) `pkg-config freetype2 --cflags` $^ -lsfml-graphics -lsfml-window -lsfml-system -lstdc++ `pkg-config freetype2 --libs` -lm -lz -lconfig -lturbojpeg $(EXTRA_LDFLAGS) -I. -iquote $(DEVKITPRO)/libnx/include -Ibuild_pc -g -o $@ gcc -Wall -O2 -g -DVERSION=\"v$(APP_VERSION)\" $(EXTRA_CFLAGS) `pkg-config freetype2 --cflags` $^ -lsfml-graphics -lsfml-window -lsfml-system -lstdc++ `pkg-config freetype2 --libs` -lm -lz -lconfig -lturbojpeg $(EXTRA_LDFLAGS) -I. -iquote $(DEVKITPRO)/libnx/include -Ibuild_pc -g -o $@
@ -89,6 +90,16 @@ build_pc/theme_icon_dark.bin.o : data/theme_icon_dark.bin
@echo $(notdir $<) @echo $(notdir $<)
@$(bin2o) @$(bin2o)
build_pc/charging_icon.bin.o : data/charging_icon.bin
mkdir -p $(dir $@)
@echo $(notdir $<)
@$(bin2o)
build_pc/battery_icon.bin.o : data/battery_icon.bin
mkdir -p $(dir $@)
@echo $(notdir $<)
@$(bin2o)
clean: clean:
rm -rf build_pc/ test test.* rm -rf build_pc/ test test.*

View File

@ -58,6 +58,7 @@ typedef union {
#include "math.h" #include "math.h"
#include "theme.h" #include "theme.h"
#include "message-box.h" #include "message-box.h"
#include "power.h"
void menuStartupPath(void); void menuStartupPath(void);
void menuStartup(void); void menuStartup(void);

View File

@ -6,6 +6,8 @@
#include "folder_icon_bin.h" #include "folder_icon_bin.h"
#include "theme_icon_dark_bin.h" #include "theme_icon_dark_bin.h"
#include "theme_icon_light_bin.h" #include "theme_icon_light_bin.h"
#include "charging_icon_bin.h"
#include "battery_icon_bin.h"
char rootPathBase[PATH_MAX]; char rootPathBase[PATH_MAX];
char rootPath[PATH_MAX+8]; char rootPath[PATH_MAX+8];
@ -107,6 +109,21 @@ static void drawImage(int x, int y, int width, int height, const uint8_t *image,
} }
} }
//Draws an RGBA8888 image masked by the passed color.
static void drawIcon(int x, int y, int width, int height, const uint8_t *image, color_t color) {
int tmpx, tmpy;
int pos;
color_t current_color;
for (tmpy=0; tmpy<height; tmpy++) {
for (tmpx=0; tmpx<width; tmpx++) {
pos = ((tmpy*width) + tmpx) * 4;
current_color = MakeColor(color.r, color.g, color.b, image[pos+3]);
DrawPixel(x+tmpx, y+tmpy, current_color);
}
}
}
uint8_t *folder_icon_small; uint8_t *folder_icon_small;
uint8_t *invalid_icon_small; uint8_t *invalid_icon_small;
uint8_t *theme_icon_small; uint8_t *theme_icon_small;
@ -406,6 +423,29 @@ void drawTime() {
} }
void drawCharge() {
char chargeString[5];
uint32_t batteryCharge;
bool isCharging;
bool validPower;
validPower = powerGetDetails(&batteryCharge, &isCharging);
if (validPower)
{
batteryCharge = (batteryCharge > 100) ? 100 : batteryCharge;
sprintf(chargeString, "%d%%", batteryCharge);
int tmpX = GetTextXCoordinate(interuiregular14, 1180, chargeString, 'r');
DrawText(interuiregular14, tmpX - 15, 0 + 47 + 10 + 21, themeCurrent.textColor, chargeString);
drawIcon(1180 - 11, 0 + 47 + 10 + 6, 10, 15, battery_icon_bin, themeCurrent.textColor);
if (isCharging)
drawIcon(tmpX - 32, 0 + 47 + 10 + 6, 9, 15, charging_icon_bin, themeCurrent.textColor);
}
}
void drawBackBtn(menu_s* menu, bool emptyDir) { void drawBackBtn(menu_s* menu, bool emptyDir) {
int x_image = 1280 - 252 - 30 - 32; int x_image = 1280 - 252 - 30 - 32;
int x_text = 1280 - 216 - 30 - 32; int x_text = 1280 - 216 - 30 - 32;
@ -462,6 +502,7 @@ void menuLoop(void) {
#endif #endif
drawTime(); drawTime();
drawCharge();
if (menu->nEntries==0 || hbmenu_state == HBMENU_NETLOADER_ACTIVE) if (menu->nEntries==0 || hbmenu_state == HBMENU_NETLOADER_ACTIVE)
{ {

8
common/power.h Normal file
View File

@ -0,0 +1,8 @@
#pragma once
#include "common.h"
void powerInit(void);
bool powerGetDetails(uint32_t *batteryCharge, bool *isCharging);
void powerExit(void);

BIN
data/battery_icon.bin Normal file

Binary file not shown.

BIN
data/charging_icon.bin Normal file

Binary file not shown.

View File

@ -45,6 +45,7 @@ int main(int argc, char **argv)
themeStartup((ThemePreset)theme); themeStartup((ThemePreset)theme);
textInit(); textInit();
powerInit();
menuStartup(); menuStartup();
launchInit(); launchInit();
@ -101,6 +102,7 @@ int main(int argc, char **argv)
fontExit(); fontExit();
launchExit(); launchExit();
powerExit();
plExit(); plExit();
setsysExit(); setsysExit();

43
nx_main/nx_power.c Normal file
View File

@ -0,0 +1,43 @@
#include <switch.h>
#include "../common/common.h"
static bool psmInitialized;
bool powerGetDetails(uint32_t *batteryCharge, bool *isCharging) {
ChargerType charger = ChargerType_None;
bool hwReadsSucceeded = false;
Result rc = 0;
*isCharging = false;
*batteryCharge = 0;
if (psmInitialized)
{
rc = psmGetBatteryChargePercentage(batteryCharge);
hwReadsSucceeded = R_SUCCEEDED(rc);
rc = psmGetChargerType(&charger);
hwReadsSucceeded &= R_SUCCEEDED(rc);
*isCharging = (charger > ChargerType_None);
}
return hwReadsSucceeded;
}
void powerInit(void) {
if (!psmInitialized)
{
Result rc = psmInitialize();
if (R_SUCCEEDED(rc))
{
psmInitialized = true;
}
}
}
void powerExit(void) {
if (psmInitialized)
{
psmExit();
psmInitialized = false;
}
}

15
pc_main/pc_power.c Normal file
View File

@ -0,0 +1,15 @@
#include "../common/common.h"
void powerInit(void) {
}
void powerExit(void) {
}
bool powerGetDetails(uint32_t *batteryCharge, bool *isCharging)
*isCharging = false;
*batteryCharge = 100;
return false;
}

BIN
resources/battery_icon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 188 B

BIN
resources/charging_icon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 380 B