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 @@
+
+
+
+
+ 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;
+ }
+ });
});