luat_lib_touchkey.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. @module touchkey
  3. @summary 触摸按键
  4. @version 1.0
  5. @date 2022.01.15
  6. @tag LUAT_USE_TOUCHKEY
  7. */
  8. #include "luat_base.h"
  9. #include "luat_touchkey.h"
  10. #include "luat_msgbus.h"
  11. /*
  12. 配置触摸按键
  13. @api touchkey.setup(id, scan_period, window, threshold)
  14. @int 传感器id,请查阅硬件文档, 例如air101/air103支持 1~15, 例如PA7对应touch id=1
  15. @int 扫描间隔,范围1 ~ 0x3F, 单位16ms,可选
  16. @int 扫描窗口,范围2-7, 可选
  17. @int 阀值, 范围0-127, 可选
  18. @return bool 成功返回true, 失败返回false
  19. @usage
  20. touchkey.setup(1)
  21. sys.subscribe("TOUCHKEY_INC", function(id, count)
  22. -- 传感器id
  23. -- 计数器,触摸次数统计
  24. log.info("touchkey", id, count)
  25. end)
  26. */
  27. static int l_touchkey_setup(lua_State *L) {
  28. luat_touchkey_conf_t conf;
  29. conf.id = (uint8_t)luaL_checkinteger(L, 1);
  30. conf.scan_period = (uint8_t)luaL_optinteger(L, 2, 0);
  31. conf.window = (uint8_t)luaL_optinteger(L, 2, 0);
  32. conf.threshold = (uint8_t)luaL_optinteger(L, 2, 0);
  33. int ret = luat_touchkey_setup(&conf);
  34. lua_pushboolean(L, ret == 0 ? 1 : 0);
  35. return 1;
  36. }
  37. /*
  38. 关闭初始触摸按键
  39. @api touchkey.close(id)
  40. @int 传感器id,请查阅硬件文档
  41. @return nil 无返回值
  42. @usage
  43. -- 不太可能需要关掉的样子
  44. touchkey.close(1)
  45. */
  46. static int l_touchkey_close(lua_State *L) {
  47. uint8_t pin = (uint8_t)luaL_checkinteger(L, 1);
  48. luat_touchkey_close(pin);
  49. return 0;
  50. }
  51. int l_touchkey_handler(lua_State *L, void* ptr) {
  52. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  53. lua_getglobal(L, "sys_pub");
  54. if (lua_isnil(L, -1)) {
  55. lua_pushinteger(L, 0);
  56. return 1;
  57. }
  58. /*
  59. @sys_pub touchkey
  60. 触摸按键消息
  61. TOUCHKEY_INC
  62. @number port, 传感器id
  63. @number state, 计数器,触摸次数统计
  64. @usage
  65. sys.subscribe("TOUCHKEY_INC", function(id, count)
  66. -- 传感器id
  67. -- 计数器,触摸次数统计
  68. log.info("touchkey", id, count)
  69. end)
  70. */
  71. lua_pushliteral(L, "TOUCHKEY_INC");
  72. lua_pushinteger(L, msg->arg1);
  73. lua_pushinteger(L, msg->arg2);
  74. lua_call(L, 3, 0);
  75. return 0;
  76. }
  77. #include "rotable2.h"
  78. static const rotable_Reg_t reg_touchkey[] =
  79. {
  80. { "setup", ROREG_FUNC(l_touchkey_setup)},
  81. { "close", ROREG_FUNC(l_touchkey_close)},
  82. { NULL, ROREG_INT(0) }
  83. };
  84. LUAMOD_API int luaopen_touchkey(lua_State *L)
  85. {
  86. luat_newlib2(L, reg_touchkey);
  87. return 1;
  88. }