支持接入外部按键驱动,实现外部按键控制界面的切换。

This commit is contained in:
Yu Xifeng 2024-12-21 09:08:42 +00:00
parent 96f2df4c09
commit 821f815d07
8 changed files with 283 additions and 49 deletions

View File

@ -0,0 +1,94 @@
#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_PR : 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 ;
}

View File

@ -31,11 +31,6 @@ extern "C" {
#include "lvgl/lvgl.h" #include "lvgl/lvgl.h"
#endif #endif
#if USE_SDL_GPU
#include "../sdl/sdl_gpu.h"
#else
#include "../sdl/sdl.h"
#endif
/********************* /*********************
* DEFINES * DEFINES
@ -52,25 +47,13 @@ extern "C" {
/** /**
* Initialize the keyboard * Initialize the keyboard
*/ */
static inline void keyboard_init(void)
{
/*Nothing to do*/
}
/**
* 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
*/
static inline void keyboard_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data)
{
sdl_keyboard_read(indev_drv, data);
}
/********************** /**********************
* MACROS * MACROS
**********************/ **********************/
extern void keyboard_init(void);
extern void keyboard_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data);
#endif /*USE_KEYBOARD*/ #endif /*USE_KEYBOARD*/

View File

@ -399,7 +399,7 @@
#endif #endif
#if USE_EVDEV || USE_BSD_EVDEV #if USE_EVDEV || USE_BSD_EVDEV
# define EVDEV_NAME "/dev/input/event4" /*You can use the "evtest" Linux tool to get the list of devices and test them*/ # define EVDEV_NAME "/dev/input/event5" /*You can use the "evtest" Linux tool to get the list of devices and test them*/
# define EVDEV_SWAP_AXES 0 /*Swap the x and y axes of the touchscreen*/ # define EVDEV_SWAP_AXES 0 /*Swap the x and y axes of the touchscreen*/
# define EVDEV_CALIBRATE 0 /*Scale and offset the touchscreen coordinates by using maximum and minimum values for each axis*/ # define EVDEV_CALIBRATE 0 /*Scale and offset the touchscreen coordinates by using maximum and minimum values for each axis*/
@ -416,7 +416,8 @@
* Keyboard of a PC (using SDL) * Keyboard of a PC (using SDL)
*------------------------------*/ *------------------------------*/
#ifndef USE_KEYBOARD #ifndef USE_KEYBOARD
# define USE_KEYBOARD 0 # define USE_KEYBOARD 1
#define KEYBOARD_NAME "/dev/input/event4" /*You can use the "evtest" Linux tool to get the list of devices and test them*/
#endif #endif
#if USE_KEYBOARD #if USE_KEYBOARD

View File

@ -18,7 +18,7 @@
#include <fcntl.h> #include <fcntl.h>
#include <limits.h> #include <limits.h>
#include "fusb.h" #include "fusb.h"
extern pthread_t Thread_Lv_Main;
extern void *Lv_main_task(void *arg); extern void *Lv_main_task(void *arg);
#endif #endif

View File

@ -259,13 +259,104 @@ void Load_Physical_screen(lv_disp_t * disp)
lv_disp_set_default(disp); lv_disp_set_default(disp);
} }
#define CANVAS_WIDTH 200
#define CANVAS_HEIGHT 200
static lv_obj_t * canvas;
/*Create colors with the indices of the palette*/
static lv_color_t cbuf[LV_CANVAS_BUF_SIZE_INDEXED_1BIT(CANVAS_WIDTH, CANVAS_HEIGHT)];
lv_color_t c0;
lv_color_t c1;
static lv_point_t points[1000]; // 存储点的数组
static uint32_t point_count = 0; // 当前点的数量
static void touch_event_cb(lv_event_t * e)
{
lv_event_code_t code = lv_event_get_code(e);
lv_obj_t * obj = lv_event_get_target(e);
if(code == LV_EVENT_PRESSED) {
// 用户按下触摸屏
point_count = 0; // 重置点的数量
printf("点击\r\n");
/*Create a buffer for the canvas*/
}
else if(code == LV_EVENT_PRESSING) {
// 用户按住并移动触摸屏
lv_point_t touch_point;
lv_indev_get_point(lv_indev_get_act(), &touch_point); // 获取触摸点位置
// 将当前点存储到数组中
if(point_count < sizeof(points) / sizeof(points[0])) {
points[point_count] = touch_point;
point_count++;
}
// printf("滑动:%d,%d\n",touch_point.x,touch_point.y);
lv_canvas_set_px_color(canvas, touch_point.x, touch_point.y, c0);
/*Create hole on the canvas*/
// uint32_t x;
// uint32_t y;
// for( y = 10; y < 30; y++) {
// for( x = 5; x < 20; x++) {
// lv_canvas_set_px_color(canvas, x, y, c0);
// }
// }
}
else if(code == LV_EVENT_RELEASED) {
// 用户松开触摸
// 可选:可以在这里添加保存绘图的操作
}
}
void Drawing_board_screen(lv_disp_t * disp)
{
lv_disp_set_default(disp);
if(disp == disp_480x320)
{
lv_obj_t* screen = lv_obj_create(lv_scr_act());
lv_obj_set_size(screen,480,320);
canvas = lv_canvas_create(lv_scr_act());
/*Create a canvas and initialize its palette*/
lv_canvas_set_buffer(canvas, cbuf, CANVAS_WIDTH, CANVAS_HEIGHT, LV_IMG_CF_INDEXED_1BIT);
lv_canvas_set_palette(canvas, 0, LV_COLOR_CHROMA_KEY);
lv_canvas_set_palette(canvas, 1, lv_palette_main(LV_PALETTE_RED));
c0.full = 0;
c1.full = 1;
/*Red background (There is no dedicated alpha channel in indexed images so LV_OPA_COVER is ignored)*/
lv_canvas_fill_bg(canvas, c1, LV_OPA_COVER);
}else{
lv_obj_t* screen = lv_obj_create(lv_scr_act());
lv_obj_set_size(screen,300,240);
lv_obj_add_event_cb(screen, touch_event_cb, LV_EVENT_ALL, NULL);
}
}
static void btn_event_cb(lv_event_t * e) static void btn_event_cb(lv_event_t * e)
{ {
lv_obj_t * btn = lv_event_get_target(e); // 获取触发事件的按钮 lv_obj_t * btn = lv_event_get_target(e); // 获取触发事件的按钮
uint32_t i = (uint32_t)lv_obj_get_user_data(btn); // 获取按钮的索引 uint32_t i = (uint32_t)lv_obj_get_user_data(btn); // 获取按钮的索引
lv_obj_t * label = lv_obj_get_child(btn, NULL); lv_obj_t * label = lv_obj_get_child(btn, NULL);
// printf("第%d按钮被按下!\n",i); // 打印信息 printf("第%d按钮被按下!\n",i); // 打印信息
// if(i == 0) {
// Drawing_board_screen(disp_480x320);
// Drawing_board_screen(disp_300x240);
// }else {
// // lv_disp_set_default(disp_480x320); // // lv_disp_set_default(disp_480x320);
// lv_font_open_Init(32); // lv_font_open_Init(32);
// /*Create style with the new font*/ // /*Create style with the new font*/
@ -280,6 +371,8 @@ static void btn_event_cb(lv_event_t * e)
// lv_disp_set_default(disp_300x240); // lv_disp_set_default(disp_300x240);
// lv_label_set_text_fmt(label, "%d:按下",i); // lv_label_set_text_fmt(label, "%d:按下",i);
// }
} }
@ -317,17 +410,17 @@ void lv_example_scroll_2(void)
lv_obj_add_style(label, &style, 0); lv_obj_add_style(label, &style, 0);
switch (i) switch (i)
{ {
// case 0: case 0:
// lv_label_set_text(label, "0"); lv_label_set_text(label, "画板测试");
// break; break;
// case 1: case 1:
// lv_label_set_text(label, "1"); lv_label_set_text(label, "手势测试");
// break; break;
default: default:
lv_label_set_text_fmt(label, "Panel %"LV_PRIu32, i); lv_label_set_text_fmt(label, "Panel %"LV_PRIu32, i);
break; break;
} }
lv_group_add_obj(Key_Matrix_Group, btn);
lv_obj_center(label); lv_obj_center(label);
// 设置按钮的索引作为用户数据 // 设置按钮的索引作为用户数据
@ -347,6 +440,7 @@ void lv_example_scroll_2(void)
// 使得滚动面板实现循环 // 使得滚动面板实现循环
lv_obj_add_event_cb(panel, panel_event_cb, LV_EVENT_SCROLL, NULL); lv_obj_add_event_cb(panel, panel_event_cb, LV_EVENT_SCROLL, NULL);
lv_obj_add_event_cb(panel, panel_event_cb, LV_EVENT_SCROLL_END, NULL); lv_obj_add_event_cb(panel, panel_event_cb, LV_EVENT_SCROLL_END, NULL);
lv_group_add_obj(Key_Matrix_Group, panel);
} }
@ -388,7 +482,7 @@ void Physical_Screen_480x320()
lv_label_set_text(Hello_lable, "LinkEase-NAS"); lv_label_set_text(Hello_lable, "LinkEase-NAS");
lv_obj_align(Hello_lable, LV_ALIGN_CENTER, 0, 0); lv_obj_align(Hello_lable, LV_ALIGN_CENTER, 0, 0);
lv_group_add_obj(Key_Matrix_Group ,Lock_screen.screen); // lv_group_add_obj(Key_Matrix_Group ,Lock_screen.screen);
lv_obj_add_event_cb(Lock_screen.screen, Lock_screen.event_cb, LV_EVENT_ALL, NULL); lv_obj_add_event_cb(Lock_screen.screen, Lock_screen.event_cb, LV_EVENT_ALL, NULL);
} }
@ -400,11 +494,32 @@ void Physical_Screen_300x240()
lv_example_scroll_2(); lv_example_scroll_2();
} }
void lvgl_app_start() void lvgl_app_start(void *arg)
{ {
uint16_t i=0; uint16_t i=0;
int num = (unsigned long long)arg;
// int res;
// /* 互斥锁上锁 */
// res = pthread_mutex_lock(&mutex_disp_Refresh);
// if (res)
// { /*获取失败*/
// printf("Thread %d lock failed\n",num);
// /* 互斥锁解锁 */
// pthread_mutex_unlock(&mutex_disp_Refresh);
// pthread_exit(NULL);
// }
Physical_Screen_480x320(); Physical_Screen_480x320();
Physical_Screen_300x240(); Physical_Screen_300x240();
// /* 互斥锁解锁 */
// pthread_mutex_unlock(&mutex_disp_Refresh);
// while (1)
// {
// sleep(1);
// }
return; return;
// lv_example_btn_1(); // lv_example_btn_1();
// return; // return;

View File

@ -1,7 +1,7 @@
#ifndef LVGL_APP_H #ifndef LVGL_APP_H
#define LVGL_APP_H #define LVGL_APP_H
#include "lvgl.h" #include "lvgl.h"
#include <pthread.h>
typedef enum typedef enum
{ {
@ -57,6 +57,7 @@ extern config_lv_page_t Lock_screen;
extern config_lv_page_t Bar_screen; extern config_lv_page_t Bar_screen;
extern lv_disp_t * disp_480x320; extern lv_disp_t * disp_480x320;
extern lv_disp_t * disp_300x240; extern lv_disp_t * disp_300x240;
extern pthread_mutex_t mutex_disp_Refresh;
extern void lv_example_freetype_1(lv_obj_t * parent); extern void lv_example_freetype_1(lv_obj_t * parent);
extern void Main_Picture_background(lv_obj_t * parent); extern void Main_Picture_background(lv_obj_t * parent);

View File

@ -3,12 +3,15 @@
#include "display/fbdev.h" #include "display/fbdev.h"
#include "display/drm.h" #include "display/drm.h"
#include "indev/evdev.h" #include "indev/evdev.h"
#include "indev/keyboard.h"
pthread_t Thread_Lv_Main; pthread_t Thread_Lv_App;
lv_disp_t * disp_480x320; lv_disp_t * disp_480x320;
lv_disp_t * disp_300x240; lv_disp_t * disp_300x240;
pthread_mutex_t mutex_disp_Refresh;
#define DISP_BUF_SIZE (1280 * 720 * 4) #define DISP_BUF_SIZE (1280 * 720 * 4)
extern void mouse_init(void); extern void mouse_init(void);
@ -114,8 +117,6 @@ void *Lv_main_task(void *arg)
#endif #endif
/* Linux input device init */ /* Linux input device init */
#if USE_EVDEV #if USE_EVDEV
evdev_init(); evdev_init();
@ -130,17 +131,54 @@ void *Lv_main_task(void *arg)
// indev_drv.disp = disp1; // indev_drv.disp = disp1;
// lv_indev_drv_update(indev_touch,&indev_drv); // lv_indev_drv_update(indev_touch,&indev_drv);
#endif #endif
#if USE_KEYBOARD
keyboard_init();
lv_indev_drv_t indev_drv_keyboard;
lv_indev_drv_init(&indev_drv_keyboard); /*Basic initialization*/
indev_drv_keyboard.type = LV_INDEV_TYPE_KEYPAD;
indev_drv_keyboard.read_cb = keyboard_read; //lv_gesture_dir_t lv_indev_get_gesture_dir(const lv_indev_t * indev)
indev_drv_keyboard.disp = disp_300x240;
lv_indev_t* indev_keyboard = lv_indev_drv_register(&indev_drv_keyboard);
lv_indev_drv_update(indev_keyboard,&indev_drv_keyboard);
Key_Matrix_Group = lv_group_create(); //创建组
lv_indev_set_group(indev_keyboard, Key_Matrix_Group); //将组绑定到输入设备
#endif
// pthread_mutex_init(&mutex_disp_Refresh, NULL);
/*Create a Demo*/ /*Create a Demo*/
//lv_demo_widgets(); //lv_demo_widgets();
// lv_demo_benchmark(); // lv_demo_benchmark();
// lv_example_btn_1(); // lv_example_btn_1();
// int res = pthread_create(&Thread_Lv_App, NULL, lvgl_app_start, (void*)(unsigned long long)(2));
// if(res != 0)
// {
// printf("create thread fail\n");
// exit(res);
// }
lvgl_app_start(); lvgl_app_start();
while(1) { while(1) {
/* 互斥锁上锁 */
// res = pthread_mutex_lock(&mutex_disp_Refresh);
// if (res)
// { /*获取失败*/
// printf("Thread %d lock failed\n",num);
// /* 互斥锁解锁 */
// pthread_mutex_unlock(&mutex_disp_Refresh);
// pthread_exit(NULL);
// }
lv_timer_handler(); lv_timer_handler();
usleep(5000); usleep(5000);
/* 互斥锁解锁 */
// pthread_mutex_unlock(&mutex_disp_Refresh);
} }
/*退出线程*/ /*退出线程*/
pthread_exit(NULL); pthread_exit(NULL);
} }

View File

@ -30,6 +30,8 @@
#define QID 0x4321 #define QID 0x4321
static pthread_t Thread_Lv_Main;
typedef struct typedef struct
{ {
int page_type;//来自哪个页面 int page_type;//来自哪个页面