GUI.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152
  1. /****************************************Copyright (c)**************************************************
  2. * File Name: GUI.c
  3. * Function Describe:
  4. * Explain:
  5. * Writer: ShiLiangWen
  6. * Date: 2016-1-8
  7. ********************************************************************************************************/
  8. #include "includes.h"
  9. extern const unsigned char g_apucLetter16[];
  10. extern const unsigned char g_apucCLetter16[];
  11. extern const unsigned char g_apucLetter24[];
  12. extern const unsigned char g_apucCLetter24[];
  13. extern const unsigned char g_apucFonts16[];
  14. extern const unsigned char g_apucCFonts16[];
  15. extern const unsigned char g_apucFonts24[];
  16. extern const unsigned char g_apucCFonts24[];
  17. //字库结构体?
  18. SUT_HZK sutHzk16;
  19. unsigned char g_Palette[256*4];//调色板 顺序是BGRA 其中A未用
  20. unsigned char g_aucLcdBuf[LCD_HEIGHT][LCD_WIDTH];//显示缓冲区
  21. //unsigned char g_aucLcdCopyBuf[LCD_HEIGHT][LCD_WIDTH];//拷贝显示缓冲区
  22. u8 g_ucWarnColorId; //警告颜色
  23. u8 g_ucForeColorId; //画笔颜色索引
  24. u8 g_ucBackColorId; //背景颜色索引
  25. u16 g_usWarnColor;//警告色
  26. u16 g_usForeColor;//前景色 RGB565值 需要根据颜色索引从当前调色板中获取
  27. u16 g_usBackColor;//背景色 RGB656值 需要根据颜色索引从当前调色板中获取
  28. void PaintToBuf(u16 x,u16 y,u16 width,u16 heigh,u8 *data);
  29. void PaintBufToLcd(u16 x,u16 y,u16 width,u16 heigh);
  30. u16 RGB888toRGB565(unsigned char R,unsigned char G, unsigned char B);
  31. void SetPaletteByBmpFile(char *filename);
  32. void UpdateColor(void);
  33. void GuiDrawDot(u16 x,u16 y);
  34. /***************************************************************************
  35. 在调色板中查找与期望颜色最接近的颜色并返回其索引
  36. 输入:ColorRGB 格式:0xRRGGBB 红绿蓝
  37. 返回:在调色板中,最接近ColorRGB的颜色索引
  38. 算法:对调色板内的每个色素点分别求出RGB之差的绝对值并求和,然后找出最小值所对应的色素点的索引即可
  39. ****************************************************************************/
  40. int FindColorIndex(unsigned long ColorRGB)
  41. {
  42. int i,j,k;
  43. int R,G,B;
  44. int fR,fG,fB;//计算期望RGB和调色板内每个RGB差的绝对值
  45. int fSum,fSumMin;
  46. R=(int)((ColorRGB>>16)&0xff);
  47. G=(int)((ColorRGB>>8)&0xff);
  48. B=(int)(ColorRGB&0xff);
  49. fSumMin=999999;
  50. k=0;
  51. for(i=0;i<256;i++){
  52. j=i*4;
  53. fB=g_Palette[j];
  54. fG=g_Palette[j+1];
  55. fR=g_Palette[j+2];
  56. //g_Palette[j+3]未用
  57. fB=B-fB;//B差
  58. if(fB<0)fB=-fB; //B差的绝对值
  59. fG=G-fG;//G差
  60. if(fG<0)fG=-fG;//G差的绝对值
  61. fR=R-fR;//R差
  62. if(fR<0)fR=-fR;//R差的绝对值
  63. //RGB差的绝对值求和
  64. fSum=fB+fG+fR;
  65. //找到RGB差的绝对值之和的最小值
  66. if(fSum<fSumMin){
  67. fSumMin=fSum;
  68. k=i;
  69. }
  70. }
  71. return k;
  72. }
  73. /***************************************************************************
  74. 设置前景色和背景色
  75. ****************************************************************************/
  76. void GuiSetColor(unsigned long ForeColor,unsigned long BackColor,unsigned long WarnColor)
  77. {
  78. //设置前景色,先在调色板找到最接近的颜色,然后作为索引保存在g_ucForeColorId中
  79. g_ucForeColorId=FindColorIndex(ForeColor);
  80. //设置背景色,先在调色板找到最接近的颜色,然后作为索引保存在g_ucBackColorId中
  81. g_ucBackColorId=FindColorIndex(BackColor);
  82. //设置警告色,先在调色板找到最接近的颜色,然后作为索引保存在g_ucBackColorId中
  83. g_ucWarnColorId=FindColorIndex(WarnColor);
  84. UpdateColor();
  85. }
  86. /***************************************************************************
  87. 设置前景色
  88. ****************************************************************************/
  89. void GuiSetForeColor(unsigned long ForeColor)
  90. {
  91. //设置前景色,先在调色板找到最接近的颜色,然后作为索引保存在g_ucForeColorId中
  92. g_ucForeColorId=FindColorIndex(ForeColor);
  93. UpdateColor();
  94. }
  95. /***************************************************************************
  96. 设置背景色
  97. ****************************************************************************/
  98. void GuiSetBackColor(unsigned long BackColor)
  99. {
  100. //设置背景色,先在调色板找到最接近的颜色,然后作为索引保存在g_ucBackColorId中
  101. g_ucBackColorId=FindColorIndex(BackColor);
  102. UpdateColor();
  103. }
  104. /****************************************************************************
  105. *设置调色板为默认调色板
  106. *所谓默认调色板,是存放在代码的调色板
  107. ****************************************************************************/
  108. void GuiSetDefaultPalette(void)
  109. {
  110. int i,j,k;
  111. j=0;
  112. for(i=0;i<256;i++){
  113. k=i*4;
  114. g_Palette[k]=g_cucPalette[j++];
  115. g_Palette[k+1]=g_cucPalette[j++];
  116. g_Palette[k+2]=g_cucPalette[j++];
  117. g_Palette[k+3]=0;
  118. }
  119. }
  120. /****************************************************************************
  121. 设置调色板为BMP文件中的调色板
  122. ****************************************************************************/
  123. void SetPaletteByBmpFile(char *filename)
  124. {
  125. int index;
  126. int FileLen;
  127. unsigned short type;
  128. index=GetFileIndex(filename);
  129. FileLen=GetFileLen(index);
  130. if(FileLen==0)return;//找不到文件,直接返回不处理
  131. //读出位图类型
  132. ReadFileData(index,28,2,(u8 *)&type);//读出int biBitCount ; // 每个像素所需的位数,必须是或 1,4,8 24(// 真彩色 ) 之一 (28-29 字节 )
  133. if(type!=0x0008)return;//非256色的BMP图,直接返回不处理。
  134. //读取调色板数据
  135. ReadFileData(index,54,1024,g_Palette);
  136. }
  137. /****************************************************************************
  138. RGB888toRGB565
  139. 将RGB888转为RGB565
  140. ****************************************************************************/
  141. u16 RGB888toRGB565(unsigned char R,unsigned char G, unsigned char B)
  142. {
  143. u8 r, g, b;
  144. r = R>>3; // 取R色的高5位
  145. g = G>>2; // 取G色的高6位
  146. b = B>>3; // 取B色的高5位
  147. return( (r<<11) + (g<<5) + (b<<0) );
  148. }
  149. /*****************************************
  150. 从字库文件HZK16中提取某汉子的字库数据
  151. 输入:hiByte--汉字的高字节 loByte--汉字的低字节
  152. 字库文件存储在sFlash中,字库文件名为HZK16
  153. 输出:pHzk--提取到的字库数据,需要预留足够空间存储。如果是16*16 则应该是16*16/8=32Byte
  154. 返回:失败返回0 成功返回长度
  155. ******************************************/
  156. int GetHzk16(u8 hiByte,u8 loByte,u8 *pHzk)
  157. {
  158. u8 Q,W;
  159. u32 offset;
  160. if(hiByte<0xa0 || loByte<0xa0)return 0;
  161. Q=hiByte-0xa0;
  162. W=loByte-0xa0;
  163. //offset=(94*(区码-1)+(位码-1))*32
  164. offset=(94 * (Q-1) + (W-1))* 32;
  165. return ReadFileData(sutHzk16.FileIndex,offset,32,pHzk);
  166. }
  167. /******************************************
  168. *汉子库初始化
  169. *输入:汉字库文件名
  170. *输出:汉字库数据首地址存放在sHZK16中
  171. *返回:1--成功 0--失败,找不到文件
  172. *******************************************/
  173. int HzkInit(const char *filename)
  174. {
  175. int i;
  176. if(g_sutFilesList.FileCount==0)return 0;
  177. memset(&sutHzk16,0,sizeof(SUT_HZK));
  178. i=GetFileIndex(filename);
  179. if(i<0)return 0;
  180. sutHzk16.width=16;
  181. sutHzk16.heigh=16;
  182. sutHzk16.len=(sutHzk16.width * sutHzk16.heigh)/8;
  183. sutHzk16.FileIndex=i;
  184. sutHzk16.FileLen=g_sutFilesList.FileInfo[i].FileLen;
  185. return 1;
  186. }
  187. /*************************************************************
  188. 将整个显示区域填充为背景色
  189. **************************************************************/
  190. void GuiClearAll(void)
  191. {
  192. memset(g_aucLcdBuf,g_ucBackColorId,sizeof(g_aucLcdBuf));
  193. PaintBufToLcd(0,0,LCD_WIDTH,LCD_HEIGHT);
  194. }
  195. /******************************************
  196. GuiInit
  197. 初始化GUI
  198. *******************************************/
  199. void GuiInit(void)
  200. {
  201. if(!HzkInit(HZK16_FILE_NAME))
  202. {
  203. printf("Can't find the HzkFile(%s)!\r\n",HZK16_FILE_NAME);
  204. }
  205. //设置默认调色板
  206. //GuiSetDefaultPalette();//设置系统调色板为程序代码中预设的调色板
  207. SetPaletteByBmpFile("logo.bmp");//从指定BMP文件中提取调色板作为系统调色板
  208. //设置前景色和背景色,设置前景色和背景色前需要先设置调色板
  209. GuiSetColor(COLOR_DARKBLUE,COLOR_WHITE,COLOR_RED);
  210. //清空所有显示区域,即将所有显示区域设置为背景色
  211. GuiClearAll();
  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. for(j=0;j<heigh;j++){
  222. t=y+j;
  223. k=j*width;
  224. for(i=0;i<width;i++){
  225. g_aucLcdBuf[t][x+i]=data[k+i];
  226. }
  227. }
  228. }
  229. /************************************************************
  230. PaintBufToLcd
  231. 将显示缓冲区的数据刷到LCD显示
  232. *************************************************************/
  233. void PaintBufToLcd(u16 x,u16 y,u16 width,u16 heigh)
  234. {
  235. int i,j,k;
  236. unsigned short RGB565;
  237. unsigned char data;
  238. unsigned char R,G,B;
  239. unsigned short r,g,b;
  240. unsigned short temp;
  241. if(x>=LCD_WIDTH || y>=LCD_HEIGHT)return;
  242. if(x+width>LCD_WIDTH)width=LCD_WIDTH-x-1;
  243. if(y+heigh>LCD_HEIGHT)heigh=LCD_HEIGHT-y-1;
  244. IWDG_ReloadCounter();//防止刷的死机
  245. LcdBlockWrite(x,y,x+width-1,y+heigh-1);
  246. for(j=0;j<heigh;j++){
  247. k=y+j;
  248. for(i=0;i<width;i++){
  249. data=g_aucLcdBuf[k][x+i];//得到颜色索引
  250. temp=data*4;//调色板每个索引对应4个字节,分别是BGRA (蓝、绿、红、透明度),目前A未用
  251. B=g_Palette[temp];
  252. G=g_Palette[temp+1];
  253. R=g_Palette[temp+2];
  254. //RGB565=RGB888toRGB565(R,G,B);
  255. r = R>>3; // 取R色的高5位
  256. g = G>>2; // 取G色的高6位
  257. b = B>>3; // 取B色的高5位
  258. RGB565=((r<<11) + (g<<5) + (b<<0));
  259. LcdSendData(RGB565 >> 8);
  260. LcdSendData(RGB565 & 0xff);
  261. }
  262. }
  263. }
  264. /***************************************************************************
  265. PaintCharToBuf
  266. 在显示缓存中写字符,并未刷到LCD显示
  267. ****************************************************************************/
  268. void PaintCharToBuf(u16 x,u16 y,u8 hiByte,u8 loByte,u8 mode)
  269. {
  270. unsigned short i,j,k,m,n;
  271. unsigned short width,height;
  272. unsigned char ByteWidth,data;
  273. unsigned char colorH,colorL;
  274. unsigned char bgcolorH,bgcolorL;
  275. const unsigned char *l_pucFont,*l_pucLetter;
  276. unsigned char *p;
  277. unsigned char font;//字体
  278. unsigned char cover;//覆盖方式
  279. unsigned char sucHzk16[32];
  280. font=mode&0xf0;
  281. cover=mode&0x0f;
  282. //=================先根据字符设置显示的宽、高、点阵数据指针等===
  283. k = 0;
  284. if(!hiByte) //english
  285. {
  286. if(font==0x00) //8 * 16
  287. {
  288. width=8;
  289. height = 16;
  290. ByteWidth = 1;
  291. l_pucFont = g_apucFonts16;
  292. l_pucLetter = g_apucLetter16;
  293. }else{ //16 * 24
  294. width=16;
  295. height = 24;
  296. ByteWidth = 2;
  297. l_pucFont = g_apucFonts24;
  298. l_pucLetter = g_apucLetter24;
  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. }
  310. else
  311. { //chinese
  312. if(font == 0x00)//16 * 16
  313. {
  314. width=16;
  315. height = 16;
  316. ByteWidth = 2;
  317. l_pucFont = sucHzk16;
  318. l_pucLetter = 0;
  319. k=0;
  320. if(32!=GetHzk16(hiByte,loByte,sucHzk16)){//从sFlash的HZK16文件中读取字库数据
  321. //找不到字库,显示"口"
  322. l_pucFont=g_apucCLetter16;
  323. }
  324. }else{//24 * 24
  325. width=24;
  326. height = 24;
  327. ByteWidth = 3;
  328. l_pucFont = g_apucCFonts24;
  329. l_pucLetter = g_apucCLetter24;
  330. while(*l_pucLetter)
  331. {
  332. if(hiByte == *l_pucLetter && loByte == *(l_pucLetter + 1))break;
  333. l_pucLetter += 2;
  334. k ++;
  335. }
  336. //查找到字库的开始位置
  337. l_pucFont += k * ByteWidth * height;
  338. }
  339. //
  340. }
  341. //====================再处理点阵数据==========
  342. if(y+height>=LCD_HEIGHT)return;
  343. if(x+width>=LCD_WIDTH)return;
  344. #if 1
  345. //根据字模在显存中画点
  346. for(j=0;j<height;j++){
  347. m=y+j;
  348. for(i=0;i<ByteWidth;i++){
  349. n=x+i*8;
  350. data=*l_pucFont++;
  351. for(k=0;k<8;k++){
  352. if(data & (0x80>>k)){//有点,画上前景色
  353. g_aucLcdBuf[m][n+k]=g_ucForeColorId;
  354. }else{//无点
  355. if(cover){//覆盖模式下,无点的位置也填上背景颜色
  356. g_aucLcdBuf[m][n+k]=g_ucBackColorId;
  357. }
  358. }
  359. }
  360. }
  361. }
  362. #else
  363. //以下直接将点阵刷到LCD,不用显存。用于测试,注意调用不要调用PaintBufToLcd,否则也刷屏了
  364. colorH=0xff;
  365. colorL=0xff;
  366. bgcolorH=0x00;
  367. bgcolorL=0x00;
  368. LcdBlockWrite(x,y,x+width-1,y+height-1);
  369. for(j=0;j<height;j++){
  370. for(i=0;i<ByteWidth;i++){
  371. data=*l_pucFont++;
  372. for(k=0;k<8;k++){
  373. if(data & (0x80>>k)){
  374. LcdSendData(colorH);
  375. LcdSendData(colorL);
  376. }else{
  377. //if(cover){
  378. LcdSendData(bgcolorH);
  379. LcdSendData(bgcolorH);
  380. //}
  381. }
  382. }
  383. }
  384. }
  385. #endif
  386. }
  387. void GuiShowStr(u16 x, u16 y,const char *string,u8 mode)
  388. {
  389. unsigned char width1,width2,heigh;
  390. unsigned char *p;
  391. unsigned short i;
  392. unsigned char font;//字体
  393. font=mode&0xf0;
  394. if(font == 0x00) //16bit font
  395. {
  396. width1 = 2;
  397. width2 = 1;
  398. heigh=16;
  399. }
  400. else //24bit font
  401. {
  402. width1 = 3;
  403. width2 = 1;
  404. heigh=24;
  405. }
  406. p = (unsigned char *)string;
  407. i = 0;
  408. while(*p)
  409. {
  410. if (*p > 0x9f) //chinese letter
  411. {
  412. PaintCharToBuf(x+i,y,*p, *(p+1),mode);
  413. i += width1 * 8;
  414. p += 2;
  415. }
  416. else //english letter
  417. {
  418. PaintCharToBuf(x + i,y,0,*p,mode);
  419. if(font==0){
  420. i += width2 * 8;
  421. }else{
  422. i += width2 * 16;
  423. }
  424. p ++;
  425. }
  426. }
  427. //将内容从显存刷到LCD显示
  428. PaintBufToLcd(x,y,i,heigh);
  429. }
  430. /************************************************************
  431. 从代码区读出图片并显示
  432. *************************************************************/
  433. void GuiShowPic(u8 x,u8 y,const unsigned char *pic)
  434. {
  435. int i,j,len;
  436. unsigned int width,heigh;
  437. u8 R,G,B;
  438. u16 RGB565;
  439. const unsigned char *data;
  440. HEADGRAY headgray;
  441. memcpy(&headgray,pic,sizeof(HEADGRAY));
  442. LcdBlockWrite(x,y,x+headgray.w-1,y+headgray.h-1);
  443. len=headgray.w * headgray.h;
  444. data=pic+sizeof(HEADGRAY);
  445. for(i=0;i<len;i++){
  446. R=data[i];
  447. R=g_cucPalette[R*3];
  448. G=data[i+1];
  449. G=g_cucPalette[G*3+1];
  450. B=data[i+2];
  451. B=g_cucPalette[B*3+2];
  452. RGB565=RGB888toRGB565(R,G,B);
  453. LcdSendData(RGB565 >> 8);
  454. LcdSendData(RGB565 & 0xff);
  455. }
  456. }
  457. /************************************************************
  458. GuiShowBmp
  459. 从sFlsh中读取BMP文件并显示
  460. *************************************************************/
  461. void GuiShowBmp(u16 x,u16 y,const char * filename)
  462. {
  463. int i,w,l;
  464. int index;
  465. int FileLen;
  466. unsigned short type;
  467. unsigned int width,heigh,SizeImage,bfOffBits;
  468. unsigned char temp[160];
  469. index=GetFileIndex(filename);
  470. FileLen=GetFileLen(index);
  471. if(FileLen==0)return;//找不到文件,直接返回不处理
  472. //读出位图类型
  473. ReadFileData(index,28,2,(u8 *)&type);//读出int biBitCount ; // 每个像素所需的位数,必须是或 1,4,8 24(// 真彩色 ) 之一 (28-29 字节 )
  474. if(type!=0x0008)return;//非256色的BMP图,直接返回不处理。
  475. //读出位图的宽度
  476. ReadFileData(index,18,4,(u8 *)&width); //int image_width ; // 位图的宽度,以像素为单位 (18-21 字节 )
  477. // printf("width=%u\r\n",width);
  478. //读出位图的高度
  479. ReadFileData(index,22,4,(u8 *)&heigh); //int image_heigh ; // 位图的高度,以像素为单位 (22-25 字节 )
  480. //printf("heigh=%u\r\n",heigh);
  481. if(width>LCD_WIDTH || heigh>LCD_HEIGHT)return;//超过LCD显示范围,不处理
  482. //读位图数据起始地址
  483. ReadFileData(index,10,4,(u8 *)&bfOffBits); //int bfOffBits ; // 位图数据的起始位置,以相对于位图 (10-13 字节 )
  484. //读取调色板数据
  485. ReadFileData(index,54,1024,g_Palette);
  486. //以下读位图数据放到显示缓冲区
  487. i=width%4;
  488. if(i>0){//每行像素数不是4的倍数,则需要补到4的倍数
  489. w=width + (4-width%4);//每行字节数为4的倍数,不够的补齐
  490. }else{
  491. w=width;
  492. }
  493. l=heigh-1;//BMP扫描顺序是:从下往上,从左往右扫描,因此先从最后一行开始读数据
  494. for(i=0;i<heigh;i++){
  495. ReadFileData(index,bfOffBits+w*l,width,temp);
  496. PaintToBuf(x,y+i,width,1,temp);
  497. l--;
  498. }
  499. //从显示缓冲区刷到LCD显示
  500. PaintBufToLcd(x,y,width,heigh);
  501. //恢复调色板到默认调色板
  502. //GuiSetDefaultPalette();
  503. }
  504. /****************************************************************************
  505. GuiClearRect
  506. 清空指定矩形区域,即将矩形区域填充背景色
  507. ****************************************************************************/
  508. void GuiClearRect(u16 x1, u16 y1, u16 x2, u16 y2)
  509. {
  510. u16 m,n;
  511. u16 i,j,h,w;
  512. if(x1>x2||y1>y2)return;
  513. if(x2>=LCD_WIDTH)x2=LCD_WIDTH-1;
  514. if(y2>=LCD_HEIGHT)y2=LCD_HEIGHT-1;
  515. w=x2-x1+1;
  516. h=y2-y1+1;
  517. for(j=0;j<h;j++){
  518. m=y1+j;
  519. for(i=0;i<w;i++){
  520. n=x1+i;
  521. g_aucLcdBuf[m][n]=g_ucBackColorId;
  522. }
  523. }
  524. PaintBufToLcd(x1,y1,w,h);
  525. }
  526. /****************************************************************************
  527. GuiClearRect
  528. 清空一个区域,填充背景色,和GuiClearRect等效,只是入口参数有区别
  529. ****************************************************************************/
  530. void GuiClearArea(u16 x, u16 y, u16 width, u16 heigh)
  531. {
  532. u16 m,n;
  533. u16 i,j;
  534. if(x>=LCD_WIDTH||y>=LCD_HEIGHT)return;
  535. if(x+width >=LCD_WIDTH)width=LCD_WIDTH-x-1;
  536. if(y+heigh >=LCD_HEIGHT)heigh=LCD_HEIGHT-y-1;
  537. for(j=0;j<heigh;j++){
  538. m=y+j;
  539. for(i=0;i<width;i++){
  540. n=x+i;
  541. g_aucLcdBuf[m][n]=g_ucBackColorId;
  542. }
  543. }
  544. PaintBufToLcd(x,y,width,heigh);
  545. }
  546. #ifdef USING_DRAW_WITH_COLOR_OPTION
  547. /****************************************************************************
  548. GuiFillRect
  549. 指定矩形区域填充前景色
  550. ****************************************************************************/
  551. void GuiFillRect(u16 x1, u16 y1, u16 x2, u16 y2,u8 colorIndex)
  552. {
  553. u16 i,j,h,w;
  554. u16 x,y;
  555. if(x1>x2||y1>y2)return;
  556. if(x1>=LCD_WIDTH || y1>=LCD_HEIGHT)return;
  557. if(x2>=LCD_WIDTH)x2=LCD_WIDTH-1;
  558. if(y2>=LCD_HEIGHT)y2=LCD_HEIGHT-1;
  559. w=x2-x1+1;
  560. h=y2-y1+1;
  561. for(j=0;j<h;j++){
  562. y=y1+j;
  563. if(y>(LCD_HEIGHT-1))y=LCD_HEIGHT-1;
  564. for(i=0;i<w;i++){
  565. x=x1+i;
  566. if(x>(LCD_WIDTH-1))x=LCD_WIDTH-1;
  567. {
  568. //g_aucLcdBuf[y][x]=g_ucForeColorId;
  569. switch(colorIndex)
  570. {
  571. case COLOR_WARN_RED:g_aucLcdBuf[y][x]=g_ucWarnColorId;break;
  572. case COLOR_DEFAULT_BLUE:
  573. default:g_aucLcdBuf[y][x]=g_ucForeColorId;break;
  574. }
  575. }
  576. }
  577. }
  578. PaintBufToLcd(x1,y1,w,h);
  579. }
  580. #else
  581. /****************************************************************************
  582. GuiFillRect
  583. 指定矩形区域填充前景色
  584. ****************************************************************************/
  585. void GuiFillRect(u16 x1, u16 y1, u16 x2, u16 y2)
  586. {
  587. u16 i,j,h,w;
  588. u16 x,y;
  589. if(x1>x2||y1>y2)return;
  590. if(x1>=LCD_WIDTH || y1>=LCD_HEIGHT)return;
  591. if(x2>=LCD_WIDTH)x2=LCD_WIDTH-1;
  592. if(y2>=LCD_HEIGHT)y2=LCD_HEIGHT-1;
  593. w=x2-x1+1;
  594. h=y2-y1+1;
  595. for(j=0;j<h;j++){
  596. y=y1+j;
  597. if(y>(LCD_HEIGHT-1))y=LCD_HEIGHT-1;
  598. for(i=0;i<w;i++){
  599. x=x1+i;
  600. if(x>(LCD_WIDTH-1))x=LCD_WIDTH-1;
  601. g_aucLcdBuf[y][x]=g_ucForeColorId;
  602. }
  603. }
  604. PaintBufToLcd(x1,y1,w,h);
  605. }
  606. #endif
  607. /****************************************************************************
  608. GuiDrawHLine
  609. 画水平线
  610. ****************************************************************************/
  611. void GuiDrawHLine(u16 x1, u16 x2, u16 y, u8 width)
  612. {
  613. if(width==0)return;
  614. if(x2<x1)return;
  615. width-=1;
  616. GuiFillRect(x1,y,x2,y+width,0);
  617. }
  618. #ifdef USING_DRAW_WITH_COLOR_OPTION//带选择填充色
  619. /****************************************************************************
  620. GuiDrawVLine
  621. 画垂直线
  622. ****************************************************************************/
  623. void GuiDrawVLine(u16 y1, u16 y2, u16 x, u8 width, u8 colorIndex)//为0时使用默认的蓝色
  624. {
  625. if(width==0)return;
  626. if(y2<y1)return;
  627. width-=1;
  628. GuiFillRect(x,y1,x+width,y2,colorIndex);
  629. }
  630. #else
  631. /****************************************************************************
  632. GuiDrawVLine
  633. 画垂直线
  634. ****************************************************************************/
  635. void GuiDrawVLine(u16 y1, u16 y2, u16 x, u8 width)
  636. {
  637. if(width==0)return;
  638. if(y2<y1)return;
  639. width-=1;
  640. GuiFillRect(x,y1,x+width,y2);
  641. }
  642. #endif
  643. /****************************************************************************
  644. GuiDrawRect
  645. 画矩形框,中间不填充
  646. ****************************************************************************/
  647. void GuiDrawRect(u16 x1, u16 y1, u16 x2, u16 y2,u8 width)
  648. {
  649. if(x1>x2||y1>y2)return;
  650. GuiDrawHLine(x1,x2,y1,width);
  651. GuiDrawHLine(x1,x2,y2,width);
  652. GuiDrawVLine(y1,y2,x1,width,COLOR_DEFAULT_BLUE);
  653. GuiDrawVLine(y1,y2,x2,width,COLOR_DEFAULT_BLUE);
  654. }
  655. /***************************************************************************
  656. 根据前景色和背景色的颜色索引g_ucForeColorId、g_ucBackColorId,从当前调色板中获取前景色和背景色g_usForeColor、g_usBackColor
  657. ****************************************************************************/
  658. void UpdateColor(void)
  659. {
  660. u16 temp,r,g,b,RGB565;
  661. u8 R,G,B;
  662. //更新前景色
  663. temp=g_ucForeColorId*4;//调色板每个索引对应4个字节,分别是BGRA (蓝、绿、红、透明度),目前A未用
  664. B=g_Palette[temp];
  665. G=g_Palette[temp+1];
  666. R=g_Palette[temp+2];
  667. r = R>>3; // 取R色的高5位
  668. g = G>>2; // 取G色的高6位
  669. b = B>>3; // 取B色的高5位
  670. RGB565=((r<<11) + (g<<5) + (b<<0));
  671. g_usForeColor=RGB565;
  672. //更新背景色
  673. temp=g_ucBackColorId*4;//调色板每个索引对应4个字节,分别是BGRA (蓝、绿、红、透明度),目前A未用
  674. B=g_Palette[temp];
  675. G=g_Palette[temp+1];
  676. R=g_Palette[temp+2];
  677. r = R>>3; // 取R色的高5位
  678. g = G>>2; // 取G色的高6位
  679. b = B>>3; // 取B色的高5位
  680. RGB565=((r<<11) + (g<<5) + (b<<0));
  681. g_usBackColor=RGB565;
  682. //更新警告色
  683. temp=g_ucWarnColorId*4;//调色板每个索引对应4个字节,分别是BGRA (蓝、绿、红、透明度),目前A未用
  684. B=g_Palette[temp];
  685. G=g_Palette[temp+1];
  686. R=g_Palette[temp+2];
  687. r = R>>3; // 取R色的高5位
  688. g = G>>2; // 取G色的高6位
  689. b = B>>3; // 取B色的高5位
  690. RGB565=((r<<11) + (g<<5) + (b<<0));
  691. g_usWarnColor=RGB565;
  692. }
  693. /****************************************************************************
  694. GuiDrawDot
  695. 画点:在显存及LCD上指定位置显示前景色!
  696. ****************************************************************************/
  697. void GuiDrawDot(u16 x,u16 y)
  698. {
  699. LcdSendCommand(0x2a);
  700. LcdSendData(x>>8);
  701. LcdSendData(x&0x00ff);
  702. LcdSendCommand(0x2b);
  703. LcdSendData(y>>8);
  704. LcdSendData(y&0x00ff);
  705. LcdSendCommand(0X2C);
  706. g_aucLcdBuf[x][y]=g_ucForeColorId;
  707. LcdSendData(g_usForeColor >> 8);
  708. LcdSendData(g_usForeColor & 0xff);
  709. }
  710. /****************************************************************************
  711. GuiShowArrow
  712. 显示箭头,箭头尺寸16*16 ,左上角坐标x,y
  713. 模式mode:
  714. 高四位代表填充: 0x--非填充(只有边缘线条) 1x--填充
  715. 低四位代表方向:x1--向左 x2--向右 x3--向上 x4--向下
  716. ****************************************************************************/
  717. void GuiShowArrow(u16 x,u16 y,u16 len,u8 mode)
  718. {
  719. u16 i;
  720. u8 dir,fill;
  721. // if(x>=(LCD_WIDTH-len) || y>=(LCD_HEIGHT-len))return;
  722. dir=mode&0x0f;
  723. fill=mode&0xf0;
  724. switch(dir)
  725. {
  726. case 1:
  727. for(i = 0 ; i < len ; i ++){
  728. GuiDrawRect(x+i,y-i,x+i,y+i,1);
  729. }
  730. break;
  731. case 2:
  732. for(i = 0 ; i < len ; i ++){
  733. GuiDrawRect(x-i,y-i,x-i,y+i,1);
  734. }
  735. break;
  736. case 3:
  737. for(i = 0 ; i < len ; i ++){
  738. GuiDrawRect(x-i,y+i,x+i,y+i,1);
  739. }
  740. break;
  741. case 4:
  742. for(i = 0 ; i < len ; i ++){
  743. GuiDrawRect(x-i,y-i,x+i,y-i,1);
  744. }
  745. break;
  746. default:
  747. break;
  748. }
  749. }
  750. /*******************************************************
  751. 移动一个矩形区域
  752. *******************************************************/
  753. void GuiMoveRect(u16 xstart,u16 ystart,u16 width,u16 height,u16 xend,u16 yend)
  754. {
  755. int i,j;
  756. int xs,ys,xe,ye;
  757. int xd,yd;
  758. if((xstart+width)>LCD_WIDTH)return;
  759. if((xend+width)>LCD_WIDTH)return;
  760. if((ystart+height)>LCD_HEIGHT)return;
  761. if((yend+height)>LCD_HEIGHT)return;
  762. xd=xend-xstart;//正数表示从左往右移动,负数表示从右往左移动
  763. yd=yend-ystart;//正数表示从上往下移动,负数表示从下往上移动
  764. if(yd>0){
  765. if(xd>0){
  766. //从左往右,从上往下
  767. for(i=height-1;i>=0;i--){
  768. for(j=width-1;j>=0;j--){
  769. xs=xstart+j;
  770. ys=ystart+i;
  771. xe=xend+j;
  772. ye=yend+i;
  773. g_aucLcdBuf[ye][xe]=g_aucLcdBuf[ys][xs];
  774. g_aucLcdBuf[ys][xs]=g_ucBackColorId;
  775. }
  776. }
  777. }else{
  778. //从右往左,从上往下
  779. for(i=height-1;i>=0;i--){
  780. for(j=0;j<width;j++){
  781. xs=xstart+j;
  782. ys=ystart+i;
  783. xe=xend+j;
  784. ye=yend+i;
  785. g_aucLcdBuf[ye][xe]=g_aucLcdBuf[ys][xs];
  786. g_aucLcdBuf[ys][xs]=g_ucBackColorId;
  787. }
  788. }
  789. }
  790. }else{
  791. if(xd>0){
  792. //从左往右,从下往上
  793. for(i=0;i<height;i++){
  794. for(j=width-1;j>=0;j--){
  795. xs=xstart+j;
  796. ys=ystart+i;
  797. xe=xend+j;
  798. ye=yend+i;
  799. g_aucLcdBuf[ye][xe]=g_aucLcdBuf[ys][xs];
  800. g_aucLcdBuf[ys][xs]=g_ucBackColorId;
  801. }
  802. }
  803. }else{
  804. //从右往左,从下往上
  805. for(i=0;i<height;i++){
  806. for(j=0;j<width;j++){
  807. xs=xstart+j;
  808. ys=ystart+i;
  809. xe=xend+j;
  810. ye=yend+i;
  811. g_aucLcdBuf[ye][xe]=g_aucLcdBuf[ys][xs];
  812. g_aucLcdBuf[ys][xs]=g_ucBackColorId;
  813. }
  814. }
  815. }
  816. }
  817. PaintBufToLcd(xstart,ystart,width,height);
  818. PaintBufToLcd(xend,yend,width,height);
  819. }
  820. /*******************************************************
  821. 获取BMP文件尺寸
  822. *******************************************************/
  823. void GetBmpSize(const char *filename,u16 *width,u16 *height)
  824. {
  825. int index;
  826. int FileLen;
  827. unsigned short type;
  828. unsigned int w,h;
  829. index=GetFileIndex(filename);
  830. FileLen=GetFileLen(index);
  831. if(FileLen==0)return;//找不到文件,直接返回不处理
  832. //读出位图类型
  833. ReadFileData(index,28,2,(u8 *)&type);//读出int biBitCount ; // 每个像素所需的位数,必须是或 1,4,8 24(// 真彩色 ) 之一 (28-29 字节 )
  834. if(type!=0x0008)return;//非256色的BMP图,直接返回不处理。
  835. //读出位图的宽度
  836. ReadFileData(index,18,4,(u8 *)&w); //int image_width ; // 位图的宽度,以像素为单位 (18-21 字节 )
  837. //printf("width=%u\r\n",w);
  838. if(w>LCD_WIDTH){
  839. *width=0;
  840. *height=0;
  841. return;
  842. }
  843. //读出位图的高度
  844. ReadFileData(index,22,4,(u8 *)&h); //int image_heigh ; // 位图的高度,以像素为单位 (22-25 字节 )
  845. //printf("heigh=%u\r\n",h);
  846. if(h>LCD_HEIGHT){
  847. *width=0;
  848. *height=0;
  849. return;
  850. }
  851. *width=w;
  852. *height=h;
  853. }
  854. /*******************************************************
  855. 显示一张BMP图,并使其移动
  856. 开始位置:(xstart,ystart) 终点位置(endx,endy)
  857. BMP图片文件名:filename
  858. 移动速度: d 每次移动的像素点
  859. *******************************************************/
  860. void GuiDrawBmpMoving(u16 xstart,u16 ystart,u16 xend,u16 yend,const char *filename,int d)
  861. {
  862. int i;
  863. u16 x,y,w,h;
  864. int xd,yd,xdd,ydd,r;
  865. w=0;h=0;
  866. GetBmpSize(filename,&w,&h);
  867. if(w==0 || h==0)return;
  868. GuiShowBmp(xstart,ystart,filename);
  869. //计算两个方向的距离xd yd
  870. xd=xend-xstart;
  871. yd=yend-ystart;
  872. //距离的绝对值xdd ydd
  873. if(yd<0) ydd=-yd;
  874. else ydd=yd;
  875. if(xd<0)xdd=-xd;
  876. else xdd=xd;
  877. if(xdd==ydd && xdd==0)return;
  878. //计算两个方向的步进xd yd,其中一个为1或-1
  879. if(xdd>ydd){
  880. if(ydd==0){
  881. yd=0;
  882. if(xd>0)xd=d;
  883. else xd=-d;
  884. }else{
  885. r=xdd/ydd;
  886. if(xd>0)xd=r*d;
  887. else xd=-r*d;
  888. if(yd>0)yd=d;
  889. else yd=-d;
  890. }
  891. }else if(ydd>xdd){
  892. if(xdd==0){
  893. xd=0;
  894. if(yd>0)yd=d;
  895. else yd=-d;
  896. }else{
  897. r=ydd/xdd;
  898. if(yd>0)yd=r*d;
  899. else yd=-r*d;
  900. if(xd>0)xd=d;
  901. else xd=-d;
  902. }
  903. }else{//xdd==ydd
  904. if(xd>0)xd=d;
  905. else xd=-d;
  906. if(yd>0)yd=d;
  907. else yd=-d;
  908. }
  909. //move loop
  910. while(xstart!=xend || ystart!=yend)
  911. {
  912. x=xstart+xd;
  913. if(xd>0 && x>xend)x=xend;
  914. else if(xd<0 && x<xend)x=xend;
  915. y=ystart+yd;
  916. if(yd>0 && y>yend)y=yend;
  917. else if(yd<0 && y<yend)y=yend;
  918. GuiMoveRect(xstart,ystart,w,h,x,y);
  919. xstart=x;
  920. ystart=y;
  921. }
  922. }
  923. /******************************************************************************
  924. GuiReverseRect
  925. 对一个矩形区域进行反色
  926. ******************************************************************************/
  927. void GuiReverseRect(u16 x,u16 y,u16 width,u16 height)
  928. {
  929. u16 i,j,m,n;
  930. if(x+width>=LCD_WIDTH)width=LCD_WIDTH-x-1;
  931. if(y+height>=LCD_HEIGHT)height=LCD_HEIGHT-y-1;
  932. for(j=0;j<height;j++){
  933. m=y+j;
  934. for(i=0;i<width;i++){
  935. n=x+i;
  936. if(g_aucLcdBuf[m][n]==g_ucBackColorId){
  937. g_aucLcdBuf[m][n]=g_ucForeColorId;
  938. }else{
  939. g_aucLcdBuf[m][n]=g_ucBackColorId;
  940. }
  941. }
  942. }
  943. PaintBufToLcd(x,y,width,height);
  944. }
  945. /******************************************************************************
  946. GuiReverseRect
  947. 对一个区域进行反色
  948. ******************************************************************************/
  949. void GuiReverseArea(u16 x1,u16 y1,u16 x2,u16 y2)
  950. {
  951. u16 i,j,m,n;
  952. if(x1>=x2 || y1>=y2)return;
  953. if(x2>LCD_WIDTH || y2>LCD_HEIGHT)return;
  954. for(j=y1;j<=y2;j++){
  955. for(i=x1;i<x2;i++){
  956. if(g_aucLcdBuf[j][i]==g_ucBackColorId){
  957. g_aucLcdBuf[j][i]=g_ucForeColorId;
  958. }else{
  959. g_aucLcdBuf[j][i]=g_ucBackColorId;
  960. }
  961. }
  962. }
  963. PaintBufToLcd(x1,y1,x2-x1+1,y2-y1+1);
  964. }
  965. /******************************************************************************
  966. GuiCopy
  967. 拷贝一个显示区域到粘贴板
  968. ******************************************************************************/
  969. void GuiCopy(u16 x1,u16 y1,u16 x2,u16 y2)
  970. {
  971. u16 i,j,h,w;
  972. u16 x,y;
  973. if(x1>x2||y1>y2)return;
  974. if(x1>=LCD_WIDTH || y1>=LCD_HEIGHT)return;
  975. if(x2>=LCD_WIDTH || y2>=LCD_HEIGHT)return;
  976. w=x2-x1+1;
  977. h=y2-y1+1;
  978. for(j=0;j<h;j++){
  979. y=y1+j;
  980. for(i=0;i<w;i++){
  981. x=x1+i;
  982. // g_aucLcdCopyBuf[y][x]=g_aucLcdBuf[y][x];
  983. }
  984. }
  985. PaintBufToLcd(x1,y1,w,h);
  986. }
  987. /******************************************************************************
  988. GuiPaste
  989. 从粘贴板粘贴一个区域到显存
  990. ******************************************************************************/
  991. void GuiPaste(u16 x1,u16 y1,u16 x2,u16 y2)
  992. {
  993. u16 i,j,h,w;
  994. u16 x,y;
  995. if(x1>x2||y1>y2)return;
  996. if(x1>=LCD_WIDTH || y1>=LCD_HEIGHT)return;
  997. if(x2>=LCD_WIDTH || y2>=LCD_HEIGHT)return;
  998. w=x2-x1+1;
  999. h=y2-y1+1;
  1000. for(j=0;j<h;j++){
  1001. y=y1+j;
  1002. for(i=0;i<w;i++){
  1003. x=x1+i;
  1004. g_aucLcdBuf[y][x]=g_aucLcdBuf[y][x]=g_ucForeColorId;
  1005. }
  1006. }
  1007. PaintBufToLcd(x1,y1,w,h);
  1008. }
  1009. void SysIniRead(void)
  1010. {
  1011. int sysIndex;
  1012. int len;
  1013. char tempData[200];//如果sys.ini文件比较大,注意修改内存
  1014. memset(sysStartTTS, 0, START_TTS_LENGTH);
  1015. sysIndex=GetFileIndex("sys.ini");
  1016. if(sysIndex==-1)
  1017. {
  1018. printf("sys.ini not found\r\n");
  1019. return;
  1020. }
  1021. len=GetFileLen(sysIndex);
  1022. if(len <= 0)
  1023. {
  1024. printf("sys.ini no data or err\r\n");
  1025. return;
  1026. }
  1027. if(len > sizeof(tempData))
  1028. {
  1029. printf("sys.ini len=%d,should<=%d\r\n",len, sizeof(tempData));
  1030. return;
  1031. }
  1032. len=ReadFileData(sysIndex,0,len,(u8*)tempData);
  1033. //读取HP_VOICE_TTS段内容,内容不超过80字节
  1034. len=GetParaFromStr(tempData, "HP_VOICE_TTS",sysStartTTS);
  1035. if(len==0)
  1036. {
  1037. printf("no segment HP_VOICE_TTS\r\n");
  1038. sysStartTTS[0]=0;
  1039. return;
  1040. }
  1041. if(0==ModemStrCmp(sysStartTTS,"NULL")) sysStartTTS[0]=0;
  1042. printf("sys.ini ok\r\n%s\r\n",sysStartTTS);
  1043. }