modify uart.on api to read raw data from uart

This commit is contained in:
funshine 2014-12-30 15:42:24 +08:00
parent a331438c9b
commit 5209c099a0
4 changed files with 121 additions and 105 deletions

View File

@ -11,15 +11,14 @@ extern UartDevice UartDev;
#define uart_putc uart0_putc #define uart_putc uart0_putc
char ICACHE_FLASH_ATTR uart_getc(void){ bool ICACHE_FLASH_ATTR uart_getc(char *c){
char c = 0;
RcvMsgBuff *pRxBuff = &(UartDev.rcv_buff); RcvMsgBuff *pRxBuff = &(UartDev.rcv_buff);
if(pRxBuff->pWritePos == pRxBuff->pReadPos){ // empty if(pRxBuff->pWritePos == pRxBuff->pReadPos){ // empty
return 0; return false;
} }
// ETS_UART_INTR_DISABLE(); // ETS_UART_INTR_DISABLE();
ETS_INTR_LOCK(); ETS_INTR_LOCK();
c = (char)*(pRxBuff->pReadPos); *c = (char)*(pRxBuff->pReadPos);
if (pRxBuff->pReadPos == (pRxBuff->pRcvMsgBuff + RX_BUFF_SIZE)) { if (pRxBuff->pReadPos == (pRxBuff->pRcvMsgBuff + RX_BUFF_SIZE)) {
pRxBuff->pReadPos = pRxBuff->pRcvMsgBuff ; pRxBuff->pReadPos = pRxBuff->pRcvMsgBuff ;
} else { } else {
@ -27,7 +26,7 @@ char ICACHE_FLASH_ATTR uart_getc(void){
} }
// ETS_UART_INTR_ENABLE(); // ETS_UART_INTR_ENABLE();
ETS_INTR_UNLOCK(); ETS_INTR_UNLOCK();
return c; return true;
} }
#if 0 #if 0
@ -43,13 +42,13 @@ start:
os_memset(buffer, 0, length); os_memset(buffer, 0, length);
while (1) while (1)
{ {
while ((ch = uart_getc()) != 0) while (uart_getc(&ch))
{ {
/* handle CR key */ /* handle CR key */
if (ch == '\r') if (ch == '\r')
{ {
char next; char next;
if ((next = uart_getc()) != 0) if (uart_getc(&next))
ch = next; ch = next;
} }
/* backspace key */ /* backspace key */

View File

@ -490,34 +490,6 @@ void ICACHE_FLASH_ATTR dojob(lua_Load *load){
do{ do{
if(load->done == 1){ if(load->done == 1){
l = c_strlen(b); l = c_strlen(b);
#if 0
if(log_fd!=-1 && l>0)
{
if(l>=10 && (c_strstr(b,"log.stop()")) )
{
fs_close(log_fd);
log_fd = -1;
noparse = 0;
NODE_ERR( "log stopping.\n" );
}
if(log_fd!=-1){ // test again
rs = fs_write(log_fd, b, l);
if(rs!=l){
fs_close(log_fd);
log_fd = -1;
} else {
rs = fs_write(log_fd, "\r\n", 2);
if(rs!=2){
fs_close(log_fd);
log_fd = -1;
}
}
}
if(noparse){
break;
}
}
#endif
if (l > 0 && b[l-1] == '\n') /* line ends with newline? */ if (l > 0 && b[l-1] == '\n') /* line ends with newline? */
b[l-1] = '\0'; /* remove it */ b[l-1] = '\0'; /* remove it */
if (load->firstline && b[0] == '=') /* first line starts with `=' ? */ if (load->firstline && b[0] == '=') /* first line starts with `=' ? */
@ -613,86 +585,100 @@ void ICACHE_FLASH_ATTR update_key_led(){
#endif #endif
extern bool uart_on_data_cb(const char *buf, size_t len); extern bool uart_on_data_cb(const char *buf, size_t len);
extern bool uart0_echo; extern bool uart0_echo;
extern bool run_input;
extern uint16_t need_len;
extern int16_t end_char;
void ICACHE_FLASH_ATTR readline(lua_Load *load){ void ICACHE_FLASH_ATTR readline(lua_Load *load){
// NODE_DBG("readline() is called.\n"); // NODE_DBG("readline() is called.\n");
update_key_led(); update_key_led();
char ch; char ch;
while ((ch = uart_getc()) != 0) while (uart_getc(&ch))
{ {
/* handle CR key */ if(run_input)
if (ch == '\r')
{ {
char next; /* handle CR key */
if ((next = uart_getc()) != 0) if (ch == '\r')
ch = next;
}
/* backspace key */
else if (ch == 0x7f || ch == 0x08)
{
if (load->line_position > 0)
{ {
if(uart0_echo) uart_putc(0x08); char next;
if(uart0_echo) uart_putc(' '); if (uart_getc(&next))
if(uart0_echo) uart_putc(0x08); ch = next;
load->line_position--;
} }
load->line[load->line_position] = 0; /* backspace key */
continue; else if (ch == 0x7f || ch == 0x08)
} {
/* EOF(ctrl+d) */ if (load->line_position > 0)
else if (ch == 0x04) {
{ if(uart0_echo) uart_putc(0x08);
if (load->line_position == 0) if(uart0_echo) uart_putc(' ');
// No input which makes lua interpreter close if(uart0_echo) uart_putc(0x08);
donejob(load); load->line_position--;
else }
load->line[load->line_position] = 0;
continue; continue;
} }
/* end of line */ /* EOF(ctrl+d) */
if (ch == '\r' || ch == '\n') else if (ch == 0x04)
{ {
load->line[load->line_position] = 0; if (load->line_position == 0)
if(uart0_echo) uart_putc('\n'); // No input which makes lua interpreter close
if(uart_on_data_cb(load->line, load->line_position)){ donejob(load);
else
continue;
}
/* end of line */
if (ch == '\r' || ch == '\n')
{
load->line[load->line_position] = 0;
if(uart0_echo) uart_putc('\n');
uart_on_data_cb(load->line, load->line_position);
if (load->line_position == 0)
{
/* Get a empty line, then go to get a new line */
c_puts(load->prmt);
os_timer_disarm(&readline_timer);
os_timer_setfn(&readline_timer, (os_timer_func_t *)readline, load);
os_timer_arm(&readline_timer, READLINE_INTERVAL, 0); // no repeat
} else {
load->done = 1;
os_timer_disarm(&lua_timer);
os_timer_setfn(&lua_timer, (os_timer_func_t *)dojob, load);
os_timer_arm(&lua_timer, READLINE_INTERVAL, 0); // no repeat
}
}
/* other control character or not an acsii character */
if (ch < 0x20 || ch >= 0x80)
{
continue;
}
/* echo */
if(uart0_echo) uart_putc(ch);
/* it's a large line, discard it */
if ( load->line_position + 1 >= load->len ){
load->line_position = 0; load->line_position = 0;
} }
if (load->line_position == 0)
{
/* Get a empty line, then go to get a new line */
c_puts(load->prmt);
os_timer_disarm(&readline_timer);
os_timer_setfn(&readline_timer, (os_timer_func_t *)readline, load);
os_timer_arm(&readline_timer, READLINE_INTERVAL, 0); // no repeat
} else {
load->done = 1;
os_timer_disarm(&lua_timer);
os_timer_setfn(&lua_timer, (os_timer_func_t *)dojob, load);
os_timer_arm(&lua_timer, READLINE_INTERVAL, 0); // no repeat
}
} }
/* other control character or not an acsii character */
if (ch < 0x20 || ch >= 0x80)
{
continue;
}
/* echo */
if(uart0_echo) uart_putc(ch);
load->line[load->line_position] = ch; load->line[load->line_position] = ch;
ch = 0;
load->line_position++; load->line_position++;
/* it's a large line, discard it */ if(!run_input)
if (load->line_position >= load->len) {
load->line_position = 0; if( ((need_len!=0) && (load->line_position >= need_len)) || \
(load->line_position >= load->len) || \
((end_char>=0) && ((unsigned char)ch==(unsigned char)end_char)) )
{
uart_on_data_cb(load->line, load->line_position);
load->line_position = 0;
}
}
ch = 0;
} }
// if there is no input from user, repeat readline() // if there is no input from user, repeat readline()
os_timer_disarm(&readline_timer); os_timer_disarm(&readline_timer);
os_timer_setfn(&readline_timer, (os_timer_func_t *)readline, load); os_timer_setfn(&readline_timer, (os_timer_func_t *)readline, load);
os_timer_arm(&readline_timer, READLINE_INTERVAL, 0); // no repeat os_timer_arm(&readline_timer, READLINE_INTERVAL, 0); // no repeat
} }
void ICACHE_FLASH_ATTR dogc(void){
if(globalL){
lua_gc(globalL, LUA_GCCOLLECT, 0);
}
}

View File

@ -12,7 +12,7 @@
static lua_State *gL = NULL; static lua_State *gL = NULL;
static int uart_receive_rf = LUA_NOREF; static int uart_receive_rf = LUA_NOREF;
static bool run_input = true; bool run_input = true;
bool ICACHE_FLASH_ATTR uart_on_data_cb(const char *buf, size_t len){ bool ICACHE_FLASH_ATTR uart_on_data_cb(const char *buf, size_t len){
if(!buf || len==0) if(!buf || len==0)
return false; return false;
@ -26,21 +26,46 @@ bool ICACHE_FLASH_ATTR uart_on_data_cb(const char *buf, size_t len){
return !run_input; return !run_input;
} }
uint16_t need_len = 0;
int16_t end_char = -1;
// Lua: uart.on("method", function, [run_input]) // Lua: uart.on("method", function, [run_input])
static int ICACHE_FLASH_ATTR uart_on( lua_State* L ) static int ICACHE_FLASH_ATTR uart_on( lua_State* L )
{ {
size_t sl; size_t sl, el;
int32_t run = 1; int32_t run = 1;
const char *method = luaL_checklstring( L, 1, &sl ); uint8_t stack = 1;
const char *method = luaL_checklstring( L, stack, &sl );
stack++;
if (method == NULL) if (method == NULL)
return luaL_error( L, "wrong arg type" ); return luaL_error( L, "wrong arg type" );
// luaL_checkanyfunction(L, 2); if( lua_type( L, stack ) == LUA_TNUMBER )
if (lua_type(L, 2) == LUA_TFUNCTION || lua_type(L, 2) == LUA_TLIGHTFUNCTION){ {
if ( lua_isnumber(L, 3) ){ need_len = ( uint16_t )luaL_checkinteger( L, stack );
run = lua_tointeger(L, 3); stack++;
end_char = -1;
if( need_len > 255 ){
need_len = 255;
return luaL_error( L, "wrong arg range" );
} }
lua_pushvalue(L, 2); // copy argument (func) to the top of stack }
else if(lua_isstring(L, stack))
{
const char *end = luaL_checklstring( L, stack, &el );
stack++;
if(el!=1){
return luaL_error( L, "wrong arg range" );
}
end_char = (int16_t)end[0];
need_len = 0;
}
// luaL_checkanyfunction(L, stack);
if (lua_type(L, stack) == LUA_TFUNCTION || lua_type(L, stack) == LUA_TLIGHTFUNCTION){
if ( lua_isnumber(L, stack+1) ){
run = lua_tointeger(L, stack+1);
}
lua_pushvalue(L, stack); // copy argument (func) to the top of stack
} else { } else {
lua_pushnil(L); lua_pushnil(L);
} }

View File

@ -303,3 +303,9 @@ sk=net.createConnection(net.TCP, 1) sk:on("receive", function(sck, c) print(c) e
sk:on("connection", function(sck) sck:send("GET / HTTPS/1.1\r\nHost: www.google.com.hk\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n") end ) sk:connect(443,"173.194.72.199") sk:on("connection", function(sck) sck:send("GET / HTTPS/1.1\r\nHost: www.google.com.hk\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n") end ) sk:connect(443,"173.194.72.199")
wifi.sta.setip({ip="192.168.18.119",netmask="255.255.255.0",gateway="192.168.18.1"}) wifi.sta.setip({ip="192.168.18.119",netmask="255.255.255.0",gateway="192.168.18.1"})
uart.on("data","\r",function(input) if input=="quit\r" then uart.on("data") else print(input) end end, 0)
uart.on("data","\n",function(input) if input=="quit\n" then uart.on("data") else print(input) end end, 0)
uart.on("data", 5 ,function(input) if input=="quit\r" then uart.on("data") else print(input) end end, 0)
uart.on("data","\r",function(input) if input=="quit" then uart.on("data") else print(input) end end, 1)