Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Protect screen from charge burn-in with timeout #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions main/gui.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ static void debug_screen() {
#define OPT_VOL 2
#define OPT_BRIGHT 3
#define OPT_CHANNEL 4
#define OPT_TIMEOUT 5

typedef struct {
int opt_id;
Expand All @@ -176,14 +177,16 @@ void option_set_text(opt_data_t *t) {
sprintf(t->opt_name, "Volume %d", kchal_get_volume());
} else if (t->opt_id==OPT_BRIGHT) {
sprintf(t->opt_name, "Bright %d", kchal_get_brightness());
} else if (t->opt_id==OPT_TIMEOUT) {
sprintf(t->opt_name, "Timeout %s", *t->opt_val?"ON":"OFF");
}
}

int option_menu_cb(int button, char **desc, kcugui_menuitem_t **menu, int item_selected, void *userptr) {
opt_data_t *od=(*menu)[item_selected].user;
if (button==KC_BTN_B) return KCUGUI_CB_CANCEL;
if (button!=KC_BTN_LEFT && button!=KC_BTN_RIGHT) return 0;
if (od->opt_id==OPT_KEYLOCK || od->opt_id==OPT_WIFI) {
if (od->opt_id==OPT_KEYLOCK || od->opt_id==OPT_WIFI || od->opt_id==OPT_TIMEOUT) {
*od->opt_val=!(*od->opt_val);
} else if (od->opt_id==OPT_VOL) {
int n=kchal_get_volume();
Expand All @@ -210,21 +213,23 @@ int option_menu_cb(int button, char **desc, kcugui_menuitem_t **menu, int item_s


static void show_options() {
int keylock=false, wifi_en=true, channel=5;
char text[5][32];
int keylock=false, wifi_en=true, channel=5, timeout=false;
char text[6][32];
opt_data_t odata[]={
{OPT_KEYLOCK, text[0], &keylock},
{OPT_WIFI, text[1], &wifi_en},
{OPT_VOL, text[2], NULL},
{OPT_BRIGHT, text[3], NULL},
{OPT_CHANNEL, text[4], &channel},
{OPT_TIMEOUT, text[5], &timeout},
};
kcugui_menuitem_t menu[]={
{text[0],0,&odata[0]},
{text[1],0,&odata[1]},
{text[4],0,&odata[4]},
{text[2],0,&odata[2]},
{text[3],0,&odata[3]},
{text[5],0,&odata[5]},
{"Debug info", 0, NULL},
{"Exit",0,NULL},
{"",KCUGUI_MENUITEM_LAST,0,NULL}
Expand All @@ -234,13 +239,14 @@ static void show_options() {
nvs_get_u8(nvsHandle, "kl", &keylock);
nvs_get_u8(nvsHandle, "wifi", &wifi_en);
nvs_get_u8(nvsHandle, "channel", &channel);
keylock&=255; wifi_en&=255; //uint8 -> int
nvs_get_u8(nvsHandle, "timeout", &timeout);
keylock&=255; wifi_en&=255; timeout&=255; //uint8 -> int
int wifi_old=wifi_en;
int channel_old=channel;
if (channel<1 || channel>13) channel=5;
for(int i=0; i<5; i++) option_set_text(&odata[i]);
for(int i=0; i<6; i++) option_set_text(&odata[i]);
int ch=-2;
while(ch!=6 && ch!=-1) {
while(ch!=7 && ch!=-1) {
ch=kcugui_menu(menu, "OPTIONS", option_menu_cb, NULL);
if (ch==0) {
keylock=!keylock;
Expand All @@ -251,12 +257,17 @@ static void show_options() {
option_set_text(&odata[1]);
}
if (ch==5) {
timeout=!timeout;
option_set_text(&odata[5]);
}
if (ch==6) {
debug_screen();
}
}
nvs_set_u8(nvsHandle, "kl", keylock);
nvs_set_u8(nvsHandle, "wifi", wifi_en);
nvs_set_u8(nvsHandle, "channel", channel);
nvs_set_u8(nvsHandle, "timeout", timeout);
if (wifi_old!=wifi_en || channel!=channel_old) {
//Reboot I guess
kchal_boot_into_new_app();
Expand Down
41 changes: 32 additions & 9 deletions main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,23 +107,46 @@ void handleCharging() {
guiInit();
guiCharging();

uint8_t timeout=false;
time_t now;
time_t maxTimeout=5;
nvs_handle nvsHandle=NULL;
if (nvs_open("8bkc", NVS_READWRITE, &nvsHandle)==ESP_OK) {
nvs_get_u8(nvsHandle, "timeout", &timeout);
}

if(timeout) {
time(&now);
maxTimeout+=now;
}

//Disable app cpu
DPORT_SET_PERI_REG_MASK(DPORT_APPCPU_CTRL_B_REG, DPORT_APPCPU_CLKGATE_EN);
//Speed down
rtc_clk_cpu_freq_set(RTC_CPU_FREQ_2M);

do {
r=kchal_get_chg_status();
if (r==KC_CHG_FULL || fixFull) {
guiFull();
printf("Full!\n");
fullCtr++;
} else if (r==KC_CHG_CHARGING) {
guiCharging(kchal_get_bat_mv()>4100);
printf("Charging...\n");
fullCtr=0;
}
if(timeout) {
time(&now);

if(now > maxTimeout) {
kcugui_cls();
kcugui_flush();
}
}
else {
if (r==KC_CHG_FULL || fixFull) {
guiFull();
printf("Full!\n");
fullCtr++;
}
else if (r==KC_CHG_CHARGING) {
guiCharging(kchal_get_bat_mv() > 4100);
printf("Charging...\n");
fullCtr = 0;
}
}
if (kchal_get_keys() & KC_BTN_POWER) {
rtc_clk_cpu_freq_set(RTC_CPU_FREQ_80M);
printf("Power btn pressed; restarting with override bit set\n");
Expand Down