嵌入式系统应用-LVGL的应用-音乐播放器
1 播放器介绍
这里采用的是有源蜂鸣器播放的音乐,采用的RTT-BeepPlayer-pkg提供音乐模板。 里面提供了四首音乐的模板。
读者可以从这篇文章获得如何移植和加载音乐播放器的——rt-thread 的软件报包-RTT-BeepPlayer-pkg
2 LVGL 绘制
2.1 背景绘制
获取当前的界面,绘制界面的背景色为:绿色。
if (main_screen == NULL)
{
// 创建窗口
main_screen = lv_scr_act();
// 背景色是蓝色
lv_obj_set_style_bg_color(main_screen,lv_palette_main(LV_PALETTE_GREEN), LV_PART_MAIN);
lv_scr_load(main_screen);
}
2.2 按键绘制
按键的创建时:lv_btn_create
添加对应的按键的动作:lv_obj_add_event_cb
可以为按键创建风格:style
2.2.1 图标下载
可以从阿里的开源图标下载你需要的图标:阿里图标
2.2.2 由于图片尺寸默认200200,利用图片工具转化成为5050
2.2.3 利用lvgl 在线转化工具,进行转化c文件
如图进行配置
2.2.4 代码显示
// 播放按键
lv_obj_t* btn_play = lv_btn_create(main_screen);
lv_obj_add_event_cb(btn_play, event_play_handler, LV_EVENT_ALL, user_data);
lv_obj_align(btn_play, LV_ALIGN_LEFT_MID, 0, 0);
lv_obj_set_size(btn_play, 50, 50);
// 下一首按键
lv_obj_t* btn_next= lv_btn_create(main_screen);
lv_obj_add_event_cb(btn_next, event_next_handler, LV_EVENT_ALL, user_data);
lv_obj_align(btn_next, LV_ALIGN_CENTER, -80, 0);
lv_obj_set_size(btn_next, 50, 50);
// 上一首按键
lv_obj_t* btn_prev = lv_btn_create(main_screen);
lv_obj_add_event_cb(btn_prev, event_prev_handler, LV_EVENT_ALL, user_data);
lv_obj_align(btn_prev, LV_ALIGN_CENTER, 80, 0);
lv_obj_set_size(btn_prev, 50, 50);
// 退出按键
lv_obj_t* btn_exit = lv_btn_create(main_screen);
lv_obj_add_event_cb(btn_exit, event_exit_handler, LV_EVENT_ALL, user_data);
lv_obj_align(btn_exit, LV_ALIGN_RIGHT_MID, 0, 0);
lv_obj_set_size(btn_exit, 50, 50);
// 风格设计
static lv_style_t style_pr;
lv_style_init(&style_pr);
/*Add a large outline when pressed*/
lv_style_set_outline_width(&style_pr, 30);
lv_style_set_outline_opa(&style_pr, LV_OPA_TRANSP);
lv_style_set_translate_y(&style_pr, 5);
lv_style_set_shadow_ofs_y(&style_pr, 3);
lv_style_set_bg_color(&style_pr, lv_palette_darken(LV_PALETTE_RED, 2));
lv_style_set_bg_grad_color(&style_pr, lv_palette_darken(LV_PALETTE_RED, 4));
lv_style_set_radius(&style_pr, LV_RADIUS_CIRCLE);
// 添加风格,按下去风格
lv_obj_add_style(btn_play, &style_pr, LV_STATE_PRESSED);
lv_obj_add_style(btn_next, &style_pr, LV_STATE_PRESSED);
lv_obj_add_style(btn_prev, &style_pr, LV_STATE_PRESSED);
lv_obj_add_style(btn_exit, &style_pr, LV_STATE_PRESSED);
static lv_style_t style_unpr;
lv_style_init(&style_unpr);
/*Add a large outline when pressed*/
lv_style_set_bg_color(&style_unpr, lv_color_white());
lv_style_set_radius(&style_unpr, LV_RADIUS_CIRCLE);
// 添加风格,默认风格
lv_obj_add_style(btn_play, &style_unpr, LV_STATE_DEFAULT);
lv_obj_add_style(btn_next, &style_unpr, LV_STATE_DEFAULT);
lv_obj_add_style(btn_prev, &style_unpr, LV_STATE_DEFAULT);
lv_obj_add_style(btn_exit, &style_unpr, LV_STATE_DEFAULT);
// 添加图标
lv_obj_t* img_play= lv_img_create(btn_play);
lv_img_set_src(img_play, &m_play);
lv_obj_align(img_play, LV_ALIGN_CENTER, 0, 0);
img_play = lv_img_create(btn_next);
lv_img_set_src(img_play, &m_next);
lv_obj_align(img_play, LV_ALIGN_CENTER, 0, 0);
img_play = lv_img_create(btn_prev);
lv_img_set_src(img_play, &m_prev);
lv_obj_align(img_play, LV_ALIGN_CENTER, 0, 0);
img_play = lv_img_create(btn_exit);
lv_img_set_src(img_play, &m_exit);
lv_obj_align(img_play, LV_ALIGN_CENTER, 0, 0);
2.3 滑动条绘制
用滑动条来控制音量:
为了滑动条不一样,为滑动条添加了风格。
// 添加滑动条,调节音乐
lv_obj_t* slider = lv_slider_create(main_screen);
lv_slider_set_value(slider, 30,LV_ANIM_ON);
lv_slider_set_range(slider, 0,80);
lv_obj_set_size(slider, 300, 30);
lv_obj_align(slider, LV_ALIGN_BOTTOM_MID, -50,-50);
static lv_style_t style_slider_vol, style_indicator, style_knob;
lv_style_init(&style_slider_vol);
lv_style_set_bg_opa(&style_slider_vol, LV_OPA_COVER);
lv_style_set_bg_color(&style_slider_vol, lv_color_hex3(0xbbb));
lv_style_set_radius(&style_slider_vol, LV_RADIUS_CIRCLE);
lv_style_set_pad_ver(&style_slider_vol, -2); /*Makes the indicator larger*/
lv_obj_add_style(slider, &style_slider_vol, LV_PART_MAIN);
lv_style_init(&style_indicator);
lv_style_set_bg_opa(&style_indicator, LV_OPA_COVER);
lv_style_set_bg_color(&style_indicator, lv_palette_main(LV_PALETTE_CYAN));
lv_style_set_radius(&style_indicator, LV_RADIUS_CIRCLE);
lv_obj_add_style(slider, &style_indicator, LV_PART_INDICATOR);
lv_style_init(&style_knob);
lv_style_set_bg_opa(&style_knob, LV_OPA_COVER);
lv_style_set_bg_color(&style_knob, lv_palette_main(LV_PALETTE_CYAN));
lv_style_set_border_color(&style_knob, lv_palette_darken(LV_PALETTE_CYAN, 3));
lv_style_set_border_width(&style_knob, 2);
lv_style_set_radius(&style_knob, LV_RADIUS_CIRCLE);
lv_style_set_pad_all(&style_knob, 6); /*Makes the knob larger*/
lv_obj_add_style(slider, &style_knob, LV_PART_KNOB);
lv_obj_add_event_cb(slider, slider_event_handler, LV_EVENT_ALL, user_data);
2.4 文本绘制
文本1: 显示当前应用的名字
文本2: 显示当前的作者
文本3: 显示播放音乐的内容
// 添加label 显示内容
lv_obj_t* label_title= lv_label_create(main_screen);
lv_obj_set_style_text_font(label_title, &lv_font_montserrat_22, 0);
lv_label_set_text(label_title, "Music Player");
lv_obj_align(label_title,LV_ALIGN_TOP_MID,0,0);
lv_obj_t* label_author = lv_label_create(main_screen);
lv_obj_set_style_text_font(label_author, &lv_font_montserrat_22, 0);
lv_label_set_text(label_author, "Design by Abner");
lv_obj_align(label_author, LV_ALIGN_BOTTOM_MID, 0, 0);
2.5 绘制效果
3 STM32 移植
3.1 前期LVGL的库移植
如果读者还没有完全掌握LVGL的移植,可以通过这篇文章来完成。
嵌入式系统应用-第十章 LVGL的移植到基于STM32的工程
3.2 按键功能加载
为播放按键,上一首按键,下一首按键添加按键动作,实现音乐的播放、下一首和上一首。
static void event_play_handler(lv_event_t* e)
{
lv_event_code_t code = lv_event_get_code(e);
struct player *player=lv_event_get_user_data(e);
enum PLAYER_STATUS player_status;
if (code == LV_EVENT_CLICKED) {
// rt_kprintf("button play is pressed\r\n");
player_control(player, PLAYER_CMD_GET_STATUS, &player_status);
if(player_status==PLAYER_STOP)
{
player_control(player, PLAYER_CMD_PLAY, RT_NULL);
music_show(player);
}
else
{
player_control(player, PLAYER_CMD_STOP, RT_NULL);
music_show(player);
}
}
}
static void event_next_handler(lv_event_t* e)
{
lv_event_code_t code = lv_event_get_code(e);
struct player *player=lv_event_get_user_data(e);
if (code == LV_EVENT_CLICKED) {
// rt_kprintf("button next is pressed\r\n");
player_control(player, PLAYER_CMD_NEXT, RT_NULL);
music_show(player);
}
}
static void event_prev_handler(lv_event_t* e)
{
lv_event_code_t code = lv_event_get_code(e);
struct player *player=lv_event_get_user_data(e);
if (code == LV_EVENT_CLICKED) {
// rt_kprintf("button prev is pressed\r\n");
player_control(player, PLAYER_CMD_LAST, RT_NULL);
music_show(player);
}
}
static void event_exit_handler(lv_event_t* e)
{
lv_event_code_t code = lv_event_get_code(e);
if (code == LV_EVENT_CLICKED) {
rt_kprintf("button exit is pressed\r\n");
}
}
3.3 滑动条功能加载
这里用滑动条调节音量,使用命令:PLAYER_CMD_SET_VOL
static void slider_event_handler(lv_event_t* e)
{
lv_event_code_t code = lv_event_get_code(e);
lv_obj_t* obj = lv_event_get_target(e);
struct player *player=lv_event_get_user_data(e);
if (code == LV_EVENT_VALUE_CHANGED) {
uint8_t vol=lv_slider_get_value(obj);
player_control(player, PLAYER_CMD_SET_VOL, &vol);
music_show(player);
}
}
3.4 效果展示
<iframe allowfullscreen="true" data-mediaembed="csdn" frameborder="0" id="A5DZUwGk-1731302814279" src="https://live.csdn.net/v/embed/433518"></iframe>1623277386
标签:播放器,LV,style,set,obj,lv,应用,LVGL,event From: https://blog.csdn.net/huanghongqi11/article/details/143671427