From cc7390c3d5d57f15a0b1330590d8fdb46345e77e Mon Sep 17 00:00:00 2001 From: maccoylton Date: Fri, 21 Aug 2020 14:14:11 +0100 Subject: [PATCH] memory optimizations in udplogger, esp-homekit and wifi thermostat --- components/UDPlogger | 2 +- components/esp-homekit | 2 +- components/esp-homekit-common-functions | 2 +- src/latest-pre-release | 1 - src/ledstrip | 152 ++++++++++++++++++++++++ src/main.c | 86 ++++++++------ src/main.h | 26 +++- 7 files changed, 231 insertions(+), 40 deletions(-) delete mode 100644 src/latest-pre-release create mode 100644 src/ledstrip diff --git a/components/UDPlogger b/components/UDPlogger index 1dbf2df..37426f8 160000 --- a/components/UDPlogger +++ b/components/UDPlogger @@ -1 +1 @@ -Subproject commit 1dbf2df9c8f8fcb861aa8b787e9d17806a28ad41 +Subproject commit 37426f82c24ca5953f9900571a72eb8bde965aea diff --git a/components/esp-homekit b/components/esp-homekit index f6ffe1b..bb6ba44 160000 --- a/components/esp-homekit +++ b/components/esp-homekit @@ -1 +1 @@ -Subproject commit f6ffe1b08ceb8f179de8bfb635948cff14273969 +Subproject commit bb6ba44bee99fe96ee54098caa14afc3a7940bbf diff --git a/components/esp-homekit-common-functions b/components/esp-homekit-common-functions index 96faae9..529d52b 160000 --- a/components/esp-homekit-common-functions +++ b/components/esp-homekit-common-functions @@ -1 +1 @@ -Subproject commit 96faae992489e0a65793435634592918213d0190 +Subproject commit 529d52bef27a0b8fd409b7b2090e39ab6051d6cf diff --git a/src/latest-pre-release b/src/latest-pre-release deleted file mode 100644 index ff70965..0000000 --- a/src/latest-pre-release +++ /dev/null @@ -1 +0,0 @@ -0.7.13 \ No newline at end of file diff --git a/src/ledstrip b/src/ledstrip new file mode 100644 index 0000000..3e40b39 --- /dev/null +++ b/src/ledstrip @@ -0,0 +1,152 @@ +rBoot4LCM v1.0.1 + 0003ffff @ 1818 + 0003ffff @ 1028 => count: 1 +led_pin=2, polarity=0, led_valid=1 +Flash Size: 32 Mbit +Flash Mode: DOUT +Flash Speed: 40 MHz +rBoot Option: Big flash +rBoot Option: RTC data + +Booting rom 0 at 2000, load addr 707d0. +␌N��NQ +�a�Q�B=�Z␑`C␁�TPQ�␑q␁���rP�␀␁�VE�␑@␌�i�@����␝ �ڂ��Q���rP�␅1�VE␋X@␌�i␄ 5��Z�␂␐ +!!f�e 2020 10:54:35 +phy ver: 273, pp ver: 8.3 + +standard_init: Start, Freep Heap=44820 +standard_init: RTC power cycle count = 1 +standard_init:SDK version: 0.9.9, free heap 42336 +get_sysparam_info: Sysparam base address 4157440, num_sectors 4 +get_sysparam_info: sysparam name: wifi_ssid, value:MacCoylton-WiFi, key length:9, value length:15 +get_sysparam_info: sysparam name: wifi_password, value:********** +get_sysparam_info: sysparam name: On, value:n, key length:2, value length:1 + WiFi check interval: + 0a 00 00 00 +get_sysparam_info: while loop status 1 +load_characteristic_from_flash: WiFi check interval: int: 10 +homekit_characteristic_bounds_check: WiFi check interval: Checking integer bounds +load_characteristic_from_flash: ota_beta: bool: load_characteristic_from_flash: Error in sysparams error:1 loading characteristic +load_characteristic_from_flash: lcm_beta: bool: load_characteristic_from_flash: Error in sysparams error:1 loading characteristic +Create accessory name +|manuf='manuf_unknown' serial='CC:50:E3:2B:22:18' model='model_unknown' revision='0.0.0' c#=0 +wifi_check_stop_start Starting Task +standard_init: End, Freep Heap=39848 +>>> wifi_config: Initializing WiFi config +>>> wifi_config: Connecting to MacCoylton-WiFi +mode : sta(cc:50:e3:2b:22:18) +add if0 + +checkWifiTask : no check performed, check interval: 10, accessory paired: 0Free Heap=38472, Free Stack=88 +scandone +add 0 +aid 17 +cnt + +connected with MacCoylton-WiFi, channel 1 +dhcp client start... +ip:192.168.1.80,mask:255.255.255.0,gw:192.168.1.254 +>>> wifi_config: Connected to WiFi network +on_wifi_ready: , Freep Heap=41904 +get_sysparam_info: Sysparam base address 4157440, num_sectors 4 +get_sysparam_info: sysparam name: wifi_ssid, value:MacCoylton-WiFi, key length:9, value length:15 +get_sysparam_info: sysparam name: wifi_password, value:********** +get_sysparam_info: sysparam name: On, value:n, key length:2, value length:1 + WiFi check interval: + 0a 00 00 00 +get_sysparam_info: while loop status 1 +on_wifi_ready Reset Reason: Default Reset +on_wifi_ready: Exception Cause: 0 +EPC 1: 0 +EPC 2: 0 +EPC 3: 0 +Exv virtul address: 0 +DEPC: 0 +Return Address:0 +on_wifi_ready: Calling homekit_server_init, Freep Heap=41904 +on_wifi_ready: After calling homekit_Server_init, Freep Heap=28772 +on_wifi_ready: End, Freep Heap=28772 + +checkWifiTask : no check performed, check interval: 10, accessory paired: 0Free Heap=28772, Free Stack=88 +>>> HomeKit: Starting server +>>> HomeKit: Using existing accessory ID: 99:9A:A0:6D:70:4C +>>> HomeKit: Configuring mDNS +HomeKit: adding mDNS TXT record md=model_unknown +HomeKit: adding mDNS TXT record pv=1.0 +HomeKit: adding mDNS TXT record id=99:9A:A0:6D:70:4C +HomeKit: adding mDNS TXT record c#=1 +HomeKit: adding mDNS TXT record s#=1 +HomeKit: adding mDNS TXT record ff=0 +HomeKit: adding mDNS TXT record sf=1 +HomeKit: adding mDNS TXT record ci=5 +HomeKit: adding mDNS TXT record sh=Vk8Pzw== +on_homekit_event: Server initialised, Free Heap=23072 +on_homekit_event: Acessory is NOT paired on initialisation, Free Heap=23072 +wifi_check_stop_start Stopping Task +mDNS: Delaying probe until IP is acquired +Got IP, resuming mDNS +mDNS: Probing 1 Led_Strip-RGBW-2826776 +mDNS: Probing 1 Led_Strip-RGBW-2826776 +mDNS: Probing 2 Led_Strip-RGBW-2826776 +mDNS: Probing 3 Led_Strip-RGBW-2826776 +mDNS: Probe successful, announcing Led_Strip-RGBW-2826776 TXT md=model_unknown pv=1.0 id=99:9A:A0:6D:70:4C c#=1 s#=1 ff=0 sf=1 ci=5 sh=Vk8Pzw== +mDNS: Announcing 1 Led_Strip-RGBW-2826776 +mDNS: Announcing 2 Led_Strip-RGBW-2826776 +mDNS: Announcing 3 Led_Strip-RGBW-2826776 +mDNS: Announcing 4 Led_Strip-RGBW-2826776 +ip:192.168.1.80,mask:255.255.255.0,gw:192.168.1.254 +>>> HomeKit: Clients connected: 1 +>>> HomeKit: [Client 1] Got new client connection from 192.168.1.162 +on_homekit_event: Client connected, Free Heap=24692 +>>> HomeKit: [Client 1] Pair Setup Step 1/3 +>>> HomeKit: Clients connected: 2 +>>> HomeKit: [Client 2] Got new client connection from 192.168.1.162 +>>> HomeKit: [Client 1] Have existing connection from 192.168.1.162 X +on_homekit_event: Client connected, Free Heap=19492 +>>> HomeKit: [Client 1] Closing client connection from 192.168.1.162 +>>> HomeKit: Clients connected: 1 +on_homekit_event: Client disconnected, Free Heap=22088 +>>> HomeKit: Clients connected: 2 +>>> HomeKit: [Client 1] Got new client connection from 192.168.1.162 +>>> HomeKit: [Client 2] Have existing connection from 192.168.1.162 X +on_homekit_event: Client connected, Free Heap=22088 +>>> HomeKit: [Client 2] Pair Setup Step 1/3 +>>> HomeKit: [Client 2] Closing client connection from 192.168.1.162 +>>> HomeKit: Clients connected: 1 +on_homekit_event: Client disconnected, Free Heap=22596 +>>> HomeKit: [Client 1] Pair Setup Step 1/3 +>>> HomeKit: Clients connected: 2 +>>> HomeKit: [Client 2] Got new client connection from 192.168.1.162 +>>> HomeKit: [Client 1] Have existing connection from 192.168.1.162 X +on_homekit_event: Client connected, Free Heap=18984 +>>> HomeKit: [Client 1] Closing client connection from 192.168.1.162 +>>> HomeKit: Clients connected: 1 +on_homekit_event: Client disconnected, Free Heap=21580 +>>> HomeKit: Clients connected: 2 +>>> HomeKit: [Client 1] Got new client connection from 192.168.1.162 +>>> HomeKit: [Client 2] Have existing connection from 192.168.1.162 X +on_homekit_event: Client connected, Free Heap=21580 +>>> HomeKit: [Client 2] Pair Setup Step 1/3 +!!! HomeKit: [Client 2] Failed to dump SPR public key (code -2) +>>> HomeKit: [Client 2] Closing client connection from 192.168.1.162 +>>> HomeKit: Clients connected: 1 +on_homekit_event: Client disconnected, Free Heap=20108 +>>> HomeKit: Clients connected: 2 +>>> HomeKit: [Client 2] Got new client connection from 192.168.1.162 +>>> HomeKit: [Client 1] Have existing connection from 192.168.1.162 X +on_homekit_event: Client connected, Free Heap=20108 +>>> HomeKit: [Client 1] Pair Setup Step 1/3 +!!! HomeKit: [Client 1] Failed to dump SPR public key (code -2) +>>> HomeKit: [Client 1] Closing client connection from 192.168.1.162 +>>> HomeKit: Clients connected: 1 +on_homekit_event: Client disconnected, Free Heap=20392 +>>> HomeKit: [Client 2] Pair Setup Step 1/3 +!!! HomeKit: [Client 2] Failed to dump SPR public key (code -2) +>>> HomeKit: [Client 2] Closing client connection from 192.168.1.162 +>>> HomeKit: Clients connected: 0 +on_homekit_event: Client disconnected, Free Heap=22632 +>>> HomeKit: Clients connected: 1 +>>> HomeKit: [Client 1] Got new client connection from 192.168.1.162 +on_homekit_event: Client connected, Free Heap=24504 +>>> HomeKit: [Client 1] Pair Setup Step 1/3 + diff --git a/src/main.c b/src/main.c index 85a752c..cc407a6 100644 --- a/src/main.c +++ b/src/main.c @@ -51,6 +51,8 @@ #define DEVICE_MODEL "RGBW" #define DEVICE_SERIAL "12345678" #define FW_VERSION "1.0" +#define IR_RECEIVE_BUFFER_SIZE 128 +#define IR_DECODE_BUFFER_SIZE 32 #define LPF_SHIFT 8 // divide by 256 #define LPF_INTERVAL 10 // in milliseconds @@ -67,7 +69,8 @@ // and apply the four other parameters in the accessories_information section #include "ota-api.h" -hsi_color_t hsi_colours[16]; +/*hsi_color_t hsi_colours[16];*/ + const int status_led_gpio = 2; /*set the gloabl variable for the led to be sued for showing status */ int led_off_value=1; /* global varibale to support LEDs set to 0 where the LED is connected to GND, 1 where +3.3v */ // Global variables @@ -84,6 +87,8 @@ int blue_default_gpio = BLUE_PWM_PIN; rgb_color_t current_color = { { 0, 0, 0, 0 } }; rgb_color_t target_color = { { 0, 0, 0, 0 } }; +hsi_color_t current_colour_hsi = { { 0, 0, 0 } }; + homekit_characteristic_t wifi_reset = HOMEKIT_CHARACTERISTIC_(CUSTOM_WIFI_RESET, false, .setter=wifi_reset_set); homekit_characteristic_t wifi_check_interval = HOMEKIT_CHARACTERISTIC_(CUSTOM_WIFI_CHECK_INTERVAL, 200, .setter=wifi_check_interval_set); /* checks the wifi is connected and flashes status led to indicated connected */ @@ -127,22 +132,19 @@ homekit_characteristic_t colours_fade = HOMEKIT_CHARACTERISTIC_(CUSTOM_COLOURS_F homekit_characteristic_t colours_smooth = HOMEKIT_CHARACTERISTIC_(CUSTOM_COLOURS_SMOOTH, false ,.setter=colours_smooth_set, .getter=colours_smooth_get); homekit_characteristic_t pure_white = HOMEKIT_CHARACTERISTIC_(CUSTOM_COLOURS_PURE_WHITE, false , .setter=colours_pure_white_set); -double __ieee754_remainder(double x, double y) { - return x - y * floor(x/y); -} void ir_dump_task(void *arg) { - ir_rx_init(IR_RX_GPIO, 1024); + ir_rx_init(IR_RX_GPIO, IR_RECEIVE_BUFFER_SIZE); ir_decoder_t *nec_decoder = ir_generic_make_decoder(&nec_protocol_config); - int16_t buffer_size = sizeof(uint8_t) * 1024; + int16_t buffer_size = sizeof(uint8_t) * IR_DECODE_BUFFER_SIZE; int8_t *buffer = malloc(buffer_size); int size=0; - int index = 0; - + printf("%s: Start, Freep Heap=%d\n", __func__, xPortGetFreeHeapSize()); + while (1) { size = ir_recv(nec_decoder, 0, buffer, buffer_size); if (size <= 0) @@ -161,8 +163,7 @@ void ir_dump_task(void *arg) { int cmd = buffer[2]; int effect = off_effect; - index = 99; - + switch (cmd) { case on_button: @@ -208,64 +209,64 @@ void ir_dump_task(void *arg) { effect = flash_effect; break; case aubergene_button: - index = aubergene_index; + current_colour_hsi = aubergene_colour; break; case cream_button: - index = cream_index; + current_colour_hsi = cream_colour; break; case purple_button: - index = purple_index; + current_colour_hsi = purple_colour; break; case pink_button: - index = pink_index; + current_colour_hsi = pink_colour; break; case blue_button: - index = blue_index; + current_colour_hsi = blue_colour; break; case light_green_button: - index = light_green_index; + current_colour_hsi = light_green_colour; break; case green5_button: - index = green5_index; + current_colour_hsi = green5_colour; break; case white_button: - index = white_index; + current_colour_hsi = white_colour; break; case light_blue_button: - index = light_blue_index; + current_colour_hsi = light_blue_colour; break; case dark_orange_button: - index = dark_orange_index; + current_colour_hsi = dark_orange_colour; break; case red_button: - index = red_index; + current_colour_hsi = red_colour; break; case green_button: - index = green_index; + current_colour_hsi = green_colour; break; case yellow_button: - index = yellow_index; + current_colour_hsi = yellow_colour; break; case green4_button: - index = green4_index; + current_colour_hsi = green4_colour; break; case orange_button: - index = orange_index; + current_colour_hsi = orange_colour; break; case sky_blue_button: - index = sky_blue_index; + current_colour_hsi = sky_blue_colour; break; default: printf ("%s: LED command unknown %d\n",__func__, buffer[cmd]); break; } - if (index != 99){ - printf ("%s: LED command %d\n",__func__, cmd); - hue.value = HOMEKIT_FLOAT (hsi_colours[index].hue); - saturation.value = HOMEKIT_FLOAT (hsi_colours[index].saturation); - brightness.value = HOMEKIT_INT (hsi_colours[index].brightness); - } + printf ("%s: LED command %d\n",__func__, cmd); + + + hue.value = HOMEKIT_FLOAT (current_colour_hsi.hue); + saturation.value = HOMEKIT_FLOAT (current_colour_hsi.saturation); + brightness.value = HOMEKIT_INT (current_colour_hsi.brightness); homekit_characteristic_notify(&hue,hue.value ); homekit_characteristic_notify(&saturation,saturation.value ); @@ -292,10 +293,12 @@ void ir_dump_task(void *arg) { } + void led_strip_init (){ /* set th default values for the GPIOs incase we need to rest them later */ + printf("%s: Start, Freep Heap=%d\n", __func__, xPortGetFreeHeapSize()); rgbw_lights_init(); @@ -304,7 +307,7 @@ void led_strip_init (){ led_saturation = saturation.value.float_value; led_brightness = brightness.value.int_value; - hsi_colours[white_index] = (hsi_color_t) {{0.0, 0.0, 100}}; +/* hsi_colours[white_index] = (hsi_color_t) {{0.0, 0.0, 100}}; hsi_colours[red_index] = (hsi_color_t) {{ 0.0, 100.0, 100}}; hsi_colours[dark_orange_index] = (hsi_color_t) { { 15, 100, 78 }}; @@ -323,8 +326,12 @@ void led_strip_init (){ hsi_colours[aubergene_index] = (hsi_color_t) { { 280, 100, 50 }}; hsi_colours[purple_index] = (hsi_color_t) { { 280, 100, 75 }}; hsi_colours[pink_index] = (hsi_color_t) { { 300, 100, 100 }}; - +*/ +/* xTaskCreate(ir_dump_task, "read_ir_task", 256, NULL, 2, NULL); +*/ + + printf("%s: Snd, Freep Heap=%d\n", __func__, xPortGetFreeHeapSize()); } @@ -378,8 +385,13 @@ homekit_server_config_t config = { void recover_from_reset (int reason){ /* called if we restarted abnormally */ + printf("%s: Start, Freep Heap=%d\n", __func__, xPortGetFreeHeapSize()); + printf ("%s: reason %d\n", __func__, reason); load_characteristic_from_flash(&on); + + printf("%s: End, Freep Heap=%d\n", __func__, xPortGetFreeHeapSize()); + } void accessory_init_not_paired (void) { @@ -388,6 +400,8 @@ void accessory_init_not_paired (void) { void accessory_init (void ){ /* initalise anything you don't want started until wifi and pairing is confirmed */ + printf("%s: Start, Freep Heap=%d\n", __func__, xPortGetFreeHeapSize()); + get_sysparam_info(); printf ("%s: GPIOS are set as follows : W=%d, R=%d, G=%d, B=%d\n",__func__, white_gpio.value.int_value,red_gpio.value.int_value, green_gpio.value.int_value, blue_gpio.value.int_value ); led_strip_init (); @@ -397,7 +411,9 @@ void accessory_init (void ){ homekit_characteristic_notify(&saturation,saturation.value ); homekit_characteristic_notify(&brightness,brightness.value ); homekit_characteristic_notify(&pure_white,pure_white.value ); - + + printf("%s: End, Freep Heap=%d\n", __func__, xPortGetFreeHeapSize()); + } void user_init(void) { diff --git a/src/main.h b/src/main.h index 7ab7acc..dec6b0d 100644 --- a/src/main.h +++ b/src/main.h @@ -30,7 +30,31 @@ enum {strobe_button=0, aubergene_button=4, up_button=9, cream_button=10,smooth_b -enum {aubergene_index, cream_index, purple_index, pink_index, blue_index, light_green_index,green5_index, white_index, light_blue_index, dark_orange_index, red_index, green_index, yellow_index, green4_index, orange_index, sky_blue_index} colour_index; +/*enum {aubergene_index, cream_index, purple_index, pink_index, blue_index, light_green_index,green5_index, white_index, light_blue_index, dark_orange_index, red_index, green_index, yellow_index, green4_index, orange_index, sky_blue_index} colour_index; +*/ + + +const static hsi_color_t white_colour = {{0.0, 0.0, 100}}; + +const static hsi_color_t red_colour = {{ 0.0, 100.0, 100}}; +const static hsi_color_t dark_orange_colour = { { 15, 100, 78 }}; +const static hsi_color_t orange_colour = { { 30, 100, 100 }}; +const static hsi_color_t cream_colour = { { 45, 100, 100 }}; +const static hsi_color_t yellow_colour = { { 60, 100, 100 }}; + +const static hsi_color_t green_colour = { { 120, 100, 100 }}; +const static hsi_color_t light_green_colour = { { 140, 100, 100 }}; +const static hsi_color_t sky_blue_colour = { { 180, 100, 100 }}; +const static hsi_color_t green4_colour = { { 180, 100, 80 }}; +const static hsi_color_t green5_colour = { { 180, 100, 60 }}; + +const static hsi_color_t blue_colour = { { 240, 100, 100 }}; +const static hsi_color_t light_blue_colour = { { 255, 100, 100 }}; +const static hsi_color_t aubergene_colour = { { 280, 100, 50 }}; +const static hsi_color_t purple_colour = { { 280, 100, 75 }}; +const static hsi_color_t pink_colour = { { 300, 100, 100 }}; + + /*int8_t mh_strobe[]={0 , -1, 0, -1 }; int8_t mh_aubergene[]={0 , -1, 4, -5 };