Procházet zdrojové kódy

add: demo/onewire库的demo

Wendal Chen před 2 roky
rodič
revize
f1189e7c98

+ 4 - 4
components/onewire/binding/luat_lib_onewire.c

@@ -52,7 +52,7 @@ static int l_onewire_ds18b20(lua_State *L)
     int ret = 0;
     ctx.id = luaL_checkinteger(L, 1);
     int check_crc = lua_toboolean(L, 2);
-    ctx.mode = luaL_checkinteger(L, 3);
+    ctx.mode = luaL_optinteger(L, 3, LUAT_ONEWIRE_MODE_GPIO);
     int32_t val = 0;
     ret = luat_onewire_ds18b20(&ctx, check_crc, &val);
     if (ret) {
@@ -81,13 +81,13 @@ else
 end
 
 */
-static int l_onewire_dht(lua_State *L)
+static int l_onewire_dht1x(lua_State *L)
 {
     luat_onewire_ctx_t ctx = {0};
     int ret = 0;
     ctx.id = luaL_checkinteger(L, 1);
     int check_crc = lua_toboolean(L, 2);
-    ctx.mode = luaL_checkinteger(L, 3);
+    ctx.mode = luaL_optinteger(L, 3, LUAT_ONEWIRE_MODE_GPIO);
     int32_t temp = 0;
     int32_t hm = 0;
     ret = luat_onewire_dht(&ctx, &temp, &hm, check_crc);
@@ -103,7 +103,7 @@ static int l_onewire_dht(lua_State *L)
 static const rotable_Reg_t reg_onewire[] =
     {
         {"ds18b20", ROREG_FUNC(l_onewire_ds18b20)},
-        {"dht1x", ROREG_FUNC(l_onewire_dht)},
+        {"dht1x", ROREG_FUNC(l_onewire_dht1x)},
         {"open", ROREG_FUNC(l_onewire_open)},
         {"close", ROREG_FUNC(l_onewire_close)},
 

+ 2 - 2
components/onewire/src/luat_onewire.c

@@ -403,8 +403,6 @@ int luat_onewire_dht(luat_onewire_ctx_t* ctx, int32_t* temp, int32_t* hm, int ch
     luat_os_exit_cri();
 
     // LLOGD("读出的数据 %02X%02X%02X%02X%02X", buff[0], buff[1], buff[2], buff[3], buff[4]);
-    *hm = buff[0]*100+buff[1];
-    *temp = buff[2]*100+buff[3];
 
     if (check_crc) {
         uint8_t check_r = 0;
@@ -415,6 +413,8 @@ int luat_onewire_dht(luat_onewire_ctx_t* ctx, int32_t* temp, int32_t* hm, int ch
         }
         // LLOGD("crc校验成功");
     }
+    *temp = buff[0]*100+buff[1];
+    *hm = buff[2]*100+buff[3];
     return 0;
 }
 

+ 62 - 0
demo/onewire/main.lua

@@ -0,0 +1,62 @@
+
+-- LuaTools需要PROJECT和VERSION这两个信息
+PROJECT = "onewire"
+VERSION = "1.0.0"
+
+log.info("main", PROJECT, VERSION)
+
+-- sys库是标配
+_G.sys = require("sys")
+
+
+sys.taskInit(function()
+    sys.wait(3000)
+
+    -- 这里以Air103的接线为例
+    -- 其他bsp不一定有pin库, 直接填具体的GPIO号就行
+    local ds18b20_pin = pin.PB20
+    local dht11_pin = pin.PB10
+    while 1 do
+        log.info("开始读取ds18b20", "单位0.1摄氏度")
+        if onewire then
+            local temp = onewire.ds18b20(ds18b20_pin, true, onewire.GPIO)
+            log.info("温度值", "onewire库", "ds18b20", temp)
+            sys.wait(1000)
+        end
+        
+        if sensor then
+            local temp = sensor.ds18b20(ds18b20_pin, true)
+            log.info("温度值", "sensor库", "ds18b20", temp)
+            sys.wait(1000)
+        end
+
+        
+        log.info("开始读取dht11", "单位0.01摄氏度和0.01%相对湿度")
+
+        if onewire then
+            local hm,temp = onewire.dht1x(dht11_pin, true, onewire.GPIO)
+            log.info("温度值", "onewire库", "dht11", temp)
+            log.info("湿度", "onewire库", "dht11", hm)
+            sys.wait(1000)
+        end
+        
+        if sensor then
+            local hm,temp = sensor.dht1x(dht11_pin, true)
+            log.info("温度值", "sensor库", "dht11", temp)
+            log.info("湿度", "sensor库", "dht11", hm)
+            sys.wait(1000)
+        end
+
+        if not onewire and not sensor then
+            log.warn("固件不含onewire库和sensor库,无法演示")
+            sys.wait(1000)
+        end
+    end
+
+end)
+
+
+-- 用户代码已结束---------------------------------------------
+-- 结尾总是这一句
+sys.run()
+-- sys.run()之后后面不要加任何语句!!!!!