lv_port_indev.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /**
  2. * @file lv_port_indev_templ.c
  3. *
  4. */
  5. /*Copy this file as "lv_port_indev.c" and set this value to "1" to enable content*/
  6. #if 1
  7. /*********************
  8. * INCLUDES
  9. *********************/
  10. #include "lv_port_indev.h"
  11. /*********************
  12. * DEFINES
  13. *********************/
  14. /**********************
  15. * TYPEDEFS
  16. **********************/
  17. /**********************
  18. * STATIC PROTOTYPES
  19. **********************/
  20. static void touchpad_init(luat_tp_config_t *luat_tp_config);
  21. static void touchpad_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data);
  22. static bool touchpad_is_pressed(void);
  23. static void touchpad_get_xy(lv_coord_t * x, lv_coord_t * y);
  24. static void mouse_init(void);
  25. static void mouse_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data);
  26. static bool mouse_is_pressed(void);
  27. static void mouse_get_xy(lv_coord_t * x, lv_coord_t * y);
  28. static void keypad_init(void);
  29. static void keypad_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data);
  30. static uint32_t keypad_get_key(void);
  31. static void encoder_init(void);
  32. static void encoder_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data);
  33. static void encoder_handler(void);
  34. static void button_init(void);
  35. static void button_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data);
  36. static int8_t button_get_pressed_id(void);
  37. static bool button_is_pressed(uint8_t id);
  38. /**********************
  39. * STATIC VARIABLES
  40. **********************/
  41. lv_indev_t * indev_touchpad;
  42. lv_indev_t * indev_mouse;
  43. lv_indev_t * indev_keypad;
  44. lv_indev_t * indev_encoder;
  45. lv_indev_t * indev_button;
  46. static int32_t encoder_diff;
  47. static lv_indev_state_t encoder_state;
  48. /**********************
  49. * MACROS
  50. **********************/
  51. /**********************
  52. * GLOBAL FUNCTIONS
  53. **********************/
  54. void lv_port_indev_init(luat_tp_config_t *luat_tp_config)
  55. {
  56. /**
  57. * Here you will find example implementation of input devices supported by LittelvGL:
  58. * - Touchpad
  59. * - Mouse (with cursor support)
  60. * - Keypad (supports GUI usage only with key)
  61. * - Encoder (supports GUI usage only with: left, right, push)
  62. * - Button (external buttons to press points on the screen)
  63. *
  64. * The `..._read()` function are only examples.
  65. * You should shape them according to your hardware
  66. */
  67. static lv_indev_drv_t indev_drv;
  68. /*------------------
  69. * Touchpad
  70. * -----------------*/
  71. /*Initialize your touchpad if you have*/
  72. touchpad_init(luat_tp_config);
  73. /*Register a touchpad input device*/
  74. lv_indev_drv_init(&indev_drv);
  75. indev_drv.user_data = luat_tp_config;
  76. indev_drv.type = LV_INDEV_TYPE_POINTER;
  77. indev_drv.read_cb = touchpad_read;
  78. indev_touchpad = lv_indev_drv_register(&indev_drv);
  79. // /*------------------
  80. // * Mouse
  81. // * -----------------*/
  82. // /*Initialize your mouse if you have*/
  83. // mouse_init();
  84. // /*Register a mouse input device*/
  85. // lv_indev_drv_init(&indev_drv);
  86. // indev_drv.type = LV_INDEV_TYPE_POINTER;
  87. // indev_drv.read_cb = mouse_read;
  88. // indev_mouse = lv_indev_drv_register(&indev_drv);
  89. // /*Set cursor. For simplicity set a HOME symbol now.*/
  90. // lv_obj_t * mouse_cursor = lv_img_create(lv_scr_act());
  91. // lv_img_set_src(mouse_cursor, LV_SYMBOL_HOME);
  92. // lv_indev_set_cursor(indev_mouse, mouse_cursor);
  93. // /*------------------
  94. // * Keypad
  95. // * -----------------*/
  96. // /*Initialize your keypad or keyboard if you have*/
  97. // keypad_init();
  98. // /*Register a keypad input device*/
  99. // lv_indev_drv_init(&indev_drv);
  100. // indev_drv.type = LV_INDEV_TYPE_KEYPAD;
  101. // indev_drv.read_cb = keypad_read;
  102. // indev_keypad = lv_indev_drv_register(&indev_drv);
  103. // /*Later you should create group(s) with `lv_group_t * group = lv_group_create()`,
  104. // *add objects to the group with `lv_group_add_obj(group, obj)`
  105. // *and assign this input device to group to navigate in it:
  106. // *`lv_indev_set_group(indev_keypad, group);`*/
  107. // /*------------------
  108. // * Encoder
  109. // * -----------------*/
  110. // /*Initialize your encoder if you have*/
  111. // encoder_init();
  112. // /*Register a encoder input device*/
  113. // lv_indev_drv_init(&indev_drv);
  114. // indev_drv.type = LV_INDEV_TYPE_ENCODER;
  115. // indev_drv.read_cb = encoder_read;
  116. // indev_encoder = lv_indev_drv_register(&indev_drv);
  117. // /*Later you should create group(s) with `lv_group_t * group = lv_group_create()`,
  118. // *add objects to the group with `lv_group_add_obj(group, obj)`
  119. // *and assign this input device to group to navigate in it:
  120. // *`lv_indev_set_group(indev_encoder, group);`*/
  121. // /*------------------
  122. // * Button
  123. // * -----------------*/
  124. // /*Initialize your button if you have*/
  125. // button_init();
  126. // /*Register a button input device*/
  127. // lv_indev_drv_init(&indev_drv);
  128. // indev_drv.type = LV_INDEV_TYPE_BUTTON;
  129. // indev_drv.read_cb = button_read;
  130. // indev_button = lv_indev_drv_register(&indev_drv);
  131. // /*Assign buttons to points on the screen*/
  132. // static const lv_point_t btn_points[2] = {
  133. // {10, 10}, /*Button 0 -> x:10; y:10*/
  134. // {40, 100}, /*Button 1 -> x:40; y:100*/
  135. // };
  136. // lv_indev_set_button_points(indev_button, btn_points);
  137. }
  138. /**********************
  139. * STATIC FUNCTIONS
  140. **********************/
  141. /*------------------
  142. * Touchpad
  143. * -----------------*/
  144. static luat_tp_data_t lvgl_tp_data[LUAT_TP_TOUCH_MAX]={0};
  145. static int luat_tp_callback(luat_tp_config_t* luat_tp_config, luat_tp_data_t* luat_tp_data){
  146. memcpy(lvgl_tp_data, luat_tp_data, sizeof(luat_tp_data_t)*LUAT_TP_TOUCH_MAX);
  147. luat_tp_irq_enable(luat_tp_config, 1);
  148. return 0;
  149. }
  150. /*Initialize your touchpad*/
  151. static void touchpad_init(luat_tp_config_t *luat_tp_config)
  152. {
  153. /*Your code comes here*/
  154. luat_tp_config->callback = luat_tp_callback;
  155. }
  156. /*Will be called by the library to read the touchpad*/
  157. static void touchpad_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data)
  158. {
  159. static lv_coord_t last_x = 0;
  160. static lv_coord_t last_y = 0;
  161. /*Save the pressed coordinates and the state*/
  162. if(touchpad_is_pressed()) {
  163. touchpad_get_xy(&last_x, &last_y);
  164. data->state = LV_INDEV_STATE_PR;
  165. }
  166. else {
  167. data->state = LV_INDEV_STATE_REL;
  168. }
  169. /*Set the last pressed coordinates*/
  170. data->point.x = last_x;
  171. data->point.y = last_y;
  172. }
  173. /*Return true is the touchpad is pressed*/
  174. static bool touchpad_is_pressed(void)
  175. {
  176. /*Your code comes here*/
  177. if (lvgl_tp_data[0].event == TP_EVENT_TYPE_DOWN || lvgl_tp_data[0].event == TP_EVENT_TYPE_MOVE){
  178. return true;
  179. }
  180. return false;
  181. }
  182. /*Get the x and y coordinates if the touchpad is pressed*/
  183. static void touchpad_get_xy(lv_coord_t * x, lv_coord_t * y)
  184. {
  185. /*Your code comes here*/
  186. (*x) = lvgl_tp_data[0].x_coordinate;
  187. (*y) = lvgl_tp_data[0].y_coordinate;
  188. }
  189. /*------------------
  190. * Mouse
  191. * -----------------*/
  192. /*Initialize your mouse*/
  193. static void mouse_init(void)
  194. {
  195. /*Your code comes here*/
  196. }
  197. /*Will be called by the library to read the mouse*/
  198. static void mouse_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data)
  199. {
  200. /*Get the current x and y coordinates*/
  201. mouse_get_xy(&data->point.x, &data->point.y);
  202. /*Get whether the mouse button is pressed or released*/
  203. if(mouse_is_pressed()) {
  204. data->state = LV_INDEV_STATE_PR;
  205. }
  206. else {
  207. data->state = LV_INDEV_STATE_REL;
  208. }
  209. }
  210. /*Return true is the mouse button is pressed*/
  211. static bool mouse_is_pressed(void)
  212. {
  213. /*Your code comes here*/
  214. return false;
  215. }
  216. /*Get the x and y coordinates if the mouse is pressed*/
  217. static void mouse_get_xy(lv_coord_t * x, lv_coord_t * y)
  218. {
  219. /*Your code comes here*/
  220. (*x) = 0;
  221. (*y) = 0;
  222. }
  223. /*------------------
  224. * Keypad
  225. * -----------------*/
  226. /*Initialize your keypad*/
  227. static void keypad_init(void)
  228. {
  229. /*Your code comes here*/
  230. }
  231. /*Will be called by the library to read the mouse*/
  232. static void keypad_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data)
  233. {
  234. static uint32_t last_key = 0;
  235. /*Get the current x and y coordinates*/
  236. mouse_get_xy(&data->point.x, &data->point.y);
  237. /*Get whether the a key is pressed and save the pressed key*/
  238. uint32_t act_key = keypad_get_key();
  239. if(act_key != 0) {
  240. data->state = LV_INDEV_STATE_PR;
  241. /*Translate the keys to LVGL control characters according to your key definitions*/
  242. switch(act_key) {
  243. case 1:
  244. act_key = LV_KEY_NEXT;
  245. break;
  246. case 2:
  247. act_key = LV_KEY_PREV;
  248. break;
  249. case 3:
  250. act_key = LV_KEY_LEFT;
  251. break;
  252. case 4:
  253. act_key = LV_KEY_RIGHT;
  254. break;
  255. case 5:
  256. act_key = LV_KEY_ENTER;
  257. break;
  258. }
  259. last_key = act_key;
  260. }
  261. else {
  262. data->state = LV_INDEV_STATE_REL;
  263. }
  264. data->key = last_key;
  265. }
  266. /*Get the currently being pressed key. 0 if no key is pressed*/
  267. static uint32_t keypad_get_key(void)
  268. {
  269. /*Your code comes here*/
  270. return 0;
  271. }
  272. /*------------------
  273. * Encoder
  274. * -----------------*/
  275. /*Initialize your keypad*/
  276. static void encoder_init(void)
  277. {
  278. /*Your code comes here*/
  279. }
  280. /*Will be called by the library to read the encoder*/
  281. static void encoder_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data)
  282. {
  283. data->enc_diff = encoder_diff;
  284. data->state = encoder_state;
  285. }
  286. /*Call this function in an interrupt to process encoder events (turn, press)*/
  287. static void encoder_handler(void)
  288. {
  289. /*Your code comes here*/
  290. encoder_diff += 0;
  291. encoder_state = LV_INDEV_STATE_REL;
  292. }
  293. /*------------------
  294. * Button
  295. * -----------------*/
  296. /*Initialize your buttons*/
  297. static void button_init(void)
  298. {
  299. /*Your code comes here*/
  300. }
  301. /*Will be called by the library to read the button*/
  302. static void button_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data)
  303. {
  304. static uint8_t last_btn = 0;
  305. /*Get the pressed button's ID*/
  306. int8_t btn_act = button_get_pressed_id();
  307. if(btn_act >= 0) {
  308. data->state = LV_INDEV_STATE_PR;
  309. last_btn = btn_act;
  310. }
  311. else {
  312. data->state = LV_INDEV_STATE_REL;
  313. }
  314. /*Save the last pressed button's ID*/
  315. data->btn_id = last_btn;
  316. }
  317. /*Get ID (0, 1, 2 ..) of the pressed button*/
  318. static int8_t button_get_pressed_id(void)
  319. {
  320. uint8_t i;
  321. /*Check to buttons see which is being pressed (assume there are 2 buttons)*/
  322. for(i = 0; i < 2; i++) {
  323. /*Return the pressed button's ID*/
  324. if(button_is_pressed(i)) {
  325. return i;
  326. }
  327. }
  328. /*No button pressed*/
  329. return -1;
  330. }
  331. /*Test if `id` button is pressed or not*/
  332. static bool button_is_pressed(uint8_t id)
  333. {
  334. /*Your code comes here*/
  335. return false;
  336. }
  337. #else /*Enable this file at the top*/
  338. /*This dummy typedef exists purely to silence -Wpedantic.*/
  339. typedef int keep_pedantic_happy;
  340. #endif