t113-c-lvgl触摸接口接入

发布时间 2023-08-17 22:07:26作者: 悠闲的小莫

整合一下最近搞的东西,顺便设计一下ui

移植触摸

复制port文件到src目录下同时改名字和删除掉不用的东西:

/**
 * @file lv_port_indev_templ.c
 *
 */

/*Copy this file as "lv_port_indev.c" and set this value to "1" to enable content*/
#if 1

/*********************
 *      INCLUDES
 *********************/
#include "lv_port_indev_template.h"
#include "../../lvgl.h"

/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/

static void touchpad_init(void);
static void touchpad_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data);
static bool touchpad_is_pressed(void);
static void touchpad_get_xy(lv_coord_t * x, lv_coord_t * y);

// static void mouse_init(void);
// static void mouse_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data);
// static bool mouse_is_pressed(void);
// static void mouse_get_xy(lv_coord_t * x, lv_coord_t * y);

// static void keypad_init(void);
// static void keypad_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data);
// static uint32_t keypad_get_key(void);

// static void encoder_init(void);
// static void encoder_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data);
// static void encoder_handler(void);

// static void button_init(void);
// static void button_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data);
// static int8_t button_get_pressed_id(void);
// static bool button_is_pressed(uint8_t id);

/**********************
 *  STATIC VARIABLES
 **********************/
lv_indev_t * indev_touchpad;
lv_indev_t * indev_mouse;
lv_indev_t * indev_keypad;
lv_indev_t * indev_encoder;
lv_indev_t * indev_button;

static int32_t encoder_diff;
static lv_indev_state_t encoder_state;

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
 **********************/

void lv_port_indev_init(void)
{
    /**
     * Here you will find example implementation of input devices supported by LittelvGL:
     *  - Touchpad
     *  - Mouse (with cursor support)
     *  - Keypad (supports GUI usage only with key)
     *  - Encoder (supports GUI usage only with: left, right, push)
     *  - Button (external buttons to press points on the screen)
     *
     *  The `..._read()` function are only examples.
     *  You should shape them according to your hardware
     */

    static lv_indev_drv_t indev_drv;

    /*------------------
     * Touchpad
     * -----------------*/

    /*Initialize your touchpad if you have*/
    touchpad_init();

    /*Register a touchpad input device*/
    lv_indev_drv_init(&indev_drv);
    indev_drv.type = LV_INDEV_TYPE_POINTER;
    indev_drv.read_cb = touchpad_read;
    indev_touchpad = lv_indev_drv_register(&indev_drv);

}

/**********************
 *   STATIC FUNCTIONS
 **********************/

/*------------------
 * Touchpad
 * -----------------*/

/*Initialize your touchpad*/
static void touchpad_init(void)
{
    /*Your code comes here*/
}

/*Will be called by the library to read the touchpad*/
static void touchpad_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data)
{
    static lv_coord_t last_x = 0;
    static lv_coord_t last_y = 0;

    /*Save the pressed coordinates and the state*/
    if(touchpad_is_pressed()) {
        touchpad_get_xy(&last_x, &last_y);
        data->state = LV_INDEV_STATE_PR;
    }
    else {
        data->state = LV_INDEV_STATE_REL;
    }

    /*Set the last pressed coordinates*/
    data->point.x = last_x;
    data->point.y = last_y;
}

/*Return true is the touchpad is pressed*/
static bool touchpad_is_pressed(void)
{
    /*Your code comes here*/

    return false;
}

/*Get the x and y coordinates if the touchpad is pressed*/
static void touchpad_get_xy(lv_coord_t * x, lv_coord_t * y)
{
    /*Your code comes here*/

    (*x) = 0;
    (*y) = 0;
}

#else /*Enable this file at the top*/

/*This dummy typedef exists purely to silence -Wpedantic.*/
typedef int keep_pedantic_happy;
#endif

问题来了,lvgl触摸最多只支持单点触摸,然而检测出来的是有id的,那么我们看看官方的例子是怎么写的:

很明显,这是读出来就放上去,不打算区分多触摸,但是这个不会导致bug吗?就好像就只有一个指针多个鼠标抢掌控权一样

经过大佬说的,lvgl参考网页的所以只有单点触摸,不管了,先写进去吧:

代码

main

#include <stdio.h>
#include "linux/fb.h"
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <string.h>
#include "lcd_run.h"
#include "lvgl.h"
#include "sys/time.h"
#include <signal.h>
#include "lvgl/src/lv_port_disp.h"
#include "lvgl/src/lv_port_indev.h"
#include "ui.h"


void sig_alm_handler(int sig_num)
{
    // printf("%s, signal number:%d,\r\n", __FUNCTION__, sig_num);
    // printf("testtiing\r\n");
    lv_tick_inc(10);//10ms
}

int main()
{
    lv_init();
    signal(SIGALRM,sig_alm_handler);//信号捕获函数,当捕捉到了信号就回调函数,而我们知道timer第一种运行的时候会产生SIGALRM信号
    struct itimerval olditv;
    struct itimerval itv;
    itv.it_interval.tv_sec = 0; //定时周期为10毫秒钟。
    itv.it_interval.tv_usec = 10000;//10000;
    itv.it_value.tv_sec = 0; //立马启动//定时器启动以后将在3秒又500微秒以后正式开始计时。
    itv.it_value.tv_usec = 500;
    setitimer(ITIMER_REAL, &itv, &olditv);//第一种,真实的时间
    //printf("nihao\n\r");
    
    lv_port_disp_init();//调用的disp函数
    lv_port_indev_init();
   //lv_demo_music();
   ui_start();
    while (1)
    {
        lv_task_handler();
        //usleep(500);
    }
    disp_dinit();
    pad_close();
    return;
}

indev:

/**
 * @file lv_port_indev_templ.c
 *
 */

/*Copy this file as "lv_port_indev.c" and set this value to "1" to enable content*/
#if 1

/*********************
 *      INCLUDES
 *********************/
#include "lv_port_indev.h"
#include <fcntl.h>  
#include <linux/input.h>  
#include "lvgl.h"

/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/

static void touchpad_init(void);
static void touchpad_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data);
static bool touchpad_is_pressed(void);
static void touchpad_get_xy(lv_coord_t * x, lv_coord_t * y);

// static void mouse_init(void);
// static void mouse_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data);
// static bool mouse_is_pressed(void);
// static void mouse_get_xy(lv_coord_t * x, lv_coord_t * y);

// static void keypad_init(void);
// static void keypad_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data);
// static uint32_t keypad_get_key(void);

// static void encoder_init(void);
// static void encoder_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data);
// static void encoder_handler(void);

// static void button_init(void);
// static void button_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data);
// static int8_t button_get_pressed_id(void);
// static bool button_is_pressed(uint8_t id);

/**********************
 *  STATIC VARIABLES
 **********************/
lv_indev_t * indev_touchpad;
lv_indev_t * indev_mouse;
lv_indev_t * indev_keypad;
lv_indev_t * indev_encoder;
lv_indev_t * indev_button;

static int32_t encoder_diff;
static lv_indev_state_t encoder_state;

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
 **********************/

void lv_port_indev_init(void)
{
    /**
     * Here you will find example implementation of input devices supported by LittelvGL:
     *  - Touchpad
     *  - Mouse (with cursor support)
     *  - Keypad (supports GUI usage only with key)
     *  - Encoder (supports GUI usage only with: left, right, push)
     *  - Button (external buttons to press points on the screen)
     *
     *  The `..._read()` function are only examples.
     *  You should shape them according to your hardware
     */

    static lv_indev_drv_t indev_drv;

    /*------------------
     * Touchpad
     * -----------------*/

    /*Initialize your touchpad if you have*/
    touchpad_init();

    /*Register a touchpad input device*/
    lv_indev_drv_init(&indev_drv);
    indev_drv.type = LV_INDEV_TYPE_POINTER;
    indev_drv.read_cb = touchpad_read;
    indev_touchpad = lv_indev_drv_register(&indev_drv);

}

/**********************
 *   STATIC FUNCTIONS
 **********************/

/*------------------
 * Touchpad
 * -----------------*/

int fd;
struct input_event in;
/*Initialize your touchpad*/
static void touchpad_init(void)
{
    /*Your code comes here*/
    char  buf[256] = { 0, };  /* RATS: Use ok */
    if ((fd = open("/dev/input/event1", O_RDWR | O_NOCTTY | O_NDELAY, 0)) >= 0) {
        //ioctl(fd, EVIOCGVERSION, &version);                     //获取版本
        ioctl(fd, EVIOCGNAME(sizeof(buf)), buf);                //设备的名字
       //ioctl(fd, EVIOCGBIT(0, sizeof(mask)), mask);       //这个是用来区分是什么设备的
        //上面基本没什么用对于触摸来说
    }
    else{
        printf("open failed !\r\n");
        return;
    }
    printf("event name is %s\r\n",buf);
}

/*Will be called by the library to read the touchpad*/
static void touchpad_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data)
{
    static lv_coord_t last_x = 0;
    static lv_coord_t last_y = 0;

    while(read(fd, &in, sizeof(struct input_event)) > 0) {
        if(in.type == EV_KEY)
        {
            if(in.code == BTN_TOUCH)
            {
                printf("press\r\n");
                if(in.value)
                {
                    data->state = LV_INDEV_STATE_PR;
                }
                else
                {
                    data->state = LV_INDEV_STATE_REL;
                }
            }
        }
        else if(in.type == EV_ABS)
        {
            switch (in.code)
            {
            case ABS_MT_POSITION_X:
                last_x = in.value;
                printf("lastx is %d\r\n",last_x);
                break;
            case ABS_MT_POSITION_Y:
                last_y = in.value;
                printf("lasty is %d\r\n",last_y);
            default:
                break;
            }
        }
     }
    // /*Save the pressed coordinates and the state*/
    // if(touchpad_is_pressed()) {
    //     touchpad_get_xy(&last_x, &last_y);
    //     data->state = LV_INDEV_STATE_PR;
    // }
    // else {
    //     data->state = LV_INDEV_STATE_REL;
    // }

    /*Set the last pressed coordinates*/
    data->point.x = last_x;
    data->point.y = last_y;
}

void pad_close(void)
{
    close(fd);
}

// /*Return true is the touchpad is pressed*/
// static bool touchpad_is_pressed(void)
// {
//     /*Your code comes here*/
//      while(read(fd, &in, sizeof(struct input_event)) > 0) {
//         if(in.type == EV_KEY)
//         {
//             if(in.code == BTN_TOUCH)
//             {
//                 if(in.value)
//                     return true;
//                 else
//                     return false;
//             }
//         }
//         else
//      }
//     return false;
// }

// /*Get the x and y coordinates if the touchpad is pressed*/
// static void touchpad_get_xy(lv_coord_t * x, lv_coord_t * y)
// {
//     /*Your code comes here*/

//     (*x) = 0;
//     (*y) = 0;
// }

#else /*Enable this file at the top*/

/*This dummy typedef exists purely to silence -Wpedantic.*/
typedef int keep_pedantic_happy;
#endif

结果

算是成功驱动了,只不过貌似就只能记录最后一个位置的位置

疑问

之前学的时候说.a也就是静态库是直接写入文件里面的,这里也确实写进去了,但是问题是像youchat这个文件链接的是.so文件啊也就是动态库,而我又没有复制动态库进去,那么这个文件为什么能够正常运行呢?很奇怪。