App-lvgl-touchPad/components/lv_drivers/indev/keyboard.c

95 lines
3.3 KiB
C

#include "keyboard.h"
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <linux/input.h>
int keyboard_fd = -1;
int evdev_key_val;
int evdev_button;
void keyboard_init(void)
{
if (keyboard_fd != -1) {
close(keyboard_fd);
return ;
}
keyboard_fd = open(KEYBOARD_NAME, O_RDWR | O_NOCTTY | O_NDELAY);
if(keyboard_fd == -1) {
perror("unable to open evdev interface:");
return false;
}
fcntl(keyboard_fd, F_SETFL, O_ASYNC | O_NONBLOCK);
}
/**
* Get the last pressed or released character from the PC's keyboard
* @param indev_drv pointer to the related input device driver
* @param data store the read data here
*/
void keyboard_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data)
{
struct input_event in;
while(read(keyboard_fd, &in, sizeof(struct input_event)) > 0) {
if(in.type == EV_KEY) {
// printf("Key_Matrix_fb:0x%X\n",in.code);
if(indev_drv->type == LV_INDEV_TYPE_KEYPAD || indev_drv->type == LV_INDEV_TYPE_ENCODER) {
switch((in.code)) {
case KEY_BACKSPACE:
data->key = LV_KEY_BACKSPACE;
break;
case KEY_ENTER:
data->key = LV_KEY_ENTER;
break;
case KEY_PREVIOUS:
data->key = LV_KEY_PREV;
break;
case KEY_NEXT:
data->key = LV_KEY_NEXT;
break;
case BTN_3:
data->key = LV_KEY_UP;
break;
case BTN_5:
data->key = LV_KEY_PREV;
// data->enc_diff = 1;
break;
case BTN_6:
data->key = LV_KEY_NEXT;
// data->enc_diff = -1;
break;
case BTN_8:
data->key = LV_KEY_ENTER;
break;
case KEY_TAB:
data->key = LV_KEY_NEXT;
break;
default:
data->key = 0;
break;
}
if (data->key != 0) {
/* Only record button state when actual output is produced to prevent widgets from refreshing */
data->state = (!in.value) ? LV_INDEV_STATE_KEY_PRESSED : LV_INDEV_STATE_REL;
// if(data->key == LV_KEY_LEFT) {
// /*emulate encoder left*/
// data->enc_diff = 1;
// }
// else if(data->key == LV_KEY_RIGHT) {
// /*emulate encoder right*/
// data->enc_diff = -1;
// }
// data->enc_diff = (!in.value) ? data->enc_diff : LV_INDEV_STATE_REL;
data->key = (!in.value) ? data->key : 0;
}
evdev_key_val = data->key;
evdev_button = data->state;
printf("key_va:0x%X state:%d enc_diff:%d\n",data->key,data->state,data->enc_diff);
}
}
}
return ;
}