Просмотр исходного кода

add: 添加touchkey库, 当前仅air101/air103支持

Wendal Chen 4 лет назад
Родитель
Сommit
3db629a8a1
3 измененных файлов с 105 добавлено и 0 удалено
  1. 2 0
      luat/include/luat_libs.h
  2. 21 0
      luat/include/luat_touchkey.h
  3. 82 0
      luat/modules/luat_lib_touchkey.c

+ 2 - 0
luat/include/luat_libs.h

@@ -107,4 +107,6 @@ LUAMOD_API int luaopen_zlib( lua_State *L );
 LUAMOD_API int luaopen_camera( lua_State *L );
 LUAMOD_API int luaopen_luf( lua_State *L );
 
+LUAMOD_API int luaopen_touchkey(lua_State *L);
+
 #endif

+ 21 - 0
luat/include/luat_touchkey.h

@@ -0,0 +1,21 @@
+
+#ifndef LUAT_TOUCHKEY_H
+#define LUAT_TOUCHKEY_H
+
+#include "luat_base.h"
+
+typedef struct luat_touchkey_conf
+{
+    uint8_t id;
+    uint8_t scan_period;
+    uint8_t window;
+    uint8_t threshold;
+}luat_touchkey_conf_t;
+
+int luat_touchkey_setup(luat_touchkey_conf_t *conf);
+
+int luat_touchkey_close(uint8_t id);
+
+int l_touchkey_handler(lua_State *L, void* ptr);
+
+#endif

+ 82 - 0
luat/modules/luat_lib_touchkey.c

@@ -0,0 +1,82 @@
+/*
+@module  touchkey
+@summary 触摸按键
+@version 1.0
+@date    2022.01.15
+*/
+#include "luat_base.h"
+#include "luat_touchkey.h"
+#include "luat_msgbus.h"
+
+/*
+配置触摸按键
+@api touchkey.setup(id, scan_period, window, threshold)
+@int 传感器id,请查阅硬件文档, 例如air101/air103支持 1~15, 例如PA7对应touch id=1
+@int 扫描间隔,范围1 ~ 0x3F, 单位16ms,可选
+@int 扫描窗口,范围2-7, 可选
+@int 阀值, 范围0-127, 可选
+@return bool 成功返回true, 失败返回false
+@usage
+touchkey.setup(1)
+sys.subscribe("TOUCHKEY_INC", function(id, count)
+    -- 传感器id
+    -- 计数器,触摸次数统计
+    log.info("touchkey", id, count)
+end)
+*/
+static int l_touchkey_setup(lua_State *L) {
+    luat_touchkey_conf_t conf;
+
+    conf.id = (uint8_t)luaL_checkinteger(L, 1);
+    conf.scan_period = (uint8_t)luaL_optinteger(L, 2, 0);
+    conf.window = (uint8_t)luaL_optinteger(L, 2, 0);
+    conf.threshold = (uint8_t)luaL_optinteger(L, 2, 0);
+
+    int ret = luat_touchkey_setup(&conf);
+    lua_pushboolean(L, ret == 0 ? 1 : 0);
+    return 1;
+}
+
+/*
+关闭初始触摸按键
+@api touchkey.close(id)
+@int 传感器id,请查阅硬件文档
+@return nil 无返回值
+@usage
+-- 不太可能需要关掉的样子
+touchkey.close(1)
+*/
+static int l_touchkey_close(lua_State *L) {
+    uint8_t pin = (uint8_t)luaL_checkinteger(L, 1);
+    luat_touchkey_close(pin);
+    return 0;
+}
+
+int l_touchkey_handler(lua_State *L, void* ptr) {
+    rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
+    lua_getglobal(L, "sys_pub");
+    if (lua_isnil(L, -1)) {
+        lua_pushinteger(L, 0);
+        return 1;
+    }
+    lua_pushliteral(L, "TOUCHKEY_INC");
+    lua_pushinteger(L, msg->arg1);
+    lua_pushinteger(L, msg->arg2);
+    lua_call(L, 3, 0);
+    return 0;
+}
+
+
+#include "rotable.h"
+static const rotable_Reg reg_touchkey[] =
+{
+    { "setup",  l_touchkey_setup,0},
+    { "close",  l_touchkey_close,0},
+    { NULL,             NULL ,          0}
+};
+
+LUAMOD_API int luaopen_touchkey(lua_State *L)
+{
+    luat_newlib(L, reg_touchkey);
+    return 1;
+}