Skip to content

Commit

Permalink
simplified and hopefully improved screen handling
Browse files Browse the repository at this point in the history
  • Loading branch information
David B Brown committed Jun 22, 2022
1 parent 6c16320 commit b0725d6
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 89 deletions.
2 changes: 1 addition & 1 deletion components/UDPlogger
Submodule UDPlogger updated 4 files
+2 −0 CMakeLists.txt
+13 −2 README.md
+62 −22 udplogger.c
+14 −6 udplogger.h
2 changes: 1 addition & 1 deletion components/esp-homekit
2 changes: 1 addition & 1 deletion components/esp-wifi-config
Submodule esp-wifi-config updated 2 files
+15 −13 README.md
+3 −3 tools/embed.py
2 changes: 1 addition & 1 deletion components/esp-wolfssl
Submodule esp-wolfssl updated 2 files
+20 −54 CMakeLists.txt
+0 −28 user_settings.h
17 changes: 9 additions & 8 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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:
Expand Down
130 changes: 54 additions & 76 deletions src/wifi_thermostat.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ static enum screen_display screen;

// I2C

ETSTimer screen_timer;
ETSTimer screen_off_timer;

#include <ota-api.h>
homekit_characteristic_t wifi_reset = HOMEKIT_CHARACTERISTIC_(CUSTOM_WIFI_RESET, false, .setter=wifi_reset_set);
Expand All @@ -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);
Expand All @@ -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);
}
Expand Down Expand Up @@ -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:
Expand All @@ -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");
}
Expand All @@ -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");
}

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

}
Expand Down Expand Up @@ -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);
Expand All @@ -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());
Expand All @@ -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);
}
}

Expand All @@ -511,22 +482,22 @@ void process_setting_update() {
current_state.value = HOMEKIT_UINT8(1);
sdk_os_timer_arm (&save_timer, SAVE_DELAY, 0 );
homekit_characteristic_notify(&current_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)) {
if (current_state.value.int_value != 2) {
current_state.value = HOMEKIT_UINT8(2);
sdk_os_timer_arm (&save_timer, SAVE_DELAY, 0 );
homekit_characteristic_notify(&current_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(&current_state, current_state.value);
switch_screen_on (true, SCREEN_DELAY);
switch_screen_on (SCREEN_DELAY);
}
}
printf("%s: End - Free Heap %d\n",__func__, xPortGetFreeHeapSize());
Expand All @@ -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)
Expand Down Expand Up @@ -605,6 +576,7 @@ homekit_accessory_t *accessories[] = {
&task_stats,
&ota_beta,
&lcm_beta,
&preserve_state,
NULL
}),
NULL
Expand Down Expand Up @@ -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());

Expand Down Expand Up @@ -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 (&current_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 (&current_state, current_state.value);
save_characteristic_to_flash(&wifi_check_interval, wifi_check_interval.value);
} else {
printf ("%s:Not preserving state\n", __func__);
}
}


Expand Down

0 comments on commit b0725d6

Please sign in to comment.