luat_lib_softkeyboard.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. @module softkeyboard
  3. @summary 软件键盘矩阵(当前仅air105支持)
  4. @version 1.0
  5. @date 2022.03.09
  6. */
  7. #include "luat_base.h"
  8. #include "luat_softkeyboard.h"
  9. #include "luat_msgbus.h"
  10. //----------------------
  11. int l_softkeyboard_handler(lua_State *L, void* ptr) {
  12. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  13. lua_getglobal(L, "sys_pub");
  14. /*
  15. @sys_pub softkeyboard
  16. 键盘矩阵消息
  17. SOFT_KB_INC
  18. @number port, keyboard id 当前固定为0, 可以无视
  19. @number data, keyboard 按键 需要配合init的map进行解析
  20. @number state, 按键状态 1 为按下, 0 为 释放
  21. @usage
  22. sys.subscribe("SOFT_KB_INC", function(port, data, state)
  23. -- port 当前固定为0, 可以无视
  24. -- data, 需要配合init的map进行解析
  25. -- state, 1 为按下, 0 为 释放
  26. log.info("keyboard", port, data, state)
  27. end)
  28. */
  29. lua_pushstring(L, "SOFT_KB_INC");
  30. lua_pushinteger(L, msg->arg1);
  31. lua_pushinteger(L, msg->arg2);
  32. lua_pushinteger(L, msg->ptr);
  33. lua_call(L, 4, 0);
  34. return 0;
  35. }
  36. /**
  37. 初始化软件键盘矩阵
  38. @api softkb.init(port, key_in, key_out)
  39. @int 预留, 当前填0
  40. @table 矩阵输入按键表
  41. @table 矩阵输出按键表
  42. @usage
  43. key_in = {pin.PD10,pin.PE00,pin.PE01,pin.PE02}
  44. key_out = {pin.PD12,pin.PD13,pin.PD14,pin.PD15}
  45. softkb.init(0,key_in,key_out)
  46. sys.subscribe("SOFT_KB_INC", function(port, data, state)
  47. -- port 当前固定为0, 可以无视
  48. -- data, 需要配合init的map进行解析
  49. -- state, 1 为按下, 0 为 释放
  50. -- TODO 详细介绍
  51. end)
  52. */
  53. int l_softkb_init(lua_State* L) {
  54. luat_softkeyboard_conf_t conf = {0};
  55. conf.port = luaL_checkinteger(L,1);
  56. if (lua_istable(L, 2)) {
  57. conf.inio_num = lua_rawlen(L, 2);
  58. conf.inio = (uint8_t*)luat_heap_calloc(conf.inio_num,sizeof(uint8_t));
  59. for (size_t i = 0; i < conf.inio_num; i++){
  60. lua_geti(L,2,i+1);
  61. conf.inio[i] = luaL_checkinteger(L,-1);
  62. lua_pop(L, 1);
  63. }
  64. }
  65. if (lua_istable(L, 3)) {
  66. conf.outio_num = lua_rawlen(L, 3);
  67. conf.outio = (uint8_t*)luat_heap_calloc(conf.outio_num,sizeof(uint8_t));
  68. for (size_t i = 0; i < conf.outio_num; i++){
  69. lua_geti(L,3,i+1);
  70. conf.outio[i] = luaL_checkinteger(L,-1);
  71. lua_pop(L, 1);
  72. }
  73. }
  74. int ret = luat_softkeyboard_init(&conf);
  75. lua_pushboolean(L, ret == 0 ? 1 : 0);
  76. return 1;
  77. }
  78. /**
  79. 删除软件键盘矩阵
  80. @api softkb.deinit(port)
  81. @int 预留, 当前填0
  82. @usage
  83. softkb.deinit(0)
  84. */
  85. int l_softkb_deinit(lua_State* L) {
  86. luat_softkeyboard_conf_t conf = {0};
  87. uint8_t softkb_port = luaL_checkinteger(L,1);
  88. int ret = luat_softkeyboard_deinit(&conf);
  89. luat_heap_free(conf.inio);
  90. luat_heap_free(conf.outio);
  91. lua_pushboolean(L, ret == 0 ? 1 : 0);
  92. return 1;
  93. }
  94. #include "rotable.h"
  95. static const rotable_Reg reg_softkb[] =
  96. {
  97. { "init", l_softkb_init, 0},
  98. { "deinit", l_softkb_deinit, 0},
  99. { NULL, NULL, 0}
  100. };
  101. LUAMOD_API int luaopen_softkb( lua_State *L ) {
  102. luat_newlib(L, reg_softkb);
  103. return 1;
  104. }