From 5209c099a0fb1a5718e593921bf3dab50deac5f1 Mon Sep 17 00:00:00 2001 From: funshine Date: Tue, 30 Dec 2014 15:42:24 +0800 Subject: [PATCH] modify uart.on api to read raw data from uart --- app/driver/readline.c | 13 ++-- app/lua/lua.c | 166 +++++++++++++++++++----------------------- app/modules/uart.c | 41 +++++++++-- examples/fragment.lua | 6 ++ 4 files changed, 121 insertions(+), 105 deletions(-) diff --git a/app/driver/readline.c b/app/driver/readline.c index 78781a06..7d755d5d 100644 --- a/app/driver/readline.c +++ b/app/driver/readline.c @@ -11,15 +11,14 @@ extern UartDevice UartDev; #define uart_putc uart0_putc -char ICACHE_FLASH_ATTR uart_getc(void){ - char c = 0; +bool ICACHE_FLASH_ATTR uart_getc(char *c){ RcvMsgBuff *pRxBuff = &(UartDev.rcv_buff); if(pRxBuff->pWritePos == pRxBuff->pReadPos){ // empty - return 0; + return false; } // ETS_UART_INTR_DISABLE(); ETS_INTR_LOCK(); - c = (char)*(pRxBuff->pReadPos); + *c = (char)*(pRxBuff->pReadPos); if (pRxBuff->pReadPos == (pRxBuff->pRcvMsgBuff + RX_BUFF_SIZE)) { pRxBuff->pReadPos = pRxBuff->pRcvMsgBuff ; } else { @@ -27,7 +26,7 @@ char ICACHE_FLASH_ATTR uart_getc(void){ } // ETS_UART_INTR_ENABLE(); ETS_INTR_UNLOCK(); - return c; + return true; } #if 0 @@ -43,13 +42,13 @@ start: os_memset(buffer, 0, length); while (1) { - while ((ch = uart_getc()) != 0) + while (uart_getc(&ch)) { /* handle CR key */ if (ch == '\r') { char next; - if ((next = uart_getc()) != 0) + if (uart_getc(&next)) ch = next; } /* backspace key */ diff --git a/app/lua/lua.c b/app/lua/lua.c index 24807a08..bd85ff4b 100644 --- a/app/lua/lua.c +++ b/app/lua/lua.c @@ -490,34 +490,6 @@ void ICACHE_FLASH_ATTR dojob(lua_Load *load){ do{ if(load->done == 1){ 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? */ b[l-1] = '\0'; /* remove it */ if (load->firstline && b[0] == '=') /* first line starts with `=' ? */ @@ -613,86 +585,100 @@ void ICACHE_FLASH_ATTR update_key_led(){ #endif extern bool uart_on_data_cb(const char *buf, size_t len); 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){ // NODE_DBG("readline() is called.\n"); update_key_led(); char ch; - while ((ch = uart_getc()) != 0) + while (uart_getc(&ch)) { - /* handle CR key */ - if (ch == '\r') + if(run_input) { - char next; - if ((next = uart_getc()) != 0) - ch = next; - } - /* backspace key */ - else if (ch == 0x7f || ch == 0x08) - { - if (load->line_position > 0) + /* handle CR key */ + if (ch == '\r') { - if(uart0_echo) uart_putc(0x08); - if(uart0_echo) uart_putc(' '); - if(uart0_echo) uart_putc(0x08); - load->line_position--; + char next; + if (uart_getc(&next)) + ch = next; } - load->line[load->line_position] = 0; - continue; - } - /* EOF(ctrl+d) */ - else if (ch == 0x04) - { - if (load->line_position == 0) - // No input which makes lua interpreter close - donejob(load); - else + /* backspace key */ + else if (ch == 0x7f || ch == 0x08) + { + if (load->line_position > 0) + { + if(uart0_echo) uart_putc(0x08); + if(uart0_echo) uart_putc(' '); + if(uart0_echo) uart_putc(0x08); + load->line_position--; + } + load->line[load->line_position] = 0; continue; - } - /* end of line */ - if (ch == '\r' || ch == '\n') - { - load->line[load->line_position] = 0; - if(uart0_echo) uart_putc('\n'); - if(uart_on_data_cb(load->line, load->line_position)){ + } + /* EOF(ctrl+d) */ + else if (ch == 0x04) + { + if (load->line_position == 0) + // No input which makes lua interpreter close + 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; } - 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; - ch = 0; load->line_position++; - /* it's a large line, discard it */ - if (load->line_position >= load->len) - load->line_position = 0; + if(!run_input) + { + 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() 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 } - -void ICACHE_FLASH_ATTR dogc(void){ - if(globalL){ - lua_gc(globalL, LUA_GCCOLLECT, 0); - } -} diff --git a/app/modules/uart.c b/app/modules/uart.c index 98d3c5dd..99d811a8 100644 --- a/app/modules/uart.c +++ b/app/modules/uart.c @@ -12,7 +12,7 @@ static lua_State *gL = NULL; 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){ if(!buf || len==0) return false; @@ -26,21 +26,46 @@ bool ICACHE_FLASH_ATTR uart_on_data_cb(const char *buf, size_t len){ return !run_input; } +uint16_t need_len = 0; +int16_t end_char = -1; // Lua: uart.on("method", function, [run_input]) static int ICACHE_FLASH_ATTR uart_on( lua_State* L ) { - size_t sl; + size_t sl, el; 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) return luaL_error( L, "wrong arg type" ); - // luaL_checkanyfunction(L, 2); - if (lua_type(L, 2) == LUA_TFUNCTION || lua_type(L, 2) == LUA_TLIGHTFUNCTION){ - if ( lua_isnumber(L, 3) ){ - run = lua_tointeger(L, 3); + if( lua_type( L, stack ) == LUA_TNUMBER ) + { + need_len = ( uint16_t )luaL_checkinteger( L, stack ); + 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 { lua_pushnil(L); } diff --git a/examples/fragment.lua b/examples/fragment.lua index 4acadc8c..7f02823e 100644 --- a/examples/fragment.lua +++ b/examples/fragment.lua @@ -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") 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)