From 614c646b3c615bcf6fe613e6ab75407f9b3507c2 Mon Sep 17 00:00:00 2001 From: Kayo Phoenix Date: Sat, 14 Nov 2015 00:35:15 +0500 Subject: [PATCH] Added sending text to console from web interface. --- esp-link/main.c | 1 + html/console.html | 101 ++++++++++++++++++++++++++++++++++++++++++++++ serial/console.c | 17 ++++++++ serial/console.h | 1 + 4 files changed, 120 insertions(+) diff --git a/esp-link/main.c b/esp-link/main.c index 1744bd0..8c00bae 100644 --- a/esp-link/main.c +++ b/esp-link/main.c @@ -57,6 +57,7 @@ HttpdBuiltInUrl builtInUrls[] = { { "/console/reset", ajaxConsoleReset, NULL }, { "/console/baud", ajaxConsoleBaud, NULL }, { "/console/text", ajaxConsole, NULL }, + { "/console/send", ajaxConsoleSend, NULL }, //Enable the line below to protect the WiFi configuration with an username/password combo. // {"/wifi/*", authBasic, myPassFn}, { "/wifi", cgiRedirect, "/wifi/wifi.html" }, diff --git a/html/console.html b/html/console.html index 9dc359c..c3b9fab 100644 --- a/html/console.html +++ b/html/console.html @@ -13,6 +13,23 @@


+      Console entry
+      
+      
+      
+      

Type the command and press ENTER. + Press ESC to clear the entry. + The UP/DOWN arrow keys can be used to get previously sent commands from history. +

+
+ + +
+
+ + +
+
@@ -41,6 +58,90 @@ function(data) { showRate(data.rate); }, function(s, st) { showNotification(st); } ); + + var sendHistory = $("#send-history"); + var inputText = $("#input-text"); + var inputAddCr = $("#input-add-cr"); + var inputAddLf = $("#input-add-lf"); + + function findHistory(text) { + for (var i = 0; i < sendHistory.children.length; i++) { + if (text == sendHistory.children[i].value) { + return i; + } + } + return null; + } + + function loadHistory(idx) { + sendHistory.value = sendHistory.children[idx].value; + inputText.value = sendHistory.children[idx].value; + } + + function navHistory(rel) { + var idx = findHistory(sendHistory.value) + rel; + if (idx < 0) { + idx = sendHistory.children.length - 1; + } + if (idx >= sendHistory.children.length) { + idx = 0; + } + loadHistory(idx); + } + + sendHistory.addEventListener("change", function(e) { + inputText.value = sendHistory.value; + }); + + function pushHistory(text) { + var idx = findHistory(text); + if (idx !== null) { + loadHistory(idx); + return false; + } + var newOption = m(''); + newOption.value = text; + sendHistory.appendChild(newOption); + sendHistory.value = text; + for (; sendHistory.children.length > 15; ) { + sendHistory.removeChild(sendHistory.children[0]); + } + return true; + } + + inputText.addEventListener("keydown", function(e) { + switch (e.keyCode) { + case 38: /* the up arrow key pressed */ + e.preventDefault(); + navHistory(-1); + break; + case 40: /* the down arrow key pressed */ + e.preventDefault(); + navHistory(+1); + break; + case 27: /* the escape key pressed */ + e.preventDefault(); + inputText.value = ""; + sendHistory.value = ""; + break; + case 13: /* the enter key pressed */ + e.preventDefault(); + var text = inputText.value; + if (inputAddCr.checked) text += '\r'; + if (inputAddLf.checked) text += '\n'; + ajaxSpin('POST', "/console/send?text=" + encodeURIComponent(text), + function(resp) { showNotification("uC sent"); pushHistory(inputText.value); }, + function(s, st) { showWarning("Error sending text to uC"); } + ); + break; + } + }); }); diff --git a/serial/console.c b/serial/console.c index d0d0dd7..68b5b55 100644 --- a/serial/console.c +++ b/serial/console.c @@ -81,6 +81,23 @@ ajaxConsoleBaud(HttpdConnData *connData) { return HTTPD_CGI_DONE; } +int ICACHE_FLASH_ATTR +ajaxConsoleSend(HttpdConnData *connData) { + if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up. + char buff[2048]; + int len, status = 400; + + // figure out where to start in buffer based on URI param + len = httpdFindArg(connData->getArgs, "text", buff, sizeof(buff)); + if (len > 0) { + uart0_tx_buffer(buff, len); + status = 200; + } + + jsonHeader(connData, status); + return HTTPD_CGI_DONE; +} + int ICACHE_FLASH_ATTR ajaxConsole(HttpdConnData *connData) { if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up. diff --git a/serial/console.h b/serial/console.h index dc58a26..6225c0a 100644 --- a/serial/console.h +++ b/serial/console.h @@ -8,6 +8,7 @@ void ICACHE_FLASH_ATTR console_write_char(char c); int ajaxConsole(HttpdConnData *connData); int ajaxConsoleReset(HttpdConnData *connData); int ajaxConsoleBaud(HttpdConnData *connData); +int ajaxConsoleSend(HttpdConnData *connData); int tplConsole(HttpdConnData *connData, char *token, void **arg); #endif