luat_lib_tp.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. @module tp
  3. @summary 触摸库
  4. @version 1.0
  5. @date 2025.02.19
  6. @demo lcd
  7. @tag LUAT_USE_TP
  8. */
  9. #include "luat_base.h"
  10. #include "luat_tp.h"
  11. #include "luat_msgbus.h"
  12. #include "luat_mem.h"
  13. #include "luat_gpio.h"
  14. #define LUAT_LOG_TAG "tp"
  15. #include "luat_log.h"
  16. typedef struct tp_reg {
  17. const char *name;
  18. const luat_tp_config_t *luat_tp_config;
  19. }tp_reg_t;
  20. static const tp_reg_t tp_regs[] = {
  21. {"gt911", &tp_config_gt911},
  22. {"jd9261t_inited", &tp_config_jd9261t_inited},
  23. {"", NULL}
  24. };
  25. static int l_tp_handler(lua_State* L, void* ptr) {
  26. rtos_msg_t *msg = (rtos_msg_t *)lua_topointer(L, -1);
  27. luat_tp_config_t* luat_tp_config = msg->ptr;
  28. luat_tp_data_t* luat_tp_data = msg->arg1;
  29. if (luat_tp_config->luat_cb) {
  30. lua_geti(L, LUA_REGISTRYINDEX, luat_tp_config->luat_cb);
  31. if (lua_isfunction(L, -1)) {
  32. lua_pushlightuserdata(L, luat_tp_config);
  33. lua_newtable(L);
  34. for (uint8_t i=0; i<LUAT_TP_TOUCH_MAX; i++){
  35. if ((TP_EVENT_TYPE_DOWN == luat_tp_data[i].event) || (TP_EVENT_TYPE_UP == luat_tp_data[i].event) || (TP_EVENT_TYPE_MOVE == luat_tp_data[i].event)){
  36. lua_newtable(L);
  37. lua_pushstring(L, "event");
  38. lua_pushinteger(L, luat_tp_data[i].event);
  39. lua_settable(L, -3);
  40. lua_pushstring(L, "track_id");
  41. lua_pushinteger(L, luat_tp_data[i].track_id);
  42. lua_settable(L, -3);
  43. lua_pushstring(L, "x");
  44. lua_pushinteger(L, luat_tp_data[i].x_coordinate);
  45. lua_settable(L, -3);
  46. lua_pushstring(L, "y");
  47. lua_pushinteger(L, luat_tp_data[i].y_coordinate);
  48. lua_settable(L, -3);
  49. lua_pushstring(L, "width");
  50. lua_pushinteger(L, luat_tp_data[i].width);
  51. lua_settable(L, -3);
  52. lua_pushstring(L, "timestamp");
  53. lua_pushinteger(L, luat_tp_data[i].timestamp);
  54. lua_settable(L, -3);
  55. lua_pushinteger(L, i + 1);
  56. lua_insert(L, -2);
  57. lua_settable(L, -3);
  58. }
  59. }
  60. lua_call(L, 2, 0);
  61. }
  62. }
  63. luat_tp_config->opts->read_done(luat_tp_config);
  64. return 0;
  65. }
  66. int l_tp_callback(luat_tp_config_t* luat_tp_config, luat_tp_data_t* luat_tp_data){
  67. for(uint8_t i = 0; i < LUAT_TP_TOUCH_MAX; i++) {
  68. if (luat_tp_data[i].event != TP_EVENT_TYPE_NONE) {
  69. rtos_msg_t msg = {.handler = l_tp_handler, .ptr=luat_tp_config, .arg1=luat_tp_data};
  70. luat_msgbus_put(&msg, 1);
  71. return 0;
  72. }
  73. }
  74. return 0;
  75. }
  76. /*
  77. 触摸初始化
  78. @api tp.init(tp, args)
  79. @string tp类型,当前支持:<br>gt911
  80. @table 附加参数,与具体设备有关:<br>port 驱动方式<br>port:硬件i2c端口,例如0,1,2...如果为软件i2c对象<br>pin_rst:复位引脚<br>pin_int:中断引脚<br>w:宽度<br>h:高度
  81. @usage
  82. // tp.init("gt911",{port=0,pin_rst = 22,pin_int = 23,w = 320,h = 480})
  83. // local softI2C = i2c.createSoft(20, 21)
  84. // tp.init("gt911",{port=softI2C,pin_rst = 22,pin_int = 23,w = 320,h = 480})
  85. */
  86. static int l_tp_init(lua_State* L){
  87. int ret;
  88. size_t len = 0;
  89. luat_tp_config_t *luat_tp_config = (luat_tp_config_t *)luat_heap_malloc(sizeof(luat_tp_config_t));
  90. if (luat_tp_config == NULL) {
  91. LLOGE("out of system memory!!!");
  92. return 0;
  93. }
  94. memset(luat_tp_config, 0x00, sizeof(luat_tp_config_t));
  95. luat_tp_config->callback = l_tp_callback;
  96. const char* tp_name = luaL_checklstring(L, 1, &len);
  97. for(int i = 0; i < 100; i++){
  98. if (strlen(tp_regs[i].name) == 0)
  99. break;
  100. if(strcmp(tp_regs[i].name,tp_name) == 0){
  101. luat_tp_config->opts = tp_regs[i].luat_tp_config;
  102. break;
  103. }
  104. }
  105. if (luat_tp_config->opts == NULL){
  106. LLOGE("tp_name:%s not found!!!", tp_name);
  107. return 0;
  108. }
  109. if (lua_isfunction(L, 3)) {
  110. lua_pushvalue(L, 3);
  111. luat_tp_config->luat_cb = luaL_ref(L, LUA_REGISTRYINDEX);
  112. }
  113. lua_settop(L, 2);
  114. lua_pushstring(L, "port");
  115. int port = lua_gettable(L, 2);
  116. if (LUA_TNUMBER == port) {
  117. luat_tp_config->i2c_id = luaL_checkinteger(L, -1);
  118. }else if(LUA_TUSERDATA == port){
  119. luat_tp_config->soft_i2c = (luat_ei2c_t*)lua_touserdata(L, -1);
  120. }else{
  121. LLOGE("port type error!!!");
  122. return 0;
  123. }
  124. lua_pop(L, 1);
  125. lua_pushstring(L, "pin_rst");
  126. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  127. luat_tp_config->pin_rst = luaL_checkinteger(L, -1);
  128. }
  129. lua_pop(L, 1);
  130. lua_pushstring(L, "pin_int");
  131. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  132. luat_tp_config->pin_int = luaL_checkinteger(L, -1);
  133. }
  134. lua_pop(L, 1);
  135. lua_pushstring(L, "w");
  136. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  137. luat_tp_config->w = luaL_checkinteger(L, -1);
  138. }
  139. lua_pop(L, 1);
  140. lua_pushstring(L, "h");
  141. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  142. luat_tp_config->h = luaL_checkinteger(L, -1);
  143. }
  144. lua_pop(L, 1);
  145. lua_pushstring(L, "refresh_rate");
  146. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  147. luat_tp_config->refresh_rate = luaL_checkinteger(L, -1);
  148. }
  149. lua_pop(L, 1);
  150. lua_pushstring(L, "int_type");
  151. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  152. luat_tp_config->int_type = luaL_checkinteger(L, -1);
  153. }
  154. lua_pop(L, 1);
  155. lua_pushstring(L, "tp_num");
  156. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  157. luat_tp_config->tp_num = luaL_checkinteger(L, -1);
  158. }
  159. lua_pop(L, 1);
  160. ret = luat_tp_init(luat_tp_config);
  161. if (ret){
  162. // luat_tp_deinit(luat_tp_config);
  163. return 0;
  164. }else{
  165. lua_pushlightuserdata(L, luat_tp_config);
  166. return 1;
  167. }
  168. }
  169. #include "rotable2.h"
  170. static const rotable_Reg_t reg_tp[] =
  171. {
  172. { "init", ROREG_FUNC(l_tp_init)},
  173. { "RISING", ROREG_INT(LUAT_GPIO_RISING_IRQ)},
  174. { "FALLING", ROREG_INT(LUAT_GPIO_FALLING_IRQ)},
  175. { NULL, ROREG_INT(0) }
  176. };
  177. LUAMOD_API int luaopen_tp(lua_State *L)
  178. {
  179. luat_newlib2(L, reg_tp);
  180. return 1;
  181. }