From 01166add8b358cd6a1db06e345a374e96dee3fd0 Mon Sep 17 00:00:00 2001 From: Joost van Viegen Date: Thu, 1 Oct 2020 23:53:42 +0200 Subject: [PATCH] worked on saving oled from charging burn-in --- main/gui.c | 23 +++++++++++++++++------ main/main.c | 41 ++++++++++++++++++++++++++++++++--------- 2 files changed, 49 insertions(+), 15 deletions(-) diff --git a/main/gui.c b/main/gui.c index 684cc05..232a9fa 100644 --- a/main/gui.c +++ b/main/gui.c @@ -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; @@ -176,6 +177,8 @@ 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"); } } @@ -183,7 +186,7 @@ int option_menu_cb(int button, char **desc, kcugui_menuitem_t **menu, int item_s 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(); @@ -210,14 +213,15 @@ 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]}, @@ -225,6 +229,7 @@ static void show_options() { {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} @@ -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; @@ -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(); diff --git a/main/main.c b/main/main.c index a70f2df..c5ecc5f 100644 --- a/main/main.c +++ b/main/main.c @@ -107,6 +107,19 @@ 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 @@ -114,16 +127,26 @@ void handleCharging() { 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");