luat_lib_lvgl_indev_ext.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. @module lvgl
  3. @summary LVGL图像库
  4. @version 1.0
  5. @date 2021.06.01
  6. */
  7. #include "luat_base.h"
  8. #include "luat_lvgl.h"
  9. #include "lvgl.h"
  10. #include "luat_mem.h"
  11. static lv_indev_data_t point_emulator_data = {0};
  12. static lv_indev_data_t keyboard_emulator_data = {0};
  13. static bool point_input_read(lv_indev_drv_t * drv, lv_indev_data_t*data) {
  14. memcpy(data, drv->user_data, sizeof(lv_indev_data_t));
  15. // if (((lv_indev_data_t*)drv->user_data)->state == LV_INDEV_STATE_PR){
  16. // ((lv_indev_data_t*)drv->user_data)->state == LV_INDEV_STATE_REL;
  17. // }
  18. return false;
  19. }
  20. #ifdef LUAT_USE_TP
  21. #include "luat_tp.h"
  22. static luat_tp_data_t* lvgl_tp_data = NULL;
  23. /*Return true is the touchpad is pressed*/
  24. static bool touchpad_is_pressed(void){
  25. /*Your code comes here*/
  26. if (lvgl_tp_data[0].event == TP_EVENT_TYPE_DOWN || lvgl_tp_data[0].event == TP_EVENT_TYPE_MOVE){
  27. return true;
  28. }
  29. return false;
  30. }
  31. static void touchpad_get_xy(lv_coord_t * x, lv_coord_t * y)
  32. {
  33. /*Your code comes here*/
  34. (*x) = lvgl_tp_data[0].x_coordinate;
  35. (*y) = lvgl_tp_data[0].y_coordinate;
  36. }
  37. static bool touch_input_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data)
  38. {
  39. static lv_coord_t last_x = 0;
  40. static lv_coord_t last_y = 0;
  41. /*Save the pressed coordinates and the state*/
  42. if(touchpad_is_pressed()) {
  43. touchpad_get_xy(&last_x, &last_y);
  44. data->state = LV_INDEV_STATE_PR;
  45. }
  46. else {
  47. data->state = LV_INDEV_STATE_REL;
  48. }
  49. /*Set the last pressed coordinates*/
  50. data->point.x = last_x;
  51. data->point.y = last_y;
  52. /*Return `false` because we are not buffering and no more data to read*/
  53. return false;
  54. }
  55. #endif
  56. /*
  57. 注册输入设备驱动
  58. @api lvgl.indev_drv_register(tp, dtp)
  59. @string 设备类型,当前支持"pointer",指针类/触摸类均可,"keyboard",键盘类型
  60. @string 设备型号,当前支持"emulator",模拟器类型
  61. @return bool 成功返回true,否则返回false
  62. @usage
  63. lvgl.indev_drv_register("pointer", "emulator")
  64. */
  65. int luat_lv_indev_drv_register(lua_State* L) {
  66. lv_indev_drv_t indev_drv;
  67. lv_indev_drv_init(&indev_drv);
  68. const char* type = luaL_checkstring(L, 1);
  69. int ok = 0;
  70. const char* dtype;
  71. if (!strcmp("pointer", type)) {
  72. indev_drv.type = LV_INDEV_TYPE_POINTER;
  73. dtype = luaL_checkstring(L, 2);
  74. if (!strcmp("emulator", dtype)) {
  75. indev_drv.user_data = &point_emulator_data;
  76. memset(indev_drv.user_data, 0, sizeof(lv_indev_data_t));
  77. indev_drv.read_cb = point_input_read;
  78. lv_indev_drv_register(&indev_drv);
  79. ok = 1;
  80. }else if(!strcmp("touch", dtype)) {
  81. #ifdef LUAT_USE_TP
  82. // indev_drv.user_data = &point_emulator_data;
  83. // memset(indev_drv.user_data, 0, sizeof(lv_indev_data_t));
  84. if (lua_isuserdata(L, 3)) {
  85. luat_tp_config_t *luat_tp_config = lua_touserdata(L, 3);
  86. lvgl_tp_data = luat_tp_config->tp_data;
  87. luat_tp_config->callback = NULL;
  88. indev_drv.read_cb = touch_input_read;
  89. lv_indev_drv_register(&indev_drv);
  90. ok = 1;
  91. }else {
  92. // log_e("touch input need tp");
  93. }
  94. #endif
  95. }
  96. //else if(!strcmp("xpt2046", type)) {
  97. // // TODO 支持xpt2046?
  98. //}
  99. }
  100. else if (!strcmp("keyboard", type)) {
  101. indev_drv.type = LV_INDEV_TYPE_KEYPAD;
  102. //dtype = luaL_checkstring(L, 2);
  103. //if (!strcmp("emulator", dtype)) {
  104. {
  105. indev_drv.user_data = &keyboard_emulator_data;
  106. memset(indev_drv.user_data, 0, sizeof(lv_indev_data_t));
  107. indev_drv.read_cb = point_input_read;
  108. lv_indev_drv_register(&indev_drv);
  109. ok = 1;
  110. }
  111. }
  112. lua_pushboolean(L, ok);
  113. return 1;
  114. }
  115. /*
  116. 更新模拟输入设备的坐标数据
  117. @api lvgl.indev_point_emulator_update(x, y, state)
  118. @int x坐标,以左上角为0,右下角为最大值
  119. @int y坐标,以左上角为0,右下角为最大值
  120. @int 状态, 0 为 释放, 1 为按下
  121. @return nil 无返回值
  122. @usage
  123. -- 模拟在屏幕上的点击,通过timeout模拟长按和短按
  124. sys.taskInit(function(x, y, timeout)
  125. lvgl.indev_point_emulator_update(x, y, 1)
  126. sys.wait(timeout)
  127. lvgl.indev_point_emulator_update(x, y, 0)
  128. end, 240, 120, 50)
  129. */
  130. int luat_lv_indev_point_emulator_update(lua_State* L) {
  131. int x = luaL_checkinteger(L, 1);
  132. int y = luaL_checkinteger(L, 2);
  133. int state = luaL_optinteger(L, 3, 1);
  134. point_emulator_data.point.x = x;
  135. point_emulator_data.point.y = y;
  136. point_emulator_data.state = state;
  137. return 0;
  138. }
  139. /*
  140. 更新键盘输入设备的按键值
  141. @api lvgl.indev_kb_update(key)
  142. @int 按键值,默认为0,按键抬起
  143. @return nil 无返回值
  144. @usage
  145. */
  146. int luat_lv_indev_keyboard_update(lua_State* L) {
  147. int key = luaL_optinteger(L, 1, 0);
  148. keyboard_emulator_data.key = key;
  149. return 0;
  150. }