Support battery indicator on RetroFW

This commit is contained in:
Vladimir Serbinenko 2022-05-06 17:28:39 +02:00
parent 9a3b8f4a0a
commit 25a8e05597
3 changed files with 72 additions and 0 deletions

View File

@ -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)
@ -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;
@ -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 */

View File

@ -23,6 +23,8 @@
#include <boolean.h>
#include <frontend/frontend_driver.h>
RETRO_BEGIN_DECLS
/* Specifies all possible image filtering
@ -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);

View File

@ -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