1 /*
2  * Copyright (c) 2006-2021, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2021-10-18     Meco Man     The first version
9  */
10 #include <lvgl.h>
11 #include <stdbool.h>
12 #include <rtdevice.h>
13 
14 #define UP_KEY      2
15 #define DOWN_KEY    18
16 #define LEFT_KEY    16
17 #define RIGHT_KEY   20
18 #define CRTL_KEY    3
19 
20 #define BUTTON0_PIN        2
21 #define BUTTON1_PIN        18
22 #define BUTTON2_PIN        16
23 #define BUTTON_WKUP_PIN    20
24 
25 lv_indev_t * button_indev;
26 
27 /*Test if `id` button is pressed or not*/
button_is_pressed(uint8_t id)28 static bool button_is_pressed(uint8_t id)
29 {
30     switch(id)
31     {
32     case 0:
33         if(rt_pin_read(BUTTON0_PIN) == PIN_LOW)
34             return true;
35         break;
36     case 1:
37         if(rt_pin_read(BUTTON1_PIN) == PIN_LOW)
38             return true;
39         break;
40     case 2:
41         if(rt_pin_read(BUTTON2_PIN) == PIN_LOW)
42             return true;
43         break;
44     case 3:
45         if(rt_pin_read(BUTTON_WKUP_PIN) == PIN_LOW)
46             return true;
47         break;
48     }
49 
50     return false;
51 }
52 
button_get_pressed_id(void)53 static int8_t button_get_pressed_id(void)
54 {
55     uint8_t i;
56 
57     /*Check to buttons see which is being pressed*/
58     for(i = 0; i < 4; i++)
59     {
60         /*Return the pressed button's ID*/
61         if(button_is_pressed(i))
62         {
63             return i;
64         }
65     }
66 
67     /*No button pressed*/
68     return -1;
69 }
70 
button_read(lv_indev_drv_t * drv,lv_indev_data_t * data)71 void button_read(lv_indev_drv_t * drv, lv_indev_data_t*data)
72 {
73     static uint32_t last_btn = 0;   /*Store the last pressed button*/
74     int btn_pr = button_get_pressed_id(); /*Get the ID (0,1,2...) of the pressed button*/
75     if(btn_pr >= 0)
76     {               /*Is there a button press? (E.g. -1 indicated no button was pressed)*/
77        last_btn = btn_pr;           /*Save the ID of the pressed button*/
78        data->state = LV_INDEV_STATE_PRESSED;  /*Set the pressed state*/
79     }
80     else
81     {
82        data->state = LV_INDEV_STATE_RELEASED; /*Set the released state*/
83     }
84 
85     data->btn_id = last_btn;            /*Save the last button*/
86 }
87 
lv_port_indev_init(void)88 void lv_port_indev_init(void)
89 {
90     static lv_indev_drv_t indev_drv;
91 
92     /* Initialize the on-board buttons */
93     rt_pin_mode(BUTTON0_PIN, PIN_MODE_INPUT);
94     rt_pin_mode(BUTTON1_PIN, PIN_MODE_INPUT);
95     rt_pin_mode(BUTTON2_PIN, PIN_MODE_INPUT);
96     rt_pin_mode(BUTTON_WKUP_PIN, PIN_MODE_INPUT);
97 
98     lv_indev_drv_init(&indev_drv);      /*Basic initialization*/
99     indev_drv.type = LV_INDEV_TYPE_BUTTON;
100     indev_drv.read_cb = button_read;
101 
102     /*Register the driver in LVGL and save the created input device object*/
103     button_indev = lv_indev_drv_register(&indev_drv);
104 }
105