Explorar o código

add: air302的i2c验证通过, 添加dht12的demo

Wendal Chen %!s(int64=5) %!d(string=hai) anos
pai
achega
a01807bd94
Modificáronse 3 ficheiros con 68 adicións e 8 borrados
  1. 12 6
      bsp/air302/air302.py
  2. 54 0
      bsp/air302/demo/dht12/main.lua
  3. 2 2
      luat/modules/luat_lib_i2c.c

+ 12 - 6
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")
@@ -236,6 +236,9 @@ def _dl(tp, _path=None):
 生成文件系统镜像
 '''
 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 +246,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 +279,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 +307,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

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

@@ -0,0 +1,54 @@
+
+-- 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)
+    i2c.setup(id, i2c.SLOW)
+
+    local data = i2c.readReg(id, 0x5C, 0, 5)
+    if not data then
+        log.info("i2c", "read reg fail")
+        return
+    end
+
+    i2c.close(id)
+    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()
+    while 1 do
+        sys.wait(5000) -- 5秒读取一次
+        log.info("dht12", read_dht12(0))
+    end
+end)
+
+-- 用户代码已结束---------------------------------------------
+-- 结尾总是这一句
+sys.run()
+-- sys.run()之后后面不要加任何语句!!!!!

+ 2 - 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;
 }