当前位置:网站首页>电子产品量产工具(4)-UI系统实现
电子产品量产工具(4)-UI系统实现
2022-08-05 05:16:00 【乛 乛 .】
电子产品量产工具
4 UI系统设计
4.1 UI系统头文件front_manager.h
在头文件中抽象出结构体来描述按键的位置、大小信息以及被按下后的处理信息
4.1.1 按钮结构体Button
typedef int (*ONDRAW_FUNC)(struct Button *ptButton, PDispBuff ptDispBuff);
typedef int (*ONPRESSED_FUNC)(struct Button *ptButton, PDispBuff ptDispBuff, PInputEvent ptInputEvent);
....
typedef struct Button {
char *name; /*按键名*/
int status; /*按键状态*/
Region tRegion; /*按键位置、大小信息*/
ONDRAW_FUNC OnDraw; /*绘制按键*/
ONPRESSED_FUNC OnPressed;/*按键处理函数*/
}Button, *PButton;
4.1.2 front_manager.h完整代码
#ifndef _UI_H
#define _UI_H
#include <common.h>
#include <disp_manager.h>
#include <input_manager.h>
#define BUTTON_DEFAULT_COLOR 0xff0000
#define BUTTON_PRESSED_COLOR 0x00ff00
#define BUTTON_TEXT_COLOR 0x000000
struct Button;
typedef int (*ONDRAW_FUNC)(struct Button *ptButton, PDispBuff ptDispBuff);
typedef int (*ONPRESSED_FUNC)(struct Button *ptButton, PDispBuff ptDispBuff, PInputEvent ptInputEvent);
typedef struct Button {
char *name;
int status;
Region tRegion;
ONDRAW_FUNC OnDraw;
ONPRESSED_FUNC OnPressed;
}Button, *PButton;
void InitButton(PButton ptButton, char *name, PRegion ptRegion, ONDRAW_FUNC OnDraw, ONPRESSED_FUNC OnPressed);
#endif
4.2 按钮button.c
4.2.1 按钮初始化函数InitButton
void InitButton(PButton ptButton, char *name, PRegion ptRegion, ONDRAW_FUNC OnDraw, ONPRESSED_FUNC OnPressed)
{
ptButton->status = 0; /*按键状态*/
ptButton->name = name; /*按键名*/
ptButton->tRegion = *ptRegion; /*按键位置信息*/
ptButton->OnDraw = OnDraw ? OnDraw : DefaultOnDraw; /*若没有指定绘制函数,使用默认绘制函数*/
ptButton->OnPressed = OnPressed ? OnPressed : DefaultOnPressed; /*若没有指定按键处理函数,使用默认按键处理函数*/
}
4.2.2 默认绘制函数InitButton
绘制出长方形按钮
static int DefaultOnDraw(struct Button *ptButton, PDispBuff ptDispBuff)
{
/* 绘制底色 */
DrawRegion(&ptButton->tRegion, BUTTON_DEFAULT_COLOR);
/* 居中写文字 */
DrawTextInRegionCentral(ptButton->name, &ptButton->tRegion, BUTTON_TEXT_COLOR);
/* flush to lcd/web */
FlushDisplayRegion(&ptButton->tRegion, ptDispBuff);
return 0;
}
4.2.3 默认按键处理函数InitButton
按下按键后按钮变色
static int DefaultOnPressed(struct Button *ptButton, PDispBuff ptDispBuff, PInputEvent ptInputEvent)
{
unsigned int dwColor = BUTTON_DEFAULT_COLOR;
ptButton->status = !ptButton->status;
if (ptButton->status)
dwColor = BUTTON_PRESSED_COLOR;
/* 绘制底色 */
DrawRegion(&ptButton->tRegion, dwColor);
/* 居中写文字 */
DrawTextInRegionCentral(ptButton->name, &ptButton->tRegion, BUTTON_TEXT_COLOR);
/* flush to lcd/web */
FlushDisplayRegion(&ptButton->tRegion, ptDispBuff);
return 0;
}
4.2.4 button.c完整代码
#include <ui.h>
static int DefaultOnDraw(struct Button *ptButton, PDispBuff ptDispBuff)
{
/* 绘制底色 */
DrawRegion(&ptButton->tRegion, BUTTON_DEFAULT_COLOR);
/* 居中写文字 */
DrawTextInRegionCentral(ptButton->name, &ptButton->tRegion, BUTTON_TEXT_COLOR);
/* flush to lcd/web */
FlushDisplayRegion(&ptButton->tRegion, ptDispBuff);
return 0;
}
static int DefaultOnPressed(struct Button *ptButton, PDispBuff ptDispBuff, PInputEvent ptInputEvent)
{
unsigned int dwColor = BUTTON_DEFAULT_COLOR;
ptButton->status = !ptButton->status;
if (ptButton->status)
dwColor = BUTTON_PRESSED_COLOR;
/* 绘制底色 */
DrawRegion(&ptButton->tRegion, dwColor);
/* 居中写文字 */
DrawTextInRegionCentral(ptButton->name, &ptButton->tRegion, BUTTON_TEXT_COLOR);
/* flush to lcd/web */
FlushDisplayRegion(&ptButton->tRegion, ptDispBuff);
return 0;
}
void InitButton(PButton ptButton, char *name, PRegion ptRegion, ONDRAW_FUNC OnDraw, ONPRESSED_FUNC OnPressed)
{
ptButton->status = 0;
ptButton->name = name;
ptButton->tRegion = *ptRegion;
ptButton->OnDraw = OnDraw ? OnDraw : DefaultOnDraw;
ptButton->OnPressed = OnPressed ? OnPressed : DefaultOnPressed;
}
4.3 测试代码
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <linux/fb.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <disp_manager.h>
#include <font_manager.h>
#include <ui.h> /*包含必要头文件*/
int main(int argc, char **argv)
{
PDispBuff ptBuffer;
int error;
Button tButton;
Region tRegion;
if (argc != 2)
{
printf("Usage: %s <font_size>\n", argv[0]); //打印用法
return -1;
}
DisplayInit(); //显示系统初始化
SelectDefaultDisplay("fb"); //选择lcd设备显示
InitDefaultDisplay(); //初始化lcd设备显示
ptBuffer = GetDisplayBuffer();//获取内存
FontsRegister(); //字体注册
error = SelectAndInitFont("freetype", argv[1]);//字体大小设置
if (error)
{
printf("SelectAndInitFont err\n");
return -1;
}
/*指定按键形状、位置信息*/
tRegion.iLeftUpX = 200;
tRegion.iLeftUpY = 200;
tRegion.iWidth = 300;
tRegion.iHeigh = 100;
InitButton(&tButton, "yiyifufu", &tRegion, NULL, NULL); //按键初始化
tButton.OnDraw(&tButton, ptBuffer); //绘制案件即显示红色按钮与对应按键名
while (1) //按键按下颜色反转
{
tButton.OnPressed(&tButton, ptBuffer, NULL);
sleep(2);
}
return 0;
}
边栏推荐
猜你喜欢
网络信息安全运营方法论 (下)
CAN、CAN FD
Thread handler handle IntentServvice handlerThread
2021电赛资源及经验总结
AIDL detailed explanation
[Database and SQL study notes] 10. (T-SQL language) functions, stored procedures, triggers
5G中切片网络的核心技术FlexE
Kubernetes常备技能
[Pytorch study notes] 9. How to evaluate the classification results of the classifier - using confusion matrix, F1-score, ROC curve, PR curve, etc. (taking Softmax binary classification as an example)
BroadCast Receiver(广播)详解
随机推荐
MSRA proposes extreme masking model ExtreMA for learning instances and distributed visual representations
Kubernetes常备技能
六、请求处理—获取请求参数系列注解是怎样工作的?
CAN、CAN FD
《基于机器视觉测量系统的工业在线检测研究》论文笔记
【数据库和SQL学习笔记】3.数据操纵语言(DML)、SELECT查询初阶用法
服务网格istio 1.12.x安装
哥廷根大学提出CLIPSeg,能同时作三个分割任务的模型
11%的参数就能优于Swin,微软提出快速预训练蒸馏方法TinyViT
CVPR 2020 - 频谱正则化
【数据库和SQL学习笔记】9.(T-SQL语言)定义变量、高级查询、流程控制(条件、循环等)
[Pytorch study notes] 10. How to quickly create your own Dataset dataset object (inherit the Dataset class and override the corresponding method)
全尺度表示的上下文非局部对齐
CVPR2020 - 自校准卷积
[After a 12] No record for a whole week
六步搞定子网划分
单变量线性回归
CVPR最佳论文得主清华黄高团队提出首篇动态网络综述
Thread handler handle IntentServvice handlerThread
【Promise高级用法】实现并行和串行API