Moved node.key() & node.led() support into node module.

It was piggybacking on the Lua readline timer which no longer exists.

Also cleaned up injection of Lua commands via CoAP & node.input().
This commit is contained in:
Johny Mattsson 2015-10-08 14:14:51 +11:00
parent fe6289a6c2
commit bf8ed500bf
6 changed files with 81 additions and 84 deletions

View File

@ -8,6 +8,8 @@
#include "lualib.h"
#include "os_type.h"
#include "user_interface.h"
#include "user_config.h"
void build_well_known_rsp(char *rsp, uint16_t rsplen);
@ -164,8 +166,6 @@ end:
}
extern lua_Load gLoad;
extern os_timer_t lua_timer;
extern void dojob(lua_Load *load);
static const coap_endpoint_path_t path_command = {2, {"v1", "c"}};
static int handle_post_command(const coap_endpoint_t *ep, coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_packet_t *outpkt, uint8_t id_hi, uint8_t id_lo)
{
@ -184,9 +184,7 @@ static int handle_post_command(const coap_endpoint_t *ep, coap_rw_buffer_t *scra
NODE_DBG("Get command:\n");
NODE_DBG(load->line); // buggy here
NODE_DBG("\nResult(if any):\n");
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
system_os_post (LUA_TASK_PRIO, LUA_PROCESS_LINE_SIG, 0);
}
return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CONTENT, COAP_CONTENTTYPE_TEXT_PLAIN);
}

View File

@ -58,9 +58,12 @@
#define LUA_OPTIMIZE_MEMORY 0
#endif /* LUA_OPTRAM */
#define READLINE_INTERVAL 80
#define LUA_TASK_PRIO USER_TASK_PRIO_0
#define LUA_PROCESS_LINE_SIG 2
#ifdef DEVKIT_VERSION_0_9
#define KEYLED_INTERVAL 80
#define KEY_SHORT_MS 200
#define KEY_LONG_MS 3000
#define KEY_SHORT_COUNT (KEY_SHORT_MS / READLINE_INTERVAL)

View File

@ -22,21 +22,12 @@
#include "os_type.h"
os_timer_t lua_timer;
lua_State *globalL = NULL;
lua_Load gLoad;
static const char *progname = LUA_PROGNAME;
#ifdef DEVKIT_VERSION_0_9
static int key_press_count = 0;
int led_high_count = LED_HIGH_COUNT_DEFAULT;
int led_low_count = LED_LOW_COUNT_DEFAULT;
static int led_count = 0;
#endif
#if 0
static void lstop (lua_State *L, lua_Debug *ar) {
(void)ar; /* unused arg. */
@ -435,8 +426,8 @@ static int pmain (lua_State *L) {
return 0;
}
void dojob(lua_Load *load);
void readline(lua_Load *load);
static void dojob(lua_Load *load);
static bool readline(lua_Load *load);
char line_buffer[LUA_MAXINPUT];
#ifdef LUA_RPC
@ -464,10 +455,7 @@ int lua_main (int argc, char **argv) {
gLoad.line_position = 0;
gLoad.prmt = get_prompt(L, 1);
// dojob(&gLoad);
os_timer_disarm(&lua_timer);
os_timer_setfn(&lua_timer, (os_timer_func_t *)dojob, &gLoad);
os_timer_arm(&lua_timer, READLINE_INTERVAL, 0); // no repeat
dojob(&gLoad);
NODE_DBG("Heap size::%d.\n",system_get_free_heap_size());
legc_set_mode( L, EGC_ALWAYS, 4096 );
@ -476,16 +464,17 @@ int lua_main (int argc, char **argv) {
return (status || s.status) ? EXIT_FAILURE : EXIT_SUCCESS;
}
void lua_handle_input (void)
void lua_handle_input (bool force)
{
readline (&gLoad);
if (force || readline (&gLoad))
dojob (&gLoad);
}
void donejob(lua_Load *load){
lua_close(load->L);
}
void dojob(lua_Load *load){
static void dojob(lua_Load *load){
size_t l, rs;
int status;
char *b = load->line;
@ -542,50 +531,8 @@ void dojob(lua_Load *load){
load->line_position = 0;
c_memset(load->line, 0, load->len);
c_puts(load->prmt);
// NODE_DBG("dojob() is called with firstline.\n");
}
#ifdef DEVKIT_VERSION_0_9
extern void key_long_press(void *arg);
extern void key_short_press(void *arg);
static bool key_short_pressed = false;
static bool key_long_pressed = false;
void update_key_led(){
uint8_t temp = 1, level = 1;
led_count++;
if(led_count>led_low_count+led_high_count){
led_count = 0; // reset led_count, the level still high
} else if(led_count>led_low_count && led_count <=led_high_count+led_low_count){
level = 1; // output high level
} else if(led_count<=led_low_count){
level = 0; // output low level
}
temp = platform_key_led(level);
if(temp == 0){ // key is pressed
key_press_count++;
if(key_press_count>=KEY_LONG_COUNT){
// key_long_press(NULL);
key_long_pressed = true;
key_short_pressed = false;
// key_press_count = 0;
} else if(key_press_count>=KEY_SHORT_COUNT){ // < KEY_LONG_COUNT
// key_short_press(NULL);
key_short_pressed = true;
}
}else{ // key is released
key_press_count = 0;
if(key_long_pressed){
key_long_press(NULL);
key_long_pressed = false;
}
if(key_short_pressed){
key_short_press(NULL);
key_short_pressed = false;
}
}
}
#endif
#ifndef uart_putc
#define uart_putc uart0_putc
#endif
@ -594,11 +541,9 @@ extern bool uart0_echo;
extern bool run_input;
extern uint16_t need_len;
extern int16_t end_char;
void readline(lua_Load *load){
static bool readline(lua_Load *load){
// NODE_DBG("readline() is called.\n");
#ifdef DEVKIT_VERSION_0_9
update_key_led();
#endif
bool need_dojob = false;
char ch;
while (uart_getc(&ch))
{
@ -646,9 +591,7 @@ void readline(lua_Load *load){
c_puts(load->prmt);
} 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
need_dojob = true;
}
continue;
}
@ -690,4 +633,6 @@ void readline(lua_Load *load){
uart_on_data_cb(load->line, load->line_position);
load->line_position = 0;
}
return need_dojob;
}

View File

@ -382,7 +382,7 @@ typedef struct __lua_load{
int lua_main( int argc, char **argv );
void lua_handle_input (void);
void lua_handle_input (bool force);
/******************************************************************************
* Copyright (C) 1994-2008 Lua.org, PUC-Rio. All rights reserved.

View File

@ -145,8 +145,58 @@ static int node_heap( lua_State* L )
static lua_State *gL = NULL;
#ifdef DEVKIT_VERSION_0_9
extern int led_high_count; // this is defined in lua.c
extern int led_low_count;
static int led_high_count = LED_HIGH_COUNT_DEFAULT;
static int led_low_count = LED_LOW_COUNT_DEFAULT;
static int led_count = 0;
static int key_press_count = 0;
static bool key_short_pressed = false;
static bool key_long_pressed = false;
static os_timer_t keyled_timer;
static void update_key_led (void *p)
{
(void)p;
uint8_t temp = 1, level = 1;
led_count++;
if(led_count>led_low_count+led_high_count){
led_count = 0; // reset led_count, the level still high
} else if(led_count>led_low_count && led_count <=led_high_count+led_low_count){
level = 1; // output high level
} else if(led_count<=led_low_count){
level = 0; // output low level
}
temp = platform_key_led(level);
if(temp == 0){ // key is pressed
key_press_count++;
if(key_press_count>=KEY_LONG_COUNT){
// key_long_press(NULL);
key_long_pressed = true;
key_short_pressed = false;
// key_press_count = 0;
} else if(key_press_count>=KEY_SHORT_COUNT){ // < KEY_LONG_COUNT
// key_short_press(NULL);
key_short_pressed = true;
}
}else{ // key is released
key_press_count = 0;
if(key_long_pressed){
key_long_press(NULL);
key_long_pressed = false;
}
if(key_short_pressed){
key_short_press(NULL);
key_short_pressed = false;
}
}
}
static void prime_keyled_timer (void)
{
os_timer_disarm (&keyled_timer);
os_timer_setfn (&keyled_timer, update_key_led, 0);
os_timer_arm (&keyled_timer, KEYLED_INTERVAL, 1);
}
// Lua: led(low, high)
static int node_led( lua_State* L )
{
@ -171,6 +221,7 @@ static int node_led( lua_State* L )
}
led_high_count = (uint32_t)high / READLINE_INTERVAL;
led_low_count = (uint32_t)low / READLINE_INTERVAL;
prime_keyled_timer();
return 0;
}
@ -246,13 +297,12 @@ static int node_key( lua_State* L )
*ref = LUA_NOREF;
}
prime_keyled_timer();
return 0;
}
#endif
extern lua_Load gLoad;
extern os_timer_t lua_timer;
extern void dojob(lua_Load *load);
// Lua: input("string")
static int node_input( lua_State* L )
{
@ -269,9 +319,7 @@ static int node_input( lua_State* L )
NODE_DBG("Get command:\n");
NODE_DBG(load->line); // buggy here
NODE_DBG("\nResult(if any):\n");
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
system_os_post (LUA_TASK_PRIO, LUA_PROCESS_LINE_SIG, 0);
}
}
return 0;

View File

@ -60,7 +60,10 @@ void task_lua(os_event_t *e){
lua_main( 2, lua_argv );
break;
case SIG_UARTINPUT:
lua_handle_input ();
lua_handle_input (false);
break;
case LUA_PROCESS_LINE_SIG:
lua_handle_input (true);
break;
default:
break;
@ -143,7 +146,7 @@ void nodemcu_init(void)
// lua_main( 3, lua_argv );
// NODE_DBG("Flash sec num: 0x%x\n", flash_get_sec_num());
task_init();
system_os_post(USER_TASK_PRIO_0,SIG_LUA,'s');
system_os_post(LUA_TASK_PRIO,SIG_LUA,'s');
}
/******************************************************************************
@ -173,6 +176,6 @@ void user_init(void)
#ifndef NODE_DEBUG
system_set_os_print(0);
#endif
system_init_done_cb(nodemcu_init);
}