From 4e61e74d0a872254134b8c20a6e32817ea52d504 Mon Sep 17 00:00:00 2001 From: fvanroie Date: Sat, 26 Dec 2020 03:39:21 +0100 Subject: [PATCH] Move sleep from gui to hasp --- src/drv/hasp_drv_touch.cpp | 6 ++--- src/hasp/hasp.cpp | 48 +++++++++++++++++++++++++++++++++++++- src/hasp/hasp.h | 11 ++++++++- src/hasp/hasp_dispatch.cpp | 3 +-- src/hasp/hasp_object.cpp | 13 +++++------ src/hasp_gui.h | 6 ----- src/main.cpp | 3 +-- 7 files changed, 68 insertions(+), 22 deletions(-) diff --git a/src/drv/hasp_drv_touch.cpp b/src/drv/hasp_drv_touch.cpp index 46dc942e..e68ee3c2 100644 --- a/src/drv/hasp_drv_touch.cpp +++ b/src/drv/hasp_drv_touch.cpp @@ -20,8 +20,8 @@ //#include "ft6x36.h" #endif -#include "../hasp/hasp_sleep.h" -extern uint8_t sleep_state; +#include "../hasp/hasp.h" // for hasp_sleep_state +extern uint8_t hasp_sleep_state; void drv_touch_init(uint8_t rotation) { @@ -76,7 +76,7 @@ bool drv_touch_read(lv_indev_drv_t * indev_driver, lv_indev_data_t * data) #ifdef TOUCH_CS uint16_t touchX, touchY; bool touched = drv_touchpad_getXY(&touchX, &touchY); - if(touched && sleep_state != HASP_SLEEP_OFF) sleep_check_state(); // update Idle + if(touched && hasp_sleep_state != HASP_SLEEP_OFF) hasp_update_sleep_state(); // update Idle // Ignore first press? diff --git a/src/hasp/hasp.cpp b/src/hasp/hasp.cpp index 850f99b5..0c12e947 100644 --- a/src/hasp/hasp.cpp +++ b/src/hasp/hasp.cpp @@ -60,6 +60,9 @@ LV_IMG_DECLARE(img_bubble_pattern) void haspLoadPage(const char * pages); //////////////////////////////////////////////////////////////////////////////////////////////////// +uint8_t hasp_sleep_state = HASP_SLEEP_OFF; // Used in hasp_drv_touch.cpp +static uint16_t sleepTimeShort = 60; // 1 second resolution +static uint16_t sleepTimeLong = 120; // 1 second resolution uint8_t haspStartDim = 100; uint8_t haspStartPage = 0; @@ -89,6 +92,49 @@ lv_font_t * hasp_get_font(uint8_t fontid) } } +/** + * WakeUp the display using a command instead of touch + */ +void hasp_wakeup() +{ + lv_disp_trig_activity(NULL); +} + +/** + * Check if sleep state needs to be updated + */ +bool IRAM_ATTR hasp_update_sleep_state() +{ + uint32_t idle = lv_disp_get_inactive_time(NULL); + + if(idle >= (sleepTimeShort + sleepTimeLong) * 1000U) { + if(hasp_sleep_state != HASP_SLEEP_LONG) { + dispatch_output_idle_state(HASP_SLEEP_LONG); + hasp_sleep_state = HASP_SLEEP_LONG; + } + } else if(idle >= sleepTimeShort * 1000U) { + if(hasp_sleep_state != HASP_SLEEP_SHORT) { + dispatch_output_idle_state(HASP_SLEEP_SHORT); + hasp_sleep_state = HASP_SLEEP_SHORT; + } + } else { + if(hasp_sleep_state != HASP_SLEEP_OFF) { + dispatch_output_idle_state(HASP_SLEEP_OFF); + hasp_sleep_state = HASP_SLEEP_OFF; + } + } + + return (hasp_sleep_state != HASP_SLEEP_OFF); +} + +/** + * Checks if we went to sleep, wake up is handled in the event handlers + */ +// void haspEverySecond() +// { +// hasp_update_sleep_state(); +// } + //////////////////////////////////////////////////////////////////////////////////////////////////// /** * Get Page Object by PageID @@ -234,7 +280,7 @@ static void custom_font_apply_cb(lv_theme_t * th, lv_obj_t * obj, lv_theme_style /** * Create a demo application */ -void haspSetup() +void haspSetup(void) { guiSetDim(haspStartDim); diff --git a/src/hasp/hasp.h b/src/hasp/hasp.h index 4b08c4de..9472552a 100644 --- a/src/hasp/hasp.h +++ b/src/hasp/hasp.h @@ -23,6 +23,9 @@ extern "C" { /********************* * DEFINES *********************/ +#define HASP_SLEEP_OFF 0 +#define HASP_SLEEP_SHORT 1 +#define HASP_SLEEP_LONG 2 /********************** * TYPEDEFS @@ -35,8 +38,10 @@ extern "C" { /** * Create a hasp application */ -void haspSetup(); +void haspSetup(void); void IRAM_ATTR haspLoop(void); +//void haspEverySecond(void); // See MACROS + void haspReconnect(void); void haspDisconnect(void); @@ -59,9 +64,13 @@ bool haspSetConfig(const JsonObject & settings); lv_font_t * hasp_get_font(uint8_t fontid); +bool IRAM_ATTR hasp_update_sleep_state(); +void hasp_wakeup(void); + /********************** * MACROS **********************/ +#define haspEverySecond hasp_update_sleep_state #endif /*HASP_USE_APP*/ diff --git a/src/hasp/hasp_dispatch.cpp b/src/hasp/hasp_dispatch.cpp index 929deebc..9c6edfa1 100644 --- a/src/hasp/hasp_dispatch.cpp +++ b/src/hasp/hasp_dispatch.cpp @@ -8,7 +8,6 @@ #include "hasp_dispatch.h" #include "hasp_object.h" -#include "hasp_sleep.h" #include "hasp.h" #include "hasp_debug.h" @@ -693,7 +692,7 @@ void dispatch_calibrate(const char *, const char *) void dispatch_wakeup(const char *, const char *) { - sleep_wakeup(); + hasp_wakeup(); } void dispatch_reboot(const char *, const char *) diff --git a/src/hasp/hasp_object.cpp b/src/hasp/hasp_object.cpp index 9d16def8..b55c7d6b 100644 --- a/src/hasp/hasp_object.cpp +++ b/src/hasp/hasp_object.cpp @@ -24,7 +24,6 @@ #include "hasp_object.h" #include "hasp_dispatch.h" #include "hasp_attribute.h" -#include "hasp_sleep.h" // ##################### Object Finders ######################################################## @@ -311,7 +310,7 @@ void IRAM_ATTR btn_event_handler(lv_obj_t * obj, lv_event_t event) return; } - sleep_check_state(); // wakeup? + hasp_update_sleep_state(); // wakeup? dispatch_object_event(obj, eventid); // send object event } @@ -323,7 +322,7 @@ void IRAM_ATTR btn_event_handler(lv_obj_t * obj, lv_event_t event) void wakeup_event_handler(lv_obj_t * obj, lv_event_t event) { if(obj == lv_disp_get_layer_sys(NULL)) { - sleep_check_state(); // wakeup? + hasp_update_sleep_state(); // wakeup? lv_obj_set_click(obj, false); // disable fist click } } @@ -336,7 +335,7 @@ void wakeup_event_handler(lv_obj_t * obj, lv_event_t event) static void btnmap_event_handler(lv_obj_t * obj, lv_event_t event) { if(event == LV_EVENT_VALUE_CHANGED) { - sleep_check_state(); // wakeup? + hasp_update_sleep_state(); // wakeup? hasp_send_obj_attribute_val(obj, lv_btnmatrix_get_active_btn(obj)); } } @@ -349,7 +348,7 @@ static void btnmap_event_handler(lv_obj_t * obj, lv_event_t event) static void table_event_handler(lv_obj_t * obj, lv_event_t event) { if(event == LV_EVENT_VALUE_CHANGED) { - sleep_check_state(); // wakeup? + hasp_update_sleep_state(); // wakeup? uint16_t row; uint16_t col; @@ -365,7 +364,7 @@ static void table_event_handler(lv_obj_t * obj, lv_event_t event) void IRAM_ATTR toggle_event_handler(lv_obj_t * obj, lv_event_t event) { if(event == LV_EVENT_VALUE_CHANGED) { - sleep_check_state(); // wakeup? + hasp_update_sleep_state(); // wakeup? hasp_send_obj_attribute_val(obj, lv_checkbox_is_checked(obj)); } } @@ -378,7 +377,7 @@ void IRAM_ATTR toggle_event_handler(lv_obj_t * obj, lv_event_t event) static void switch_event_handler(lv_obj_t * obj, lv_event_t event) { if(event == LV_EVENT_VALUE_CHANGED) { - sleep_check_state(); // wakeup? + hasp_update_sleep_state(); // wakeup? hasp_send_obj_attribute_val(obj, lv_switch_get_state(obj)); } } diff --git a/src/hasp_gui.h b/src/hasp_gui.h index 3659beaf..ea2caa94 100644 --- a/src/hasp_gui.h +++ b/src/hasp_gui.h @@ -7,10 +7,6 @@ #include "ArduinoJson.h" #include "lvgl.h" -#define HASP_SLEEP_OFF 0 -#define HASP_SLEEP_SHORT 1 -#define HASP_SLEEP_LONG 2 - /* ===== Default Event Processors ===== */ void guiSetup(); void IRAM_ATTR guiLoop(void); @@ -24,12 +20,10 @@ void guiTakeScreenshot(const char * pFileName); // to file void guiTakeScreenshot(); // webclient /* ===== Getter and Setter Functions ===== */ -void guiWakeUp(void); void guiSetDim(int8_t level); int8_t guiGetDim(void); void guiSetBacklight(bool lighton); bool guiGetBacklight(); -bool IRAM_ATTR guiCheckSleep(); /* ===== Read/Write Configuration ===== */ #if HASP_USE_CONFIG > 0 diff --git a/src/main.cpp b/src/main.cpp index 8ad578b9..911247b0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,7 +10,6 @@ #include "hasp_oobe.h" #include "hasp/hasp_dispatch.h" -#include "hasp/hasp_sleep.h" #include "hasp/hasp.h" #include "net/hasp_network.h" @@ -156,7 +155,7 @@ void loop() /* Timer Loop */ if(millis() - mainLastLoopTime >= 1000) { /* Runs Every Second */ - sleepEverySecond(); + haspEverySecond(); debugEverySecond(); // statusupdate #if HASP_USE_OTA > 0 otaEverySecond(); // progressbar