Skip to content

Commit

Permalink
Support battery indicator on RetroFW
Browse files Browse the repository at this point in the history
  • Loading branch information
phcoder committed May 6, 2022
1 parent 9a3b8f4 commit 25a8e05
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
62 changes: 62 additions & 0 deletions dingux/dingux_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@
/* Miyoo defines */
#define MIYOO_BATTERY_VOLTAGE_NOW_FILE "/sys/class/power_supply/miyoo-battery/voltage_now"

/* RetroFW */
#define RETROFW_BATTERY_VOLTAGE_NOW_FILE "/proc/jz/battery"

/* Enables/disables downscaling when using
* the IPU hardware scaler */
bool dingux_ipu_set_downscaling_enable(bool enable)
Expand Down Expand Up @@ -249,6 +252,64 @@ bool dingux_ipu_reset(void)
#endif
}

#if defined(RETROFW)
static uint64_t read_battery_ignore_size(const char *path)
{
int64_t file_len = 0;
char file_buf[20];
int sys_file_value = 0;
RFILE *file;

/* Check whether file exists */
if (!path_is_valid(path))
return -1;

memset(file_buf, 0, sizeof(file_buf));

file = filestream_open(path,
RETRO_VFS_FILE_ACCESS_READ,
RETRO_VFS_FILE_ACCESS_HINT_NONE);

if (!file)
{
return -1;
}

file_len = filestream_read(file, file_buf, sizeof(file_buf) - 1);
if (filestream_close(file) != 0)
if (file)
free(file);

if (file_len <= 0)
return -1;

return strtoul(file_buf, NULL, 10);
}

int retrofw_get_battery_level(enum frontend_powerstate *state)
{
/* retrofw battery only provides "voltage_now". Values are based on gmenu2x with some interpolation */
uint32_t rawval = read_battery_ignore_size(RETROFW_BATTERY_VOLTAGE_NOW_FILE);
int voltage_now = rawval & 0x7fffffff;
if (voltage_now > 10000) {
*state = FRONTEND_POWERSTATE_NONE;
return -1;
}
if (rawval & 0x80000000) {
*state = FRONTEND_POWERSTATE_CHARGING;
if (voltage_now > 4000)
*state = FRONTEND_POWERSTATE_CHARGED;
} else
*state = FRONTEND_POWERSTATE_ON_POWER_SOURCE;
if (voltage_now < 0) return -1; // voltage_now not available
if (voltage_now > 4000) return 100;
if (voltage_now > 3700) return 40 + (voltage_now - 3700) / 5;
if (voltage_now > 3520) return 20 + (voltage_now - 3520) / 9;
if (voltage_now > 3330) return 1 + (voltage_now - 3330) * 10;
return 0;
}
#else

static int dingux_read_battery_sys_file(const char *path)
{
int64_t file_len = 0;
Expand Down Expand Up @@ -333,6 +394,7 @@ int dingux_get_battery_level(void)
return dingux_read_battery_sys_file(DINGUX_BATTERY_CAPACITY_FILE);
#endif
}
#endif

/* Fetches the path of the base 'retroarch'
* directory */
Expand Down
5 changes: 5 additions & 0 deletions dingux/dingux_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

#include <boolean.h>

#include <frontend/frontend_driver.h>

RETRO_BEGIN_DECLS

/* Specifies all possible image filtering
Expand Down Expand Up @@ -101,6 +103,9 @@ bool dingux_ipu_reset(void);
/* Fetches internal battery level */
int dingux_get_battery_level(void);

/* Fetches internal battery level */
int retrofw_get_battery_level(enum frontend_powerstate *source);

/* Fetches the path of the base 'retroarch'
* directory */
void dingux_get_base_path(char *path, size_t len);
Expand Down
5 changes: 5 additions & 0 deletions frontend/drivers/platform_unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -1222,6 +1222,11 @@ static enum frontend_powerstate frontend_unix_get_powerstate(
*percent = battery_level;

ret = (enum frontend_powerstate)powerstate;
#elif defined(RETROFW)
*percent = retrofw_get_battery_level(&ret);

/* 'Time left' reporting is unsupported */
*seconds = -1;
#elif defined(DINGUX)
/* Dingux seems to have limited battery
* reporting capability - if we get a valid
Expand Down

0 comments on commit 25a8e05

Please sign in to comment.