acid-drop

- Hacking the planet from a LilyGo T-Deck using custom firmware
git clone git://git.acid.vegas/acid-drop.git
Log | Files | Refs | Archive | README | LICENSE

lv_port_indev_template.c (10243B)

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