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