luat_lib_lvgl_indev_ext.c 4.8 KB

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