Forráskód Böngészése

Merge branch 'master' of https://gitee.com/openLuat/LuatOS into master

chenxuuu 5 éve
szülő
commit
f84267fbdd
3 módosított fájl, 132 hozzáadás és 10 törlés
  1. 18 8
      bsp/air302/air302.py
  2. 57 0
      bsp/air302/demo/dht12/main.lua
  3. 57 2
      luat/modules/luat_lib_i2c.c

+ 18 - 8
bsp/air302/air302.py

@@ -33,7 +33,7 @@ config['air302'] = {
     "TOOLS_PATH": ".\\tools\\",
     "MAIN_LUA_DEBUG" : "false",
     "LUA_DEBUG" : "false",
-    "COM_PORT" : "COM56" # 请修改local.ini文件
+    "COM_PORT" : "COM59" # 请修改local.ini文件
 }
 if os.path.exists("local.ini") :
     config.read("local.ini")
@@ -214,28 +214,35 @@ def _dl(tp, _path=None):
     cmd = [FTC_PATH + "FlashToolCLI.exe", "-p", COM_PORT, "burnbatch", "--imglist"]
     if tp == "rom" or tp == "full":
         if os.path.exists(PLAT_ROOT + "out/ec616_0h00/air302/air302.bin") :
+            print("P1 COPY beta version from PLAT_ROOT dir", PLAT_ROOT + "out/ec616_0h00/air302/air302.bin")
             shutil.copy(PLAT_ROOT + "out/ec616_0h00/air302/air302.bin", FTC_PATH + "system.bin")
         elif EC_PATH.endswith(".ec") :
+            print("P1. Unzip luatos.bin from " + EC_PATH)
             import zipfile
             with zipfile.ZipFile(EC_PATH) as zip :
                 with open(FTC_PATH + "system.bin", "wb") as f:
                     f.write(zip.read("luatos.bin"))
         elif EC_PATH.endswith(".bin"):
+            print("P1. Using bin file from " + EC_PATH)
             shutil.copy(EC_PATH, FTC_PATH + "system.bin")
         else:
-            print("Bad EC_PATH")
+            print("Bad EC_PATH : " + EC_PATH)
             return
         cmd += ["system"]
         cmd += ["bootloader"]
     if tp == "fs" or tp == "full" :
         cmd += ["flexfile2"]
-    print("CALL", " ".join(cmd))
+    print("P2. Call", " ".join(cmd))
     subprocess.check_call(cmd, cwd=FTC_PATH)
+    print("P3. Done")
 
 '''
 生成文件系统镜像
 '''
 def _lfs(_path=None):
+    print("============================================================")
+    print(" Build LittltFS disk image")
+    print("============================================================")
     _disk = FTC_PATH + "disk"
     if os.path.exists(_disk) :
         shutil.rmtree(_disk)
@@ -243,10 +250,12 @@ def _lfs(_path=None):
 
     if not _path:
         _path = USER_PATH
+    print("P1. User Lua Dir == ", os.path.abspath(_path))
     # 收集需要处理的文件列表
     _paths = []
     # 首先,遍历lib目录
     if os.path.exists(LIB_PATH) :
+        print("P1. Lib  Lua Dir == ", os.path.abspath(LIB_PATH))
         for name in os.listdir(LIB_PATH) :
             _paths.append(LIB_PATH + name)
     # 然后遍历user目录
@@ -274,18 +283,18 @@ def _lfs(_path=None):
             else:
                 print("LUA_DEBUG", LUA_DEBUG, "False" == LUA_DEBUG)
             cmd += ["-o", FTC_PATH + "disk/" + os.path.basename(name) + "c", os.path.basename(name)]
-            print("CALL", " ".join(cmd))
+            print("P2. CALL Luac >> ", os.path.abspath(name))
             subprocess.check_call(cmd, cwd=os.path.dirname(name))
         # 其他文件直接拷贝
         else:
-            print("COPY", name, FTC_PATH + "disk/" + os.path.basename(name))
+            print("P2. COPY", name, FTC_PATH + "disk/" + os.path.basename(name))
             shutil.copy(name, FTC_PATH + "disk/" + os.path.basename(name))
     if TAG_PROJECT == "" or TAG_VERSION == "" :
         print("!!!!!!!miss PROJECT or/and VERSION!!!!!!!!!!")
 
     for root, dirs, files in os.walk(FTC_PATH + "disk", topdown=False):
         import struct
-        print("write flashx.bin", root)
+        print("P3. Make flashx.bin", FTC_PATH + "disk/flashx.bin")
         with open(FTC_PATH + "disk/flashx.bin", "wb") as f :
             # 写入文件头
             f.write(struct.pack("<HHI", 0x1234, 0x00, 0x00))
@@ -302,11 +311,12 @@ def _lfs(_path=None):
     if TAG_PROJECT != "" and TAG_VERSION != "":
         # otademo_1.2.7_LuatOS_V0003_ec616
         TAG_NAME = "%s_%s_LuatOS_V0003_ec616.bin" % (TAG_PROJECT, TAG_VERSION)
-        print("update bin --> " + TAG_NAME)
+        print("P4. OTA Update bin --> " + TAG_NAME)
         shutil.copy(FTC_PATH + "disk/flashx.bin", TAG_NAME)
 
-    print("CALL mklfs for disk.fs")
+    print("P5. CALL mklfs to make disk.fs")
     subprocess.check_call([TOOLS_PATH + "mklfs.exe"], cwd=FTC_PATH)
+    print("6. LFS DONE")
 
 def main():
     argc = 1

+ 57 - 0
bsp/air302/demo/dht12/main.lua

@@ -0,0 +1,57 @@
+
+-- LuaTools需要PROJECT和VERSION这两个信息
+PROJECT = "dht12"
+VERSION = "1.0.0"
+
+-- sys库是标配
+_G.sys = require("sys")
+
+-- https://datasheet.lcsc.com/szlcsc/DHT12-Digital-temperature-and-humidity-sensor_C83989.pdf
+-- Air302只有一个i2c, id=0
+-- 如果读不到数据, 请尝试以下操作
+-- 1. 调换SCL和SDA
+-- 2. 确保SCL和SDA均有上拉到VCC(3.3v), 1k~10k
+
+-- -- 初始化并打开I2C操作DHT12
+-- local function read_dht12(id)
+
+--     local data = i2c.readReg(id, 0x5C, 0, 5)
+--     if not data then
+--         log.info("i2c", "read reg fail")
+--         return
+--     end
+
+    
+--     log.info("DHT12 HEX data: ", data:toHex())
+--     -- 分别是湿度整数,湿度小数,温度整数,温度湿度
+--     local _, h_H, h_L, t_H, t_L,crc = pack.unpack(data, 'b5')
+--     log.info("DHT12 data: ", h_H, h_L, t_H, t_L)
+--     -- 计算校验和, 前4位的值相加应该等于最后一位的值
+--     if (((h_H + h_L + t_H + t_L) & 0xFF )) ~= crc then
+--         log.info("DHT12", "check crc fail")
+--         return "0.0", "0.0"
+--     end
+--     -- 需要考虑温度低于0度的情况, t_L第0位是符号位
+--     local t_L2 = tonumber(t_L)
+--     if t_L2 > 127 then
+--         return h_H .. ".".. h_L, "-" .. t_H .. "." .. tostring(t_L2 - 128)
+--     else
+--         return h_H .. ".".. h_L, t_H .. "." .. t_L
+--     end
+-- end
+
+sys.taskInit(function()
+    local id = 0
+    while 1 do
+        sys.wait(5000) -- 5秒读取一次
+        i2c.setup(id, i2c.SLOW)
+        --log.info("dht12", read_dht12(0)) -- 传统方式读取,请取消read_dht12方法的注释
+        log.info("dht12", i2c.readDHT12(id))
+        i2c.close(id)
+    end
+end)
+
+-- 用户代码已结束---------------------------------------------
+-- 结尾总是这一句
+sys.run()
+-- sys.run()之后后面不要加任何语句!!!!!

+ 57 - 2
luat/modules/luat_lib_i2c.c

@@ -37,7 +37,7 @@ i2c初始化
 @return int 成功就返回1,否则返回0
 @usage
 -- 初始化i2c1
-if i2c.setup(1) then
+if i2c.setup(1) == 0 then
     log.info("存在 i2c1")
 else
     i2c.close(1) -- 关掉
@@ -45,7 +45,7 @@ end
 */
 static int l_i2c_setup(lua_State *L) {
     int re = luat_i2c_setup(luaL_checkinteger(L, 1), luaL_optinteger(L, 2, 0), luaL_optinteger(L, 3, 0));
-    lua_pushinteger(L, re == 0 ? 1 : 0);
+    lua_pushinteger(L, re == 0 ? luaL_optinteger(L, 2, 0) : -1);
     return 1;
 }
 
@@ -177,6 +177,58 @@ static int l_i2c_close(lua_State *L) {
     return 0;
 }
 
+/*
+从i2c总线读取DHT12的温湿度数据
+@api i2c.readDHT(id)
+@int 设备id, 例如i2c1的id为1, i2c2的id为2
+@int DHT12的设备地址,默认0x5C
+@return boolean 读取成功返回true,否则返回false
+@return int 湿度值,单位0.1%, 例如 591 代表 59.1%
+@return int 温度值,单位0.1摄氏度, 例如 292 代表 29.2摄氏度
+@usage
+-- 从i2c0读取DHT12
+i2c.setup(0)
+local re, H, T = i2c.readDHT(0)
+if re then
+    log.info("dht12", H, T)
+end
+*/
+static int l_i2c_readDHT12(lua_State *L) {
+    int id = luaL_checkinteger(L, 1);
+    int addr = luaL_optinteger(L, 2, 0x5C);
+    char buff[5] = {0};
+    char temp = 0x00;
+    int result = luat_i2c_send(id, addr, &temp, 1);
+    result = luat_i2c_recv(id, addr, buff, 5);
+    if (result) {
+        lua_pushboolean(L, 0);
+        return 1;
+    }
+    else {
+        if (buff[0] == 0 && buff[1] == 0 && buff[2] == 0 && buff[3] == 0 && buff[4] == 0) {
+            LLOGD("DHT12 DATA emtry");
+            lua_pushboolean(L, 0);
+            return 1;
+        }
+        // 检查crc值
+        LLOGD("DHT12 DATA %02X%02X%02X%02X%02X", buff[0], buff[1], buff[2], buff[3], buff[4]);
+        uint8_t crc_act = (uint8_t)buff[0] + (uint8_t)buff[1] + (uint8_t)buff[2] + (uint8_t)buff [3];
+        uint8_t crc_exp = (uint8_t)buff[4];
+        if (crc_act != crc_exp) {
+            LLOGD("DATA12 DATA crc not ok");
+            lua_pushboolean(L, 0);
+            return 1;
+        }
+        lua_pushboolean(L, 1);
+        lua_pushinteger(L, (uint8_t)buff[0] * 10 + (uint8_t)buff[1]);
+        if (((uint8_t)buff[2]) > 127)
+            lua_pushinteger(L, ((uint8_t)buff[2] - 128) * -10 + (uint8_t)buff[3]);
+        else
+            lua_pushinteger(L, (uint8_t)buff[2] * 10 + (uint8_t)buff[3]);
+        return 3;
+    }
+}
+
 #include "rotable.h"
 static const rotable_Reg reg_i2c[] =
 {
@@ -187,6 +239,9 @@ static const rotable_Reg reg_i2c[] =
     { "writeReg", l_i2c_write_reg, 0},
     { "readReg", l_i2c_read_reg, 0},
     { "close", l_i2c_close, 0},
+
+    { "readDHT12", l_i2c_readDHT12, 0},
+
     { "FAST",  NULL, 1},
     { "SLOW",  NULL, 0},
 	{ NULL, NULL, 0}