diff --git a/Makefile b/Makefile index 75c00d8..751305f 100644 --- a/Makefile +++ b/Makefile @@ -64,9 +64,7 @@ ESP_HOSTNAME ?= esp8266 # which modules (subdirectories) of the project to include in compiling #MODULES = driver user lwip/api lwip/app lwip/core lwip/core/ipv4 lwip/netif MODULES = espfs httpd user serial -EXTRA_INCDIR = include \ - . \ - lib/heatshrink/ +EXTRA_INCDIR = include . lib/heatshrink/ # libraries used in this project, mainly provided by the SDK LIBS = c gcc hal phy pp net80211 wpa main lwip diff --git a/html/console.tpl b/html/console.tpl new file mode 100644 index 0000000..32ed8c4 --- /dev/null +++ b/html/console.tpl @@ -0,0 +1,11 @@ +Console - ESP Link + + + +
+

ESP Link - Debug Console

+
+%console%
+
+
+ diff --git a/html/index.tpl b/html/index.tpl index f789db7..70c4bd8 100644 --- a/html/index.tpl +++ b/html/index.tpl @@ -5,7 +5,9 @@

Home | Wifi | - Serial | LED | Help

+ Serial | LED | Help | + Console +

ESP Link

diff --git a/user/console.c b/user/console.c new file mode 100644 index 0000000..a676c18 --- /dev/null +++ b/user/console.c @@ -0,0 +1,65 @@ +#include +#include "console.h" + +// Web console for the esp8266 to replace outputting to uart1. +// The web console has a 1KB circular in-memory buffer which os_printf prints into and +// the HTTP handler simply displays the buffer content on a web page. + +#define BUF_MAX (1024) +static char console_buf[BUF_MAX]; +static int console_wr, console_rd; + +static void ICACHE_FLASH_ATTR +console_write(char c) { + int wr = (console_wr+1)%BUF_MAX; + if (wr != console_rd) { + console_buf[console_wr] = c; + console_wr = wr; + } +} + +static char ICACHE_FLASH_ATTR +console_read(void) { + char c = 0; + if (console_rd != console_wr) { + c = console_buf[console_rd]; + console_rd = (console_rd+1) % BUF_MAX; + } + return c; +} + +static void ICACHE_FLASH_ATTR +console_write_char(char c) { + if (c == '\n') console_write('\r'); + console_write(c); +} + +//===== Display a web page with the console +int ICACHE_FLASH_ATTR +tplConsole(HttpdConnData *connData, char *token, void **arg) { + if (token==NULL) return HTTPD_CGI_DONE; + + if (os_strcmp(token, "console") == 0) { + char buf[128]; + int n = 0; + while (console_rd != console_wr) { + buf[n++] = console_read(); + if (n == 128) { + httpdSend(connData, buf, n); + n = 0; + } + } + if (n > 0) httpdSend(connData, buf, n); + } else { + httpdSend(connData, "Unknown\n", -1); + } + return HTTPD_CGI_DONE; +} + +void ICACHE_FLASH_ATTR consoleInit() { + console_wr = 0; + console_rd = 0; + os_install_putc1((void *)console_write_char); +} + + diff --git a/user/console.h b/user/console.h new file mode 100644 index 0000000..bcb8d67 --- /dev/null +++ b/user/console.h @@ -0,0 +1,9 @@ +#ifndef CONSOLE_H +#define CONSOLE_H + +#include "httpd.h" + +void consoleInit(void); +int tplConsole(HttpdConnData *connData, char *token, void **arg); + +#endif diff --git a/user/user_main.c b/user/user_main.c index 49e1ed5..455a39b 100644 --- a/user/user_main.c +++ b/user/user_main.c @@ -23,6 +23,7 @@ #include "uart.h" #include "serbridge.h" #include "status.h" +#include "console.h" #define MCU_RESET 12 #define MCU_ISP 13 #include @@ -66,6 +67,7 @@ HttpdBuiltInUrl builtInUrls[]={ {"/led.tpl", cgiEspFsTemplate, tplLed}, {"/index.tpl", cgiEspFsTemplate, tplCounter}, {"/led.cgi", cgiLed, NULL}, + {"/console.tpl", cgiEspFsTemplate, tplConsole}, //Routines to make the /wifi URL and everything beneath it work. @@ -124,4 +126,5 @@ void user_init(void) { os_timer_arm(&prHeapTimer, 3000, 1); #endif os_printf("** esp-link ready\n"); + consoleInit(); }