From b0725d6db700d8612c6525d2d48d4d856902c85a Mon Sep 17 00:00:00 2001 From: David B Brown Date: Wed, 22 Jun 2022 17:41:02 +0100 Subject: [PATCH] simplified and hopefully improved screen handling --- components/UDPlogger | 2 +- components/esp-homekit | 2 +- components/esp-homekit-common-functions | 2 +- components/esp-wifi-config | 2 +- components/esp-wolfssl | 2 +- src/Makefile | 17 ++-- src/wifi_thermostat.c | 130 ++++++++++-------------- 7 files changed, 68 insertions(+), 89 deletions(-) diff --git a/components/UDPlogger b/components/UDPlogger index 37426f8..5565ac1 160000 --- a/components/UDPlogger +++ b/components/UDPlogger @@ -1 +1 @@ -Subproject commit 37426f82c24ca5953f9900571a72eb8bde965aea +Subproject commit 5565ac131e9127d79d136f251a82d9732e8909e5 diff --git a/components/esp-homekit b/components/esp-homekit index bb6ba44..5bc96b3 160000 --- a/components/esp-homekit +++ b/components/esp-homekit @@ -1 +1 @@ -Subproject commit bb6ba44bee99fe96ee54098caa14afc3a7940bbf +Subproject commit 5bc96b3d0814c7c4d126fabe228c63f55f0078f6 diff --git a/components/esp-homekit-common-functions b/components/esp-homekit-common-functions index 088895d..212765a 160000 --- a/components/esp-homekit-common-functions +++ b/components/esp-homekit-common-functions @@ -1 +1 @@ -Subproject commit 088895d37f609eb70924a5eba87fffef03b2509b +Subproject commit 212765a8c3a7283a00ee4e749e423ffd904b9092 diff --git a/components/esp-wifi-config b/components/esp-wifi-config index 22509d4..ce830f4 160000 --- a/components/esp-wifi-config +++ b/components/esp-wifi-config @@ -1 +1 @@ -Subproject commit 22509d40ba3e1dd790458f1b81dbb5be636f145c +Subproject commit ce830f465e3aeccc324300ca2b0c1bbf3e8955e5 diff --git a/components/esp-wolfssl b/components/esp-wolfssl index e56663b..3af6120 160000 --- a/components/esp-wolfssl +++ b/components/esp-wolfssl @@ -1 +1 @@ -Subproject commit e56663b0d66f23c38927bb00a0185acf61ab337b +Subproject commit 3af6120a7f35fa68baecc0f1941041cd8b683766 diff --git a/src/Makefile b/src/Makefile index 1af92d4..f2e2b70 100644 --- a/src/Makefile +++ b/src/Makefile @@ -4,21 +4,21 @@ EXTRA_COMPONENTS = \ extras/dht \ extras/http-parser \ extras/rboot-ota \ - extras/dhcpserver \ + extras/dhcpserver \ extras/ssd1306 \ extras/fonts \ extras/i2c \ $(abspath ../components/esp-wolfssl) \ $(abspath ../components/esp-cjson) \ $(abspath ../components/esp-homekit)\ - $(abspath ../components/esp-wifi-config)\ - $(abspath ../components/UDPlogger)\ + $(abspath ../components/esp-wifi-config)\ + $(abspath ../components/UDPlogger)\ $(abspath ../components/esp-qrcode)\ $(abspath ../components/esp-adv-button)\ $(abspath ../components/esp-homekit-common-functions/led_codes)\ $(abspath ../components/esp-homekit-common-functions/ota)\ - $(abspath ../components/esp-homekit-common-functions/custom_characteristics)\ - $(abspath ../components/esp-homekit-common-functions/shared_functions) + $(abspath ../components/esp-homekit-common-functions/custom_characteristics)\ + $(abspath ../components/esp-homekit-common-functions/shared_functions) FONTS_TERMINUS_BOLD_8X14_ISO8859_1 = 1 @@ -33,8 +33,9 @@ HOMEKIT_MAX_CLIENTS = 16 HOMEKIT_SMALL = 0 HOMEKIT_SPI_FLASH_BASE_ADDR ?= 0x8c000 -EXTRA_CFLAGS += -I../.. -DHOMEKIT_SHORT_APPLE_UUIDS -DFONTS_TERMINUS_BOLD_8X14_ISO8859_1 -DFONTS_TERMINUS_BOLD_14X28_ISO8859_1 -DFONTS_TERMINUS_6X12_ISO8859_1 -EXTRA_CFLAGS += -DHOMEKIT_DEBUG +EXTRA_CFLAGS += -Os -I../.. -DHOMEKIT_SHORT_APPLE_UUIDS -DFONTS_TERMINUS_BOLD_8X14_ISO8859_1 -DFONTS_TERMINUS_BOLD_14X28_ISO8859_1 -DFONTS_TERMINUS_6X12_ISO8859_1 +#EXTRA_CFLAGS += -DHOMEKIT_DEBUG +#EXTRA_CFLAGS += -DHOMEKIT_MDNS_DEBUG EXTRA_CFLAGS += -DHOMEKIT_OVERCLOCK_PAIR_VERIFY EXTRA_CFLAGS += -DHOMEKIT_OVERCLOCK_PAIR_SETUP EXTRA_CFLAGS += -DUDPLOG_PRINTF_TO_UDP @@ -44,7 +45,7 @@ EXTRA_CFLAGS += -DconfigUSE_TRACE_FACILITY include $(SDK_PATH)/common.mk signature: - $(shell /usr/local/opt/openssl/bin/openssl sha384 -binary -out firmware/main.bin.sig firmware/main.bin) + $(shell openssl sha384 -binary -out firmware/main.bin.sig firmware/main.bin) $(shell printf "%08x" `cat firmware/main.bin | wc -c`| xxd -r -p >>firmware/main.bin.sig) monitor: diff --git a/src/wifi_thermostat.c b/src/wifi_thermostat.c index 4253d32..67655ab 100644 --- a/src/wifi_thermostat.c +++ b/src/wifi_thermostat.c @@ -134,7 +134,7 @@ static enum screen_display screen; // I2C -ETSTimer screen_timer; +ETSTimer screen_off_timer; #include homekit_characteristic_t wifi_reset = HOMEKIT_CHARACTERISTIC_(CUSTOM_WIFI_RESET, false, .setter=wifi_reset_set); @@ -143,6 +143,7 @@ homekit_characteristic_t wifi_check_interval = HOMEKIT_CHARACTERISTIC_(CUSTOM_ homekit_characteristic_t task_stats = HOMEKIT_CHARACTERISTIC_(CUSTOM_TASK_STATS, true , .setter=task_stats_set); homekit_characteristic_t ota_beta = HOMEKIT_CHARACTERISTIC_(CUSTOM_OTA_BETA, false, .setter=ota_beta_set); homekit_characteristic_t lcm_beta = HOMEKIT_CHARACTERISTIC_(CUSTOM_LCM_BETA, false, .setter=lcm_beta_set); +homekit_characteristic_t preserve_state = HOMEKIT_CHARACTERISTIC_(CUSTOM_PRESERVE_STATE, true, .setter=preserve_state_set); homekit_characteristic_t ota_trigger = API_OTA_TRIGGER; homekit_characteristic_t name = HOMEKIT_CHARACTERISTIC_(NAME, DEVICE_NAME); @@ -152,13 +153,13 @@ homekit_characteristic_t model = HOMEKIT_CHARACTERISTIC_(MODEL, D homekit_characteristic_t revision = HOMEKIT_CHARACTERISTIC_(FIRMWARE_REVISION, FW_VERSION); -void switch_screen_on (bool screen_state, int time_to_be_on); +void switch_screen_on (int time_to_be_on); void display_logo (); void thermostat_identify_task(void *_args) { led_code(LED_GPIO, IDENTIFY_ACCESSORY); - switch_screen_on (true,10*SECOND_TICKS); + switch_screen_on (10*SECOND_TICKS); display_logo (); vTaskDelete(NULL); } @@ -229,7 +230,7 @@ static void ssd1306_task(void *pvParameters) goto error_loop; } - sprintf(target_temp_string, "%g", (float)target_temperature.value.float_value); + sprintf(target_temp_string, "%2.1f", (float)target_temperature.value.float_value); switch( (int)current_state.value.int_value) { case 0: @@ -256,8 +257,8 @@ static void ssd1306_task(void *pvParameters) printf("Error printing mode\n"); } - sprintf(temperature_string, "%g", (float)current_temperature.value.float_value); - sprintf(humidity_string, "%g", (float)current_humidity.value.float_value); + sprintf(temperature_string, "%2.1f", (float)current_temperature.value.float_value); + sprintf(humidity_string, "%2.1f", (float)current_humidity.value.float_value); if (ssd1306_draw_string(&display, display_buffer, font_builtin_fonts[FONT_FACE_TERMINUS_BOLD_8X14_ISO8859_1], 30, 41 , temperature_string, OLED_COLOR_WHITE, OLED_COLOR_BLACK) < 1){ printf("Error printing temperature\n"); } @@ -282,44 +283,27 @@ static void ssd1306_task(void *pvParameters) } } +void switch_screen_off (){ + + screen_on = false; + ssd1306_display_on(&display, false); + sdk_os_timer_disarm (&screen_off_timer ); /* esnuer the screen off timer is disabled */ + printf("Screen turned off and off timer disbaled\n"); +} -void switch_screen_on ( bool screen_state, int time_to_be_on){ +void switch_screen_on (int time_to_be_on){ - if (screen_on ==true && screen_state ==true){ - if (time_to_be_on >0 ){ - /* if its already on and the time to be on > 0 then reset the timer, otherwise do nothing */ - sdk_os_timer_disarm (&screen_timer ); - sdk_os_timer_arm(&screen_timer, time_to_be_on, 0); - } - } - - if ( screen_on == true && screen_state == false){ - /* if its on and we called for off, switch it off and disable the timer */ - screen_on = false; - ssd1306_display_on(&display, screen_on); - sdk_os_timer_disarm (&screen_timer ); - } - - if ( screen_on == false && screen_state == true){ - /* if it off and we called for it to be on, switch it on */ - screen_on = screen_state; - ssd1306_display_on(&display, screen_on); - if (time_to_be_on > 0){ - /* if the timer is > 0m, set the timer */ - sdk_os_timer_arm(&screen_timer, time_to_be_on, 0); - printf("Screen off timer set to %d\n", time_to_be_on); - } - } - - printf("Screen_on screen on state: %d\n", screen_state); - + screen_on = true; + ssd1306_display_on(&display, true); + sdk_os_timer_arm(&screen_off_timer, time_to_be_on, 0); + printf("Screen turned on and off timer set to %d\n", time_to_be_on); } -void screen_timer_fn (){ - - switch_screen_on (false , 0); +void screen_off_timer_fn (){ + + switch_screen_off (); printf("Screen Off timer fucntion\n"); } @@ -343,7 +327,7 @@ void screen_init(void) ssd1306_set_whole_display_lighting(&display, false); ssd1306_set_scan_direction_fwd(&display, false); ssd1306_set_segment_remapping_enabled(&display, true); - sdk_os_timer_setfn(&screen_timer, screen_timer_fn, NULL); + sdk_os_timer_setfn(&screen_off_timer, screen_off_timer_fn, NULL); printf("%s: end, Free Heap %d\n", __func__, xPortGetFreeHeapSize()); } @@ -421,7 +405,7 @@ void qrcode_show(homekit_server_config_t *config) { qrcode_print(&qrcode); // print on console - switch_screen_on (true,0); + switch_screen_on (0); ssd1306_fill_rectangle(&display, display_buffer, 0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT, OLED_COLOR_BLACK); ssd1306_draw_string(&display, display_buffer, font_builtin_fonts[FONT_FACE_TERMINUS_6X12_ISO8859_1], 0, 26, config->password, OLED_COLOR_WHITE, OLED_COLOR_BLACK); @@ -444,7 +428,7 @@ void qrcode_hide() { return; ssd1306_clear_screen(&display); - switch_screen_on ( false, 0); + switch_screen_off(); qrcode_shown = false; printf("%s: End, Free Heap %d\n", __func__, xPortGetFreeHeapSize()); @@ -458,45 +442,32 @@ void qrcode_hide() { void on_update(homekit_characteristic_t *ch, homekit_value_t value, void *context) { process_setting_update(); - switch_screen_on (true, SCREEN_DELAY); sdk_os_timer_arm (&save_timer, SAVE_DELAY, 0 ); } void up_button_callback(uint8_t gpio, void* args, const uint8_t param) { - if (screen_on == false){ - /* if the screen is currently off the just switch it on */ - switch_screen_on (true, SCREEN_DELAY); - } else { - /* the screen is on and the button press is to updated the value */ - switch_screen_on (true, SCREEN_DELAY); /* extend the screen delay */ - printf("Button UP single press\n"); - if (target_temperature.value.float_value <= (target_temperature.max_value[0] - 0.5)) - { - target_temperature.value.float_value += 0.5; - sdk_os_timer_arm (&save_timer, SAVE_DELAY, 0 ); - homekit_characteristic_notify(&target_temperature, target_temperature.value); - } + switch_screen_on (SCREEN_DELAY); /* ensure the screen is on */ + printf("Button UP single press\n"); + if (target_temperature.value.float_value <= (target_temperature.max_value[0] - 0.5)) + { + target_temperature.value.float_value += 0.5; + sdk_os_timer_arm (&save_timer, SAVE_DELAY, 0 ); + homekit_characteristic_notify(&target_temperature, target_temperature.value); } } void down_button_callback(uint8_t gpio, void* args, const uint8_t param) { - if (screen_on == false){ - /* if the screen is currently off the just switch it on */ - switch_screen_on (true, SCREEN_DELAY); - } else { - /* the screen is on and the button press is to updated the value */ - switch_screen_on (true, SCREEN_DELAY); /* extend the screen delay */ - printf("Button DOWN single press\n"); - if (target_temperature.value.float_value >= (target_temperature.min_value[0] + 0.5)) - { - target_temperature.value.float_value -= 0.5; - sdk_os_timer_arm (&save_timer, SAVE_DELAY, 0 ); - homekit_characteristic_notify(&target_temperature, target_temperature.value); - } + switch_screen_on (SCREEN_DELAY); /* ensure the screen is on */ + printf("Button DOWN single press\n"); + if (target_temperature.value.float_value >= (target_temperature.min_value[0] + 0.5)) + { + target_temperature.value.float_value -= 0.5; + sdk_os_timer_arm (&save_timer, SAVE_DELAY, 0 ); + homekit_characteristic_notify(&target_temperature, target_temperature.value); } } @@ -511,7 +482,7 @@ void process_setting_update() { current_state.value = HOMEKIT_UINT8(1); sdk_os_timer_arm (&save_timer, SAVE_DELAY, 0 ); homekit_characteristic_notify(¤t_state, current_state.value); - switch_screen_on (true, SCREEN_DELAY); + switch_screen_on (SCREEN_DELAY); } } else if ((state == 2 && current_temperature.value.float_value > target_temperature.value.float_value) || (state == 3 && current_temperature.value.float_value > cooling_threshold.value.float_value)) { @@ -519,14 +490,14 @@ void process_setting_update() { current_state.value = HOMEKIT_UINT8(2); sdk_os_timer_arm (&save_timer, SAVE_DELAY, 0 ); homekit_characteristic_notify(¤t_state, current_state.value); - switch_screen_on (true, SCREEN_DELAY); + switch_screen_on (SCREEN_DELAY); } } else { if (current_state.value.int_value != 0) { current_state.value = HOMEKIT_UINT8(0); sdk_os_timer_arm (&save_timer, SAVE_DELAY, 0 ); homekit_characteristic_notify(¤t_state, current_state.value); - switch_screen_on (true, SCREEN_DELAY); + switch_screen_on (SCREEN_DELAY); } } printf("%s: End - Free Heap %d\n",__func__, xPortGetFreeHeapSize()); @@ -549,7 +520,7 @@ void temperature_sensor_task(void *_args) { /*uxTaskGetStackHighWaterMark returns a number in bytes, stack is created in words, so device by 4 to get nujber of words left on stack */ if (success) { - printf("%s: Got readings: temperature %g, humidity %g, Stack Words left: %lu\n", __func__, temperature_value, humidity_value, uxTaskGetStackHighWaterMark(NULL)/4); + printf("%s: Got readings: temperature %2.1f, humidity %2.1f, Stack Words left: %lu\n", __func__, temperature_value, humidity_value, uxTaskGetStackHighWaterMark(NULL)/4); temp_diff = abs (current_temperature.value.float_value - temperature_value); humidity_diff = abs (current_humidity.value.float_value - humidity_value); if (temperature_value >= current_temperature.min_value[0] && temperature_value <= current_temperature.max_value[0] && temp_diff >= TEMP_DIFF_NOTIFY_TRIGGER) @@ -605,6 +576,7 @@ homekit_accessory_t *accessories[] = { &task_stats, &ota_beta, &lcm_beta, + &preserve_state, NULL }), NULL @@ -645,7 +617,7 @@ void thermostat_init() { gpio_enable(LED_GPIO, GPIO_OUTPUT); xTaskCreate(ssd1306_task, "ssd1306_task", 384, NULL, tskIDLE_PRIORITY+1, NULL); - sdk_os_timer_arm(&screen_timer, SCREEN_DELAY, 0); + sdk_os_timer_arm(&screen_off_timer, SCREEN_DELAY, 0); printf("%s: Screen Taks and Timer, Freep Heap=%d\n", __func__, xPortGetFreeHeapSize()); @@ -692,9 +664,15 @@ void recover_from_reset (int reason){ void save_characteristics ( ){ /* called by save timer*/ printf ("%s:\n", __func__); - save_characteristic_to_flash (&target_temperature, target_temperature.value); - save_characteristic_to_flash (¤t_state, current_state.value); - save_characteristic_to_flash(&wifi_check_interval, wifi_check_interval.value); + save_characteristic_to_flash(&preserve_state, preserve_state.value); + if ( preserve_state.value.bool_value == true){ + printf ("%s:Preserving state\n", __func__); + save_characteristic_to_flash (&target_temperature, target_temperature.value); + save_characteristic_to_flash (¤t_state, current_state.value); + save_characteristic_to_flash(&wifi_check_interval, wifi_check_interval.value); + } else { + printf ("%s:Not preserving state\n", __func__); + } }