简介
本次贪吃蛇的小游戏开发环境计划使用RT-thread + LVGL的组合,在上一篇已经使用RT-thread 官方的bsp在pico 板卡上运行起来,我们在此基础上开始LVGL的适配。LCD 驱动程序的实现使用的活动资料包的LCD 的驱动程序在此不需要额外开发,原理图上LCD 的背光控制引脚GPIO20,示例程序的LCD 的pwm 通道配置是有不正确的,对应的通道应该是PWM2 A,示例工程中配置的是PWM2 B.
PICO rt-thread LVGL 软件包使用
rt-thread 的pico bsp 已经适配了lvgl 对接的屏幕可能稍有差别,在rt-thread ENV 环境下开启配置即可安装lvgl的软件包及示例demo,输入pkgs --update 即可更新软件包至本地.
开启后修改bsp/raspberry-pico/applications/lvgl/lv_conf.h 屏幕参数及数据格式
本次贪吃蛇任务中需要通过按键来控制行进方向,可以将按键采集注册到LVGL inputevent 中,通过LVGL 任务采集按键状态并刷新显示,添加以下代码检测按键状态。
/* * Copyright (c) 2006-2021, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes * 2021-10-18 Meco Man The first version */ #include <lvgl.h> #include <stdbool.h> #include <rtdevice.h> #define BUTTON0_SWA_PIN 12 #define BUTTON1_SWB_PIN 13 #define BUTTON2_SWX_PIN 14 #define BUTTON3_SWY_PIN 15 lv_indev_t * button_indev; /*Test if `id` button is pressed or not*/ static bool button_is_pressed(uint8_t id) { switch(id) { case 0: if(rt_pin_read(BUTTON0_SWA_PIN) == PIN_LOW) return true; break; case 1: if(rt_pin_read(BUTTON1_SWB_PIN) == PIN_LOW) return true; break; case 2: if(rt_pin_read(BUTTON2_SWX_PIN) == PIN_LOW) return true; break; case 3: if(rt_pin_read(BUTTON3_SWY_PIN) == PIN_LOW) return true; break; } return false; } static int8_t button_get_pressed_id(void) { uint8_t i; /*Check to buttons see which is being pressed*/ for(i = 0; i < 4; i++) { /*Return the pressed button's ID*/ if(button_is_pressed(i)) { return i; } } /*No button pressed*/ return -1; } void button_read(lv_indev_drv_t * drv, lv_indev_data_t*data) { static uint32_t last_btn = 0; /*Store the last pressed button*/ int btn_pr = button_get_pressed_id(); /*Get the ID (0,1,2...) of the pressed button*/ if(btn_pr >= 0) { /*Is there a button press? (E.g. -1 indicated no button was pressed)*/ last_btn = btn_pr; /*Save the ID of the pressed button*/ data->state = LV_INDEV_STATE_PRESSED; /*Set the pressed state*/ rt_kprintf("btn_pr %d PRESSEDn",btn_pr); } else { data->state = LV_INDEV_STATE_RELEASED; /*Set the released state*/ //rt_kprintf("btn_pr %d RELEASEDn",btn_pr); } data->btn_id = last_btn; /*Save the last button*/ } void lv_port_indev_init(void) { static lv_indev_drv_t indev_drv; /* Initialize the on-board buttons */ rt_pin_mode(BUTTON0_SWA_PIN, PIN_MODE_INPUT_PULLUP); rt_pin_mode(BUTTON1_SWB_PIN, PIN_MODE_INPUT_PULLUP); rt_pin_mode(BUTTON2_SWX_PIN, PIN_MODE_INPUT_PULLUP); rt_pin_mode(BUTTON3_SWY_PIN, PIN_MODE_INPUT_PULLUP); lv_indev_drv_init(&indev_drv); /*Basic initialization*/ indev_drv.type = LV_INDEV_TYPE_BUTTON; indev_drv.read_cb = button_read; /*Register the driver in LVGL and save the created input device object*/ button_indev = lv_indev_drv_register(&indev_drv); }
更新程序运行LVGL 的demo显示正常,刷新率比较高时会出现花屏,应该是性能的原因,本次的实验不会需要这么高的刷新率。
下一步使用lvgl 描画GUI结合贪吃蛇的游戏算法完成游戏的设计开发。