GUI.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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. SUT_HZK sutHzk16;
  18. unsigned char g_Palette[256*4];//调色板
  19. /****************************************************************************
  20. *设置调色板为默认调色板
  21. ****************************************************************************/
  22. void SetDefaultPalette(void)
  23. {
  24. int i,j;
  25. j=0;
  26. for(i=0;i<256;i++){
  27. g_Palette[i]=g_cucPalette[j++];
  28. g_Palette[i+1]=g_cucPalette[j++];
  29. g_Palette[i+2]=g_cucPalette[j++];
  30. g_Palette[i+3]=0;
  31. }
  32. }
  33. /****************************************************************************
  34. GUI_Color565
  35. 将RGB888转为RGB565
  36. ****************************************************************************/
  37. u16 GUI_Color565(unsigned char R,unsigned char G, unsigned char B)
  38. {
  39. u8 r, g, b;
  40. r = R>>3; // 取R色的高5位
  41. g = G>>2; // 取G色的高6位
  42. b = B>>3; // 取B色的高5位
  43. return( (r<<11) + (g<<5) + (b<<0) );
  44. }
  45. /****************************************************************************
  46. GuiShowPic
  47. 显示一张图片
  48. 此函数只能显示在程序区中的图片数据,采用默认调色板
  49. ****************************************************************************/
  50. void GuiShowPic(u8 x,u8 y,const unsigned char *pic)
  51. {
  52. int i,len;
  53. u8 R,G,B;
  54. u16 RGB565;
  55. const unsigned char *data;
  56. HEADGRAY headgray;
  57. memcpy(&headgray,pic,sizeof(HEADGRAY));
  58. LcdBlockWrite(x,y,x+headgray.w-1,y+headgray.h-1);
  59. len=headgray.w * headgray.h;
  60. data=pic+sizeof(HEADGRAY);
  61. for(i=0;i<len;i++){
  62. R=data[i];
  63. R=g_cucPalette[R*3];
  64. G=data[i+1];
  65. G=g_cucPalette[G*3+1];
  66. B=data[i+2];
  67. B=g_cucPalette[B*3+2];
  68. RGB565=GUI_Color565(R,G,B);
  69. LcdSendData(RGB565 >> 8);
  70. LcdSendData(RGB565 & 0xff);
  71. }
  72. }
  73. /*****************************************
  74. 从字库文件HZK16中提取某汉子的字库数据
  75. 输入:hiByte--汉字的高字节 loByte--汉字的低字节
  76. 字库文件存储在sFlash中,字库文件名为HZK16
  77. 输出:pHzk--提取到的字库数据,需要预留足够空间存储。如果是16*16 则应该是16*16/8=32Byte
  78. 返回:失败返回0 成功返回长度
  79. ******************************************/
  80. int GetHzk16(u8 hiByte,u8 loByte,u8 *pHzk)
  81. {
  82. u8 Q,W;
  83. u32 offset;
  84. if(hiByte<0xa0 || loByte<0xa0)return 0;
  85. Q=hiByte-0xa0;
  86. W=loByte-0xa0;
  87. //offset=(94*(区码-1)+(位码-1))*32
  88. offset=(94 * (Q-1) + (W-1))* 32;
  89. return ReadFileData(sutHzk16.FileIndex,offset,32,pHzk);
  90. }
  91. /******************************************
  92. *汉子库初始化
  93. *输入:汉字库文件名
  94. *输出:汉字库数据首地址存放在sHZK16中
  95. *返回:1--成功 0--失败,找不到文件
  96. *******************************************/
  97. int HzkInit(const char *filename)
  98. {
  99. int i;
  100. if(g_sutFilesList.FileCount==0)return 0;
  101. memset(&sutHzk16,0,sizeof(SUT_HZK));
  102. i=GetFileIndex(filename);
  103. if(i<0)return 0;
  104. sutHzk16.width=16;
  105. sutHzk16.heigh=16;
  106. sutHzk16.len=(sutHzk16.width * sutHzk16.heigh)/8;
  107. sutHzk16.FileIndex=i;
  108. sutHzk16.FileLen=g_sutFilesList.FileInfo[i].FileLen;
  109. return 1;
  110. }
  111. /******************************************
  112. GuiInit
  113. *******************************************/
  114. void GuiInit(void)
  115. {
  116. if(!HzkInit(HZK16_FILE_NAME))
  117. {
  118. printf("Can't find the HzkFile(%s)!\r\n",HZK16_FILE_NAME);
  119. }
  120. }
  121. /***************************************************************************
  122. *在LCD上显示一个字符
  123. ****************************************************************************/
  124. void GuiShowChar(u16 x,u16 y,u8 hiByte,u8 loByte,u8 mode)
  125. {
  126. unsigned short i,j,k,m;
  127. unsigned short width,height;
  128. unsigned char ByteWidth,data;
  129. unsigned char colorH,colorL;
  130. unsigned char bgcolorH,bgcolorL;
  131. const unsigned char *l_pucFont,*l_pucLetter;
  132. unsigned char *p;
  133. unsigned char font;//字体
  134. unsigned char cover;//覆盖方式
  135. unsigned char sucHzk16[32];
  136. font=mode&0xf0;
  137. cover=mode&0x0f;
  138. k = 0;
  139. if(!hiByte) //english
  140. {
  141. if(font==0x00) //8 * 16
  142. {
  143. width=8;
  144. height = 16;
  145. ByteWidth = 1;
  146. l_pucFont = g_apucFonts16;
  147. l_pucLetter = g_apucLetter16;
  148. }else{ //16 * 24
  149. width=16;
  150. height = 24;
  151. ByteWidth = 2;
  152. l_pucFont = g_apucFonts24;
  153. l_pucLetter = g_apucLetter24;
  154. }
  155. //
  156. while(*l_pucLetter)
  157. {
  158. if(loByte == *l_pucLetter)break;
  159. l_pucLetter ++;
  160. k ++;
  161. }
  162. //查找到字库的开始位置
  163. l_pucFont += k * ByteWidth * height;
  164. }
  165. else
  166. { //chinese
  167. if(font == 0x00)//16 * 16
  168. {
  169. width=16;
  170. height = 16;
  171. ByteWidth = 2;
  172. l_pucFont = sucHzk16;
  173. l_pucLetter = 0;
  174. k=0;
  175. if(32!=GetHzk16(hiByte,loByte,sucHzk16)){//从sFlash的HZK16文件中读取字库数据
  176. //找不到字库,显示"口"
  177. l_pucFont=g_apucCLetter16;
  178. }
  179. }else{//24 * 24
  180. width=24;
  181. height = 24;
  182. ByteWidth = 3;
  183. l_pucFont = g_apucCFonts24;
  184. l_pucLetter = g_apucCLetter24;
  185. while(*l_pucLetter)
  186. {
  187. if(hiByte == *l_pucLetter && loByte == *(l_pucLetter + 1))break;
  188. l_pucLetter += 2;
  189. k ++;
  190. }
  191. //查找到字库的开始位置
  192. l_pucFont += k * ByteWidth * height;
  193. }
  194. //
  195. }
  196. //刷屏显示
  197. colorH=g_usColor>>8;
  198. colorL=g_usColor&0xff;
  199. bgcolorH=g_usBackColor>>8;
  200. bgcolorL=g_usBackColor&0xff;
  201. LcdBlockWrite(x,y,x+width-1,y+height-1);
  202. for(j=0;j<height;j++){
  203. for(i=0;i<ByteWidth;i++){
  204. data=*l_pucFont++;
  205. for(k=0;k<8;k++){
  206. if(data & (0x80>>k)){
  207. LcdSendData(colorH);
  208. LcdSendData(colorL);
  209. }else{
  210. //if(cover){
  211. LcdSendData(bgcolorH);
  212. LcdSendData(bgcolorH);
  213. //}
  214. }
  215. }
  216. }
  217. }
  218. }
  219. /******************************************************************************
  220. GuiShowStr
  221. 指定位置显示字符串
  222. 左上角坐标:x,y
  223. 显示内容:string
  224. 模式:mode 高4位代表字体,低4位为模式
  225. ******************************************************************************/
  226. void GuiShowStr(u16 x, u16 y,const char *string,u8 mode)
  227. {
  228. unsigned char width1,width2;
  229. unsigned char *p;
  230. unsigned short i;
  231. unsigned char font;//字体
  232. font=mode&0xf0;
  233. if(font == 0x00) //16bit font
  234. {
  235. width1 = 2;
  236. width2 = 1;
  237. }
  238. else //24bit font
  239. {
  240. width1 = 3;
  241. width2 = 1;
  242. }
  243. p = (unsigned char *)string;
  244. i = 0;
  245. while(*p)
  246. {
  247. if (*p > 0x9f) //chinese letter
  248. {
  249. GuiShowChar(x+i,y,*p, *(p+1),mode);
  250. i += width1 * 8;
  251. p += 2;
  252. }
  253. else //english letter
  254. {
  255. GuiShowChar(x + i,y,0,*p,mode);
  256. if(font==0){
  257. i += width2 * 8;
  258. }else{
  259. i += width2 * 16;
  260. }
  261. p ++;
  262. }
  263. }
  264. }
  265. /************************************************************
  266. *
  267. *************************************************************/
  268. void GuPaintLcd(u16 x,u16 y,u16 width,u16 heigh,u8 *buf)
  269. {
  270. int i,j;
  271. unsigned short RGB565;
  272. unsigned char data;
  273. unsigned char R,G,B;
  274. unsigned short temp;
  275. LcdBlockWrite(x,y,x+width-1,y+heigh-1);
  276. for(j=0;j<heigh;j++){
  277. for(i=0;i<width;i++){
  278. data=buf[j*width+i];
  279. temp=data*4;
  280. B=g_Palette[temp];
  281. G=g_Palette[temp+1];
  282. R=g_Palette[temp+2];
  283. RGB565=GUI_Color565(R,G,B);
  284. LcdSendData(RGB565 >> 8);
  285. LcdSendData(RGB565 & 0xff);
  286. }
  287. }
  288. }
  289. /************************************************************
  290. *
  291. *************************************************************/
  292. void GuiShowBMP(u16 x,u16 y,const char * filename)
  293. {
  294. int i,w,l;
  295. int index;
  296. int FileLen;
  297. unsigned short type;
  298. unsigned int width,heigh,SizeImage,bfOffBits;
  299. unsigned char temp[160];
  300. index=GetFileIndex(filename);
  301. FileLen=GetFileLen(index);
  302. if(FileLen==0)return;//找不到文件,直接返回不处理
  303. //读出位图类型
  304. ReadFileData(index,28,2,(u8 *)&type);//读出int biBitCount ; // 每个像素所需的位数,必须是或 1,4,8 24(// 真彩色 ) 之一 (28-29 字节 )
  305. if(type!=0x0008)return;//非256色的BMP图,直接返回不处理。
  306. //读出位图的宽度
  307. ReadFileData(index,18,4,(u8 *)&width); //int image_width ; // 位图的宽度,以像素为单位 (18-21 字节 )
  308. printf("width=%u\r\n",width);
  309. //读出位图的高度
  310. ReadFileData(index,22,4,(u8 *)&heigh); //int image_heigh ; // 位图的高度,以像素为单位 (22-25 字节 )
  311. printf("heigh=%u\r\n",heigh);
  312. if(width>LCD_WIDTH || heigh>LCD_HIGH)return;//超过LCD显示范围,不处理
  313. //#if 0 //有此代码 会处理掉图片 不显示
  314. // //读出位图数据大小
  315. // ReadFileData(index,34,4,(u8 *)&SizeImage); //int SizeImage ; // 位图 数据 的大小,以字节为单位 (34-37 字节 )
  316. // printf("SizeImage=%u\r\n",SizeImage);
  317. // if((width*heigh)!=SizeImage)return;//位图数据不合理,可能图片经过压缩,不处理
  318. //#endif
  319. //读位图数据起始地址
  320. ReadFileData(index,10,4,(u8 *)&bfOffBits); //int bfOffBits ; // 位图数据的起始位置,以相对于位图 (10-13 字节 )
  321. //读取调色板数据
  322. ReadFileData(index,54,1024,g_Palette);
  323. //以下读位图数据并刷屏
  324. i= width%4;
  325. if(i > 0){
  326. w=width + (4-width%4);//每行字节数为4的倍数,不够的补齐
  327. }else{
  328. w=width;
  329. }
  330. l=heigh-1;//BMP扫描顺序是:从下往上,从左往右扫描,因此先从最后一行开始读数据
  331. for(i=0;i<heigh;i++){
  332. ReadFileData(index,bfOffBits+w*l,width,temp);
  333. GuPaintLcd(x,y+i,width,1,temp);
  334. l--;
  335. }
  336. //恢复调色板到默认调色板
  337. SetDefaultPalette();
  338. }
  339. void GuiShowLogoGIF(void)
  340. {
  341. int x,y,t;
  342. x=0;y=58;
  343. t=100;
  344. GuiShowBMP(x,y,"logo1.bmp");
  345. DelayMs(t);
  346. GuiShowBMP(x,y,"logo2.bmp");
  347. DelayMs(t);
  348. GuiShowBMP(x,y,"logo3.bmp");
  349. }
  350. /**************************************************/
  351. //信号量
  352. /****************************************************/
  353. /***********************************
  354. ??????
  355. **********************************/
  356. void GuiShowSingle(int CSQ)
  357. {
  358. static int siCSQ=123456;
  359. if(siCSQ!=CSQ){
  360. siCSQ=CSQ;
  361. }else return;
  362. GuiShowBMP(0,0,"Single.bmp");
  363. if(CSQ<1 || CSQ==99){
  364. LcdClearRect(7,4,21,14);
  365. }else if(CSQ<8){
  366. LcdDrawVLine(12,14,7,2);
  367. LcdClearRect(11,4,21,14);
  368. }else if(CSQ<16){
  369. LcdDrawVLine(12,14,7,2);
  370. LcdDrawVLine(9,14,11,2);
  371. LcdClearRect(15,4,21,14);
  372. }else if(CSQ<22){
  373. LcdDrawVLine(12,14,7,2);
  374. LcdDrawVLine(9,14,11,2);
  375. LcdDrawVLine(7,14,15,2);
  376. LcdClearRect(19,4,21,14);
  377. }else{
  378. LcdDrawVLine(12,14,7,2);
  379. LcdDrawVLine(9,14,11,2);
  380. LcdDrawVLine(7,14,15,2);
  381. LcdDrawVLine(4,14,19,2);
  382. }
  383. }
  384. void GuiShowBat(int bat)
  385. {
  386. static int Vbat=600;
  387. if(Vbat!=bat){
  388. Vbat=bat;
  389. }else return;
  390. GuiShowBMP(40,0,"VBAT1.bmp");
  391. if(bat<330){
  392. LcdDrawVLine(5,12,47,0);
  393. LcdDrawVLine(5,12,52,0);
  394. LcdDrawVLine(5,12,57,0);
  395. LcdDrawVLine(5,12,62,0);
  396. }
  397. else if(bat<350){//335-350
  398. LcdDrawVLine(5,12,62,4);
  399. }
  400. else if(bat<377){//351-377
  401. LcdDrawVLine(5,12,57,4);
  402. LcdDrawVLine(5,12,62,4);
  403. }
  404. else if(bat<392){//378-392
  405. LcdDrawVLine(5,12,52,4);
  406. LcdDrawVLine(5,12,57,4);
  407. LcdDrawVLine(5,12,62,4);
  408. }
  409. else if(bat>410){//400
  410. LcdDrawVLine(5,12,47,4);
  411. LcdDrawVLine(5,12,52,4);
  412. LcdDrawVLine(5,12,57,4);
  413. LcdDrawVLine(5,12,62,4);
  414. }
  415. }