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