From 581015e9a91c01501dfaa643f1f379ddba4c6884 Mon Sep 17 00:00:00 2001 From: KlausMu Date: Wed, 24 Jan 2024 07:38:09 +0100 Subject: [PATCH] setting timout via GUI now works, not only dropdown without functionality --- Platformio/src/gui.cpp | 2 +- Platformio/src/gui_irReceiver.cpp | 2 +- Platformio/src/gui_settings.cpp | 68 +++++++++++++++++++------- Platformio/src/keys.cpp | 2 +- Platformio/src/main.cpp | 6 +-- Platformio/src/preferences_storage.cpp | 1 + Platformio/src/sleep.cpp | 14 +++++- Platformio/src/sleep.h | 8 +-- 8 files changed, 74 insertions(+), 29 deletions(-) diff --git a/Platformio/src/gui.cpp b/Platformio/src/gui.cpp index 47dcd49..62f8f90 100644 --- a/Platformio/src/gui.cpp +++ b/Platformio/src/gui.cpp @@ -57,7 +57,7 @@ void my_touchpad_read(lv_indev_drv_t * indev_driver, lv_indev_data_t * data){ bool touched = false; if ((touchX > 0) || (touchY > 0)) { touched = true; - standbyTimer = SLEEP_TIMEOUT; + resetStandbyTimer(); } if( !touched ){ diff --git a/Platformio/src/gui_irReceiver.cpp b/Platformio/src/gui_irReceiver.cpp index de51d3d..f8e632d 100644 --- a/Platformio/src/gui_irReceiver.cpp +++ b/Platformio/src/gui_irReceiver.cpp @@ -44,7 +44,7 @@ void printReceivedMessages(bool clearMessages = false) { } void showNewIRmessage(String message) { - standbyTimer = SLEEP_TIMEOUT; // Reset the sleep timer when a IR message is received + resetStandbyTimer(); // Reset the sleep timer when a IR message is received // Serial.printf(" new IR message received: %s\r\n", message.c_str()); // const char *a = message.c_str(); diff --git a/Platformio/src/gui_settings.cpp b/Platformio/src/gui_settings.cpp index 32a91a2..5d4add9 100644 --- a/Platformio/src/gui_settings.cpp +++ b/Platformio/src/gui_settings.cpp @@ -19,6 +19,23 @@ static void WakeEnableSetting_event_cb(lv_event_t * e){ wakeupByIMUEnabled = lv_obj_has_state(lv_event_get_target(e), LV_STATE_CHECKED); } +// timout event handler +static void timout_event_cb(lv_event_t * e){ + lv_obj_t * drop = lv_event_get_target(e); + uint16_t selected = lv_dropdown_get_selected(drop); + switch (selected) { + case 0: {actualSleepTimeout = 10000; break;} + case 1: {actualSleepTimeout = 20000; break;} + case 2: {actualSleepTimeout = 40000; break;} + case 3: {actualSleepTimeout = 60000; break;} + case 4: {actualSleepTimeout = 180000; break;} + case 5: {actualSleepTimeout = 600000; break;} + case 6: {actualSleepTimeout = 3600000; break;} + } + // Serial.printf("New timeout: %lu ms\r\n", actualSleepTimeout); + resetStandbyTimer(); +} + void init_gui_settings(lv_obj_t* tabview) { lv_obj_t* tab = lv_tabview_add_tab(tabview, "Settings"); @@ -35,7 +52,7 @@ void init_gui_settings(lv_obj_t* tabview) { lv_label_set_text(menuLabel, "Display"); lv_obj_t* menuBox = lv_obj_create(tab); - lv_obj_set_size(menuBox, lv_pct(100), 77); // 109, 32 weniger wegen fehlendem Timeout + lv_obj_set_size(menuBox, lv_pct(100), 109); lv_obj_set_style_bg_color(menuBox, color_primary, LV_PART_MAIN); lv_obj_set_style_border_width(menuBox, 0, LV_PART_MAIN); @@ -69,23 +86,38 @@ void init_gui_settings(lv_obj_t* tabview) { lv_obj_add_event_cb(wakeToggle, WakeEnableSetting_event_cb, LV_EVENT_VALUE_CHANGED, NULL); if(wakeupByIMUEnabled) lv_obj_add_state(wakeToggle, LV_STATE_CHECKED); // set default state - // menuLabel = lv_label_create(menuBox); - // lv_label_set_text(menuLabel, "Timeout"); - // lv_obj_align(menuLabel, LV_ALIGN_TOP_LEFT, 0, 64); - // lv_obj_t* drop = lv_dropdown_create(menuBox); - // lv_dropdown_set_options(drop, "10s\n" - // "30s\n" - // "1m\n" - // "3m"); - // lv_obj_align(drop, LV_ALIGN_TOP_RIGHT, 0, 61); - // lv_obj_set_size(drop, 70, 22); - // //lv_obj_set_style_text_font(drop, &lv_font_montserrat_12, LV_PART_MAIN); - // //lv_obj_set_style_text_font(lv_dropdown_get_list(drop), &lv_font_montserrat_12, LV_PART_MAIN); - // lv_obj_set_style_pad_top(drop, 1, LV_PART_MAIN); - // lv_obj_set_style_bg_color(drop, color_primary, LV_PART_MAIN); - // lv_obj_set_style_bg_color(lv_dropdown_get_list(drop), color_primary, LV_PART_MAIN); - // lv_obj_set_style_border_width(lv_dropdown_get_list(drop), 1, LV_PART_MAIN); - // lv_obj_set_style_border_color(lv_dropdown_get_list(drop), lv_color_hex(0x505050), LV_PART_MAIN); + menuLabel = lv_label_create(menuBox); + lv_label_set_text(menuLabel, "Timeout"); + lv_obj_align(menuLabel, LV_ALIGN_TOP_LEFT, 0, 64); + lv_obj_t* drop = lv_dropdown_create(menuBox); + lv_dropdown_set_options(drop, "10s\n" + "20s\n" + "40s\n" + "1m\n" + "3m\n" + "10m\n" + "1h"); // 1h for debug purposes, if you don't want the device to go to slepp + // if you add more options here, do the same in timout_event_cb() + switch (actualSleepTimeout) { + case 10000: {lv_dropdown_set_selected(drop, 0); break;} + case 20000: {lv_dropdown_set_selected(drop, 1); break;} + case 40000: {lv_dropdown_set_selected(drop, 2); break;} + case 60000: {lv_dropdown_set_selected(drop, 3); break;} + case 180000: {lv_dropdown_set_selected(drop, 4); break;} + case 600000: {lv_dropdown_set_selected(drop, 5); break;} + case 3600000: {lv_dropdown_set_selected(drop, 6); break;} + } + lv_dropdown_set_selected_highlight(drop, true); + lv_obj_align(drop, LV_ALIGN_TOP_RIGHT, 0, 61); + lv_obj_set_size(drop, 70, 22); + //lv_obj_set_style_text_font(drop, &lv_font_montserrat_12, LV_PART_MAIN); + //lv_obj_set_style_text_font(lv_dropdown_get_list(drop), &lv_font_montserrat_12, LV_PART_MAIN); + lv_obj_set_style_pad_top(drop, 1, LV_PART_MAIN); + lv_obj_set_style_bg_color(drop, color_primary, LV_PART_MAIN); + lv_obj_set_style_bg_color(lv_dropdown_get_list(drop), color_primary, LV_PART_MAIN); + lv_obj_set_style_border_width(lv_dropdown_get_list(drop), 1, LV_PART_MAIN); + lv_obj_set_style_border_color(lv_dropdown_get_list(drop), lv_color_hex(0x505050), LV_PART_MAIN); + lv_obj_add_event_cb(drop, timout_event_cb, LV_EVENT_VALUE_CHANGED, NULL); // // Add another label, then a settings box for WiFi // menuLabel = lv_label_create(tab); diff --git a/Platformio/src/keys.cpp b/Platformio/src/keys.cpp index 085fce0..ef7be01 100644 --- a/Platformio/src/keys.cpp +++ b/Platformio/src/keys.cpp @@ -154,7 +154,7 @@ void keypad_loop(void) { // we are not allowed to do this, because of the same reason as above // continue; } else { - standbyTimer = SLEEP_TIMEOUT; // Reset the sleep timer when a button is pressed + resetStandbyTimer(); // Reset the sleep timer when a button is pressed } char keyChar = customKeypad.key[i].kchar; int keyCode = customKeypad.key[i].kcode; diff --git a/Platformio/src/main.cpp b/Platformio/src/main.cpp index 469fa97..eec5f79 100644 --- a/Platformio/src/main.cpp +++ b/Platformio/src/main.cpp @@ -25,6 +25,9 @@ void setup() { // --- Startup --- Serial.begin(115200); + // Restore settings from internal flash memory + init_preferences(); + // Button Pin Definition init_keys(); @@ -38,9 +41,6 @@ void setup() { // init TFT init_tft(); - // Restore settings from internal flash memory - init_preferences(); - // init GUI init_gui(); diff --git a/Platformio/src/preferences_storage.cpp b/Platformio/src/preferences_storage.cpp index adcd85e..347658b 100644 --- a/Platformio/src/preferences_storage.cpp +++ b/Platformio/src/preferences_storage.cpp @@ -11,6 +11,7 @@ void init_preferences(void) { preferences.begin("settings", false); if(preferences.getBool("alreadySetUp")){ wakeupByIMUEnabled = preferences.getBool("wkpByIMU"); + actualSleepTimeout = preferences.getUInt("slpTimeout"); backlight_brightness = preferences.getUChar("blBrightness"); currentScreen = preferences.getUChar("currentScreen"); allDevsPowered = preferences.getUChar("allDevsPowered"); diff --git a/Platformio/src/sleep.cpp b/Platformio/src/sleep.cpp index c61dd5f..4f638e5 100644 --- a/Platformio/src/sleep.cpp +++ b/Platformio/src/sleep.cpp @@ -14,11 +14,16 @@ #include "device_keyboard_ble.h" int motion = 0; -int standbyTimer = SLEEP_TIMEOUT; +uint32_t actualSleepTimeout; +uint32_t standbyTimer; bool wakeupByIMUEnabled = true; LIS3DH IMU(I2C_MODE, 0x19); // Default constructor is I2C, addr 0x19. byte wakeup_reason; +void resetStandbyTimer() { + standbyTimer = actualSleepTimeout; +} + void activityDetection(){ static int accXold; static int accYold; @@ -33,7 +38,7 @@ void activityDetection(){ standbyTimer -= 100; if(standbyTimer < 0) standbyTimer = 0; // If the motion exceeds the threshold, the standbyTimer is reset - if(motion > MOTION_THRESHOLD) standbyTimer = SLEEP_TIMEOUT; + if(motion > MOTION_THRESHOLD) resetStandbyTimer(); // Store the current acceleration and time accXold = accX; @@ -97,6 +102,7 @@ void configIMUInterrupts() void enterSleep(){ // Save settings to internal flash memory preferences.putBool("wkpByIMU", wakeupByIMUEnabled); + preferences.putUInt("slpTimeout", actualSleepTimeout); preferences.putUChar("blBrightness", backlight_brightness); preferences.putUChar("currentScreen", currentScreen); preferences.putUChar("allDevsPowered", allDevsPowered); @@ -162,6 +168,10 @@ void enterSleep(){ } void init_sleep() { + if (actualSleepTimeout == 0){ + actualSleepTimeout = DEFAULT_SLEEP_TIMEOUT; + } + // Find out wakeup cause if(esp_sleep_get_wakeup_cause() == ESP_SLEEP_WAKEUP_EXT1){ if(log(esp_sleep_get_ext1_wakeup_status())/log(2) == 13) wakeup_reason = WAKEUP_BY_IMU; diff --git a/Platformio/src/sleep.h b/Platformio/src/sleep.h index 0c8ca39..e74367d 100644 --- a/Platformio/src/sleep.h +++ b/Platformio/src/sleep.h @@ -7,10 +7,11 @@ #define ACC_INT 20 // IMU declarations -#define SLEEP_TIMEOUT 20000 // time until device enters sleep mode in milliseconds -#define MOTION_THRESHOLD 50 // motion above threshold keeps device awake -extern int standbyTimer; +#define DEFAULT_SLEEP_TIMEOUT 20000 // time until device enters sleep mode in milliseconds +extern uint32_t actualSleepTimeout; +extern uint32_t standbyTimer; extern bool wakeupByIMUEnabled; +#define MOTION_THRESHOLD 50 // motion above threshold keeps device awake // Other declarations extern byte wakeup_reason; @@ -22,5 +23,6 @@ void enterSleep(); void init_sleep(); void setup_IMU(); void check_activity(); +void resetStandbyTimer(); #endif /*__SLEEP_H__*/