GUI.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  1. #include "includes.h"
  2. extern const unsigned char g_apucLetter16[];
  3. extern const unsigned char g_apucCLetter16[];
  4. extern const unsigned char g_apucLetter24[];
  5. extern const unsigned char g_apucCLetter24[];
  6. extern const unsigned char g_apucFonts16[];
  7. extern const unsigned char g_apucCFonts16[];
  8. extern const unsigned char g_apucFonts24[];
  9. extern const unsigned char g_apucCFonts24[];
  10. extern const unsigned char g_apucFonts12[];
  11. extern const unsigned char g_apucLetter12[];
  12. SUT_HZK sutHzk16;
  13. SUT_HZK sutHzk12;
  14. unsigned char g_Palette[256*4];//调色板 顺序是BGRA 其中A未用
  15. u8 g_ucWarnColorId; //警告颜色
  16. u8 g_ucForeColorId; //画笔颜色索引
  17. u8 g_ucBackColorId; //背景颜色索引
  18. u8 g_ucSel_ColorId; //选定区域颜色索引
  19. u16 g_usWarnColor;//警告色
  20. u16 g_usForeColor;//前景色 RGB565值 需要根据颜色索引从当前调色板中获取
  21. u16 g_usBackColor;//背景色 RGB656值 需要根据颜色索引从当前调色板中获取
  22. void PaintToBuf(u16 x,u16 y,u16 width,u16 heigh,u8 *data);
  23. void PaintBufToLcd(u16 x,u16 y,u16 width,u16 heigh,unsigned char colorID);
  24. u16 RGB888toRGB565(unsigned char R,unsigned char G, unsigned char B);
  25. void SetPaletteByBmpFile(char *filename);
  26. void UpdateColor(void);
  27. void GuiDrawDot(u16 x,u16 y);
  28. void UpdateDot(unsigned char colorID);
  29. /***************************************************************************
  30. 在调色板中查找与期望颜色最接近的颜色并返回其索引
  31. 输入:ColorRGB 格式:0xRRGGBB 红绿蓝
  32. 返回:在调色板中,最接近ColorRGB的颜色索引
  33. 算法:对调色板内的每个色素点分别求出RGB之差的绝对值并求和,然后找出最小值所对应的色素点的索引即可
  34. ****************************************************************************/
  35. int FindColorIndex(unsigned long ColorRGB)
  36. {
  37. int i,j,k;
  38. int R,G,B;
  39. int fR,fG,fB;//计算期望RGB和调色板内每个RGB差的绝对值
  40. int fSum,fSumMin;
  41. R=(int)((ColorRGB>>16)&0xff);
  42. G=(int)((ColorRGB>>8)&0xff);
  43. B=(int)(ColorRGB&0xff);
  44. fSumMin=999999;
  45. k=0;
  46. for(i=0;i<256;i++){
  47. j=i*4;
  48. fB=g_Palette[j];
  49. fG=g_Palette[j+1];
  50. fR=g_Palette[j+2];
  51. //g_Palette[j+3]未用
  52. fB=B-fB;//B差
  53. if(fB<0)fB=-fB; //B差的绝对值
  54. fG=G-fG;//G差
  55. if(fG<0)fG=-fG;//G差的绝对值
  56. fR=R-fR;//R差
  57. if(fR<0)fR=-fR;//R差的绝对值
  58. //RGB差的绝对值求和
  59. fSum=fB+fG+fR;
  60. //找到RGB差的绝对值之和的最小值
  61. if(fSum<fSumMin){
  62. fSumMin=fSum;
  63. k=i;
  64. }
  65. }
  66. return k;
  67. }
  68. /***************************************************************************
  69. 设置前景色和背景色
  70. ****************************************************************************/
  71. void GuiSetColor(unsigned long ForeColor,unsigned long BackColor,unsigned long WarnColor,unsigned long SelColor)
  72. {
  73. //设置前景色,先在调色板找到最接近的颜色,然后作为索引保存在g_ucForeColorId中
  74. g_ucForeColorId=FindColorIndex(ForeColor);
  75. //设置背景色,先在调色板找到最接近的颜色,然后作为索引保存在g_ucBackColorId中
  76. g_ucBackColorId=FindColorIndex(BackColor);
  77. //设置警告色,先在调色板找到最接近的颜色,然后作为索引保存在g_ucBackColorId中
  78. g_ucWarnColorId=FindColorIndex(WarnColor);
  79. //设置选定颜色
  80. g_ucSel_ColorId=FindColorIndex(SelColor);
  81. UpdateColor();
  82. }
  83. /***************************************************************************
  84. 设置前景色
  85. ****************************************************************************/
  86. void GuiSetForeColor(unsigned long ForeColor)
  87. {
  88. //设置前景色,先在调色板找到最接近的颜色,然后作为索引保存在g_ucForeColorId中
  89. g_ucForeColorId=FindColorIndex(ForeColor);
  90. UpdateColor();
  91. }
  92. /***************************************************************************
  93. 设置背景色
  94. ****************************************************************************/
  95. void GuiSetBackColor(unsigned long BackColor)
  96. {
  97. //设置背景色,先在调色板找到最接近的颜色,然后作为索引保存在g_ucBackColorId中
  98. g_ucBackColorId=FindColorIndex(BackColor);
  99. UpdateColor();
  100. }
  101. /****************************************************************************
  102. 设置调色板为BMP文件中的调色板
  103. ****************************************************************************/
  104. void SetPaletteByBmpFile(char *filename)
  105. {
  106. int index;
  107. int FileLen;
  108. unsigned short type;
  109. index=GetFileIndex(filename);
  110. FileLen=GetFileLen(index);
  111. if(FileLen==0)return;//找不到文件,直接返回不处理
  112. //读出位图类型
  113. ReadFileData(index,28,2,(u8 *)&type);//读出int biBitCount ; // 每个像素所需的位数,必须是或 1,4,8 24(// 真彩色 ) 之一 (28-29 字节 )
  114. if(type!=0x0008)return;//非256色的BMP图,直接返回不处理。
  115. //读取调色板数据
  116. ReadFileData(index,54,1024,g_Palette);
  117. }
  118. /****************************************************************************
  119. RGB888toRGB565
  120. 将RGB888转为RGB565
  121. ****************************************************************************/
  122. u16 RGB888toRGB565(unsigned char R,unsigned char G, unsigned char B)
  123. {
  124. u8 r, g, b;
  125. r = R>>3; // 取R色的高5位
  126. g = G>>2; // 取G色的高6位
  127. b = B>>3; // 取B色的高5位
  128. return( (r<<11) + (g<<5) + (b<<0) );
  129. }
  130. /*****************************************
  131. 从字库文件HZK16中提取某汉子的字库数据
  132. 输入:hiByte--汉字的高字节 loByte--汉字的低字节
  133. 字库文件存储在sFlash中,字库文件名为HZK16
  134. 输出:pHzk--提取到的字库数据,需要预留足够空间存储。如果是16*16 则应该是16*16/8=32Byte
  135. 返回:失败返回0 成功返回长度
  136. ******************************************/
  137. int GetHzk(u8 hiByte,u8 loByte,u8 *pHzk,u8 font)
  138. {
  139. u8 Q,W;
  140. u32 offset;
  141. if(hiByte<0xa0 || loByte<0xa0)return 0;
  142. Q=hiByte-0xa0;
  143. W=loByte-0xa0;
  144. if(!font)
  145. {
  146. offset=(94 * (Q-1) + (W-1))* 24;
  147. return ReadFileData(sutHzk12.FileIndex,offset,24,pHzk);
  148. }else
  149. {
  150. offset=(94 * (Q-1) + (W-1))* 32;
  151. return ReadFileData(sutHzk16.FileIndex,offset,32,pHzk);
  152. }
  153. }
  154. /******************************************
  155. *汉子库初始化
  156. *输入:汉字库文件名
  157. *输出:汉字库数据首地址存放在sHZK16中
  158. *返回:1--成功 0--失败,找不到文件
  159. *******************************************/
  160. int HzkInit(const char *filename)
  161. {
  162. int i;
  163. if(g_sutFilesList.FileCount==0)return 0;
  164. if(!strcmp(filename,"HZK16"))
  165. {
  166. memset(&sutHzk16,0,sizeof(SUT_HZK));
  167. i=GetFileIndex(filename);
  168. if(i<0)return 0;
  169. sutHzk16.width=16;
  170. sutHzk16.heigh=16;
  171. sutHzk16.len=(sutHzk16.width * sutHzk16.heigh)/8;
  172. sutHzk16.FileIndex=i;
  173. sutHzk16.FileLen=g_sutFilesList.FileInfo[i].FileLen;
  174. return 1;
  175. }else if(!strcmp(filename,"HZK12")){
  176. memset(&sutHzk12,0,sizeof(SUT_HZK));
  177. i=GetFileIndex(filename);
  178. if(i<0)return 0;
  179. sutHzk12.FileIndex=i;
  180. sutHzk12.FileLen=g_sutFilesList.FileInfo[i].FileLen;
  181. return 1;
  182. }
  183. }
  184. void GuiClearAll(void)
  185. {
  186. GuiFillRect(0,STATUS_BAR_HEIGH,159,STATUS_ITEM_HEIGH-1,COLOR_WARN_RED);
  187. PaintBufToLcd(0,STATUS_ITEM_HEIGH,LCD_WIDTH,LCD_HEIGHT,g_ucBackColorId);
  188. }
  189. /******************************************
  190. GuiInit
  191. 初始化GUI
  192. *******************************************/
  193. void GuiInit(void)
  194. {
  195. if(!HzkInit(HZK16_FILE_NAME))
  196. {
  197. printf("Can't find the Hzk16File(%s)!\r\n",HZK16_FILE_NAME);
  198. }
  199. if(!HzkInit(HZK12_FILE_NAME))
  200. {
  201. printf("Can't find the Hzk12File(%s)!\r\n",HZK12_FILE_NAME);
  202. }
  203. //设置默认调色板
  204. //GuiSetDefaultPalette();//设置系统调色板为程序代码中预设的调色板
  205. SetPaletteByBmpFile("allpic.bmp");//从指定BMP文件中提取调色板作为系统调色板
  206. //设置前景色和背景色,设置前景色和背景色前需要先设置调色板
  207. GuiSetColor(COLOR_DARKBLUE,COLOR_WHITE,COLOR_RED,COLOR_SEL_BULUE);
  208. //清空所有显示区域,即将所有显示区域设置为背景色
  209. //GuiClearAll();
  210. GuiShowBmp(0,0,"welcome.bmp");
  211. //GuiShowBmp(0,0,"Palace0.bmp");
  212. }
  213. /************************************************************
  214. GuiPainToBuf
  215. 将位图数据刷到显示缓冲区
  216. 位图数据按:每行从左到右,从上行到下行扫描
  217. *************************************************************/
  218. void PaintToBuf(u16 x,u16 y,u16 width,u16 heigh,u8 *data)
  219. {
  220. int i,j,k,t;
  221. unsigned short RGB565;
  222. unsigned char R,G,B;
  223. unsigned short r,g,b;
  224. for(j=0;j<heigh;j++){
  225. t=y+j;
  226. k=j*width;
  227. for(i=0;i<width;i++){ //width=40
  228. //UpdateDot(data[k+3*i]);
  229. B=data[3*i];
  230. G=data[3*i+1];
  231. R=data[3*i+2];
  232. r = R>>3; // 取R色的高5位
  233. g = G>>2; // 取G色的高6位
  234. b = B>>3; // 取B色的高5位
  235. RGB565=((r<<11) + (g<<5) + (b<<0));
  236. LcdSendData(RGB565 >> 8);
  237. LcdSendData(RGB565 & 0xff);
  238. }
  239. }
  240. }
  241. /************************************************************
  242. PaintBufToLcd
  243. 将显示缓冲区的数据刷到LCD显示
  244. *************************************************************/
  245. void PaintBufToLcd(u16 x,u16 y,u16 width,u16 heigh,unsigned char colorID)
  246. {
  247. int i,j,k;
  248. unsigned short RGB565;
  249. unsigned short temp;
  250. if(x>=LCD_WIDTH || y>=LCD_HEIGHT)return;
  251. if(x+width>LCD_WIDTH)width=LCD_WIDTH-x-1;
  252. if(y+heigh>LCD_HEIGHT)heigh=LCD_HEIGHT-y-1;
  253. IWDG_ReloadCounter();//防止刷的死机
  254. temp=colorID*4;
  255. RGB565=RGB888toRGB565(g_Palette[temp+2],g_Palette[temp+1],g_Palette[temp]);
  256. LcdBlockWrite(x,y,x+width-1,y+heigh-1);
  257. for(j=0;j<heigh;j++){
  258. k=y+j;
  259. for(i=0;i<width;i++){
  260. LcdSendData(RGB565 >> 8);
  261. LcdSendData(RGB565 & 0xff);
  262. }
  263. }
  264. }
  265. // 12 *12 16*16
  266. void PaintCharToBuf(u16 x,u16 y,u8 hiByte,u8 loByte,u8 mode,u8 rev)
  267. {
  268. unsigned short i,j,k,m,n;
  269. unsigned short width,height;
  270. unsigned char ByteWidth,data;
  271. unsigned char colorH,colorL;
  272. unsigned char bgcolorH,bgcolorL;
  273. const unsigned char *l_pucFont,*l_pucLetter;
  274. unsigned char *p;
  275. unsigned char font;//字体
  276. unsigned char cover;//覆盖方式
  277. unsigned char sucHzk16[32];
  278. unsigned char sucHzk12[24];
  279. unsigned char lcdData;
  280. font=mode&0xf0;
  281. cover=mode&0x0f;
  282. //===先根据字符设置显示的宽、高、点阵数据指针等===
  283. k = 0;
  284. if(!hiByte) //english
  285. {
  286. if(font==0x00) //8* 12
  287. {
  288. width=8;//8
  289. height = 12; //12
  290. ByteWidth = 1;
  291. l_pucFont = g_apucFonts12;
  292. l_pucLetter = g_apucLetter12;
  293. }else{ //8*16
  294. width=8;
  295. height = 16;
  296. ByteWidth = 1;
  297. l_pucFont = g_apucFonts16;
  298. l_pucLetter = g_apucLetter16;
  299. }
  300. //
  301. while(*l_pucLetter)
  302. {
  303. if(loByte == *l_pucLetter)break;
  304. l_pucLetter ++;
  305. k ++;
  306. }
  307. //查找到字库的开始位置
  308. l_pucFont += k * ByteWidth * height;
  309. }else
  310. { //chinese
  311. if(font == 0x00)//12*16
  312. {
  313. width=16;
  314. height = 12;
  315. ByteWidth = 2;
  316. l_pucFont = sucHzk12;
  317. l_pucLetter = 0;
  318. k=0;
  319. if(sizeof(sucHzk12)!=GetHzk(hiByte,loByte,sucHzk12,font)){//从sFlash的HZK文件中读取字库数据
  320. //找不到字库,显示"口"
  321. l_pucFont=g_apucCLetter16;
  322. }
  323. }else{//16*16
  324. width=16;
  325. height = 16;
  326. ByteWidth = 2;
  327. l_pucFont = sucHzk16;
  328. l_pucLetter = 0;
  329. k=0;
  330. if(sizeof(sucHzk16)!=GetHzk(hiByte,loByte,sucHzk16,font)){//从sFlash的HZK16文件中读取字库数据
  331. //找不到字库,显示"口"
  332. l_pucFont=g_apucCLetter16;
  333. }
  334. }
  335. //
  336. }
  337. //====================再处理点阵数据==========
  338. if(y+height>LCD_HEIGHT)return;
  339. if(x+width>LCD_WIDTH)return;
  340. LcdBlockWrite(x,y,x+width-1,y+height-1);//8,12
  341. //以下直接将点阵刷到LCD,不用显存。用于测试,注意调用不要调用PaintBufToLcd,否则也刷屏了
  342. //根据字模在显存中画点
  343. for(j=0;j<height;j++){
  344. m=y+j;
  345. for(i=0;i<ByteWidth;i++){
  346. n=x+i*8;
  347. data=*l_pucFont++;
  348. for(k=0;k<8;k++){
  349. if(data & (0x80>>k)){//有点,画上前景色
  350. if(m<STATUS_BAR_HEIGH){ //bar 状态栏 颜色
  351. if(rev==0) UpdateDot(g_ucBackColorId);
  352. else UpdateDot(g_ucForeColorId);
  353. }else if(m<STATUS_ITEM_HEIGH){ //标题颜色
  354. if(rev==0) UpdateDot(g_ucBackColorId);//
  355. else UpdateDot(g_ucWarnColorId);
  356. }else if(m<MIDDLE_CONTENT_HEIGH){ //中间正文颜色
  357. if(rev==0) UpdateDot(0);//
  358. else UpdateDot(g_ucBackColorId);
  359. }else { // 下面指示文本颜色
  360. if(rev==0) UpdateDot(g_ucForeColorId);
  361. else UpdateDot(g_ucBackColorId);
  362. }
  363. }else{//无点
  364. if(m<STATUS_BAR_HEIGH){
  365. if(rev==0) UpdateDot(g_ucForeColorId);
  366. else UpdateDot(g_ucBackColorId);
  367. }else if(m<STATUS_ITEM_HEIGH){
  368. if(rev==0) UpdateDot(g_ucWarnColorId);
  369. else UpdateDot(g_ucForeColorId);
  370. }else if(m<MIDDLE_CONTENT_HEIGH){
  371. if(rev==0) UpdateDot(g_ucBackColorId);
  372. else UpdateDot(g_ucSel_ColorId);//需要和反色部分颜色一样
  373. }else {
  374. if(rev==0) UpdateDot(g_ucBackColorId);
  375. else UpdateDot(g_ucForeColorId);
  376. }
  377. }
  378. }
  379. }
  380. }
  381. }
  382. //rev 0 正常显示 否则反显
  383. void GuiShowStr(u16 x, u16 y,const char *string,u8 mode,u8 rev)
  384. {
  385. unsigned char width1,width2,heigh;
  386. unsigned char *p;
  387. unsigned short i;
  388. unsigned char font;//字体
  389. font=mode&0xf0;
  390. if(font == 0x00) //12*12 font
  391. {
  392. width1 = 12;
  393. width2 = 1;
  394. heigh=12;
  395. }
  396. else //16*16 font
  397. {
  398. width1 = 16;
  399. width2 = 1;
  400. heigh=16;
  401. }
  402. p = (unsigned char *)string;
  403. i = 0;
  404. while(*p)
  405. {
  406. if (*p > 0x9f) //chinese letter
  407. {
  408. PaintCharToBuf(x+i,y,*p, *(p+1),mode,rev);
  409. i += width1 ;
  410. p += 2;
  411. }
  412. else //english letter
  413. {
  414. PaintCharToBuf(x + i,y,0,*p,mode,rev);
  415. if(font==0){
  416. i += width2 * 8;
  417. }else{
  418. i += width2 * 8;
  419. }
  420. p ++;
  421. }
  422. }
  423. //将内容从显存刷到LCD显示
  424. //PaintBufToLcd(x,y,i,heigh);
  425. }
  426. //
  427. int WidthBytes( int nBits, int nWidth )
  428. {
  429. int nWidthBytes;
  430. nWidthBytes = nWidth;
  431. if( nBits == 1 ) nWidthBytes = ( nWidth + 7 ) / 8;
  432. else if( nBits == 4 ) nWidthBytes = ( nWidth + 1 ) / 2;
  433. else if( nBits == 16 ) nWidthBytes = nWidth * 2;
  434. else if( nBits == 24 ) nWidthBytes = nWidth * 3;
  435. else if( nBits == 32 ) nWidthBytes = nWidth * 4;
  436. while( ( nWidthBytes & 3 ) != 0 ) nWidthBytes++;
  437. return( nWidthBytes );
  438. }
  439. /************************************************************
  440. GuiShowBmp
  441. 从sFlsh中读取BMP文件并显示
  442. *************************************************************/
  443. void GuiShowBmp(u16 x,u16 y,const char * filename)
  444. {
  445. int i,w,l;
  446. int index;
  447. int FileLen;
  448. unsigned short type;
  449. unsigned int width,heigh,SizeImage,bfOffBits;
  450. unsigned char temp[480];//160
  451. index=GetFileIndex(filename);
  452. FileLen=GetFileLen(index);
  453. if(FileLen==0)return;//找不到文件,直接返回不处理
  454. //读出位图类型
  455. ReadFileData(index,28,2,(u8 *)&type);//读出int biBitCount ; // 每个像素所需的位数,必须是或 1,4,8 24(// 真彩色 ) 之一 (28-29 字节 )
  456. //if(type!=0x0008)return;//非256色的BMP图,直接返回不处理。
  457. //读出位图的宽度
  458. ReadFileData(index,18,4,(u8 *)&width); //int image_width ; // 位图的宽度,以像素为单位 (18-21 字节 )
  459. // printf("width=%u\r\n",width);
  460. //读出位图的高度
  461. ReadFileData(index,22,4,(u8 *)&heigh); //int image_heigh ; // 位图的高度,以像素为单位 (22-25 字节 )
  462. //printf("heigh=%u\r\n",heigh);
  463. if(width>LCD_WIDTH || heigh>LCD_HEIGHT)return;//超过LCD显示范围,不处理
  464. //读位图数据起始地址
  465. ReadFileData(index,10,4,(u8 *)&bfOffBits); //int bfOffBits ; // 位图数据的起始位置,以相对于位图 (10-13 字节 )
  466. //以下读位图数据放到显示缓冲区
  467. w=WidthBytes(24,width);//24位图
  468. l=heigh-1;//BMP扫描顺序是:从下往上,从左往右扫描,因此先从最后一行开始读数据
  469. LcdBlockWrite(x,y,x+width-1,y+heigh-1);
  470. for(i=0;i<heigh;i++){
  471. //printf("num===%d\r\n",bfOffBits+w*l);//*3
  472. ReadFileData(index,bfOffBits+w*l,w,temp);// *3 width*3
  473. PaintToBuf(x,y+i,width,1,temp);
  474. l--;
  475. }
  476. //从显示缓冲区刷到LCD显示
  477. //PaintBufToLcd(x,y,width,heigh);
  478. //恢复调色板到默认调色板
  479. //GuiSetDefaultPalette();
  480. }
  481. /****************************************************************************
  482. GuiClearRect
  483. 清空指定矩形区域,即将矩形区域填充背景色
  484. ****************************************************************************/
  485. void GuiClearRect(u16 x1, u16 y1, u16 x2, u16 y2)
  486. {
  487. u16 m,n;
  488. u16 i,j,h,w;
  489. if(x1>x2||y1>y2)return;
  490. if(x2>=LCD_WIDTH)x2=LCD_WIDTH-1;
  491. if(y2>=LCD_HEIGHT)y2=LCD_HEIGHT-1;
  492. w=x2-x1+1;
  493. h=y2-y1+1;
  494. LcdBlockWrite(x1,y1,x1+w-1,y1+h-1);
  495. for(j=0;j<h;j++){
  496. m=y1+j;
  497. for(i=0;i<w;i++){
  498. n=x1+i;
  499. if(m<STATUS_BAR_HEIGH){
  500. UpdateDot(g_ucForeColorId);
  501. }else if(m<STATUS_ITEM_HEIGH){
  502. UpdateDot(g_ucWarnColorId);
  503. }else {
  504. UpdateDot(g_ucBackColorId);
  505. }
  506. //UpdateDot(g_ucBackColorId);
  507. }
  508. }
  509. //PaintBufToLcd(x1,y1,w,h);
  510. }
  511. /****************************************************************************
  512. GuiClearRect
  513. 清空一个区域,填充背景色,和GuiClearRect等效,只是入口参数有区别
  514. ****************************************************************************/
  515. void GuiClearArea(u16 x, u16 y, u16 width, u16 heigh)
  516. {
  517. u16 m,n;
  518. u16 i,j;
  519. if(x>=LCD_WIDTH||y>=LCD_HEIGHT)return;
  520. if(x+width >=LCD_WIDTH)width=LCD_WIDTH-x-1;
  521. if(y+heigh >=LCD_HEIGHT)heigh=LCD_HEIGHT-y-1;
  522. LcdBlockWrite(x,y,x+width-1,y+heigh-1);
  523. for(j=0;j<heigh;j++){
  524. m=y+j;
  525. for(i=0;i<width;i++){
  526. n=x+i;
  527. UpdateDot(g_ucBackColorId);
  528. }
  529. }
  530. //PaintBufToLcd(x,y,width,heigh);
  531. }
  532. #ifdef USING_DRAW_WITH_COLOR_OPTION
  533. /****************************************************************************
  534. GuiFillRect
  535. 指定矩形区域填充前景色
  536. ****************************************************************************/
  537. void GuiFillRect(u16 x1, u16 y1, u16 x2, u16 y2,u8 colorIndex)
  538. {
  539. u16 i,j,h,w;
  540. u16 x,y;
  541. if(x1>x2||y1>y2)return;
  542. if(x1>=LCD_WIDTH || y1>=LCD_HEIGHT)return;
  543. if(x2>=LCD_WIDTH)x2=LCD_WIDTH-1;
  544. if(y2>=LCD_HEIGHT)y2=LCD_HEIGHT-1;
  545. w=x2-x1+1;
  546. h=y2-y1+1;
  547. LcdBlockWrite(x1,y1,x1+w-1,y1+h-1);
  548. for(j=0;j<h;j++){
  549. y=y1+j;
  550. if(y>(LCD_HEIGHT-1))y=LCD_HEIGHT-1;
  551. for(i=0;i<w;i++){
  552. x=x1+i;
  553. if(x>(LCD_WIDTH-1))x=LCD_WIDTH-1;
  554. {
  555. //g_aucLcdBuf[y][x]=g_ucForeColorId;
  556. switch(colorIndex)
  557. {
  558. case COLOR_SELECT_BLUE:UpdateDot(g_ucSel_ColorId);break;
  559. case COLOR_WARN_RED:UpdateDot(g_ucWarnColorId);break;
  560. case COLOR_WHITE_BACK: UpdateDot(g_usBackColor);break;
  561. case COLOR_DEFAULT_BLUE:
  562. default:UpdateDot(g_ucForeColorId);break;
  563. }
  564. }
  565. }
  566. }
  567. //PaintBufToLcd(x1,y1,w,h);
  568. }
  569. #else
  570. /****************************************************************************
  571. GuiFillRect
  572. 指定矩形区域填充前景色
  573. ****************************************************************************/
  574. void GuiFillRect(u16 x1, u16 y1, u16 x2, u16 y2)
  575. {
  576. u16 i,j,h,w;
  577. u16 x,y;
  578. if(x1>x2||y1>y2)return;
  579. if(x1>=LCD_WIDTH || y1>=LCD_HEIGHT)return;
  580. if(x2>=LCD_WIDTH)x2=LCD_WIDTH-1;
  581. if(y2>=LCD_HEIGHT)y2=LCD_HEIGHT-1;
  582. w=x2-x1+1;
  583. h=y2-y1+1;
  584. LcdBlockWrite(x1,y1,x1+w-1,y1+h-1);
  585. for(j=0;j<h;j++){
  586. y=y1+j;
  587. if(y>(LCD_HEIGHT-1))y=LCD_HEIGHT-1;
  588. for(i=0;i<w;i++){
  589. x=x1+i;
  590. if(x>(LCD_WIDTH-1))x=LCD_WIDTH-1;
  591. UpdateDot(g_ucForeColorId);
  592. }
  593. }
  594. //PaintBufToLcd(x1,y1,w,h);
  595. }
  596. #endif
  597. /****************************************************************************
  598. GuiDrawHLine
  599. 画水平线
  600. ****************************************************************************/
  601. void GuiDrawHLine(u16 x1, u16 x2, u16 y, u8 width)
  602. {
  603. if(width==0)return;
  604. if(x2<x1)return;
  605. width-=1;
  606. GuiFillRect(x1,y,x2,y+width,0);
  607. }
  608. #ifdef USING_DRAW_WITH_COLOR_OPTION//带选择填充色
  609. /****************************************************************************
  610. GuiDrawVLine
  611. 画垂直线
  612. ****************************************************************************/
  613. void GuiDrawVLine(u16 y1, u16 y2, u16 x, u8 width, u8 colorIndex)//为0时使用默认的蓝色
  614. {
  615. if(width==0)return;
  616. if(y2<y1)return;
  617. width-=1;
  618. GuiFillRect(x,y1,x+width,y2,colorIndex);
  619. }
  620. #else
  621. /****************************************************************************
  622. GuiDrawVLine
  623. 画垂直线
  624. ****************************************************************************/
  625. void GuiDrawVLine(u16 y1, u16 y2, u16 x, u8 width)
  626. {
  627. if(width==0)return;
  628. if(y2<y1)return;
  629. width-=1;
  630. GuiFillRect(x,y1,x+width,y2);
  631. }
  632. #endif
  633. /****************************************************************************
  634. GuiDrawRect
  635. 画矩形框,中间不填充
  636. ****************************************************************************/
  637. //GuiDrawRect(x+i,y-i,x+i,y+i,1);
  638. void GuiDrawRect(u16 x1, u16 y1, u16 x2, u16 y2,u8 width)
  639. {
  640. if(x1>x2||y1>y2)return;
  641. GuiDrawHLine(x1,x2,y1,width);
  642. GuiDrawHLine(x1,x2,y2,width);
  643. GuiDrawVLine(y1,y2,x1,width,COLOR_DEFAULT_BLUE);
  644. GuiDrawVLine(y1,y2,x2,width,COLOR_DEFAULT_BLUE);
  645. }
  646. /***************************************************************************
  647. 根据前景色和背景色的颜色索引g_ucForeColorId、g_ucBackColorId,从当前调色板中获取前景色和背景色g_usForeColor、g_usBackColor
  648. ****************************************************************************/
  649. void UpdateColor(void)
  650. {
  651. u16 temp,r,g,b,RGB565;
  652. u8 R,G,B;
  653. //更新前景色
  654. temp=g_ucForeColorId*4;//调色板每个索引对应4个字节,分别是BGRA (蓝、绿、红、透明度),目前A未用
  655. B=g_Palette[temp];
  656. G=g_Palette[temp+1];
  657. R=g_Palette[temp+2];
  658. r = R>>3; // 取R色的高5位
  659. g = G>>2; // 取G色的高6位
  660. b = B>>3; // 取B色的高5位
  661. RGB565=((r<<11) + (g<<5) + (b<<0));
  662. g_usForeColor=RGB565;
  663. //更新背景色
  664. temp=g_ucBackColorId*4;//调色板每个索引对应4个字节,分别是BGRA (蓝、绿、红、透明度),目前A未用
  665. B=g_Palette[temp];
  666. G=g_Palette[temp+1];
  667. R=g_Palette[temp+2];
  668. r = R>>3; // 取R色的高5位
  669. g = G>>2; // 取G色的高6位
  670. b = B>>3; // 取B色的高5位
  671. RGB565=((r<<11) + (g<<5) + (b<<0));
  672. g_usBackColor=RGB565;
  673. //更新警告色
  674. temp=g_ucWarnColorId*4;//调色板每个索引对应4个字节,分别是BGRA (蓝、绿、红、透明度),目前A未用
  675. B=g_Palette[temp];
  676. G=g_Palette[temp+1];
  677. R=g_Palette[temp+2];
  678. r = R>>3; // 取R色的高5位
  679. g = G>>2; // 取G色的高6位
  680. b = B>>3; // 取B色的高5位
  681. RGB565=((r<<11) + (g<<5) + (b<<0));
  682. g_usWarnColor=RGB565;
  683. }
  684. /****************************************************************************
  685. GuiShowArrow
  686. 显示箭头,箭头尺寸16*16 ,左上角坐标x,y
  687. 模式mode:
  688. 高四位代表填充: 0x--非填充(只有边缘线条) 1x--填充
  689. 低四位代表方向:x1--向左 x2--向右 x3--向上 x4--向下
  690. ****************************************************************************/
  691. void GuiShowArrow(u16 x,u16 y,u16 len,u8 mode)
  692. {
  693. u16 i;
  694. u8 dir,fill;
  695. // if(x>=(LCD_WIDTH-len) || y>=(LCD_HEIGHT-len))return;
  696. dir=mode&0x0f;
  697. fill=mode&0xf0;
  698. switch(dir)
  699. {
  700. case 1:
  701. for(i = 0 ; i < len ; i ++){
  702. GuiDrawRect(x+i,y-i,x+i,y+i,1);
  703. }
  704. break;
  705. case 2:
  706. for(i = 0 ; i < len ; i ++){
  707. GuiDrawRect(x-i,y-i,x-i,y+i,1);
  708. }
  709. break;
  710. case 3:
  711. for(i = 0 ; i < len ; i ++){
  712. GuiDrawRect(x-i,y+i,x+i,y+i,1);
  713. }
  714. break;
  715. case 4:
  716. for(i = 0 ; i < len ; i ++){
  717. GuiDrawRect(x-i,y-i,x+i,y-i,1);
  718. }
  719. break;
  720. default:
  721. break;
  722. }
  723. }
  724. /************************************************************************
  725. ShowButton
  726. 显示一个按钮
  727. *************************************************************************/
  728. void ShowButton(u16 x,u16 y,const char *str)
  729. {
  730. int len;
  731. len=GetStrXLen((char *)str);
  732. GuiClearRect(x,y,x+len+8,y+20);
  733. GuiShowStr(x+4,y+2,str,0x01,1);
  734. //GuiReverseRect(x,y,(x+len+8),(y+16+4));
  735. }
  736. void ShowMessageBox(char *msg)
  737. {
  738. char buf[70];
  739. u16 len,x,y;
  740. y=LCD_HEIGHT/2;
  741. GuiClearRect(0,y-20,159,y+20);
  742. GuiDrawRect(2,y-18,157,y+18,1);
  743. StrIntercept(buf,msg,18);
  744. len=GetStrXLen(buf);
  745. x=(LCD_WIDTH-len)/2;
  746. GuiShowStr(x,y-8,buf,0x01,0);
  747. }
  748. //注意调用此API前需要确定调用了
  749. //LcdBlockWrite
  750. void UpdateDot(unsigned char colorID){
  751. unsigned short RGB565;
  752. unsigned char R,G,B;
  753. unsigned short r,g,b;
  754. unsigned int temp;
  755. #if 1
  756. temp=colorID*4;
  757. B=g_Palette[temp];
  758. G=g_Palette[temp+1];
  759. R=g_Palette[temp+2];
  760. r = R>>3; // 取R色的高5位
  761. g = G>>2; // 取G色的高6位
  762. b = B>>3; // 取B色的高5位
  763. RGB565=((r<<11) + (g<<5) + (b<<0));
  764. LcdSendData(RGB565 >> 8);
  765. LcdSendData(RGB565 & 0xff);
  766. #else
  767. R=colorID;
  768. G=colorID++;
  769. B=colorID++;
  770. r = R>>3; // 取R色的高5位
  771. g = G>>2; // 取G色的高6位
  772. b = B>>3; // 取B色的高5位
  773. RGB565=((r<<11) + (g<<5) + (b<<0));
  774. LcdSendData(RGB565 >> 8);
  775. LcdSendData(RGB565 & 0xff);
  776. #endif
  777. }
  778. void lcdTestFlash(){
  779. static char i;
  780. switch(i){
  781. case 0:GuiClearAll();break;
  782. case 1:GuiShowStr(2,2,"hello world",1,0);break;
  783. case 2:GuiShowBmp(1,20,"speaker.bmp");break;
  784. case 3:GuiDrawRect(1,75, 41,85,1);break;
  785. case 4:GuiFillRect(1,87,41,100,COLOR_BLUE);break;
  786. case 5:GuiClearRect(1,87,41,100);break;
  787. case 6:GuiFillRect(1,87,41,100,COLOR_BLUE);break;
  788. case 7:GuiClearArea(1,87,41,14);break;
  789. case 8:GuiShowArrow(10,101,8,4);break;
  790. case 9:ShowButton(1,107,"菜单");break;//GuiShowStr(2,110,"reversed zone",1,1);break;
  791. default:break;
  792. }
  793. if(++i>15) i=0;
  794. }
  795. //stack init
  796. //typedef struct UI_STACK{
  797. // char sta[4];
  798. // char OK_back;//确认返回
  799. // char top;
  800. // unsigned char FastUiChange;//快捷方式进入
  801. //}UI_STACK;
  802. void Init_stack(struct UI_STACK* s)
  803. {
  804. memset(s->sta,0,sizeof(s->sta));
  805. s->OK_back=0;
  806. s->top=-1;
  807. s->FastUiChange=0;
  808. }
  809. //push stack
  810. int PushUiStack(struct UI_STACK *s,int data)
  811. {
  812. if(s->top==4){
  813. printf("stack full\r\n");
  814. return 1;
  815. }
  816. (s->top)++;
  817. s->sta[s->top]=data;
  818. return 0;
  819. }
  820. //pull stack
  821. int PullUiStack(struct UI_STACK *s)
  822. {
  823. int tmp;
  824. if(s->top<0){
  825. printf("stack is empty\r\n");
  826. return -1;
  827. }else {
  828. tmp=(s->sta)[s->top];
  829. (s->top)--;
  830. return tmp;
  831. }
  832. }