一、移植文件介绍
二、移植部分
第一步:创建LVGL文件夹
第二步:
构造LVGL文件夹:LVGL - GUI - lvgl -
第三步:添加文件
3.1 从examples中添加2个.c文件
3.2 从src中添加文件
draw文件
extra文件
第四步:
三、Keil软件部分
my_gui.c文件
#include "stm32f4xx.h" // Device header
#include "my_gui.h"
#include "lvgl.h"
#include "lv_port_disp_template.h"
#include "lv_port_indev_template.h"
lv_obj_t* ta_adress;
lv_obj_t* ta_password;
lv_obj_t* label_password_r;
lv_obj_t* label_password;
lv_obj_t* label_adress_r;
lv_obj_t* label_adress;
lv_obj_t* keyboard;
lv_obj_t* btn_sure;
lv_obj_t* btn_cancel;
lv_obj_t *msgbox;
int num = 0;
static void event_cb(lv_event_t* e)
{
lv_event_code_t code = lv_event_get_code(e);
lv_obj_t* target = lv_event_get_target(e);
if(target == ta_adress)
{
if(code == LV_EVENT_FOCUSED) //选中与键盘进行关联
{
lv_keyboard_set_textarea(keyboard,ta_adress);
}
else if(code == LV_EVENT_VALUE_CHANGED)
{
const char* txt = lv_textarea_get_text(ta_adress);
if(strcmp(txt, "111") == 0)
lv_label_set_text(label_adress_r, LV_SYMBOL_OK);
else
lv_label_set_text(label_adress_r, "");
}
}
else if(target == ta_password)
{
if(code == LV_EVENT_FOCUSED)
{
lv_keyboard_set_textarea(keyboard,ta_password);
}
else if(code == LV_EVENT_VALUE_CHANGED)
{
const char* txt2 = lv_textarea_get_text(ta_password);
if(strcmp(txt2, "123456") == 0)
lv_label_set_text(label_password_r, LV_SYMBOL_OK);
else
lv_label_set_text(label_password_r, "");
}
}
}
//
static void event_cb_msgbox(lv_event_t* e)
{
lv_event_code_t code = lv_event_get_code(e);
lv_obj_t *target = lv_event_get_current_target(e);
const char* txt = lv_textarea_get_text(ta_adress);
const char* txt3 = lv_textarea_get_text(ta_password);
if(target == btn_sure)
{
if(strcmp(txt, "111") == 0 && strcmp(txt3, "123456") == 0)
lv_obj_clear_flag(msgbox, LV_OBJ_FLAG_HIDDEN);
}
else if(code == LV_EVENT_PRESSED)
{
if(lv_msgbox_get_active_btn(msgbox) == 2) /* 获取按钮索引 */
lv_obj_add_flag(msgbox, LV_OBJ_FLAG_HIDDEN); /* 隐藏消息框 */
}
}
//主程序
void my_gui(void)
{
lv_init();
lv_port_disp_init();
lv_port_indev_init();
//1.设置地址输入文本框
ta_adress = lv_textarea_create(lv_scr_act());
lv_obj_set_size(ta_adress, lv_obj_get_width(lv_scr_act())/2.5, lv_obj_get_height(lv_scr_act())/12);
lv_obj_align(ta_adress, LV_ALIGN_TOP_LEFT, lv_obj_get_width(lv_scr_act())/6, lv_obj_get_height(lv_scr_act())/4);
lv_obj_remove_style(ta_adress, NULL, LV_PART_SCROLLBAR);
//lv_textarea_set_placeholder_text(ta_adress, "adress");
lv_obj_add_event_cb(ta_adress, event_cb, LV_EVENT_ALL,NULL);
//2.设置密码输入文本框
ta_password = lv_textarea_create(lv_scr_act());
lv_obj_set_size(ta_password, lv_obj_get_width(lv_scr_act())/2.5, lv_obj_get_height(lv_scr_act())/12);
lv_obj_align_to(ta_password,ta_adress,LV_ALIGN_OUT_BOTTOM_MID, 0, lv_obj_get_width(lv_scr_act())/22);
lv_obj_remove_style(ta_password, NULL, LV_PART_SCROLLBAR);
//lv_textarea_set_password_mode(ta_password, true);
//lv_textarea_set_placeholder_text(ta_password, "pick-up Code");
lv_obj_add_event_cb(ta_password, event_cb, LV_EVENT_ALL,NULL);
//3.创建地址正确标签
label_adress_r = lv_label_create(lv_scr_act());
lv_label_set_text(label_adress_r, "");
lv_obj_set_style_text_font(label_adress_r, &lv_font_montserrat_20, LV_PART_MAIN);
lv_obj_align_to(label_adress_r,ta_adress, LV_ALIGN_OUT_RIGHT_MID,lv_obj_get_width(lv_scr_act())/22,0);
//4.创建取件码正确标签
label_password_r = lv_label_create(lv_scr_act());
lv_label_set_text(label_password_r, "");
lv_obj_set_style_text_font(label_password_r, &lv_font_montserrat_20, LV_PART_MAIN);
lv_obj_align_to(label_password_r,ta_password, LV_ALIGN_OUT_RIGHT_MID,lv_obj_get_width(lv_scr_act())/22,0);
//5.创建地址标签
label_adress = lv_label_create(lv_scr_act());
lv_label_set_text(label_adress, "adress");
lv_obj_set_style_text_font(label_adress, &lv_font_montserrat_14, LV_PART_MAIN);
lv_obj_align_to(label_adress,ta_adress, LV_ALIGN_OUT_LEFT_MID,0,0);
//6.创建取件码标签
label_password = lv_label_create(lv_scr_act());
lv_label_set_text(label_password, "Code");
lv_obj_set_style_text_font(label_password, &lv_font_montserrat_14, LV_PART_MAIN);
lv_obj_align_to(label_password,ta_password, LV_ALIGN_OUT_LEFT_MID,0,0);
//7.确定按钮
btn_sure = lv_btn_create(lv_scr_act());
lv_obj_set_size(btn_sure, lv_obj_get_width(lv_scr_act())/6, lv_obj_get_height(lv_scr_act())/8);
lv_obj_align_to(btn_sure, ta_password, LV_ALIGN_OUT_BOTTOM_LEFT, 0, lv_obj_get_height(lv_scr_act())/9);
lv_obj_t* btn_sure_label = lv_label_create(btn_sure);
lv_obj_align(btn_sure_label, LV_ALIGN_CENTER, 0 ,0 );
lv_label_set_text(btn_sure_label, "LOGIN");
lv_obj_set_style_text_font(btn_sure_label, &lv_font_montserrat_12, LV_PART_MAIN);
lv_obj_add_event_cb(btn_sure, event_cb_msgbox, LV_EVENT_PRESSED,NULL);
//8.取消按钮
btn_cancel = lv_btn_create(lv_scr_act());
lv_obj_set_size(btn_cancel, lv_obj_get_width(lv_scr_act())/6, lv_obj_get_height(lv_scr_act())/8);
lv_obj_align_to(btn_cancel, btn_sure, LV_ALIGN_OUT_RIGHT_MID, lv_obj_get_width(lv_scr_act())/14, 0);
lv_obj_t* btn_cancel_label = lv_label_create(btn_cancel);
lv_obj_align(btn_cancel_label, LV_ALIGN_CENTER, 0 ,0 );
lv_label_set_text(btn_cancel_label, "CANCEL");
lv_obj_set_style_text_font(btn_cancel_label, &lv_font_montserrat_12, LV_PART_MAIN);
//9.消息框
static const char *btns[] = { " ", " ", "OK","" };
msgbox = lv_msgbox_create( lv_scr_act(), "LOGIN", "Pickup successful!", btns, true );
lv_obj_align(msgbox, LV_ALIGN_CENTER, -(lv_obj_get_width(lv_scr_act())/5), 0);
lv_obj_set_size(msgbox, lv_obj_get_width(lv_scr_act())/2.5, lv_obj_get_height(lv_scr_act())/2);
lv_obj_add_flag(msgbox, LV_OBJ_FLAG_HIDDEN);
//获取到按钮部分
lv_obj_t* btn = lv_msgbox_get_btns(msgbox);
//按钮背景设为透明
lv_obj_set_style_bg_opa(btn, 0, LV_PART_ITEMS);
lv_obj_set_style_shadow_width(btn, 0, LV_PART_ITEMS);
lv_obj_set_style_text_color(btn, lv_color_hex(0xff0000),LV_PART_ITEMS);
lv_obj_add_event_cb(ta_adress, event_cb_msgbox, LV_EVENT_VALUE_CHANGED,NULL);
lv_obj_add_event_cb(ta_password, event_cb_msgbox, LV_EVENT_VALUE_CHANGED,NULL);
lv_obj_add_event_cb(msgbox, event_cb_msgbox, LV_EVENT_PRESSED,NULL);
//设置键盘
keyboard = lv_keyboard_create(lv_scr_act());
lv_keyboard_set_mode(keyboard, LV_KEYBOARD_MODE_NUMBER);
lv_obj_align(keyboard, LV_ALIGN_RIGHT_MID, -10, 0);
lv_obj_set_size(keyboard, lv_obj_get_width(lv_scr_act())/2.5, lv_obj_get_height(lv_scr_act())/1.5);
}
main.c文件
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "lcd.h"
#include "key.h"
#include "touch.h"
#include "timer.h"
#include "my_gui.h"
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置系统中断优先级分组2
delay_init(168); //初始化延时函数
uart_init(115200); //初始化串口波特率为115200
LED_Init(); //初始化LED
LCD_Init(); //LCD初始化
KEY_Init(); //按键初始化
TIM3_Int_Init(1000, 84);
my_gui();
while(1)
{
delay_ms(5);
lv_timer_handler();
}
}
记得定义定时器,需要定时器驱动
//定时器3中断服务函数
void TIM3_IRQHandler(void)
{
if(TIM_GetITStatus(TIM3,TIM_IT_Update)==SET) //溢出中断
{
LED1=!LED1;//DS1翻转
lv_tick_inc(1);
}
TIM_ClearITPendingBit(TIM3,TIM_IT_Update); //清除中断标志位
}
四、VS模拟器部分
五、实物连接部分