diff --git a/html/console.tpl b/html/console.tpl index b828ccea..17d0c13f 100644 --- a/html/console.tpl +++ b/html/console.tpl @@ -9,12 +9,12 @@

The Microcontroller console shows the last 1024 characters received from UART0, to which a microcontroller is typically attached.

- Reset µC + Reset µC  Baud: - 57600 - 115200 - 230400 - 460800 + 57600 + 115200 + 230400 + 460800


     
@@ -36,14 +36,14 @@
 
         if(xhr.status !== 200) {
           //console.log("handleEv error cb");
-          errCb(xhr);
+          if (errCb != null) errCb(xhr);
           return;
         }
 
         // all is well  
         if(xhr.readyState === 4) {
           //console.log("handleEv success cb");
-          okCb(xhr, JSON.parse(xhr.responseText));
+          if (okCb != null) okCb(xhr, JSON.parse(xhr.responseText));
         }
       } catch(e) {return;}
     }
@@ -59,7 +59,7 @@
       el.innerHTML = "";
     }
     window.setTimeout(function() {
-      loadJSON("/console?start=" + el.textEnd, updateText, retryLoad);
+      loadJSON("/console/text?start=" + el.textEnd, updateText, retryLoad);
     }, delay);
   }
 
@@ -87,8 +87,28 @@
     fetchText(1000);
   }
 
+  function baudButton(baud) {
+    document.getElementById(""+baud+"-button").addEventListener("click", function(e) {
+      console.log("switching to", baud, "baud");
+      e.preventDefault();
+      loadJSON("/console/baud?rate="+baud);
+    });
+  }
+
   window.onload = function() {
     fetchText(100);
+
+    document.getElementById("reset-button").addEventListener("click", function(e) {
+      el = document.getElementById("console");
+      e.preventDefault();
+      //console.log("reset click");
+      el.innerHTML = "";
+      loadJSON("/console/reset");
+    });
+    baudButton(57600);
+    baudButton(115200);
+    baudButton(230400);
+    baudButton(460800);
   }
 
 
diff --git a/serial/console.c b/serial/console.c
index cf2c37ba..647e0fb4 100644
--- a/serial/console.c
+++ b/serial/console.c
@@ -1,6 +1,8 @@
 #include 
 #include "uart.h"
 #include "cgi.h"
+#include "uart.h"
+#include "serbridge.h"
 #include "console.h"
 
 // Microcontroller console capturing the last 1024 characters received on the uart so
@@ -49,6 +51,30 @@ jsonHeader(HttpdConnData *connData, int code) {
 	httpdEndHeaders(connData);
 }
 
+int ICACHE_FLASH_ATTR
+ajaxConsoleReset(HttpdConnData *connData) {
+	jsonHeader(connData, 200);
+	serbridgeReset();
+	return HTTPD_CGI_DONE;
+}
+
+int ICACHE_FLASH_ATTR
+ajaxConsoleBaud(HttpdConnData *connData) {
+	char buff[2048];
+	int len;
+	len = httpdFindArg(connData->getArgs, "rate", buff, sizeof(buff));
+	if (len > 0) {
+		int rate = atoi(buff);
+		if (rate >= 9600 && rate <= 1000000) {
+			jsonHeader(connData, 200);
+			uart0_baud(rate);
+			return HTTPD_CGI_DONE;
+		}
+	}
+	jsonHeader(connData, 400);
+	return HTTPD_CGI_DONE;
+}
+
 int ICACHE_FLASH_ATTR
 ajaxConsole(HttpdConnData *connData) {
 	char buff[2048];
diff --git a/serial/console.h b/serial/console.h
index 91fa8e67..dc58a267 100644
--- a/serial/console.h
+++ b/serial/console.h
@@ -6,6 +6,8 @@
 void consoleInit(void);
 void ICACHE_FLASH_ATTR console_write_char(char c);
 int ajaxConsole(HttpdConnData *connData);
+int ajaxConsoleReset(HttpdConnData *connData);
+int ajaxConsoleBaud(HttpdConnData *connData);
 int tplConsole(HttpdConnData *connData, char *token, void **arg);
 
 #endif
diff --git a/serial/serbridge.c b/serial/serbridge.c
index 2b2d0dc2..8d8f46ed 100644
--- a/serial/serbridge.c
+++ b/serial/serbridge.c
@@ -172,6 +172,13 @@ telnetUnwrap(uint8_t *inBuf, int len, uint8_t state)
 	return state;
 }
 
+void ICACHE_FLASH_ATTR serbridgeReset() {
+	os_printf("MCU reset gpio%d\n", mcu_reset_pin);
+	GPIO_OUTPUT_SET(mcu_reset_pin, 0);
+	os_delay_us(100L);
+	GPIO_OUTPUT_SET(mcu_reset_pin, 1);
+}
+
 
 // Receive callback
 static void ICACHE_FLASH_ATTR serbridgeRecvCb(void *arg, char *data, unsigned short len) {
diff --git a/serial/serbridge.h b/serial/serbridge.h
index 93352383..bcb3d7a6 100644
--- a/serial/serbridge.h
+++ b/serial/serbridge.h
@@ -34,5 +34,6 @@ struct serbridgeConnData {
 
 void ICACHE_FLASH_ATTR serbridgeInit(int port, uint8_t reset_pin, uint8_t isp_pin);
 void ICACHE_FLASH_ATTR serbridgeUartCb(char *buf, int len);
+void ICACHE_FLASH_ATTR serbridgeReset();
 
 #endif /* __SER_BRIDGE_H__ */
diff --git a/serial/uart.c b/serial/uart.c
index 252113ee..23fb3b16 100644
--- a/serial/uart.c
+++ b/serial/uart.c
@@ -237,6 +237,11 @@ uart_recvTask(os_event_t *events)
 	ETS_UART_INTR_ENABLE();
 }
 
+void ICACHE_FLASH_ATTR
+uart0_baud(int rate) {
+  uart_div_modify(UART0, UART_CLK_FREQ / rate);
+}
+
 /******************************************************************************
  * FunctionName : uart_init
  * Description  : user interface for init uart
diff --git a/serial/uart.h b/serial/uart.h
index 1b769bce..2d1d4328 100644
--- a/serial/uart.h
+++ b/serial/uart.h
@@ -21,4 +21,6 @@ STATUS uart_tx_one_char(uint8 uart, uint8 c);
 // with all new characters.
 void ICACHE_FLASH_ATTR uart_add_recv_cb(UartRecv_cb cb);
 
+void ICACHE_FLASH_ATTR uart0_baud(int rate);
+
 #endif /* __UART_H__ */
diff --git a/user/user_main.c b/user/user_main.c
index 1733134e..21fc306f 100644
--- a/user/user_main.c
+++ b/user/user_main.c
@@ -68,7 +68,9 @@ HttpdBuiltInUrl builtInUrls[]={
 	{"/help.tpl", cgiEspFsTemplate, tplCounter},
 	{"/log.tpl", cgiEspFsTemplate, tplLog},
 	{"/console.tpl", cgiEspFsTemplate, tplConsole},
-	{"/console", ajaxConsole, NULL},
+	{"/console/reset", ajaxConsoleReset, NULL},
+	{"/console/baud", ajaxConsoleBaud, NULL},
+	{"/console/text", ajaxConsole, NULL},
 
 	//Routines to make the /wifi URL and everything beneath it work.