luat_lib_softkeyboard.c 3.4 KB

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