فهرست منبع

update: 因为LuatOS支持循环定时器,优化sys.lua的定时循环逻辑
add: 更新playit逻辑,加上时间同步
fix: airkiss的日志没有正确显示出来
change: 调用wlan.connect后,设置自动重连为真
add: luat主线程退出后,提示60s后重启

Wendal Chen 6 سال پیش
والد
کامیت
daac5b4163
7فایلهای تغییر یافته به همراه53 افزوده شده و 41 حذف شده
  1. 2 2
      luat/modules/luat_lib_rtos.c
  2. 4 2
      luat/modules/luat_lib_wlan.c
  3. 4 0
      luat/modules/luat_main.c
  4. 1 1
      luat/rtt/luat_timer_rtt.c
  5. 1 1
      luat/rtt/rtt_airkiss.c
  6. 12 8
      script/app/playit/main.lua
  7. 29 27
      script/lib/sys.lua

+ 2 - 2
luat/modules/luat_lib_rtos.c

@@ -37,8 +37,8 @@ static int l_timer_handler(lua_State *L, void* ptr) {
     // luat_printf("l_timer_handler id=%ld\n", timer->id);
     lua_pushinteger(L, MSG_TIMER);
     lua_pushinteger(L, timer->id);
-    lua_pushinteger(L, timer->timeout);
     lua_pushinteger(L, timer->repeat);
+    //lua_pushinteger(L, timer->timeout);
     if (timer->repeat == 0) {
         luat_timer_stop(timer);
         luat_heap_free(timer);
@@ -46,7 +46,7 @@ static int l_timer_handler(lua_State *L, void* ptr) {
     else if (timer->repeat > 0) {
         timer->repeat --;
     }
-    return 4;
+    return 3;
 }
 
 /*

+ 4 - 2
luat/modules/luat_lib_wlan.c

@@ -101,8 +101,6 @@ static void _wlan_connect(void* params) {
 @usage wlan.connect("uiot", "1234567890")
 */
 static int l_wlan_connect(lua_State *L) {
-    //强制GC一次
-    //lua_gc(L, LUA_GCCOLLECT, 0);
     // 更新参数
     size_t len;
     const char* _ssid = luaL_checklstring(L, 1, &len);
@@ -129,6 +127,10 @@ static int l_wlan_connect(lua_State *L) {
         lua_pushstring(L, "fail to start wlan thread");
         return 2;
     }
+    // 自动重连
+    
+    rt_wlan_config_autoreconnect(1);
+    
     return 0;
 }
 

+ 4 - 0
luat/modules/luat_main.c

@@ -6,6 +6,7 @@
 #include "luat_log.h"
 #include "stdio.h"
 #include "luat_msgbus.h"
+#include "luat_timer.h"
 
 
 static int report (lua_State *L, int status);
@@ -100,5 +101,8 @@ int luat_main (int argc, char **argv, int _) {
   result = lua_toboolean(L, -1);  /* get result */
   report(L, status);
   //lua_close(L);
+  luat_log_info("luat.main", "Lua VM exit!! reboot in 60s");
+  luat_timer_mdelay(60*1000);
+  luat_os_reboot(result);
   return (result && status == LUA_OK) ? 0 : 2;
 }

+ 1 - 1
luat/rtt/luat_timer_rtt.c

@@ -21,7 +21,7 @@ static void rt_timer_callback(void *param) {
 }
 
 int luat_timer_start(luat_timer_t* timer) {
-    rt_sprintf(timer_name, "t%ld", timer->id); // TODO: 可能与rtt的命名长度冲突
+    rt_sprintf(timer_name, "t%06X", timer->id);
     LOG_D("rtt timer name=%s", timer_name);
     rt_tick_t time = timer->timeout;
     rt_uint8_t flag = timer->repeat ? RT_TIMER_FLAG_PERIODIC : RT_TIMER_FLAG_ONE_SHOT;

+ 1 - 1
luat/rtt/rtt_airkiss.c

@@ -9,7 +9,7 @@
 #include <stdlib.h>
 
 #define DBG_TAG           "wlan.airkiss"
-#define DBG_LVL           DBG_DEBUG
+#define DBG_LVL           DBG_INFO
 #include <rtdbg.h>
 
 typedef struct rtt_airkiss_ctx

+ 12 - 8
script/app/playit/main.lua

@@ -10,10 +10,12 @@ local TAG = "main"
 
 sys.subscribe("WLAN_READY", function ()
     print("!!! wlan ready event !!!")
+    -- 马上进行时间同步
+    socket.ntpSync()
 end)
 
 ----------------------------------------------------------------------
--- 对接SSD1306
+-- 对接SSD1306, 当前显示一行就好了
 function display_str(str)
     disp.clear()
     disp.drawStr(str, 1, 18)
@@ -102,13 +104,12 @@ end)
 -- 业务流程, 联网后定时发送温度数据到服务器
 sys.taskInit(function()
     while 1 do 
-        sys.waitUntil("WLAN_READY", 30000)
         if wlan.ready() == 1 then
-            sys.wait(5*1000)
-            print("prepare ds18b20 ...")
+            sys.wait(1000)
+            log.info("ds18b20", "start to read ds18b20 ...")
             local temp = (sensor.ds18b20(28) or "")
-            print("TEMP: " .. temp)
-            display_str("TEMP: " .. temp)
+            log.info("ds18b20", "TEMP: ", temp, os.date())
+            display_str("Temp: " .. temp  .. " rssi:" .. tostring(wlan.rssi()))
             local t = {"GET /api/w60x/report/ds18b20?mac=", wlan.get_mac(), "&temp=", temp, " HTTP/1.0\r\n",
                     "Host: site0.cn\r\n",
                     "User-Agent: LuatOS/0.1.0\r\n",
@@ -117,8 +118,11 @@ sys.taskInit(function()
             --print(data)
             -- TODO: 改成socket/netc对象
             socket.tsend("site0.cn", 80, table.concat(t))
-            print("tsend complete, sleep 30s")
-            sys.wait(30*1000)
+            log.info("network", "tsend complete, sleep 5s")
+            sys.wait(5000)
+        else
+            log.warn("main", "wlan is not ready yet")
+            sys.waitUntil("WLAN_READY", 30000)
         end
     end
 end)

+ 29 - 27
script/lib/sys.lua

@@ -13,9 +13,9 @@ local log = _G.log
 SCRIPT_LIB_VER = "1.0.0"
 
 -- TaskID最大值
-local TASK_TIMER_ID_MAX = 0x1FFFFFFF
+local TASK_TIMER_ID_MAX = 0x1FFFFF
 -- msgId 最大值(请勿修改否则会发生msgId碰撞的危险)
-local MSG_TIMER_ID_MAX = 0x7FFFFFFF
+local MSG_TIMER_ID_MAX = 0x7FFFFF
 
 -- 任务定时器id
 local taskTimerId = 0
@@ -124,7 +124,7 @@ end
 function sys.timerStop(val, ...)
     -- val 为定时器ID
     if type(val) == 'number' then
-        timerPool[val], para[val], loop[val] = nil
+        timerPool[val], para[val] = nil
         rtos.timer_stop(val)
     else
         for k, v in pairs(timerPool) do
@@ -133,7 +133,7 @@ function sys.timerStop(val, ...)
                 -- 可变参数相同
                 if cmpTable({...}, para[k]) then
                     rtos.timer_stop(k)
-                    timerPool[k], para[k], loop[val] = nil
+                    timerPool[k], para[k] = nil
                     break
                 end
             end
@@ -149,17 +149,12 @@ function sys.timerStopAll(fnc)
     for k, v in pairs(timerPool) do
         if type(v) == "table" and v.cb == fnc or v == fnc then
             rtos.timer_stop(k)
-            timerPool[k], para[k], loop[k] = nil
+            timerPool[k], para[k] = nil
         end
     end
 end
 
---- 开启一个定时器
--- @param fnc 定时器回调函数
--- @number ms 整数,最大定时126322567毫秒
--- @param ... 可变参数 fnc的参数
--- @return number 定时器ID,如果失败,返回nil
-function sys.timerStart(fnc, ms, ...)
+function sys.timerAdvStart(fnc, ms, _repeat, ...)
     --回调函数和时长检测
     --assert(fnc ~= nil, "sys.timerStart(first param) is nil !")
     --assert(ms > 0, "sys.timerStart(Second parameter) is <= zero !")
@@ -180,7 +175,7 @@ function sys.timerStart(fnc, ms, ...)
         end
     end
     --调用底层接口启动定时器
-    if rtos.timer_start(msgId, ms) ~= 1 then log.debug("rtos.timer_start error") return end
+    if rtos.timer_start(msgId, ms, _repeat) ~= 1 then return end
     --如果存在可变参数,在定时器参数表中保存参数
     if #arg ~= 0 then
         para[msgId] = arg
@@ -189,15 +184,22 @@ function sys.timerStart(fnc, ms, ...)
     return msgId
 end
 
+--- 开启一个定时器
+-- @param fnc 定时器回调函数
+-- @number ms 整数,最大定时126322567毫秒
+-- @param ... 可变参数 fnc的参数
+-- @return number 定时器ID,如果失败,返回nil
+function sys.timerStart(fnc, ms, ...)
+    return sys.timerAdvStart(fnc, ms, 0, ...)
+end
+
 --- 开启一个循环定时器
 -- @param fnc 定时器回调函数
 -- @number ms 整数,最大定时126322567毫秒
 -- @param ... 可变参数 fnc的参数
 -- @return number 定时器ID,如果失败,返回nil
 function sys.timerLoopStart(fnc, ms, ...)
-    local tid = sys.timerStart(fnc, ms, ...)
-    if tid then loop[tid] = ms end
-    return tid
+    return sys.timerAdvStart(fnc, ms, -1, ...)
 end
 
 --- 判断某个定时器是否处于开启状态
@@ -243,10 +245,10 @@ end
 -- @param callback 消息回调处理
 -- @usage unsubscribe("NET_STATUS_IND", callback)
 function sys.unsubscribe(id, callback)
-    --if type(id) ~= "string" or (type(callback) ~= "function" and type(callback) ~= "thread") then
-    --    log.warn("warning: sys.unsubscribe invalid parameter", id, callback)
-    --    return
-    --end
+    if type(id) ~= "string" or (type(callback) ~= "function" and type(callback) ~= "thread") then
+        log.warn("warning: sys.unsubscribe invalid parameter", id, callback)
+        return
+    end
     if subscribers[id] then subscribers[id][callback] = nil end
 end
 
@@ -311,15 +313,15 @@ local function safeRun()
         else
             local cb = timerPool[param]
             --如果不是循环定时器,从定时器id表中删除此定时器
-            if not loop[param] then timerPool[param] = nil end
+            if exparam == 0 then timerPool[param] = nil end
             if para[param] ~= nil then
                 cb(unpack(para[param]))
-                if not loop[param] then para[param] = nil end
+                if exparam == 0 then para[param] = nil end
             else
                 cb()
             end
             --如果是循环定时器,继续启动此定时器
-            if loop[param] then rtos.timer_start(param, loop[param]) end
+            --if loop[param] then rtos.timer_start(param, loop[param]) end
         end
     --其他消息(音频消息、充电管理消息、按键消息等)
     --elseif type(msg) == "number" then
@@ -335,12 +337,12 @@ end
 function sys.run()
     local result, err
     while true do
-        if sRollBack then
+        --if sRollBack then
             safeRun()
-        else
-            result, err = pcall(safeRun)
-            if not result then rtos.restart(err) end
-        end
+        --else
+        --    result, err = pcall(safeRun)
+        --    if not result then rtos.restart(err) end
+        --end
     end
 end