Skip to content

Commit

Permalink
updated battery logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaun Inman committed May 4, 2022
1 parent 4e33ccd commit 91da5a2
Show file tree
Hide file tree
Showing 16 changed files with 107 additions and 101 deletions.
Binary file added skeleton/.system/res/bad-battery-fill.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added skeleton/.system/res/bad-battery-line.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed skeleton/.system/res/battery-0.png
Binary file not shown.
Binary file removed skeleton/.system/res/battery-1.png
Binary file not shown.
Binary file removed skeleton/.system/res/battery-2.png
Binary file not shown.
Binary file removed skeleton/.system/res/battery-3.png
Binary file not shown.
Binary file removed skeleton/.system/res/battery-4.png
Binary file not shown.
Binary file removed skeleton/.system/res/battery-5.png
Binary file not shown.
File renamed without changes
Binary file added skeleton/.system/res/battery-fill.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added skeleton/.system/res/battery-line.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
102 changes: 32 additions & 70 deletions src/common/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -408,8 +408,11 @@ static SDL_Color gray = {GRAY_TRIAD};
static SDL_Color shadow25 = {SHADOW25_TRIAD};
static SDL_Color shadow50 = {SHADOW50_TRIAD};

#define BATTERY_IMAGE_COUNT 7
static SDL_Surface* battery[BATTERY_IMAGE_COUNT];
static SDL_Surface* battery_charging;
static SDL_Surface* battery_fill;
static SDL_Surface* battery_line;
static SDL_Surface* battery_fill_bad;
static SDL_Surface* battery_line_bad;
void GFX_init(void) {
char font_path[256];
sprintf(font_path, "%s/%s", Paths.resDir, "BPreplayBold-unhinted.otf");
Expand All @@ -432,14 +435,13 @@ void GFX_init(void) {
settings_volume = GFX_loadImage("settings-volume.png");
settings_mute = GFX_loadImage("settings-mute.png");

char battery_path[256];
for (int i=0; i<BATTERY_IMAGE_COUNT; i++) {
sprintf(battery_path, "battery-%i.png", i);
battery[i] = GFX_loadImage(battery_path);
}
battery_charging = GFX_loadImage("battery-charging.png");
battery_fill = GFX_loadImage("battery-fill.png");
battery_line = GFX_loadImage("battery-line.png");
battery_fill_bad = GFX_loadImage("bad-battery-fill.png");
battery_line_bad = GFX_loadImage("bad-battery-line.png");

puts("GFX_init");
fflush(stdout);
puts("GFX_init"); fflush(stdout);
}
void GFX_ready(void) {
screen = SDL_GetVideoSurface(); // :cold_sweat:
Expand All @@ -454,6 +456,8 @@ SDL_Surface* GFX_loadImage(char* path) {
}

void GFX_quit(void) {
puts("GFX_quit"); fflush(stdout);

SDL_FreeSurface(rule);
SDL_FreeSurface(button);
SDL_FreeSurface(bg_white);
Expand All @@ -464,9 +468,11 @@ void GFX_quit(void) {
SDL_FreeSurface(settings_volume);
SDL_FreeSurface(settings_mute);

for (int i=0; i<BATTERY_IMAGE_COUNT; i++) {
SDL_FreeSurface(battery[i]);
}
SDL_FreeSurface(battery_charging);
SDL_FreeSurface(battery_fill);
SDL_FreeSurface(battery_line);
SDL_FreeSurface(battery_fill_bad);
SDL_FreeSurface(battery_line_bad);

TTF_CloseFont(font_s);
TTF_CloseFont(font_m);
Expand Down Expand Up @@ -666,8 +672,20 @@ SDL_Surface* GFX_getText(char* text, int size, int color) {
}

void GFX_blitBattery(SDL_Surface* surface, int x, int y) {
int charge = getSmoothBatteryLevel();
SDL_BlitSurface(battery[charge], NULL, surface, &(SDL_Rect){x,y});
if (isCharging()) SDL_BlitSurface(battery_charging, NULL, surface, &(SDL_Rect){x,y});
else {
int charge = getInt("/tmp/battery");
SDL_Surface* fill = charge<=17 ? battery_fill_bad : battery_fill;
SDL_Surface* line = charge<=10 ? battery_line_bad : battery_line;
SDL_BlitSurface(line, NULL, surface, &(SDL_Rect){x,y});

x += 4;
y += 6;

int h = fill->h * (float)charge / 100;
int oy = fill->h - h;
SDL_BlitSurface(fill, &(SDL_Rect){0,oy,fill->w,h}, surface, &(SDL_Rect){x,y+oy});
}
}
void GFX_blitSettings(SDL_Surface* surface, int x, int y, int icon, int value, int min_value, int max_value) {
if (x==Screen.menu.settings.x) GFX_blitWindow(surface, x,y,Screen.settings.width,Screen.settings.height, 0);
Expand Down Expand Up @@ -714,65 +732,9 @@ void fauxSleep(void) {
exitSleep();
}

static int was_charging = 0;
int getSmoothBatteryLevel(void) {
int is_charging = isCharging();
if (is_charging) {
was_charging = 1;
return 6;
}

#define kBatteryReadings 10

static int values[kBatteryReadings];
static int total = 0;
static int i = 0;
static int ready = 0;
static unsigned long last_ticks = 0;

int value = getBatteryLevel();
unsigned long now_ticks = SDL_GetTicks();
if (now_ticks-last_ticks>1000*10 || was_charging) {
ready = 0;
was_charging = 0;
last_ticks = now_ticks;
}

// first run (or first in the last 10 seconds), fill up the buffer
if (!ready) {
for (i=0; i<kBatteryReadings; i++) {
values[i] = value;
}
total = value * kBatteryReadings;
ready = 1;
i = 0;
}
// subsequent calls, update average
else {
total -= values[i];
values[i] = value;
total += value;
i += 1;
if (i>=kBatteryReadings) i -= kBatteryReadings;
value = roundf((float)total / kBatteryReadings);
}
if (value<0) value = 0;
if (value>5) value = 5;
return value;
#undef kBatteryReadings
}

int isCharging(void) {
return getInt("/sys/devices/gpiochip0/gpio/gpio59/value");
}
int getBatteryLevel(void) {
int min = 505; // was 439
int max = 546; // was 500
int value = getInt("/tmp/adc");
int scaled = (value - min) * 6 / (max - min);
if (scaled>5) return 5;
else return scaled;
}

// TODO: toggling this crashes gambatte in GB mode (but not GBC!), GBA, and FC but none of the others...
#define GOVERNOR_PATH "/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor"
Expand Down
2 changes: 0 additions & 2 deletions src/common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -306,10 +306,8 @@ void GFX_blitSettings(SDL_Surface* surface, int x, int y, int icon, int value, i

void waitForWake(void);
void fauxSleep(void);
int getSmoothBatteryLevel(void);

int isCharging(void);
int getBatteryLevel(void);

void powerOff(void);

Expand Down
62 changes: 54 additions & 8 deletions src/keymon/keymon.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ typedef struct {
#define IOCTL_SAR_INIT _IO(SARADC_IOC_MAGIC, 0)
#define IOCTL_SAR_SET_CHANNEL_READ_VALUE _IO(SARADC_IOC_MAGIC, 1)

static SAR_ADC_CONFIG_READ adcCfg = {0,0};
static SAR_ADC_CONFIG_READ adc_config = {0,0};
static int is_charging = 0;
static int eased_charge = 0;
static int sar_fd = 0;
static struct input_event ev;
static int input_fd = 0;
Expand All @@ -77,19 +79,63 @@ void quit(int exitcode) {
exit(exitcode);
}

static int isCharging(void) {
int i = 0;
FILE *file = fopen("/sys/devices/gpiochip0/gpio/gpio59/value", "r");
if (file!=NULL) {
fscanf(file, "%i", &i);
fclose(file);
}
return i;
}

static void initADC(void) {
sar_fd = open("/dev/sar", O_WRONLY);
ioctl(sar_fd, IOCTL_SAR_INIT, NULL);
}
static void checkADC(void) {
ioctl(sar_fd, IOCTL_SAR_SET_CHANNEL_READ_VALUE, &adcCfg);
int was_charging = is_charging;
is_charging = isCharging();

ioctl(sar_fd, IOCTL_SAR_SET_CHANNEL_READ_VALUE, &adc_config);

// TODO: this needs to be recalcuated after sleep too (not just charging) :thinking_face:
// TODO: in a STOP signal handler?
int current_charge = 0;
if (adc_config.adc_value>=528) {
current_charge = adc_config.adc_value - 478;
}
else if (adc_config.adc_value>=512){
current_charge = adc_config.adc_value * 2.125 - 1068;
}
else if (adc_config.adc_value>=480){
current_charge = adc_config.adc_value * 0.51613 - 243.742;
}

if (current_charge<0) current_charge = 0;
else if (current_charge>100) current_charge = 100;

static int first_run = 1;
if (first_run || (was_charging && !is_charging)) {
first_run = 0;
eased_charge = current_charge;
}
else if (eased_charge<current_charge) {
eased_charge += 1;
if (eased_charge>100) eased_charge = 100;
}
else if (eased_charge>current_charge) {
eased_charge -= 1;
if (eased_charge<0) eased_charge = 0;
}

int adc_fd = open("/tmp/adc", O_CREAT | O_WRONLY);
if (adc_fd>0) {
char val[8];
sprintf(val, "%d", adcCfg.adc_value);
write(adc_fd, val, strlen(val));
close(adc_fd);
// new implementation
int bat_fd = open("/tmp/battery", O_CREAT | O_WRONLY);
if (bat_fd>0) {
char value[3];
sprintf(value, "%d", eased_charge);
write(bat_fd, value, strlen(value));
close(bat_fd);
}
}
static void* runADC(void *arg) {
Expand Down
2 changes: 1 addition & 1 deletion third-party/SDL-1.2
Submodule SDL-1.2 updated 1 files
+4 −8 src/video/SDL_video.c
40 changes: 20 additions & 20 deletions todo.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

- added Single-use paks to extras Tools (Add/Remove MiniUI Boot Logo and Update Boot Command)
- fixed swapfile disabled after use
- updated battery icon logic (thanks Totofaki!)

-------------------------------

- match Onion's battery reading?
https://github.com/jimgraygit/Onion/blob/main/src/ADC/main.c#L112
ease towards the current value by 1% every check
- check me_bind_action in overrides to make sure we're mapping all buttons by name
eg. gambatte is mapped, pokemini is not
because pokemini hasn't defined any :face_palm:
beetle-pce-fast might be looking for the wrong name because it's named mednafen_pce_fast_libretro.so

- add Virtual Boy to extras?

- picoarch
check emulator Player Controls for Fast Fwd and remove if present and doesn't work
Expand All @@ -27,7 +31,6 @@
- BUG: Sega CD might need a short delay to resume audio like pokemini
https://github.com/shauninman/picoarch/blob/miniui-miyoomini/patches/pokemini/0001-fix-resume-audio.patch

- auto-adjust brightness based on screen effect?
- faqs
- document auto.sh
- can `trap "sync && reboot" EXIT` or similar be used to catch errors in auto.sh and prevent stock from polluting the SD card?
Expand All @@ -42,17 +45,17 @@
- BUG: say doesn't seem to respect extra line returns at end of msg
- BUG: repo doesn't checkout correct branch for submodules when cloned...

- look into an audioserver-like approach to eliminating pop
- I think picoarch already has a resampler...

- BUG: I think show et al need to make sure pan is set to 0,0?
using after progressui sometimes results in the image not showing

- use smsplus-gx for SMS/GG?
- look into supafaust
is it just the new hotness or an actual improvement over snes9x
it might have MSU-1 support, which, eh, is fine for extras but not essential

- BUG: I think show et al need to make sure pan is set to 0,0?
using after progressui sometimes results in the image not showing

- use smsplus-gx for SMS/GG?
- look into an audioserver-like approach to eliminating pop
- I think picoarch already has a resampler...

- BUG: default st8 assumes first disc on multi-disc games
- leaving for now
Expand All @@ -61,17 +64,14 @@
ugh, this will be harder than it needs to be because of the old Union cross-platform separation
- press L on a save state to lock/unlock it? to make it the default state to open instead of a cold launch?

picoarch should truncate the common parts of a group of options, not the (usually unique) ends

add enable-simple-mode to readme

show, confirm, and say don't sleep (neither does Files.pak)
thinking face
- picoarch should truncate the common parts of a group of options, not the (usually unique) ends

wrap all $(dirname "$0") in quotes to properly escape paths with spaces
or store the result in a var then quote the var when using
- do another pass at defaults?

- help entry for confirm is inaccessible (args are optional so there's no good way to provide without testing for something like -h or --help)
- auto-adjust brightness based on screen effect?

change all logging from &> log to > log 2>&1

do another pass at defaults?

- show, confirm, and say don't sleep (neither does Files.pak)
thinking face

0 comments on commit 91da5a2

Please sign in to comment.