当前位置: 首页 > news >正文

LVGL3(Helloworld)

1 HelloWorld

用的lvgl/examples/get_started/lv_example_get_started_1.c

代码:

/*** @file main.c**//**********************      INCLUDES*********************/#ifndef _DEFAULT_SOURCE#define _DEFAULT_SOURCE /* needed for usleep() */
#endif#include <stdlib.h>
#include <stdio.h>
#ifdef _MSC_VER#include <Windows.h>
#else#include <unistd.h>#include <pthread.h>
#endif
#include "lvgl/lvgl.h"
#include "lvgl/examples/lv_examples.h"
#include "lvgl/demos/lv_demos.h"
#include <SDL.h>#include "hal/hal.h"/**********************      DEFINES*********************//***********************      TYPEDEFS**********************//***********************  STATIC PROTOTYPES**********************//***********************  STATIC VARIABLES**********************//***********************      MACROS**********************//***********************   GLOBAL FUNCTIONS**********************/#if LV_USE_OS != LV_OS_FREERTOSint main(int argc, char **argv)
{(void)argc; /*Unused*/(void)argv; /*Unused*//*Initialize LVGL*/lv_init();/*Initialize the HAL (display, input devices, tick) for LVGL*/sdl_hal_init(320, 480);/* Run the default demo *//* To try a different demo or example, replace this with one of: *//* - lv_demo_benchmark(); *//* - lv_demo_stress(); *//* - lv_example_label_1(); *//* - etc. *///lv_demo_widgets();lv_example_get_started_1();while(1) {/* Periodically call the lv_task handler.* It could be done in a timer interrupt or an OS task too.*/uint32_t sleep_time_ms = lv_timer_handler();if(sleep_time_ms == LV_NO_TIMER_READY){sleep_time_ms =  LV_DEF_REFR_PERIOD;}
#ifdef _MSC_VERSleep(sleep_time_ms);
#elseusleep(sleep_time_ms * 1000);
#endif}return 0;
}#endif/***********************   STATIC FUNCTIONS**********************/

实际代码:

#include "../lv_examples.h"
#if LV_BUILD_EXAMPLES && LV_USE_LABEL/*** Basic example to create a "Hello world" label*/
void lv_example_get_started_1(void)
{/*Change the active screen's background color*/lv_obj_set_style_bg_color(lv_screen_active(), lv_color_hex(0x003a57), LV_PART_MAIN);/*Create a white label, set its text and align it to the center*/lv_obj_t * label = lv_label_create(lv_screen_active());lv_label_set_text(label, "Hello world");lv_obj_set_style_text_color(lv_screen_active(), lv_color_hex(0xffffff), LV_PART_MAIN);lv_obj_align(label, LV_ALIGN_CENTER, 0, 0);
}#endif

从代码看还是不难,第一部分就是init,设置分辨率,之后就是sleep。

第二部分就是对界面具体的设置。只有5行。

第一行就是设置背景,第二行是创建一个字符串,第三行是设置字符串内容。第四行是设置字符原色,第五行是设置字符位置。

好了,代码看起来真的不难。就是环境折腾好久。。。

2 Button

经常做界面的朋友都知道,GUI其实最本质的功能就是信息交互。交互一个是输入一个是输出。上面的text可以用来输出,输入呢?和机器打交道,传统的其实没有文字,因为传统的计算机不怎么懂文字(AI除外),其实最常用的就是按键。

正好里面有第二个示例就是按键的。

/*** Create a button with a label and react on click event.*/
void lv_example_get_started_2(void)
{lv_obj_t * btn = lv_button_create(lv_screen_active());     /*Add a button the current screen*/lv_obj_set_pos(btn, 10, 10);                            /*Set its position*/lv_obj_set_size(btn, 120, 50);                          /*Set its size*/lv_obj_add_event_cb(btn, btn_event_cb, LV_EVENT_ALL, NULL);           /*Assign a callback to the button*/lv_obj_t * label = lv_label_create(btn);          /*Add a label to the button*/lv_label_set_text(label, "Button");                     /*Set the labels text*/lv_obj_center(label);
}

可以看到,代码也很简单。

第一行是创建一个按键。

第二行设置按键的位置。

第三行设置按键的带下。

第四行设置按键事件的处理。

第五行创建文字,这里是按键上的文字。

第六行设置文字的内容。

第七行是设置按键上文字的位置。

按键的回调:

static void btn_event_cb(lv_event_t * e)
{lv_event_code_t code = lv_event_get_code(e);lv_obj_t * btn = lv_event_get_target_obj(e);if(code == LV_EVENT_CLICKED) {static uint8_t cnt = 0;cnt++;/*Get the first child of the button which is the label and change its text*/lv_obj_t * label = lv_obj_get_child(btn, 0);lv_label_set_text_fmt(label, "Button: %d", cnt);}
}

代码其实也很简单。首先是获取按键的指针,然后获取按键的事件,然后一个自加一的变量。最后将按键的字符串改写。这里是获取按键的第一个子对象获得字符串指针。不知道这个第一个是怎么定义的,是第一个加进去的吗?就是lv_label_create(btn)。目前还不清楚,后面再试试吧。

好了,效果如下:

http://www.dtcms.com/a/545450.html

相关文章:

  • 量化交易网站开发自己的网站做弹出广告
  • 三明市建设局网站官网网络营销方案
  • CODESYS中基于CAA File库的CSV文件读写与表格可视化全解析
  • PRA(流程机器人自动化)与智能体(AI Agent)主要区别与分析
  • GPT-3 技术报告
  • C++数据结构(链表和list)
  • 【Maven】mac安装maven
  • 有哪些网站能够免费找到素材wordpress 制作小工具栏
  • 深入剖析:仓颉语言的性能优化核心技术
  • .Net Core基于EasyCore.EventBus实现事件总线
  • 公司怎么做网站推广郑州包装设计公司
  • 阿里云服务器上构建基于PoS的以太坊2.0私有链
  • 如何把网站推广出编程代码怎么学
  • C++ 单调栈
  • 电商网站开发 上海wordpress 登陆 没反应
  • 服务器网站备案wordpress三道杠菜单
  • mysql upsert 用法(批量保存或更新)
  • 海康相机与机器人标定
  • 十年后,AI会赋予工业怎样的力量?
  • 西安市建设协会网站高级搜索入口
  • 东莞个人网站推广建设做中东市场哪个网站合适
  • Spring Boot 3 整合 LiteFlow:轻量级流程编排框架学习
  • Rust:WebSocket支持的实现
  • 代刷开通建设网站Wordpress怎么添加购买页面
  • 做网站几个步骤网址推荐你会感谢我的
  • 黑马商城day7-消息可靠性
  • wpsapi
  • Postman实现jwt发送请求
  • 网站正在备案什么是网络营销 职能是什么
  • 【AI】Prompt 提示词工程