我需要在屏幕创建了一个窗口,在窗口下创建TEXT控件显示一个动态的数字,为了避免刷新是闪烁,在主函数里添加代码:
WM_SetCreateFlags(WM_CF_MEMDEV);
但是添加了之后背景图片却显示不了(不添加上面函数图片显示正常),运行报错提示:
GUI_PNG.c:Error in _png_cexcept_errot().
详细代码如下:
#include "DIALOG.h"
#include "backgrond.h"
#define ID_WINDOW_0 (GUI_ID_USER + 0x00)
#define ID_IMAGE_0 (GUI_ID_USER + 0x01)
#define ID_TEXT_0 (GUI_ID_USER + 0x02)
#define ID_IMAGE_0_IMAGE_0 0x00
#define RECOMMENDED_MEMORY (1024L * 160)
static const GUI_WIDGET_CREATE_INFO _aDialogCreate[] = {
{ WINDOW_CreateIndirect, "Window", ID_WINDOW_0, 0, 0, 800, 480, 0, 0x0, 0 },
{ IMAGE_CreateIndirect, "Image_png", ID_IMAGE_0, 0, 0, 800, 480, 0, 0, 0 },
{ TEXT_CreateIndirect, "Text1", ID_TEXT_0, 30, 198, 100, 41, 0, 0x64, 0 },
};
static const void * _GetImageById(U32 Id, U32 * pSize) {
switch (Id) {
case ID_IMAGE_0_IMAGE_0:
*pSize = sizeof(_acBackgrond);
return (const void *)_acBackgrond;
}
return NULL;
}
static void _cbDialog(WM_MESSAGE * pMsg) {
const void * pData;
WM_HWIN hItem;
U32 FileSize;
switch (pMsg->MsgId) {
case WM_INIT_DIALOG:
WINDOW_SetBkColor(pMsg->hWin, GUI_GRAY);
// Initialization of 'Image_png'
hItem = WM_GetDialogItem(pMsg->hWin, ID_IMAGE_0);
pData = _GetImageById(ID_IMAGE_0_IMAGE_0, &FileSize);
IMAGE_SetPNG(hItem, pData, FileSize);
// Initialization of 'Text1'
hItem = WM_GetDialogItem(pMsg->hWin, ID_TEXT_0);
TEXT_SetFont(hItem, GUI_FONT_D32);
TEXT_SetTextColor(hItem, GUI_MAKE_COLOR(0x00FFFFFF));
TEXT_SetText(hItem, "00");
break;
default:
WM_DefaultProc(pMsg);
break;
}
}
// USER START (Optionally insert additional public code)
void MainTask(void) {
WM_HWIN hWin;
WM_HWIN hText;
char acText[3] = { 0 };
int value=0;
WM_SetCreateFlags(WM_CF_MEMDEV);
GUI_Init();
if (GUI_ALLOC_GetNumFreeBytes() < RECOMMENDED_MEMORY) {
GUI_ErrorOut("Not enough memory available.");
return;
}
hWin = GUI_CreateDialogBox(_aDialogCreate, GUI_COUNTOF(_aDialogCreate), _cbDialog, WM_HBKWIN, 0, 0);
while (1) {
GUI_Delay(10);
sprintf(acText, "%2d", value++);
hText = WM_GetDialogItem(hWin, ID_TEXT_0);
TEXT_SetText(hText, acText);
}
}
// USER END
添加
WM_SetCreateFlags(WM_CF_MEMDEV);
错误提示:
不添加下行代码会出现闪烁,图片中看不出来
WM_SetCreateFlags(WM_CF_MEMDEV);
问题分析解决:尝试增加GUIConf.c中分配的内存,模拟器默认的内存设置为2M,需要改大分配的内存(更改宏定义)大小比如5M
#define GUI_NUMBYTES 0x500000
emWin解压缩PNG所需的最小内存量计算如下:
App。RAM要求=(xSize + 1)* ySize * 4 + 54 Kbytes
请注意,标志WM_CF_MEMDEV也会导致所需内存增加。有更好的解决方案可以避免闪烁,例如多缓冲(使用GUIDRV_Lin驱动程序)或使用缓存(使用GUIDRV_FlexColor)。
PNG 解压缩大约需要 21 Kb RAM 用于与图像大小无关的解压缩和依赖大小的字节量。RAM 要求可按以下方式计算:大约 RAM 要求 = (X-Size + 1)* Y 大小 * 4 + 21Kbytes
请注意,标志WM_CF_MEMDEV也会导致所需内存增加。有更好的解决方案可以避免闪烁,例如多缓冲(使用GUIDRV_Lin驱动程序)或使用缓存(使用GUIDRV_FlexColor)。
参考链接:https://forum.segger.com/index.php/Thread/6178-About-the-IMAGE-SetPNG-function-running-error/#post22698