07_瑞萨GUI(LVGL)移植实战教程之LVGL对接EC11旋转编码器驱动

本系列教程配套出有视频教程,观看地址:https://www.bilibili.com/video/BV1gV4y1e7Sg

7. LVGL对接EC11旋转编码器驱动

本次实验我们向LVGL库中对接EC11旋转编码器驱动,让我们能通过EC11旋转编码器操作UI。

7.1 复制工程

上次实验得出的工程我们可以通过复制在原有的基础上得到一个新的工程。

如果你不清楚复制工程的步骤,请参考阅读第三章实验的步骤教程。

本次实验我们的项目命名为:05_dshanmcu_ra6m5_lvgl_display_touchpad_encoder

7.2 对接驱动

打开 05_dshanmcu_ra6m5_lvgl_display_touchpad_encoder\dshanmcu_ra6m5\drivers\drv_gpio_ec11.c 文件,做如下修改:

将第 9 行的代码改为如下:

c 复制代码
#define DRV_GPIO_EC11_USE_LVGL  (1)

打开 05_dshanmcu_ra6m5_lvgl_display_touchpad_encoder\dshanmcu_ra6m5\drivers\lv_port_indev.c 文件,下面对其进行修改适配我们的工程:

  1. 在第 14 行空白处添加头文件包含:
c 复制代码
#include "drv_gpio_ec11.h"
  1. 修改 lv_port_indev_init 函数为如下代码:
c 复制代码
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
     */
    /*------------------
     * Touchpad
     * -----------------*/

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

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

    /*------------------
     * Mouse
     * -----------------*/

    /*Initialize your mouse if you have*/
    //mouse_init();

    /*Register a mouse input device*/
    //lv_indev_drv_init(&indev_drv);
    //indev_drv.type = LV_INDEV_TYPE_POINTER;
    //indev_drv.read_cb = mouse_read;
    //indev_mouse = lv_indev_drv_register(&indev_drv);

    /*Set cursor. For simplicity set a HOME symbol now.*/
    //lv_obj_t * mouse_cursor = lv_img_create(lv_scr_act());
    //lv_img_set_src(mouse_cursor, LV_SYMBOL_HOME);
    //lv_indev_set_cursor(indev_mouse, mouse_cursor);

    /*------------------
     * Keypad
     * -----------------*/

    /*Initialize your keypad or keyboard if you have*/
    //keypad_init();

    /*Register a keypad input device*/
    //lv_indev_drv_init(&indev_drv);
    //indev_drv.type = LV_INDEV_TYPE_KEYPAD;
    //indev_drv.read_cb = keypad_read;
    //indev_keypad = lv_indev_drv_register(&indev_drv);

    /*Later you should create group(s) with `lv_group_t * group = lv_group_create()`,
     *add objects to the group with `lv_group_add_obj(group, obj)`
     *and assign this input device to group to navigate in it:
     *`lv_indev_set_group(indev_keypad, group);`*/

    /*------------------
     * Encoder
     * -----------------*/

    /*Initialize your encoder if you have*/
    encoder_init();

    /*Register a encoder input device*/
    static lv_indev_drv_t indev_encoder_drv;
    lv_indev_drv_init(&indev_encoder_drv);
    indev_encoder_drv.type = LV_INDEV_TYPE_ENCODER;
    indev_encoder_drv.read_cb = encoder_read;
    indev_encoder = lv_indev_drv_register(&indev_encoder_drv);

    /*Later you should create group(s) with `lv_group_t * group = lv_group_create()`,
     *add objects to the group with `lv_group_add_obj(group, obj)`
     *and assign this input device to group to navigate in it:
     *`lv_indev_set_group(indev_encoder, group);`*/

    /*------------------
     * Button
     * -----------------*/

    /*Initialize your button if you have*/
    //button_init();

    /*Register a button input device*/
    //lv_indev_drv_init(&indev_drv);
    //indev_drv.type = LV_INDEV_TYPE_BUTTON;
    //indev_drv.read_cb = button_read;
    //indev_button = lv_indev_drv_register(&indev_drv);

    /*Assign buttons to points on the screen*/
    //static const lv_point_t btn_points[2] = {
    //    {10, 10},   /*Button 0 -> x:10; y:10*/
    //    {40, 100},  /*Button 1 -> x:40; y:100*/
    //};
    //lv_indev_set_button_points(indev_button, btn_points);

    lv_group_t *g = lv_group_create();
    lv_group_set_default(g);
    lv_indev_set_group(indev_touchpad, g);
    lv_indev_set_group(indev_encoder, g);
}
  1. 修改 encoder_init 函数为如下代码:
c 复制代码
static void encoder_init(void)
{
    /*Your code comes here*/
    fsp_err_t err;

    err = drv_gpio_ec11_init();
    if(FSP_SUCCESS != err)
    {
        printf ("%s %d\r\n", __FUNCTION__, __LINE__);
        __BKPT();
    }
}
  1. 修改 encoder_read 函数为如下代码:
c 复制代码
/*Will be called by the library to read the encoder*/
static void encoder_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data)
{
    uint32_t handler_start = lv_tick_get();

    if(drv_gpio_ec11_get_pin_state(EC11_PIN_KEY))
    {
        if(handler_start >= drv_gpio_ec11_get_pin_press_tick(EC11_PIN_KEY))
        {
            encoder_diff = 0;
            encoder_state = LV_INDEV_STATE_PR;
            drv_gpio_ec11_set_pin_state(EC11_PIN_KEY, 0);
        }
    }
    else if(drv_gpio_ec11_get_pin_state(EC11_PIN_S1))
    {
        if(handler_start >= drv_gpio_ec11_get_pin_press_tick(EC11_PIN_S1))
        {
            encoder_diff -= 1;
            drv_gpio_ec11_set_pin_state(EC11_PIN_S1, 0);
        }
    }
    else if(drv_gpio_ec11_get_pin_state(EC11_PIN_S2))
    {
        if(handler_start >= drv_gpio_ec11_get_pin_press_tick(EC11_PIN_S2))
        {
            encoder_diff += 1;
            drv_gpio_ec11_set_pin_state(EC11_PIN_S2, 0);
        }
    }
    else
    {
        bsp_io_level_t level;
        g_ioport.p_api->pinRead(&g_ioport_ctrl, EC11_PIN_KEY, &level);
        if(level)
        {
            encoder_state = LV_INDEV_STATE_REL;
            encoder_diff = 0;
        }
    }

    data->enc_diff = encoder_diff;
    data->state = encoder_state;
}

7.3 调用app

打开 05_dshanmcu_ra6m5_lvgl_display_touchpad_encoder\dshanmcu_ra6m5\src\hal_entry.c 将 hal_entry 函数修改为如下所示的代码:

c 复制代码
void hal_entry(void)
{
    /* TODO: add your own code here */
    //app_uart_test();
    //app_i2c_touchpad_test();
    //app_spi_display_test();
    app_lvgl_test();
    //app_ec11_test();

#if BSP_TZ_SECURE_BUILD
    /* Enter non-secure code */
    R_BSP_NonSecureEnter();
#endif
}

7.4 验证效果

点击编译按钮,再点击 debug 按钮,将程序烧写到开发板中。操作EC11编码器(左转、右转、按下)会看到UI也会跟着变化。
本节完

相关推荐
独处东汉3 小时前
freertos开发空气检测仪之输入子系统结构体设计
数据结构·人工智能·stm32·单片机·嵌入式硬件·算法
czy87874753 小时前
机智云 MCU OTA可以对MCU程序进行无线远程升级。
单片机·嵌入式硬件
A9better5 小时前
嵌入式开发学习日志52——二值与计数信号量
单片机·嵌入式硬件·学习
日更嵌入式的打工仔7 小时前
(实用向)中断服务程序(ISR)的优化方向
笔记·单片机
想放学的刺客8 小时前
单片机嵌入式试题(第25)嵌入式系统可靠性设计与外设驱动异常处理
stm32·单片机·嵌入式硬件·mcu·物联网
淘晶驰AK8 小时前
大学如何自学嵌入式开发?
单片机·嵌入式硬件
一路往蓝-Anbo9 小时前
第 1 篇:对象池模式 (Object Pool) —— 裸机下的动态内存革命
jvm·数据库·stm32·单片机·嵌入式硬件·网络协议·tcp/ip
大神与小汪9 小时前
STM32WB55蓝牙广播数据
stm32·单片机·嵌入式硬件
秋深枫叶红11 小时前
嵌入式第五十一篇——IMX6ULL中断和EPIT定时器
单片机·嵌入式硬件
【赫兹威客】浩哥11 小时前
【赫兹威客】Arduino安装教程
stm32·单片机·嵌入式硬件