5.电子量产工具——UI系统|电子量产工具——5.UI系统

电子量产工具——5.UI系统
文章目录

  • 电子量产工具——5.UI系统
  • 软件总框架
  • 一、数据结构抽象
    • 1.1 框架
    • 1.2 代码 ui.h
  • 二、按键编程
    • 2.1 框架
    • 2.2 代码 botton.c
    • 2.3 代码 在disp_manager.c增加函数
    • 2.4 计算在LCD中 设计的按钮且 如何将文字显示在按钮正中间
  • 三、测试代码 ui_test.c
  • 四、总结

软件总框架 5.电子量产工具——UI系统|电子量产工具——5.UI系统
文章图片

一、数据结构抽象 1.1 框架 5.电子量产工具——UI系统|电子量产工具——5.UI系统
文章图片

1.2 代码 ui.h
ui.h
#ifndef _UI_H #define _UI_H#include #include #include #define BUTTON_DEFAULT_COLOR 0xff0000 #define BUTTON_PRESSED_COLOR 0x00ff00 #define BUTTON_TEXT_COLOR 0x000000struct 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

二、按键编程 2.1 框架 5.电子量产工具——UI系统|电子量产工具——5.UI系统
文章图片

2.2 代码 botton.c
botton.c
#include 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; }

2.3 代码 在disp_manager.c增加函数
void DrawTextInregionCentral(char *name, PRegion ptRegion, unsigned int dwColor) { int n = strlen(name); int iFontSize = ptRegion->iWidth / n / 2; int iOriginX, iOriginY; int i = 0; int error; FontBitMap tFontBitMap; if(iFontSize > ptRegion->iHeigh) iFontSize = ptRegion->iHeigh; iOriginX = (ptRegion->iWidth - n * iFontSize)/2 + ptRegion->iLeftUpX; iOriginY = (ptRegion->iHeigh - iFontSize)/2 + iFontSize + ptRegion->iLeftUpY; SetFontSize(iFontSize); while (name[i]) { /* get bitmap */ tFontBitMap.iCurOriginX = iOriginX; tFontBitMap.iCurOriginY = iOriginY; error = GetFontBitMap(name[i], &tFontBitMap); if (error) { printf("SelectAndInitFont err\n"); return ; }/* draw on buffer */ DrawFontBitMap(&tFontBitMap, dwColor); iOriginX = tFontBitMap.iNextOriginX; iOriginY = tFontBitMap.iNextOriginY; i++; } }void DrawFontBitMap(PFontBitMap ptFontBitMap, unsigned int dwColor) { int i, j, p, q; int x = ptFontBitMap->tRegion.iLeftUpX; int y = ptFontBitMap->tRegion.iLeftUpY; int x_max = x + ptFontBitMap->tRegion.iWidth; int y_max = y + ptFontBitMap->tRegion.iHeigh; int width = ptFontBitMap->tRegion.iWidth; unsigned char *buffer = ptFontBitMap->pucBuffer; //printf("x = %d, y = %d\n", x, y); for ( j = y, q = 0; j < y_max; j++, q++ ) { for ( i = x, p = 0; i < x_max; i++, p++ ) { if ( i < 0|| j < 0|| i >= g_tDispBuff.iXres || j >= g_tDispBuff.iYres ) continue; //image[j][i] |= bitmap->buffer[q * bitmap->width + p]; if (buffer[q * width + p]) PutPixel(i, j, dwColor); } } }void DrawRegion(PRegion ptRegion, unsigned int dwColor) { int x = ptRegion->iLeftUpX; int y = ptRegion->iLeftUpY; int width = ptRegion->iWidth; int iHeigh = ptRegion->iHeigh; int i, j; for (j = y; j < y + iHeigh; j++) { for (i = x; i < x + width; i++) { PutPixel(i, j, dwColor); } } }

2.4 计算在LCD中 设计的按钮且 如何将文字显示在按钮正中间 【5.电子量产工具——UI系统|电子量产工具——5.UI系统】5.电子量产工具——UI系统|电子量产工具——5.UI系统
文章图片

三、测试代码 ui_test.c
ui_test.c
#include #include #include #include #include #include #include #include #include #include #include #include #include #define FONTDATAMAX 4096static const unsigned char fontdata_8x16[FONTDATAMAX] = { /* 0 0x00 '^@' */ 0x00, /* 00000000 */ 0x00, /* 00000000 */ 0x00, /* 00000000 */ 0x00, /* 00000000 */ 0x00, /* 00000000 */ 0x00, /* 00000000 */ ................... ................... /* 255 0xff '' */ 0x00, /* 00000000 */ 0x00, /* 00000000 */ 0x00, /* 00000000 */ 0x00, /* 00000000 */ 0x00, /* 00000000 */ 0x00, /* 00000000 */ 0x00, /* 00000000 */ 0x00, /* 00000000 */ 0x00, /* 00000000 */ 0x00, /* 00000000 */ 0x00, /* 00000000 */ 0x00, /* 00000000 */ 0x00, /* 00000000 */ 0x00, /* 00000000 */ 0x00, /* 00000000 */ 0x00, /* 00000000 */}; void lcd_put_ascii(int x, int y, unsigned char c) { unsigned char *dots = (unsigned char *)&fontdata_8x16[c*16]; int i, b; unsigned char byte; for (i = 0; i < 16; i++) { byte = dots[i]; for (b = 7; b >= 0; b--) { if (byte & (1<\n", argv[0]); return -1; } DisplayInit(); SelectDefaultDisplay("fb"); InitDefaultDisplay(); ptBuffer = GetDisplayBuffer(); FontsRegister(); error = SelectAndInitFont("freetype", argv[1]); if (error) { printf("SelectAndInitFont err\n"); return -1; } tRegion.iLeftUpX = 100; tRegion.iLeftUpY = 100; tRegion.iWidth= 200; tRegion.iHeigh= 100; InitButton(&tButton, "test", &tRegion, NULL, NULL); tButton.OnDraw(&tButton, ptBuffer); while (1) { tButton.OnPressed(&tButton, ptBuffer, NULL); sleep(2); } return 0; }

四、总结 待写

    推荐阅读