/** * @file lv_port_indev_templ.c * */ /*Copy this file as "lv_port_indev.c" and set this value to "1" to enable content*/ #if 1 /********************* * INCLUDES *********************/ #include "Board_include.h" #include "lv_port_indev.h" #include "lvgl.h" #include "lvgl_src.h" /********************* * DEFINES *********************/ /********************** * TYPEDEFS **********************/ /********************** * STATIC PROTOTYPES **********************/ void touchpad_init(void); void touchpad_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data); bool touchpad_is_pressed(void); void touchpad_get_xy(lv_coord_t * x, lv_coord_t * y); void mouse_init(void); void mouse_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data); bool mouse_is_pressed(void); void mouse_get_xy(lv_coord_t * x, lv_coord_t * y); void keypad_init(void); void keypad_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data); uint32_t keypad_get_key(void); void encoder_init(void); void encoder_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data); void encoder_handler(void); void button_init(void); void button_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data); int8_t button_get_pressed_id(void); bool button_is_pressed(uint8_t id); /********************** * STATIC VARIABLES **********************/ lv_indev_t * indev_touchpad; lv_indev_t * indev_mouse; lv_indev_t * indev_keypad; lv_indev_t * indev_encoder; lv_indev_t * indev_button; static int32_t encoder_diff; static lv_indev_state_t encoder_state; /********************** * MACROS **********************/ /********************** * GLOBAL FUNCTIONS **********************/ void lv_port_indev_init(void) { /** * Here you will find example implementation of input devices supported by LittelvGL: * - Touchpad * - Mouse (with cursor support) * - Keypad (supports GUI usage only with key) * - Encoder (supports GUI usage only with: left, right, push) * - Button (external buttons to press points on the screen) * * The `..._read()` function are only examples. * You should shape them according to your hardware */ static lv_indev_drv_t indev_drv_tp,indev_drv_kb; /*------------------ * Touchpad * -----------------*/ /*Initialize your touchpad if you have*/ touchpad_init(); /*Register a touchpad input device*/ lv_indev_drv_init(&indev_drv_tp); indev_drv_tp.type = LV_INDEV_TYPE_POINTER; indev_drv_tp.read_cb = touchpad_read; indev_touchpad = lv_indev_drv_register(&indev_drv_tp); /*------------------ * Mouse * -----------------*/ /*Initialize your mouse if you have*/ // mouse_init(); // // /*Register a mouse input device*/ // lv_indev_drv_init(&indev_drv); // indev_drv.type = LV_INDEV_TYPE_POINTER; // indev_drv.read_cb = mouse_read; //// indev_mouse = lv_indev_drv_register(&indev_drv); // // /*Set cursor. For simplicity set a HOME symbol now.*/ // lv_obj_t * mouse_cursor = lv_img_create(lv_scr_act()); // lv_img_set_src(mouse_cursor, LV_SYMBOL_HOME); // lv_indev_set_cursor(indev_mouse, mouse_cursor); // #if BSP_USE_KEY == 1 /*------------------ * Keypad * -----------------*/ /*Initialize your keypad or keyboard if you have*/ keypad_init(); /*Register a keypad input device*/ lv_indev_drv_init(&indev_drv_kb); indev_drv_kb.type = LV_INDEV_TYPE_KEYPAD; indev_drv_kb.read_cb = keypad_read; indev_keypad = lv_indev_drv_register(&indev_drv_kb); /*Later you should create group(s) with `lv_group_t * group = lv_group_create()`, *add objects to the group with `lv_group_add_obj(group, obj)` *and assign this input device to group to navigate in it: *`lv_indev_set_group(indev_keypad, group);`*/ #endif // /*------------------ // * Encoder // * -----------------*/ // // /*Initialize your encoder if you have*/ // encoder_init(); // // /*Register a encoder input device*/ // lv_indev_drv_init(&indev_drv); // indev_drv.type = LV_INDEV_TYPE_ENCODER; // indev_drv.read_cb = encoder_read; //// indev_encoder = lv_indev_drv_register(&indev_drv); // // /*Later you should create group(s) with `lv_group_t * group = lv_group_create()`, // *add objects to the group with `lv_group_add_obj(group, obj)` // *and assign this input device to group to navigate in it: // *`lv_indev_set_group(indev_encoder, group);`*/ // // /*------------------ // * Button // * -----------------*/ // // /*Initialize your button if you have*/ // button_init(); // // /*Register a button input device*/ // lv_indev_drv_init(&indev_drv); // indev_drv.type = LV_INDEV_TYPE_BUTTON; // indev_drv.read_cb = button_read; //// indev_button = lv_indev_drv_register(&indev_drv); // // /*Assign buttons to points on the screen*/ // static const lv_point_t btn_points[2] = { // {10, 10}, /*Button 0 -> x:10; y:10*/ // {40, 100}, /*Button 1 -> x:40; y:100*/ // }; // lv_indev_set_button_points(indev_button, btn_points); } /********************** * STATIC FUNCTIONS **********************/ /*------------------ * Touchpad * -----------------*/ /*Initialize your touchpad*/ void touchpad_init(void) { /*Your code comes here*/ TP_Init(); } /*Will be called by the library to read the touchpad*/ void touchpad_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data) { static lv_coord_t last_x = 0; static lv_coord_t last_y = 0; /*Save the pressed coordinates and the state*/ if(touchpad_is_pressed()) { touchpad_get_xy(&last_x, &last_y); data->state = LV_INDEV_STATE_PR; } else { data->state = LV_INDEV_STATE_REL; } /*Set the last pressed coordinates*/ data->point.x = last_x; data->point.y = last_y; } /*Return true is the touchpad is pressed*/ bool touchpad_is_pressed(void) { /*Your code comes here*/ return TP_Pressed(); // return false; } /*Get the x and y coordinates if the touchpad is pressed*/ void touchpad_get_xy(lv_coord_t * x, lv_coord_t * y) { /*Your code comes here*/ TP_ScanXY(); (*x) = tp_dev.x[0]; (*y) = tp_dev.y[0]; // (*x) = 0; // (*y) = 0; } /*------------------ * Mouse * -----------------*/ /*Initialize your mouse*/ void mouse_init(void) { /*Your code comes here*/ } /*Will be called by the library to read the mouse*/ void mouse_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data) { /*Get the current x and y coordinates*/ mouse_get_xy(&data->point.x, &data->point.y); /*Get whether the mouse button is pressed or released*/ if(mouse_is_pressed()) { data->state = LV_INDEV_STATE_PR; } else { data->state = LV_INDEV_STATE_REL; } } /*Return true is the mouse button is pressed*/ bool mouse_is_pressed(void) { /*Your code comes here*/ return false; } /*Get the x and y coordinates if the mouse is pressed*/ void mouse_get_xy(lv_coord_t * x, lv_coord_t * y) { /*Your code comes here*/ (*x) = 0; (*y) = 0; } /*------------------ * Keypad * -----------------*/ /*Initialize your keypad*/ void keypad_init(void) { /*Your code comes here*/ #if BSP_USE_KEY == 1 InitKeyBoard(); #endif } /*Will be called by the library to read the mouse*/ void keypad_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data) { static uint32_t last_key = 0; #if BSP_USE_KEY == 1 /*Get the current x and y coordinates*/ mouse_get_xy(&data->point.x, &data->point.y); /*Get whether the a key is pressed and save the pressed key*/ uint32_t act_key = keypad_get_key(); if(act_key != 0) { data->state = LV_INDEV_STATE_PR; /*Translate the keys to LVGL control characters according to your key definitions*/ switch(act_key) { case KEY_DOWN: act_key = LV_KEY_NEXT; break; case KEY_UP: act_key = LV_KEY_PREV; break; case KEY_LEFT: act_key = LV_KEY_LEFT; break; case KEY_RIGHT: act_key = LV_KEY_RIGHT; break; case KEY_ENTER: act_key = LV_KEY_ENTER; break; case KEY_ESC: act_key = LV_KEY_ESC; break; } last_key = act_key; } else { data->state = LV_INDEV_STATE_REL; } #endif data->key = last_key; } /*Get the currently being pressed key. 0 if no key is pressed*/ uint32_t keypad_get_key(void) { /*Your code comes here*/ #if BSP_USE_KEY == 1 return KeyPressed(); #else return 0; #endif } /*------------------ * Encoder * -----------------*/ /*Initialize your keypad*/ void encoder_init(void) { /*Your code comes here*/ } /*Will be called by the library to read the encoder*/ void encoder_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data) { data->enc_diff = encoder_diff; data->state = encoder_state; } /*Call this function in an interrupt to process encoder events (turn, press)*/ void encoder_handler(void) { /*Your code comes here*/ encoder_diff += 0; encoder_state = LV_INDEV_STATE_REL; } /*------------------ * Button * -----------------*/ /*Initialize your buttons*/ void button_init(void) { /*Your code comes here*/ } /*Will be called by the library to read the button*/ void button_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data) { static uint8_t last_btn = 0; /*Get the pressed button's ID*/ int8_t btn_act = button_get_pressed_id(); if(btn_act >= 0) { data->state = LV_INDEV_STATE_PR; last_btn = btn_act; } else { data->state = LV_INDEV_STATE_REL; } /*Save the last pressed button's ID*/ data->btn_id = last_btn; } /*Get ID (0, 1, 2 ..) of the pressed button*/ int8_t button_get_pressed_id(void) { uint8_t i; /*Check to buttons see which is being pressed (assume there are 2 buttons)*/ for(i = 0; i < 2; i++) { /*Return the pressed button's ID*/ if(button_is_pressed(i)) { return i; } } /*No button pressed*/ return -1; } /*Test if `id` button is pressed or not*/ bool button_is_pressed(uint8_t id) { /*Your code comes here*/ return false; } #else /*Enable this file at the top*/ /*This dummy typedef exists purely to silence -Wpedantic.*/ typedef int keep_pedantic_happy; #endif