luat_lib_tp.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. #ifdef LUAT_USE_LCD
  15. #include "luat_lcd.h"
  16. #endif
  17. #define LUAT_LOG_TAG "tp"
  18. #include "luat_log.h"
  19. typedef struct tp_reg {
  20. const char *name;
  21. const luat_tp_config_t *luat_tp_config;
  22. }tp_reg_t;
  23. static const tp_reg_t tp_regs[] = {
  24. {"gt911", &tp_config_gt911},
  25. {"gt9157", &tp_config_gt9157},
  26. {"jd9261t", &tp_config_jd9261t},
  27. {"jd9261t_inited", &tp_config_jd9261t_inited},
  28. {"", NULL}
  29. };
  30. static int l_tp_handler(lua_State* L, void* ptr) {
  31. rtos_msg_t *msg = (rtos_msg_t *)lua_topointer(L, -1);
  32. luat_tp_config_t* luat_tp_config = msg->ptr;
  33. luat_tp_data_t* luat_tp_data = msg->arg1;
  34. if (luat_tp_config->luat_cb) {
  35. lua_geti(L, LUA_REGISTRYINDEX, luat_tp_config->luat_cb);
  36. if (lua_isfunction(L, -1)) {
  37. lua_pushlightuserdata(L, luat_tp_config);
  38. lua_newtable(L);
  39. for (uint8_t i=0; i<LUAT_TP_TOUCH_MAX; i++){
  40. 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)){
  41. lua_newtable(L);
  42. lua_pushstring(L, "event");
  43. lua_pushinteger(L, luat_tp_data[i].event);
  44. lua_settable(L, -3);
  45. lua_pushstring(L, "track_id");
  46. lua_pushinteger(L, luat_tp_data[i].track_id);
  47. lua_settable(L, -3);
  48. lua_pushstring(L, "x");
  49. lua_pushinteger(L, luat_tp_data[i].x_coordinate);
  50. lua_settable(L, -3);
  51. lua_pushstring(L, "y");
  52. lua_pushinteger(L, luat_tp_data[i].y_coordinate);
  53. lua_settable(L, -3);
  54. lua_pushstring(L, "width");
  55. lua_pushinteger(L, luat_tp_data[i].width);
  56. lua_settable(L, -3);
  57. lua_pushstring(L, "timestamp");
  58. lua_pushinteger(L, luat_tp_data[i].timestamp);
  59. lua_settable(L, -3);
  60. lua_pushinteger(L, i + 1);
  61. lua_insert(L, -2);
  62. lua_settable(L, -3);
  63. }
  64. }
  65. lua_call(L, 2, 0);
  66. }
  67. }
  68. luat_tp_config->opts->read_done(luat_tp_config);
  69. return 0;
  70. }
  71. int l_tp_callback(luat_tp_config_t* luat_tp_config, luat_tp_data_t* luat_tp_data){
  72. for(uint8_t i = 0; i < LUAT_TP_TOUCH_MAX; i++) {
  73. if (luat_tp_data[i].event != TP_EVENT_TYPE_NONE) {
  74. rtos_msg_t msg = {.handler = l_tp_handler, .ptr=luat_tp_config, .arg1=luat_tp_data};
  75. luat_msgbus_put(&msg, 1);
  76. return 0;
  77. }
  78. }
  79. luat_tp_config->opts->read_done(luat_tp_config);
  80. return -1;
  81. }
  82. /*
  83. 触摸初始化
  84. @api tp.init(tp, args)
  85. @string tp类型,当前支持:<br>gt911 <br>gt9157 <br>jd9261t
  86. @table 附加参数,与具体设备有关:<br>port 驱动方式<br>port:硬件i2c端口,例如0,1,2...如果为软件i2c对象<br>pin_rst:复位引脚<br>pin_int:中断引脚<br>w:宽度<br>h:高度
  87. @function 回调函数, 回调参数:tp_device,tp_data:触摸数据,内部为多个触摸点数据表,每个表中有参数event:触摸事件 x:x坐标 y:y坐标
  88. @return tp_device
  89. @usage
  90. // tp.init("gt911",{port=0,pin_rst = 22,pin_int = 23,w = 320,h = 480})
  91. // local softI2C = i2c.createSoft(20, 21)
  92. // tp.init("gt911",{port=softI2C,pin_rst = 22,pin_int = 23,w = 320,h = 480})
  93. */
  94. static int l_tp_init(lua_State* L){
  95. int ret;
  96. size_t len = 0;
  97. luat_tp_config_t *luat_tp_config = (luat_tp_config_t *)luat_heap_malloc(sizeof(luat_tp_config_t));
  98. if (luat_tp_config == NULL) {
  99. LLOGE("out of system memory!!!");
  100. return 0;
  101. }
  102. memset(luat_tp_config, 0x00, sizeof(luat_tp_config_t));
  103. luat_tp_config->callback = l_tp_callback;
  104. luat_tp_config->pin_rst = LUAT_GPIO_NONE;
  105. #ifdef LUAT_USE_LCD
  106. luat_lcd_conf_t *lcd_conf = luat_lcd_get_default();
  107. if (lcd_conf){
  108. luat_tp_config->w = lcd_conf->w;
  109. luat_tp_config->h = lcd_conf->h;
  110. luat_tp_config->direction = lcd_conf->direction;
  111. if (lcd_conf->direction == LUAT_LCD_ROTATE_0 || lcd_conf->direction == LUAT_LCD_ROTATE_180){
  112. luat_tp_config->w = lcd_conf->w;
  113. luat_tp_config->h = lcd_conf->h;
  114. }else{
  115. luat_tp_config->w = lcd_conf->h;
  116. luat_tp_config->h = lcd_conf->w;
  117. }
  118. }
  119. #endif
  120. const char* tp_name = luaL_checklstring(L, 1, &len);
  121. for(int i = 0; i < 100; i++){
  122. if (strlen(tp_regs[i].name) == 0)
  123. break;
  124. if(strcmp(tp_regs[i].name,tp_name) == 0){
  125. luat_tp_config->opts = tp_regs[i].luat_tp_config;
  126. break;
  127. }
  128. }
  129. if (luat_tp_config->opts == NULL){
  130. LLOGE("tp_name:%s not found!!!", tp_name);
  131. return 0;
  132. }
  133. if (lua_isfunction(L, 3)) {
  134. lua_pushvalue(L, 3);
  135. luat_tp_config->luat_cb = luaL_ref(L, LUA_REGISTRYINDEX);
  136. }
  137. lua_settop(L, 2);
  138. lua_pushstring(L, "port");
  139. int port = lua_gettable(L, 2);
  140. if (LUA_TNUMBER == port) {
  141. luat_tp_config->i2c_id = luaL_checkinteger(L, -1);
  142. }else if(LUA_TUSERDATA == port){
  143. luat_tp_config->soft_i2c = (luat_ei2c_t*)lua_touserdata(L, -1);
  144. lua_pushvalue(L, -1);
  145. luat_tp_config->soft_i2c->ei2c_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  146. }else{
  147. LLOGE("port type error!!!");
  148. return 0;
  149. }
  150. lua_pop(L, 1);
  151. lua_pushstring(L, "pin_rst");
  152. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  153. luat_tp_config->pin_rst = luaL_checkinteger(L, -1);
  154. }
  155. lua_pop(L, 1);
  156. lua_pushstring(L, "pin_int");
  157. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  158. luat_tp_config->pin_int = luaL_checkinteger(L, -1);
  159. }
  160. lua_pop(L, 1);
  161. lua_pushstring(L, "w");
  162. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  163. luat_tp_config->w = luaL_checkinteger(L, -1);
  164. }
  165. lua_pop(L, 1);
  166. lua_pushstring(L, "h");
  167. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  168. luat_tp_config->h = luaL_checkinteger(L, -1);
  169. }
  170. lua_pop(L, 1);
  171. lua_pushstring(L, "refresh_rate");
  172. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  173. luat_tp_config->refresh_rate = luaL_checkinteger(L, -1);
  174. }
  175. lua_pop(L, 1);
  176. lua_pushstring(L, "int_type");
  177. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  178. luat_tp_config->int_type = luaL_checkinteger(L, -1);
  179. }
  180. lua_pop(L, 1);
  181. lua_pushstring(L, "tp_num");
  182. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  183. luat_tp_config->tp_num = luaL_checkinteger(L, -1);
  184. }
  185. lua_pop(L, 1);
  186. // lua_pushstring(L, "swap_xy");
  187. // if (LUA_TBOOLEAN == lua_gettable(L, 2)) {
  188. // luat_tp_config->swap_xy = lua_toboolean(L, -1);
  189. // }
  190. // lua_pop(L, 1);
  191. ret = luat_tp_init(luat_tp_config);
  192. if (ret){
  193. // luat_tp_deinit(luat_tp_config);
  194. return 0;
  195. }else{
  196. lua_pushlightuserdata(L, luat_tp_config);
  197. return 1;
  198. }
  199. }
  200. #include "rotable2.h"
  201. static const rotable_Reg_t reg_tp[] =
  202. {
  203. { "init", ROREG_FUNC(l_tp_init)},
  204. //@const EVENT_NONE number 空事件,不应出现
  205. { "EVENT_NONE", ROREG_INT(TP_EVENT_TYPE_NONE)},
  206. //@const EVENT_DOWN number 按下
  207. { "EVENT_DOWN", ROREG_INT(TP_EVENT_TYPE_DOWN)},
  208. //@const EVENT_UP number 抬起
  209. { "EVENT_UP", ROREG_INT(TP_EVENT_TYPE_UP)},
  210. //@const EVENT_MOVE number 移动
  211. { "EVENT_MOVE", ROREG_INT(TP_EVENT_TYPE_MOVE)},
  212. { "EVENT_TYPE_DOWN", ROREG_INT(TP_EVENT_TYPE_DOWN)},
  213. { "EVENT_TYPE_DOWN", ROREG_INT(TP_EVENT_TYPE_DOWN)},
  214. { "EVENT_TYPE_UP", ROREG_INT(TP_EVENT_TYPE_UP)},
  215. { "EVENT_TYPE_MOVE", ROREG_INT(TP_EVENT_TYPE_MOVE)},
  216. { "RISING", ROREG_INT(LUAT_GPIO_RISING_IRQ)},
  217. { "FALLING", ROREG_INT(LUAT_GPIO_FALLING_IRQ)},
  218. { NULL, ROREG_INT(0) }
  219. };
  220. LUAMOD_API int luaopen_tp(lua_State *L)
  221. {
  222. luat_newlib2(L, reg_tp);
  223. return 1;
  224. }