本帖最后由 峰谷 于 2024-12-9 22:12 编辑
本次设计一个传感器数据动态展示界面,并且添加时间信息。
生成针对KEIL工具链的代码,然后再打开工程。在setup_scr_screen.c源文件里面可以看到UI相关组件的代码。
- void setup_scr_screen(lv_ui *ui)
- {
- //Write codes screen
- ui->screen = lv_obj_create(NULL);
- lv_obj_set_size(ui->screen, 480, 320);
- lv_obj_set_scrollbar_mode(ui->screen, LV_SCROLLBAR_MODE_OFF);
- //Write style for screen, Part: LV_PART_MAIN, State: LV_STATE_DEFAULT.
- lv_obj_set_style_bg_opa(ui->screen, 0, LV_PART_MAIN|LV_STATE_DEFAULT);
- //Write codes screen_nxpChart
- ui->screen_nxpChart = lv_chart_create(ui->screen);
- lv_chart_set_type(ui->screen_nxpChart, LV_CHART_TYPE_LINE);
- lv_chart_set_div_line_count(ui->screen_nxpChart, 3, 5);
- lv_chart_set_point_count(ui->screen_nxpChart, 5);
- lv_chart_set_range(ui->screen_nxpChart, LV_CHART_AXIS_PRIMARY_Y, 0, 100);
- lv_chart_set_axis_tick(ui->screen_nxpChart, LV_CHART_AXIS_PRIMARY_Y, 10, 5, 5, 4, true, 40);
- lv_chart_set_range(ui->screen_nxpChart, LV_CHART_AXIS_SECONDARY_Y, 0, 100);
- lv_chart_set_axis_tick(ui->screen_nxpChart, LV_CHART_AXIS_PRIMARY_X, 10, 5, 5, 4, true, 40);
- lv_chart_set_zoom_x(ui->screen_nxpChart, 256);
- lv_chart_set_zoom_y(ui->screen_nxpChart, 256);
- ui->screen_nxpChart_0 = lv_chart_add_series(ui->screen_nxpChart, lv_color_hex(0x000000), LV_CHART_AXIS_PRIMARY_Y);
- #if LV_USE_FREEMASTER == 0
- lv_chart_set_next_value(ui->screen_nxpChart, ui->screen_nxpChart_0, 1);
- lv_chart_set_next_value(ui->screen_nxpChart, ui->screen_nxpChart_0, 20);
- lv_chart_set_next_value(ui->screen_nxpChart, ui->screen_nxpChart_0, 30);
- lv_chart_set_next_value(ui->screen_nxpChart, ui->screen_nxpChart_0, 40);
- lv_chart_set_next_value(ui->screen_nxpChart, ui->screen_nxpChart_0, 5);
- #endif
- lv_obj_set_pos(ui->screen_nxpChart, 34, 24);
- lv_obj_set_size(ui->screen_nxpChart, 411, 273);
- lv_obj_set_scrollbar_mode(ui->screen_nxpChart, LV_SCROLLBAR_MODE_OFF);
- //Write style for screen_nxpChart, Part: LV_PART_MAIN, State: LV_STATE_DEFAULT.
- lv_obj_set_style_bg_opa(ui->screen_nxpChart, 255, LV_PART_MAIN|LV_STATE_DEFAULT);
- lv_obj_set_style_bg_color(ui->screen_nxpChart, lv_color_hex(0xffffff), LV_PART_MAIN|LV_STATE_DEFAULT);
- lv_obj_set_style_bg_grad_dir(ui->screen_nxpChart, LV_GRAD_DIR_NONE, LV_PART_MAIN|LV_STATE_DEFAULT);
- lv_obj_set_style_border_width(ui->screen_nxpChart, 1, LV_PART_MAIN|LV_STATE_DEFAULT);
- lv_obj_set_style_border_opa(ui->screen_nxpChart, 255, LV_PART_MAIN|LV_STATE_DEFAULT);
- lv_obj_set_style_border_color(ui->screen_nxpChart, lv_color_hex(0xe8e8e8), LV_PART_MAIN|LV_STATE_DEFAULT);
- lv_obj_set_style_border_side(ui->screen_nxpChart, LV_BORDER_SIDE_FULL, LV_PART_MAIN|LV_STATE_DEFAULT);
- lv_obj_set_style_radius(ui->screen_nxpChart, 0, LV_PART_MAIN|LV_STATE_DEFAULT);
- lv_obj_set_style_line_width(ui->screen_nxpChart, 2, LV_PART_MAIN|LV_STATE_DEFAULT);
- lv_obj_set_style_line_color(ui->screen_nxpChart, lv_color_hex(0xe8e8e8), LV_PART_MAIN|LV_STATE_DEFAULT);
- lv_obj_set_style_line_opa(ui->screen_nxpChart, 255, LV_PART_MAIN|LV_STATE_DEFAULT);
- lv_obj_set_style_shadow_width(ui->screen_nxpChart, 0, LV_PART_MAIN|LV_STATE_DEFAULT);
- //Write style for screen_nxpChart, Part: LV_PART_TICKS, State: LV_STATE_DEFAULT.
- lv_obj_set_style_text_color(ui->screen_nxpChart, lv_color_hex(0x151212), LV_PART_TICKS|LV_STATE_DEFAULT);
- lv_obj_set_style_text_font(ui->screen_nxpChart, &lv_font_montserratMedium_12, LV_PART_TICKS|LV_STATE_DEFAULT);
- lv_obj_set_style_text_opa(ui->screen_nxpChart, 255, LV_PART_TICKS|LV_STATE_DEFAULT);
- lv_obj_set_style_line_width(ui->screen_nxpChart, 2, LV_PART_TICKS|LV_STATE_DEFAULT);
- lv_obj_set_style_line_color(ui->screen_nxpChart, lv_color_hex(0xe8e8e8), LV_PART_TICKS|LV_STATE_DEFAULT);
- lv_obj_set_style_line_opa(ui->screen_nxpChart, 255, LV_PART_TICKS|LV_STATE_DEFAULT);
- //Write codes screen_label_1
- ui->screen_label_1 = lv_label_create(ui->screen);
- lv_label_set_text(ui->screen_label_1, "NXP MCXN947 Cool");
- lv_label_set_long_mode(ui->screen_label_1, LV_LABEL_LONG_WRAP);
- lv_obj_set_pos(ui->screen_label_1, 112, 6);
- lv_obj_set_size(ui->screen_label_1, 245, 23);
- //Write style for screen_label_1, Part: LV_PART_MAIN, State: LV_STATE_DEFAULT.
- lv_obj_set_style_border_width(ui->screen_label_1, 0, LV_PART_MAIN|LV_STATE_DEFAULT);
- lv_obj_set_style_radius(ui->screen_label_1, 0, LV_PART_MAIN|LV_STATE_DEFAULT);
- lv_obj_set_style_text_color(ui->screen_label_1, lv_color_hex(0x000000), LV_PART_MAIN|LV_STATE_DEFAULT);
- lv_obj_set_style_text_font(ui->screen_label_1, &lv_font_montserratMedium_16, LV_PART_MAIN|LV_STATE_DEFAULT);
- lv_obj_set_style_text_opa(ui->screen_label_1, 255, LV_PART_MAIN|LV_STATE_DEFAULT);
- lv_obj_set_style_text_letter_space(ui->screen_label_1, 2, LV_PART_MAIN|LV_STATE_DEFAULT);
- lv_obj_set_style_text_line_space(ui->screen_label_1, 0, LV_PART_MAIN|LV_STATE_DEFAULT);
- lv_obj_set_style_text_align(ui->screen_label_1, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN|LV_STATE_DEFAULT);
- lv_obj_set_style_bg_opa(ui->screen_label_1, 0, LV_PART_MAIN|LV_STATE_DEFAULT);
- lv_obj_set_style_pad_top(ui->screen_label_1, 0, LV_PART_MAIN|LV_STATE_DEFAULT);
- lv_obj_set_style_pad_right(ui->screen_label_1, 0, LV_PART_MAIN|LV_STATE_DEFAULT);
- lv_obj_set_style_pad_bottom(ui->screen_label_1, 0, LV_PART_MAIN|LV_STATE_DEFAULT);
- lv_obj_set_style_pad_left(ui->screen_label_1, 0, LV_PART_MAIN|LV_STATE_DEFAULT);
- lv_obj_set_style_shadow_width(ui->screen_label_1, 0, LV_PART_MAIN|LV_STATE_DEFAULT);
- //Write codes screen_digital_clock_1
- static bool screen_digital_clock_1_timer_enabled = false;
- ui->screen_digital_clock_1 = lv_dclock_create(ui->screen, "11:25:50 AM");
- if (!screen_digital_clock_1_timer_enabled) {
- lv_timer_create(screen_digital_clock_1_timer, 1000, NULL);
- screen_digital_clock_1_timer_enabled = true;
- }
- lv_obj_set_pos(ui->screen_digital_clock_1, 289, 56);
- lv_obj_set_size(ui->screen_digital_clock_1, 130, 36);
- //Write style for screen_digital_clock_1, Part: LV_PART_MAIN, State: LV_STATE_DEFAULT.
- lv_obj_set_style_radius(ui->screen_digital_clock_1, 0, LV_PART_MAIN|LV_STATE_DEFAULT);
- lv_obj_set_style_text_color(ui->screen_digital_clock_1, lv_color_hex(0xffffff), LV_PART_MAIN|LV_STATE_DEFAULT);
- lv_obj_set_style_text_font(ui->screen_digital_clock_1, &lv_font_montserratMedium_16, LV_PART_MAIN|LV_STATE_DEFAULT);
- lv_obj_set_style_text_opa(ui->screen_digital_clock_1, 255, LV_PART_MAIN|LV_STATE_DEFAULT);
- lv_obj_set_style_text_letter_space(ui->screen_digital_clock_1, 0, LV_PART_MAIN|LV_STATE_DEFAULT);
- lv_obj_set_style_text_align(ui->screen_digital_clock_1, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN|LV_STATE_DEFAULT);
- lv_obj_set_style_bg_opa(ui->screen_digital_clock_1, 255, LV_PART_MAIN|LV_STATE_DEFAULT);
- lv_obj_set_style_bg_color(ui->screen_digital_clock_1, lv_color_hex(0x2195f6), LV_PART_MAIN|LV_STATE_DEFAULT);
- lv_obj_set_style_bg_grad_dir(ui->screen_digital_clock_1, LV_GRAD_DIR_NONE, LV_PART_MAIN|LV_STATE_DEFAULT);
- lv_obj_set_style_pad_top(ui->screen_digital_clock_1, 7, LV_PART_MAIN|LV_STATE_DEFAULT);
- lv_obj_set_style_pad_right(ui->screen_digital_clock_1, 0, LV_PART_MAIN|LV_STATE_DEFAULT);
- lv_obj_set_style_pad_bottom(ui->screen_digital_clock_1, 0, LV_PART_MAIN|LV_STATE_DEFAULT);
- lv_obj_set_style_pad_left(ui->screen_digital_clock_1, 0, LV_PART_MAIN|LV_STATE_DEFAULT);
- lv_obj_set_style_shadow_width(ui->screen_digital_clock_1, 0, LV_PART_MAIN|LV_STATE_DEFAULT);
- //The custom code of screen.
- //Update current screen layout.
- lv_obj_update_layout(ui->screen);
- }
复制代码
在上段代码的倒数第二行,增加如下代码,来动态更新图表数据。
- chart = ui->screen_nxpChart;
- chart_ser1 = ui->screen_nxpChart_0;
- lv_chart_set_update_mode(ui->screen_nxpChart,LV_CHART_UPDATE_MODE_SHIFT); //设置循环刷新
- lv_timer_create(add_data, 1, NULL);
复制代码
本段代码创建了一个LVGL的内部定时间,单位是毫秒。该回调函数的实现如下:
- static void add_data(lv_timer_t * timer)
- {
- LV_UNUSED(timer);
- static uint32_t cnt = 0;
- lv_chart_set_next_value(chart, chart_ser1, lv_rand(20, 90));
- cnt++;
- }
复制代码
这样就可以使用随机数来验证图表是否能正常刷新显示。 数字时钟的UI代码如下:
- //Write codes screen_digital_clock_1
- static bool screen_digital_clock_1_timer_enabled = false;
- ui->screen_digital_clock_1 = lv_dclock_create(ui->screen, "11:25:50 AM");
- if (!screen_digital_clock_1_timer_enabled) {
- lv_timer_create(screen_digital_clock_1_timer, 1000, NULL);
- screen_digital_clock_1_timer_enabled = true;
- }
- lv_obj_set_pos(ui->screen_digital_clock_1, 289, 56);
- lv_obj_set_size(ui->screen_digital_clock_1, 130, 36);
- //Write style for screen_digital_clock_1, Part: LV_PART_MAIN, State: LV_STATE_DEFAULT.
- lv_obj_set_style_radius(ui->screen_digital_clock_1, 0, LV_PART_MAIN|LV_STATE_DEFAULT);
- lv_obj_set_style_text_color(ui->screen_digital_clock_1, lv_color_hex(0xffffff), LV_PART_MAIN|LV_STATE_DEFAULT);
- lv_obj_set_style_text_font(ui->screen_digital_clock_1, &lv_font_montserratMedium_16, LV_PART_MAIN|LV_STATE_DEFAULT);
- lv_obj_set_style_text_opa(ui->screen_digital_clock_1, 255, LV_PART_MAIN|LV_STATE_DEFAULT);
- lv_obj_set_style_text_letter_space(ui->screen_digital_clock_1, 0, LV_PART_MAIN|LV_STATE_DEFAULT);
- lv_obj_set_style_text_align(ui->screen_digital_clock_1, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN|LV_STATE_DEFAULT);
- lv_obj_set_style_bg_opa(ui->screen_digital_clock_1, 255, LV_PART_MAIN|LV_STATE_DEFAULT);
- lv_obj_set_style_bg_color(ui->screen_digital_clock_1, lv_color_hex(0x2195f6), LV_PART_MAIN|LV_STATE_DEFAULT);
- lv_obj_set_style_bg_grad_dir(ui->screen_digital_clock_1, LV_GRAD_DIR_NONE, LV_PART_MAIN|LV_STATE_DEFAULT);
- lv_obj_set_style_pad_top(ui->screen_digital_clock_1, 7, LV_PART_MAIN|LV_STATE_DEFAULT);
- lv_obj_set_style_pad_right(ui->screen_digital_clock_1, 0, LV_PART_MAIN|LV_STATE_DEFAULT);
- lv_obj_set_style_pad_bottom(ui->screen_digital_clock_1, 0, LV_PART_MAIN|LV_STATE_DEFAULT);
- lv_obj_set_style_pad_left(ui->screen_digital_clock_1, 0, LV_PART_MAIN|LV_STATE_DEFAULT);
- lv_obj_set_style_shadow_width(ui->screen_digital_clock_1, 0, LV_PART_MAIN|LV_STATE_DEFAULT);
复制代码
实物效果:
可以看到数字时钟正常运行,动态图表可以显示随机数,刷新时候无闪烁,非常不错!
接下来把开发板的I3C传感器数据采集过来,动态展示。 初始化相关的PIN脚: - const port_pin_config_t port1_11_pinD3_config = {/* Internal pull-up/down resistor is disabled */
- kPORT_PullDisable,
- /* Low internal pull resistor value is selected. */
- kPORT_LowPullResistor,
- /* Fast slew rate is configured */
- kPORT_FastSlewRate,
- /* Passive input filter is disabled */
- kPORT_PassiveFilterDisable,
- /* Open drain output is disabled */
- kPORT_OpenDrainDisable,
- /* Low drive strength is configured */
- kPORT_LowDriveStrength,
- /* Pin is configured as I3C1_PUR */
- kPORT_MuxAlt10,
- /* Digital input enabled */
- kPORT_InputBufferEnable,
- /* Digital input is not inverted */
- kPORT_InputNormal,
- /* Pin Control Register fields [15:0] are not locked */
- kPORT_UnlockRegister};
- /* PORT1_11 (pin D3) is configured as I3C1_PUR */
- PORT_SetPinConfig(PORT1, 11U, &port1_11_pinD3_config);
- const port_pin_config_t port1_16_pinF6_config = {/* Internal pull-up resistor is enabled */
- kPORT_PullUp,
- /* Low internal pull resistor value is selected. */
- kPORT_LowPullResistor,
- /* Fast slew rate is configured */
- kPORT_FastSlewRate,
- /* Passive input filter is disabled */
- kPORT_PassiveFilterDisable,
- /* Open drain output is disabled */
- kPORT_OpenDrainDisable,
- /* Low drive strength is configured */
- kPORT_LowDriveStrength,
- /* Pin is configured as I3C1_SDA */
- kPORT_MuxAlt10,
- /* Digital input enabled */
- kPORT_InputBufferEnable,
- /* Digital input is not inverted */
- kPORT_InputNormal,
- /* Pin Control Register fields [15:0] are not locked */
- kPORT_UnlockRegister};
- /* PORT1_16 (pin F6) is configured as I3C1_SDA */
- PORT_SetPinConfig(PORT1, 16U, &port1_16_pinF6_config);
- const port_pin_config_t port1_17_pinF4_config = {/* Internal pull-up resistor is enabled */
- kPORT_PullUp,
- /* Low internal pull resistor value is selected. */
- kPORT_LowPullResistor,
- /* Fast slew rate is configured */
- kPORT_FastSlewRate,
- /* Passive input filter is disabled */
- kPORT_PassiveFilterDisable,
- /* Open drain output is disabled */
- kPORT_OpenDrainDisable,
- /* Low drive strength is configured */
- kPORT_LowDriveStrength,
- /* Pin is configured as I3C1_SCL */
- kPORT_MuxAlt10,
- /* Digital input enabled */
- kPORT_InputBufferEnable,
- /* Digital input is not inverted */
- kPORT_InputNormal,
- /* Pin Control Register fields [15:0] are not locked */
- kPORT_UnlockRegister};
- /* PORT1_17 (pin F4) is configured as I3C1_SCL */
- PORT_SetPinConfig(PORT1, 17U, &port1_17_pinF4_config);
复制代码- static void add_data(lv_timer_t * timer)
- {
- static double temperature;
- LV_UNUSED(timer);
- P3T1755_ReadTemperature(&p3t1755Handle, &temperature);
- lv_chart_set_next_value(chart, chart_ser1, temperature);
- }
复制代码
用手触摸温度传感器,数据动态变化。
至此完成了第一个基于GUI Guider设计的温度传感器采集展示小项目。
|