|
| 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 | +/*Test if `id` button is pressed or not*/ |
| 26 | +static bool button_is_pressed(uint8_t id) |
| 27 | +{ |
| 28 | + switch(id) |
| 29 | + { |
| 30 | + case 0: |
| 31 | + if(rt_pin_read(BUTTON0_PIN) == PIN_LOW) |
| 32 | + return true; |
| 33 | + break; |
| 34 | + case 1: |
| 35 | + if(rt_pin_read(BUTTON1_PIN) == PIN_LOW) |
| 36 | + return true; |
| 37 | + break; |
| 38 | + case 2: |
| 39 | + if(rt_pin_read(BUTTON2_PIN) == PIN_LOW) |
| 40 | + return true; |
| 41 | + break; |
| 42 | + case 3: |
| 43 | + if(rt_pin_read(BUTTON_WKUP_PIN) == PIN_LOW) |
| 44 | + return true; |
| 45 | + break; |
| 46 | + } |
| 47 | + |
| 48 | + return false; |
| 49 | +} |
| 50 | + |
| 51 | +static int8_t button_get_pressed_id(void) |
| 52 | +{ |
| 53 | + uint8_t i; |
| 54 | + |
| 55 | + /*Check to buttons see which is being pressed*/ |
| 56 | + for(i = 0; i < 4; i++) |
| 57 | + { |
| 58 | + /*Return the pressed button's ID*/ |
| 59 | + if(button_is_pressed(i)) |
| 60 | + { |
| 61 | + return i; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + /*No button pressed*/ |
| 66 | + return -1; |
| 67 | +} |
| 68 | + |
| 69 | +void button_read(lv_indev_drv_t * drv, lv_indev_data_t*data) |
| 70 | +{ |
| 71 | + static uint32_t last_btn = 0; /*Store the last pressed button*/ |
| 72 | + int btn_pr = button_get_pressed_id(); /*Get the ID (0,1,2...) of the pressed button*/ |
| 73 | + if(btn_pr >= 0) |
| 74 | + { /*Is there a button press? (E.g. -1 indicated no button was pressed)*/ |
| 75 | + last_btn = btn_pr; /*Save the ID of the pressed button*/ |
| 76 | + data->state = LV_INDEV_STATE_PRESSED; /*Set the pressed state*/ |
| 77 | + } |
| 78 | + else |
| 79 | + { |
| 80 | + data->state = LV_INDEV_STATE_RELEASED; /*Set the released state*/ |
| 81 | + } |
| 82 | + |
| 83 | + data->btn_id = last_btn; /*Save the last button*/ |
| 84 | +} |
| 85 | + |
| 86 | + |
| 87 | +lv_indev_t * button_indev; |
| 88 | + |
| 89 | +void lv_port_indev_init(void) |
| 90 | +{ |
| 91 | + static lv_indev_drv_t indev_drv; |
| 92 | + |
| 93 | + /* Initialize the on-board buttons */ |
| 94 | + rt_pin_mode(BUTTON0_PIN, PIN_MODE_INPUT); |
| 95 | + rt_pin_mode(BUTTON1_PIN, PIN_MODE_INPUT); |
| 96 | + rt_pin_mode(BUTTON2_PIN, PIN_MODE_INPUT); |
| 97 | + rt_pin_mode(BUTTON_WKUP_PIN, PIN_MODE_INPUT); |
| 98 | + |
| 99 | + lv_indev_drv_init(&indev_drv); /*Basic initialization*/ |
| 100 | + indev_drv.type = LV_INDEV_TYPE_BUTTON; |
| 101 | + indev_drv.read_cb = button_read; |
| 102 | + |
| 103 | + /*Register the driver in LVGL and save the created input device object*/ |
| 104 | + button_indev = lv_indev_drv_register(&indev_drv); |
| 105 | +} |
0 commit comments