Skip to content

Commit

Permalink
added uController console; misc improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
tve committed May 26, 2015
1 parent 79495f6 commit 8bb8e11
Show file tree
Hide file tree
Showing 19 changed files with 227 additions and 183 deletions.
6 changes: 4 additions & 2 deletions html/console.tpl
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<html><head><title>Console - ESP Link</title>
<html><head><title>MCU Console - ESP Link</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id="main">
<div id="topnav">%topnav%</div>
<h1><span class="esp">esp</span> link - Debug Console</h1>
<h1><span class="esp">esp</span> link - Microcontroller Console</h1>
<p>The Microcontroller console shows the last 1024 characters received from UART0, to which
a microcontroller is tpically attached.</p>
<pre class="console">
%console%
</pre>
Expand Down
Binary file added html/favicon.ico
Binary file not shown.
14 changes: 14 additions & 0 deletions html/log.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<html><head><title>Log - ESP Link</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id="main">
<div id="topnav">%topnav%</div>
<h1><span class="esp">esp</span> link - Debug Log</h1>
<p>The debug log shows the 1024 last characters printed by the esp-link software itself to
its own debug log.</p>
<pre class="console">
%log%
</pre>
</div>
</body></html>
3 changes: 2 additions & 1 deletion httpd/httpd.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ static const MimeMap mimeTypes[]={
{"jpg", "image/jpeg"},
{"jpeg", "image/jpeg"},
{"png", "image/png"},
{"tpl", "text/html; charset=UTF-8"},
{NULL, "text/html"}, //default value
};

Expand Down Expand Up @@ -214,7 +215,7 @@ void ICACHE_FLASH_ATTR httpdEndHeaders(HttpdConnData *conn) {
void ICACHE_FLASH_ATTR httpdRedirect(HttpdConnData *conn, char *newUrl) {
char buff[1024];
int l;
l=os_sprintf(buff, "HTTP/1.1 302 Found\r\nServer: esp8266-httpd/"HTTPDVER"\r\nConnection: close\r\nLocation: %s\r\n\r\nMoved to %s\r\n", newUrl, newUrl);
l=os_sprintf(buff, "HTTP/1.0 302 Found\r\nServer: esp8266-httpd/"HTTPDVER"\r\nConnection: close\r\nLocation: %s\r\n\r\nRedirecting to %s\r\n", newUrl, newUrl);
httpdSend(conn, buff, l);
}

Expand Down
8 changes: 4 additions & 4 deletions httpd/httpdespfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ Connector to let httpd use the espfs filesystem to serve the files in it.
/*
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* Jeroen Domburg <jeroen@spritesmods.com> wrote this file. As long as you retain
* this notice you can do whatever you want with this stuff. If we meet some day,
* and you think this stuff is worth it, you can buy me a beer in return.
* Jeroen Domburg <jeroen@spritesmods.com> wrote this file. As long as you retain
* this notice you can do whatever you want with this stuff. If we meet some day,
* and you think this stuff is worth it, you can buy me a beer in return.
* ----------------------------------------------------------------------------
*/

Expand All @@ -30,7 +30,7 @@ int ICACHE_FLASH_ATTR cgiEspFsHook(HttpdConnData *connData) {
char buff[1024];
char acceptEncodingBuffer[64];
int isGzip;

if (connData->conn==NULL) {
//Connection aborted. Clean up.
espFsClose(file);
Expand Down
65 changes: 65 additions & 0 deletions serial/console.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include <esp8266.h>
#include "uart.h"
#include "cgi.h"
#include "console.h"

// Microcontroller console capturing the last 1024 characters received on the uart so
// they can be shown on a web page

#define BUF_MAX (1024)
static char console_buf[BUF_MAX];
static int console_wr, console_rd;
static int console_pos; // offset since reset of console_rd position

static void ICACHE_FLASH_ATTR
console_write(char c) {
int wr = (console_wr+1)%BUF_MAX;
if (wr == console_rd) {
console_rd = (console_rd+1) % BUF_MAX; // full, eat first char
console_pos++;
}
console_buf[console_wr] = c;
console_wr = wr;
}

// return previous character in console, 0 if at start
static char ICACHE_FLASH_ATTR
console_prev(void) {
if (console_wr == console_rd) return 0;
return console_buf[(console_wr+1+BUF_MAX)%BUF_MAX];
}

void ICACHE_FLASH_ATTR
console_write_char(char c) {
if (c == '\n' && console_prev() != '\r') 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;
char buff[256];

if (os_strcmp(token, "console") == 0) {
if (console_wr > console_rd) {
httpdSend(connData, console_buf+console_rd, console_wr-console_rd);
} else if (console_rd != console_wr) {
httpdSend(connData, console_buf+console_rd, BUF_MAX-console_rd);
httpdSend(connData, console_buf, console_wr);
}
} else if (os_strcmp(token, "topnav")==0) {
printNav(buff);
httpdSend(connData, buff, -1);
} else {
httpdSend(connData, "Unknown\n", -1);
}
return HTTPD_CGI_DONE;
}

void ICACHE_FLASH_ATTR consoleInit() {
console_wr = 0;
console_rd = 0;
}


2 changes: 1 addition & 1 deletion user/console.h → serial/console.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "httpd.h"

void consoleInit(void);
void ICACHE_FLASH_ATTR console_uart(bool enable);
void ICACHE_FLASH_ATTR console_write_char(char c);
int tplConsole(HttpdConnData *connData, char *token, void **arg);

#endif
12 changes: 7 additions & 5 deletions serial/serbridge.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "uart.h"
#include "serbridge.h"
#include "console.h"

#if 1
// GPIO for esp-03 module with gpio12->reset, gpio13->isp, gpio2->"ser" LED
Expand Down Expand Up @@ -290,11 +291,12 @@ static void ICACHE_FLASH_ATTR serbridgeConnectCb(void *arg) {
// callback with a buffer of characters that have arrived on the uart
void ICACHE_FLASH_ATTR
serbridgeUartCb(char *buf, int length) {
// push the buffer into the microcontroller console
for (int i=0; i<length; i++)
console_write_char(buf[i]);
// push the buffer into each open connection
int s = 0;
for (int i = 0; i < MAX_CONN; ++i) {
if (connData[i].conn) {
s++;
espbuffsend(&connData[i], buf, length);
}
}
Expand All @@ -314,10 +316,10 @@ void ICACHE_FLASH_ATTR serbridgeInit(int port) {

PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDI_U, FUNC_GPIO12);
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTCK_U , FUNC_GPIO13);
GPIO_OUTPUT_SET(MCU_ISP, 1);
GPIO_OUTPUT_SET(MCU_RESET, 0);
//GPIO_OUTPUT_SET(MCU_ISP, 1);
//GPIO_OUTPUT_SET(MCU_RESET, 0);
#ifdef MCU_LED
//GPIO_OUTPUT_SET(MCU_LED, 1);
GPIO_OUTPUT_SET(MCU_LED, 1);
#endif

espconn_regist_connectcb(&serbridgeConn, serbridgeConnectCb);
Expand Down
7 changes: 3 additions & 4 deletions user/cgi.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ flash as a binary. Also handles the hit counter on the main page.

#include <esp8266.h>
#include "cgi.h"
#include "io.h"


//cause I can't be bothered to write an ioGetLed()
Expand All @@ -34,7 +33,7 @@ int ICACHE_FLASH_ATTR cgiLed(HttpdConnData *connData) {
len=httpdFindArg(connData->post->buff, "led", buff, sizeof(buff));
if (len!=0) {
currLedState=atoi(buff);
ioLed(currLedState);
//ioLed(currLedState);
}

httpdRedirect(connData, "led.tpl");
Expand Down Expand Up @@ -82,8 +81,8 @@ int ICACHE_FLASH_ATTR tplCounter(HttpdConnData *connData, char *token, void **ar
}

static char *navLinks[][2] = {
{ "Home", "/index.tpl" }, { "Wifi", "/wifi/wifi.tpl" }, { "Serial", "/index.tpl" },
{ "Esp log", "/console.tpl" }, { "Help", "/help.tpl" },
{ "Home", "/index.tpl" }, { "Wifi", "/wifi/wifi.tpl" }, { "\xC2\xB5""C Console", "/console.tpl" },
{ "Esp log", "/log.tpl" }, { "Help", "/help.tpl" },
{ 0, 0 },
};

Expand Down
10 changes: 4 additions & 6 deletions user/cgiwifi.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Cgi/template routines for the /wifi url.
#include "cgiwifi.h"
#include "cgi.h"
#include "status.h"
#include "console.h"
#include "log.h"

//Enable this to disallow any changes in AP settings
//#define DEMO_MODE
Expand Down Expand Up @@ -239,17 +239,15 @@ static void ICACHE_FLASH_ATTR resetTimerCb(void *arg) {
wifi_set_opmode(1);
os_timer_arm(&resetTimer, RESET_TIMEOUT, 0);
}
os_printf("Turning off uart console\n");
os_delay_us(4*1000L); // time for uart to flush
console_uart(false);
log_uart(false);
// no more resetTimer at this point, gotta use physical reset to recover if in trouble
} else {
if (m != 3) {
os_printf("Wifi connect failed. Going into STA+AP mode..\n");
wifi_set_opmode(3);
}
console_uart(true);
os_printf("Enabling/continuing uart console\n");
log_uart(true);
os_printf("Enabling/continuing uart log\n");
os_timer_arm(&resetTimer, RESET_TIMEOUT, 0);
}
}
Expand Down
53 changes: 12 additions & 41 deletions user/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,63 +3,35 @@
#include "cgi.h"
#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.
// Microcontroller console capturing the last 1024 characters received on the uart so
// they can be shown on a web page

#define BUF_MAX (1024)
static char console_buf[BUF_MAX];
static int console_wr, console_rd;
static bool console_no_uart; // start out printing to uart
static bool console_newline; // at start of a new line

void ICACHE_FLASH_ATTR
console_uart(bool enable) {
if (!enable && !console_no_uart) {
os_printf("Turning OFF uart console\n");
os_delay_us(4*1000L); // time for uart to flush
console_no_uart = !enable;
} else if (enable && console_no_uart) {
console_no_uart = !enable;
os_printf("Turning ON uart console\n");
}
}
static int console_pos; // offset since reset of console_rd position

static void ICACHE_FLASH_ATTR
console_write(char c) {
int wr = (console_wr+1)%BUF_MAX;
if (wr == console_rd)
if (wr == console_rd) {
console_rd = (console_rd+1) % BUF_MAX; // full, eat first char
console_pos++;
}
console_buf[console_wr] = c;
console_wr = wr;
}

#if 0
// return previous character in console, 0 if at start
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;
console_prev(void) {
if (console_wr == console_rd) return 0;
return console_buf[(console_wr-1+BUF_MAX)%BUF_MAX];
}
#endif

static void ICACHE_FLASH_ATTR
void ICACHE_FLASH_ATTR
console_write_char(char c) {
// Uart output unless disabled
if (!console_no_uart) {
if (console_newline) {
uart0_write_char('>');
uart0_write_char(' ');
console_newline = false;
}
uart0_write_char(c);
console_newline = c == '\n';
}
// Store in console buffer
if (c == '\n') console_write('\r');
if (c == '\n' && console_prev() != '\r') console_write('\r');
console_write(c);
}

Expand Down Expand Up @@ -88,7 +60,6 @@ tplConsole(HttpdConnData *connData, char *token, void **arg) {
void ICACHE_FLASH_ATTR consoleInit() {
console_wr = 0;
console_rd = 0;
os_install_putc1((void *)console_write_char);
}


51 changes: 0 additions & 51 deletions user/io.c

This file was deleted.

7 changes: 0 additions & 7 deletions user/io.h

This file was deleted.

0 comments on commit 8bb8e11

Please sign in to comment.