lv_port_indev.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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 = NULL;
  145. /*Initialize your touchpad*/
  146. static void touchpad_init(luat_tp_config_t *luat_tp_config)
  147. {
  148. /*Your code comes here*/
  149. lvgl_tp_data = luat_tp_config->tp_data;
  150. }
  151. /*Will be called by the library to read the touchpad*/
  152. static void touchpad_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data)
  153. {
  154. static lv_coord_t last_x = 0;
  155. static lv_coord_t last_y = 0;
  156. /*Save the pressed coordinates and the state*/
  157. if(touchpad_is_pressed()) {
  158. touchpad_get_xy(&last_x, &last_y);
  159. data->state = LV_INDEV_STATE_PR;
  160. }
  161. else {
  162. data->state = LV_INDEV_STATE_REL;
  163. }
  164. /*Set the last pressed coordinates*/
  165. data->point.x = last_x;
  166. data->point.y = last_y;
  167. }
  168. /*Return true is the touchpad is pressed*/
  169. static bool touchpad_is_pressed(void){
  170. /*Your code comes here*/
  171. if (lvgl_tp_data[0].event == TP_EVENT_TYPE_DOWN || lvgl_tp_data[0].event == TP_EVENT_TYPE_MOVE){
  172. return true;
  173. }
  174. return false;
  175. }
  176. /*Get the x and y coordinates if the touchpad is pressed*/
  177. static void touchpad_get_xy(lv_coord_t * x, lv_coord_t * y)
  178. {
  179. /*Your code comes here*/
  180. (*x) = lvgl_tp_data[0].x_coordinate;
  181. (*y) = lvgl_tp_data[0].y_coordinate;
  182. }
  183. /*------------------
  184. * Mouse
  185. * -----------------*/
  186. /*Initialize your mouse*/
  187. static void mouse_init(void)
  188. {
  189. /*Your code comes here*/
  190. }
  191. /*Will be called by the library to read the mouse*/
  192. static void mouse_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data)
  193. {
  194. /*Get the current x and y coordinates*/
  195. mouse_get_xy(&data->point.x, &data->point.y);
  196. /*Get whether the mouse button is pressed or released*/
  197. if(mouse_is_pressed()) {
  198. data->state = LV_INDEV_STATE_PR;
  199. }
  200. else {
  201. data->state = LV_INDEV_STATE_REL;
  202. }
  203. }
  204. /*Return true is the mouse button is pressed*/
  205. static bool mouse_is_pressed(void)
  206. {
  207. /*Your code comes here*/
  208. return false;
  209. }
  210. /*Get the x and y coordinates if the mouse is pressed*/
  211. static void mouse_get_xy(lv_coord_t * x, lv_coord_t * y)
  212. {
  213. /*Your code comes here*/
  214. (*x) = 0;
  215. (*y) = 0;
  216. }
  217. /*------------------
  218. * Keypad
  219. * -----------------*/
  220. /*Initialize your keypad*/
  221. static void keypad_init(void)
  222. {
  223. /*Your code comes here*/
  224. }
  225. /*Will be called by the library to read the mouse*/
  226. static void keypad_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data)
  227. {
  228. static uint32_t last_key = 0;
  229. /*Get the current x and y coordinates*/
  230. mouse_get_xy(&data->point.x, &data->point.y);
  231. /*Get whether the a key is pressed and save the pressed key*/
  232. uint32_t act_key = keypad_get_key();
  233. if(act_key != 0) {
  234. data->state = LV_INDEV_STATE_PR;
  235. /*Translate the keys to LVGL control characters according to your key definitions*/
  236. switch(act_key) {
  237. case 1:
  238. act_key = LV_KEY_NEXT;
  239. break;
  240. case 2:
  241. act_key = LV_KEY_PREV;
  242. break;
  243. case 3:
  244. act_key = LV_KEY_LEFT;
  245. break;
  246. case 4:
  247. act_key = LV_KEY_RIGHT;
  248. break;
  249. case 5:
  250. act_key = LV_KEY_ENTER;
  251. break;
  252. }
  253. last_key = act_key;
  254. }
  255. else {
  256. data->state = LV_INDEV_STATE_REL;
  257. }
  258. data->key = last_key;
  259. }
  260. /*Get the currently being pressed key. 0 if no key is pressed*/
  261. static uint32_t keypad_get_key(void)
  262. {
  263. /*Your code comes here*/
  264. return 0;
  265. }
  266. /*------------------
  267. * Encoder
  268. * -----------------*/
  269. /*Initialize your keypad*/
  270. static void encoder_init(void)
  271. {
  272. /*Your code comes here*/
  273. }
  274. /*Will be called by the library to read the encoder*/
  275. static void encoder_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data)
  276. {
  277. data->enc_diff = encoder_diff;
  278. data->state = encoder_state;
  279. }
  280. /*Call this function in an interrupt to process encoder events (turn, press)*/
  281. static void encoder_handler(void)
  282. {
  283. /*Your code comes here*/
  284. encoder_diff += 0;
  285. encoder_state = LV_INDEV_STATE_REL;
  286. }
  287. /*------------------
  288. * Button
  289. * -----------------*/
  290. /*Initialize your buttons*/
  291. static void button_init(void)
  292. {
  293. /*Your code comes here*/
  294. }
  295. /*Will be called by the library to read the button*/
  296. static void button_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data)
  297. {
  298. static uint8_t last_btn = 0;
  299. /*Get the pressed button's ID*/
  300. int8_t btn_act = button_get_pressed_id();
  301. if(btn_act >= 0) {
  302. data->state = LV_INDEV_STATE_PR;
  303. last_btn = btn_act;
  304. }
  305. else {
  306. data->state = LV_INDEV_STATE_REL;
  307. }
  308. /*Save the last pressed button's ID*/
  309. data->btn_id = last_btn;
  310. }
  311. /*Get ID (0, 1, 2 ..) of the pressed button*/
  312. static int8_t button_get_pressed_id(void)
  313. {
  314. uint8_t i;
  315. /*Check to buttons see which is being pressed (assume there are 2 buttons)*/
  316. for(i = 0; i < 2; i++) {
  317. /*Return the pressed button's ID*/
  318. if(button_is_pressed(i)) {
  319. return i;
  320. }
  321. }
  322. /*No button pressed*/
  323. return -1;
  324. }
  325. /*Test if `id` button is pressed or not*/
  326. static bool button_is_pressed(uint8_t id)
  327. {
  328. /*Your code comes here*/
  329. return false;
  330. }
  331. #else /*Enable this file at the top*/
  332. /*This dummy typedef exists purely to silence -Wpedantic.*/
  333. typedef int keep_pedantic_happy;
  334. #endif