setting timout via GUI now works, not only dropdown without functionality

This commit is contained in:
KlausMu 2024-01-24 07:38:09 +01:00
parent 2fda106f4e
commit 581015e9a9
8 changed files with 74 additions and 29 deletions

View file

@ -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 ){

View file

@ -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();

View file

@ -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);

View file

@ -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;

View file

@ -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();

View file

@ -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");

View file

@ -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;

View file

@ -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__*/