luat_lib_tp.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #include "luat_base.h"
  2. #include "luat_tp.h"
  3. #include "luat_msgbus.h"
  4. #include "luat_gpio.h"
  5. #define LUAT_LOG_TAG "tp"
  6. #include "luat_log.h"
  7. // tp.init(ic_name,{port,pin_rst,pin_int,w,h,int_type,})
  8. // local softI2C = i2c.createSoft(20, 21)
  9. // tp.init("gt911",{port=softI2C,pin_rst = 22,pin_int = 23,w = 320,h = 480})
  10. // tp.init("gt911",{port=0,pin_rst = 22,pin_int = 23,w = 320,h = 480})
  11. typedef struct tp_reg {
  12. const char *name;
  13. const luat_tp_config_t *luat_tp_config;
  14. }tp_reg_t;
  15. static const tp_reg_t tp_regs[] = {
  16. {"gt911", &tp_config_gt911},
  17. {"", NULL}
  18. };
  19. static int l_tp_handler(lua_State* L, void* ptr) {
  20. rtos_msg_t *msg = (rtos_msg_t *)lua_topointer(L, -1);
  21. luat_tp_config_t* luat_tp_config = msg->ptr;
  22. luat_tp_data_t* luat_tp_data = msg->arg1;
  23. if (luat_tp_config->luat_cb) {
  24. lua_geti(L, LUA_REGISTRYINDEX, luat_tp_config->luat_cb);
  25. if (lua_isfunction(L, -1)) {
  26. lua_pushlightuserdata(L, luat_tp_config);
  27. lua_newtable(L);
  28. for (uint8_t i=0; i<LUAT_TP_TOUCH_MAX; i++){
  29. 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)){
  30. lua_newtable(L);
  31. lua_pushstring(L, "event");
  32. lua_pushinteger(L, luat_tp_data[i].event);
  33. lua_settable(L, -3);
  34. lua_pushstring(L, "track_id");
  35. lua_pushinteger(L, luat_tp_data[i].track_id);
  36. lua_settable(L, -3);
  37. lua_pushstring(L, "x");
  38. lua_pushinteger(L, luat_tp_data[i].x_coordinate);
  39. lua_settable(L, -3);
  40. lua_pushstring(L, "y");
  41. lua_pushinteger(L, luat_tp_data[i].y_coordinate);
  42. lua_settable(L, -3);
  43. lua_pushstring(L, "width");
  44. lua_pushinteger(L, luat_tp_data[i].width);
  45. lua_settable(L, -3);
  46. lua_pushstring(L, "timestamp");
  47. lua_pushinteger(L, luat_tp_data[i].timestamp);
  48. lua_settable(L, -3);
  49. lua_pushinteger(L, i + 1);
  50. lua_insert(L, -2);
  51. lua_settable(L, -3);
  52. }
  53. }
  54. lua_call(L, 2, 0);
  55. }
  56. }
  57. luat_gpio_irq_enable(luat_tp_config->pin_int, 1);
  58. return 0;
  59. }
  60. int l_tp_callback(luat_tp_config_t* luat_tp_config, luat_tp_data_t* luat_tp_data){
  61. rtos_msg_t msg = {.handler = l_tp_handler, .ptr=luat_tp_config, .arg1=luat_tp_data};
  62. luat_msgbus_put(&msg, 1);
  63. return 0;
  64. }
  65. static int l_tp_init(lua_State* L){
  66. int ret;
  67. size_t len = 0;
  68. luat_tp_config_t *luat_tp_config = (luat_tp_config_t *)lua_newuserdata(L, sizeof(luat_tp_config_t));
  69. if (luat_tp_config == NULL) {
  70. LLOGE("out of system memory!!!");
  71. return 0;
  72. }
  73. memset(luat_tp_config, 0x00, sizeof(luat_tp_config_t));
  74. luat_tp_config->callback = l_tp_callback;
  75. const char* tp_name = luaL_checklstring(L, 1, &len);
  76. for(int i = 0; i < 100; i++){
  77. if (strlen(tp_regs[i].name) == 0)
  78. break;
  79. if(strcmp(tp_regs[i].name,tp_name) == 0){
  80. luat_tp_config->opts = tp_regs[i].luat_tp_config;
  81. break;
  82. }
  83. }
  84. if (luat_tp_config->opts == NULL){
  85. LLOGE("tp_name:%s not found!!!", tp_name);
  86. return 0;
  87. }
  88. if (lua_isfunction(L, 3)) {
  89. lua_pushvalue(L, 3);
  90. luat_tp_config->luat_cb = luaL_ref(L, LUA_REGISTRYINDEX);
  91. }
  92. lua_settop(L, 2);
  93. lua_pushstring(L, "port");
  94. int port = lua_gettable(L, 2);
  95. if (LUA_TNUMBER == port) {
  96. luat_tp_config->i2c_id = luaL_checkinteger(L, -1);
  97. }else if(LUA_TUSERDATA == port){
  98. luat_tp_config->soft_i2c = (luat_ei2c_t*)lua_touserdata(L, -1);
  99. }else{
  100. LLOGE("port type error!!!");
  101. return 0;
  102. }
  103. lua_pop(L, 1);
  104. lua_pushstring(L, "pin_rst");
  105. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  106. luat_tp_config->pin_rst = luaL_checkinteger(L, -1);
  107. }
  108. lua_pop(L, 1);
  109. lua_pushstring(L, "pin_int");
  110. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  111. luat_tp_config->pin_int = luaL_checkinteger(L, -1);
  112. }
  113. lua_pop(L, 1);
  114. lua_pushstring(L, "w");
  115. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  116. luat_tp_config->w = luaL_checkinteger(L, -1);
  117. }
  118. lua_pop(L, 1);
  119. lua_pushstring(L, "h");
  120. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  121. luat_tp_config->h = luaL_checkinteger(L, -1);
  122. }
  123. lua_pop(L, 1);
  124. lua_pushstring(L, "refresh_rate");
  125. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  126. luat_tp_config->refresh_rate = luaL_checkinteger(L, -1);
  127. }
  128. lua_pop(L, 1);
  129. lua_pushstring(L, "int_type");
  130. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  131. luat_tp_config->int_type = luaL_checkinteger(L, -1);
  132. }
  133. lua_pop(L, 1);
  134. lua_pushstring(L, "tp_num");
  135. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  136. luat_tp_config->tp_num = luaL_checkinteger(L, -1);
  137. }
  138. lua_pop(L, 1);
  139. // luat_tp_config->luat_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  140. ret = luat_tp_init(luat_tp_config);
  141. if (ret){
  142. // luat_tp_deinit(luat_tp_config);
  143. // luaL_unref(L, LUA_REGISTRYINDEX, luat_tp_config->luat_ref);
  144. return 0;
  145. }else{
  146. lua_pushlightuserdata(L, luat_tp_config);
  147. return 1;
  148. }
  149. }
  150. #include "rotable2.h"
  151. static const rotable_Reg_t reg_tp[] =
  152. {
  153. { "init", ROREG_FUNC(l_tp_init)},
  154. { "RISING", ROREG_INT(TP_INT_TYPE_RISING_EDGE)},
  155. { "FALLING", ROREG_INT(TP_INT_TYPE_FALLING_EDGE)},
  156. { NULL, ROREG_INT(0) }
  157. };
  158. LUAMOD_API int luaopen_tp(lua_State *L)
  159. {
  160. luat_newlib2(L, reg_tp);
  161. return 1;
  162. }