void luaTask(void *params){ doScript(); while(1){ portTickType xLastWakeTime, startTickTime; const portTickType xFrequency = LUA_1Hz; startTickTime = xLastWakeTime = xTaskGetTickCount(); if (getShouldReloadScript()){ doScript(); setShouldReloadScript(0); } lockLua(); lua_getglobal(g_lua, LUA_PERIODIC_FUNCTION); if (! lua_isnil(g_lua,-1)){ if (lua_pcall(g_lua, 0, 0, 0) != 0){ // TODO log or indicate error. store this in a "Last Error" // SendString("Error calling "); // SendString(LUA_PERIODIC_FUNCTION); // SendString("(): "); // SendString( lua_tostring(g_lua,-1)); // SendCrlf(); lua_pop(g_lua,1); } } else{ // //handle missing function error lua_pop(g_lua,1); } unlockLua(); vTaskDelayUntil( &xLastWakeTime, xFrequency ); } }
void WriteScriptPage(Serial *serial, unsigned int argc, char **argv){ if (argc < 2){ put_commandError(serial, ERROR_CODE_INVALID_PARAM); return; } unsigned int page = modp_atoi(argv[1]); char *scriptPage = ""; if (argc >= 3) scriptPage = argv[2]; if (page >=0 && page < SCRIPT_PAGES){ if (argc >= 2) unescape(scriptPage); lockLua(); vPortEnterCritical(); pr_info_int(strlen(scriptPage)); pr_info("="); pr_info(scriptPage); pr_info("\r\n"); int result = flashScriptPage(page, scriptPage); vPortExitCritical(); unlockLua(); if (result == 0){ put_commandOK(serial); } else{ put_commandError(serial, result); } } else{ put_commandError(serial, ERROR_CODE_INVALID_PARAM); } }
void ExecLuaInterpreter(Serial *serial, unsigned int argc, char **argv) { g_interactive_mode = 1; serial->put_s("Entering Lua Interpreter. enter 'exit' to leave"); put_crlf(serial); lua_State *L = getLua(); cmd_context *cmdContext = get_command_context(); char * luaLine = cmdContext->lineBuffer; int result; while(1) { serial->put_s("> "); interactive_read_line(serial, luaLine, cmdContext->lineBufferSize); if (strcmp(luaLine,"exit") == 0) break; lockLua(); lua_gc(L,LUA_GCCOLLECT,0); result = luaL_loadbuffer(L, luaLine, strlen(luaLine), ""); if (0 != result) { serial->put_s("error: ("); serial->put_s(lua_tostring(L,-1)); serial->put_s(")"); put_crlf(serial); lua_pop(L,1); } else { lua_pushvalue(L,-1); result = lua_pcall(L,0,0,0); if (0 != result) { serial->put_s("error: ("); serial->put_s(lua_tostring(L,-1)); serial->put_s(")"); put_crlf(serial); lua_pop(L,1); } lua_pop(L,1); } unlockLua(); } g_interactive_mode = 0; }
void startLuaTask(){ g_shouldReloadScript = 0; vSemaphoreCreateBinary(xLuaLock); lockLua(); g_lua=lua_newstate( myAlloc, NULL); //open optional libraries luaopen_base(g_lua); luaopen_table(g_lua); luaopen_string(g_lua); luaopen_math(g_lua); registerBaseLuaFunctions(g_lua); unlockLua(); xTaskCreate( luaTask, ( signed portCHAR * ) "luaTask", LUA_STACK_SIZE, NULL, LUA_PRIORITY, NULL); }
static void doScript(void){ lockLua(); luaL_dostring(g_lua,getScript()); unlockLua(); }