From 8d7419b6665ed88825dbb056915573796c73bbb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Sat, 9 Sep 2017 03:45:35 +0200 Subject: [PATCH] You wouldn't believe this simple trick that freed 10 kB of RAM! --- CMakeLists.txt | 3 +- esp_iot_sdk_v1.5.2/include/osapi.h | 6 +- esphttpdconfig.mk | 4 +- html_orig/js/app.js | 239 +++++++++++++++-------------- libesphttpd | 2 +- user/apars_csi.c | 4 +- user/character_sets.h | 6 +- user/routes.c | 2 +- user/routes.h | 2 +- 9 files changed, 136 insertions(+), 132 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 33ed3d9..a5a59a5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -110,8 +110,6 @@ set(SOURCE_FILES user/serial.h user/routes.c user/routes.h - user/uart_asyncdrv.c- - user/uart_asyncdrv.h- user/cgi_main.c user/cgi_main.h user/cgi_sockets.c @@ -155,6 +153,7 @@ include_directories(esp_iot_sdk_v1.5.2/include) add_definitions( -D__ets__ -DICACHE_FLASH + -DUSE_OPTIMIZE_PRINTF=1 -DHTTPD_MAX_CONNECTIONS=5 -DHTTPD_STACKSIZE=1000 -DICACHE_FLASH_ATTR= diff --git a/esp_iot_sdk_v1.5.2/include/osapi.h b/esp_iot_sdk_v1.5.2/include/osapi.h index 5cac8dd..9b352b2 100755 --- a/esp_iot_sdk_v1.5.2/include/osapi.h +++ b/esp_iot_sdk_v1.5.2/include/osapi.h @@ -35,9 +35,9 @@ #define os_snprintf ets_snprintf #ifdef USE_OPTIMIZE_PRINTF -#define os_printf(fmt, ...) do { \ - static const char flash_str[] ICACHE_RODATA_ATTR STORE_ATTR = fmt; \ - os_printf_plus(flash_str, ##__VA_ARGS__); \ +#define os_printf(fmt, ...) do { \ + static const char flash_str[] ICACHE_RODATA_ATTR STORE_ATTR = fmt; \ + os_printf_plus(flash_str, ##__VA_ARGS__); \ } while(0) #else #define os_printf os_printf_plus diff --git a/esphttpdconfig.mk b/esphttpdconfig.mk index 981ec4e..1bbac87 100644 --- a/esphttpdconfig.mk +++ b/esphttpdconfig.mk @@ -54,4 +54,6 @@ GLOBAL_CFLAGS = \ -DHTTPD_MAX_POST_LEN=512 \ -DDEBUG_INPUT=0 \ -DDEBUG_HEAP=1 \ - -DDEBUG_MALLOC=0 + -DDEBUG_MALLOC=0 \ + -mforce-l32 \ + -DUSE_OPTIMIZE_PRINTF=1 diff --git a/html_orig/js/app.js b/html_orig/js/app.js index a97b633..1d828bd 100644 --- a/html_orig/js/app.js +++ b/html_orig/js/app.js @@ -1609,135 +1609,138 @@ function tr(key) { return _tr[key] || '?'+key+'?'; } w.startScanning = startScanning; })(window.WiFi = {}); /** Handle connections */ -var Conn = (function() { - var ws; - var heartbeatTout; - var pingIv; - var xoff = false; - var autoXoffTout; - - function onOpen(evt) { - console.log("CONNECTED"); - } - - function onClose(evt) { - console.warn("SOCKET CLOSED, code "+evt.code+". Reconnecting..."); - setTimeout(function() { - init(); - }, 200); - // this happens when the buffer gets fucked up via invalid unicode. - // we basically use polling instead of socket then - } - - function onMessage(evt) { - try { - // . = heartbeat - switch (evt.data.charAt(0)) { - case 'B': - case 'T': - case 'S': - Screen.load(evt.data); - break; - - case '-': - //console.log('xoff'); - xoff = true; - autoXoffTout = setTimeout(function(){xoff=false;}, 250); - break; - - case '+': - //console.log('xon'); - xoff = false; - clearTimeout(autoXoffTout); - break; - } - heartbeat(); - } catch(e) { - console.error(e); - } - } +var Conn = (function () { + var ws; + var heartbeatTout; + var pingIv; + var xoff = false; + var autoXoffTout; + var reconTout; + + var pageShown = false; + + function onOpen(evt) { + console.log("CONNECTED"); + doSend("i"); + } - function canSend() { - return !xoff; - } + function onClose(evt) { + console.warn("SOCKET CLOSED, code " + evt.code + ". Reconnecting..."); + clearTimeout(reconTout); + reconTout = setTimeout(function () { + init(); + }, 2000); + // this happens when the buffer gets fucked up via invalid unicode. + // we basically use polling instead of socket then + } - function doSend(message) { - if (_demo) { - console.log("TX: ", message); - return true; // Simulate success - } - if (xoff) { - // TODO queue - console.log("Can't send, flood control."); - return false; - } + function onMessage(evt) { + try { + // . = heartbeat + switch (evt.data.charAt(0)) { + case 'B': + case 'T': + case 'S': + Screen.load(evt.data); + if(!pageShown) { + showPage(); + pageShown = true; + } + break; + + case '-': + //console.log('xoff'); + xoff = true; + autoXoffTout = setTimeout(function () { + xoff = false; + }, 250); + break; + + case '+': + //console.log('xon'); + xoff = false; + clearTimeout(autoXoffTout); + break; + } + heartbeat(); + } catch (e) { + console.error(e); + } + } - if (!ws) return false; // for dry testing - if (ws.readyState != 1) { - console.error("Socket not ready"); - return false; - } - if (typeof message != "string") { - message = JSON.stringify(message); - } - ws.send(message); - return true; - } + function canSend() { + return !xoff; + } - function init() { - if (_demo) { - console.log("Demo mode!"); - Screen.load(_demo_screen); - showPage(); - return; - } - heartbeat(); + function doSend(message) { + if (_demo) { + console.log("TX: ", message); + return true; // Simulate success + } + if (xoff) { + // TODO queue + console.log("Can't send, flood control."); + return false; + } - ws = new WebSocket("ws://"+_root+"/term/update.ws"); - ws.onopen = onOpen; - ws.onclose = onClose; - ws.onmessage = onMessage; + if (!ws) return false; // for dry testing + if (ws.readyState != 1) { + console.error("Socket not ready"); + return false; + } + if (typeof message != "string") { + message = JSON.stringify(message); + } + ws.send(message); + return true; + } - console.log("Opening socket."); + function init() { + if (_demo) { + console.log("Demo mode!"); + Screen.load(_demo_screen); + showPage(); + return; + } - // Ask for initial data - $.get('http://'+_root+'/term/init', function(resp, status) { - if (status !== 200) location.reload(true); - console.log("Data received!"); - Screen.load(resp); - heartbeat(); + clearTimeout(reconTout); + clearTimeout(heartbeatTout); - showPage(); - }); - } + ws = new WebSocket("ws://" + _root + "/term/update.ws"); + ws.onopen = onOpen; + ws.onclose = onClose; + ws.onmessage = onMessage; + console.log("Opening socket."); + heartbeat(); + } - function heartbeat() { - clearTimeout(heartbeatTout); - heartbeatTout = setTimeout(heartbeatFail, 2000); - } + function heartbeat() { + clearTimeout(heartbeatTout); + heartbeatTout = setTimeout(heartbeatFail, 2000); + } - function heartbeatFail() { - console.error("Heartbeat lost, probing server..."); - pingIv = setInterval(function() { - console.log("> ping"); - $.get('http://'+_root+'/system/ping', function(resp, status) { - if (status == 200) { - clearInterval(pingIv); - console.info("Server ready, reloading page..."); - location.reload(); - } - }, { - timeout: 100, - }); - }, 500); - } + function heartbeatFail() { + console.error("Heartbeat lost, probing server..."); + pingIv = setInterval(function () { + console.log("> ping"); + $.get('http://' + _root + '/system/ping', function (resp, status) { + if (status == 200) { + clearInterval(pingIv); + console.info("Server ready, reloading page..."); + location.reload(); + } + }, { + timeout: 100, + }); + }, 1000); + } - return { - ws: null, - init: init, - send: doSend, - canSend: canSend, // check flood control - }; + return { + ws: null, + init: init, + send: doSend, + canSend: canSend, // check flood control + }; })(); /** * User input diff --git a/libesphttpd b/libesphttpd index 13fa224..3479ab3 160000 --- a/libesphttpd +++ b/libesphttpd @@ -1 +1 @@ -Subproject commit 13fa224963081e9ff298abd74a59faafcb9bf816 +Subproject commit 3479ab3efcb4581669370cde6a607f936ff5515a diff --git a/user/apars_csi.c b/user/apars_csi.c index e0de27d..8fb738d 100644 --- a/user/apars_csi.c +++ b/user/apars_csi.c @@ -760,7 +760,7 @@ do_csi_xterm_screen_cmd(CSI_Data *opts) // data tables for the DECREPTPARM command response struct DECREPTPARM_parity { int parity; const char * msg; }; -static const struct DECREPTPARM_parity DECREPTPARM_parity_arr[] = { +static const struct DECREPTPARM_parity DECREPTPARM_parity_arr[] ESP_CONST_DATA = { {PARITY_NONE, "1"}, {PARITY_ODD, "4"}, {PARITY_EVEN, "5"}, @@ -768,7 +768,7 @@ static const struct DECREPTPARM_parity DECREPTPARM_parity_arr[] = { }; struct DECREPTPARM_baud { int baud; const char * msg; }; -static const struct DECREPTPARM_baud DECREPTPARM_baud_arr[] = { +static const struct DECREPTPARM_baud DECREPTPARM_baud_arr[] ESP_CONST_DATA = { {BIT_RATE_300, "48"}, {BIT_RATE_600, "56"}, {BIT_RATE_1200, "64"}, diff --git a/user/character_sets.h b/user/character_sets.h index 2e4b688..4c1943e 100644 --- a/user/character_sets.h +++ b/user/character_sets.h @@ -12,7 +12,7 @@ #define CODEPAGE_A_BEGIN 35 #define CODEPAGE_A_END 35 -static const u16 codepage_A[] = +static const u16 codepage_A[] ESP_CONST_DATA = {// Unicode ASCII SYM // %%BEGIN:A%% 0x20a4, // 35 # £ @@ -26,7 +26,7 @@ static const u16 codepage_A[] = * translates VT100 ACS escape codes to Unicode values. * Based on rxvt-unicode screen.C table. */ -static const u16 codepage_0[] = +static const u16 codepage_0[] ESP_CONST_DATA = {// Unicode ASCII SYM // %%BEGIN:0%% 0x2666, // 96 ` ♦ @@ -66,7 +66,7 @@ static const u16 codepage_0[] = #define CODEPAGE_1_BEGIN 33 #define CODEPAGE_1_END 126 -static const u16 codepage_1[] = +static const u16 codepage_1[] ESP_CONST_DATA = {// Unicode ASCII SYM DOS // %%BEGIN:1%% 0x263A, // 33 ! ☺ (1) - low ASCII symbols from DOS, moved to +32 diff --git a/user/routes.c b/user/routes.c index 6523cc3..74bb866 100644 --- a/user/routes.c +++ b/user/routes.c @@ -22,7 +22,7 @@ static int wifiPassFn(HttpdConnData *connData, int no, char *user, int userLen, /** * Application routes */ -HttpdBuiltInUrl routes[] = { +const HttpdBuiltInUrl routes[] ESP_CONST_DATA = { // redirect func for the captive portal ROUTE_CGI_ARG("*", cgiRedirectApClientToHostname, "esp-terminal.ap"), diff --git a/user/routes.h b/user/routes.h index cc266c0..2ba843f 100644 --- a/user/routes.h +++ b/user/routes.h @@ -4,7 +4,7 @@ #include #include -extern HttpdBuiltInUrl routes[]; +extern const HttpdBuiltInUrl routes[]; /** Broadcast screen state to sockets */ void screen_notifyChange();