gui.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. #include "gui.h"
  2. #include "lcdDrv.h"
  3. #include "nwy_file.h"
  4. #include "log.h"
  5. #include "fonts.h"
  6. #include "board.h"
  7. #include "uiEntry.h"
  8. #define COLOR_BACKGROUND COLOR_WHITE
  9. #define COLOR_FOREGROUND COLOR_BLUE
  10. unsigned int g_backColor;
  11. unsigned int g_foreColor;
  12. unsigned int guiGetBackColor(void){return g_backColor;}
  13. unsigned int guiGetForeColor(void){return g_foreColor;}
  14. static unsigned short RGB888_2_RGB565(unsigned int rgb888){
  15. unsigned short r,g,b,rgb565;
  16. b=(rgb888 >> (16+3))&0x1F;
  17. g=(rgb888 >> (8+2))&0x3f;
  18. r=(rgb888 >> (0+3))&0x1f;
  19. rgb565=((r<<11) + (g<<5) + (b<<0));
  20. return rgb565;
  21. }
  22. static void guiDrawDot(unsigned short colorData){
  23. LCD_DataWrite_ST7735(colorData >> 8);
  24. LCD_DataWrite_ST7735(colorData & 0xff);
  25. }
  26. static void guiBlockSet(unsigned short sx, unsigned short ex, unsigned short sy, unsigned short ey){
  27. LCD_CtrlWrite_ST7735(0x2a);
  28. guiDrawDot(sx);
  29. guiDrawDot(sy);
  30. LCD_CtrlWrite_ST7735(0x2B);
  31. guiDrawDot(ex);
  32. guiDrawDot(ey);
  33. LCD_CtrlWrite_ST7735(0x2C);
  34. }
  35. #define KEEP_HZK_OPENED //ê?·?±£3????t3£?a
  36. static int guiGetHzk(unsigned char hiByte, unsigned char loByte, unsigned char *pHzk, FONT_MODEENUM fontMode){
  37. unsigned char Q,W;
  38. unsigned int offset;
  39. const char hzk_12x12[]="HZK12";
  40. const char hzk_16x16[]="HZK16";
  41. char *hzk_ptr=NULL;
  42. static int fd_12=NULL,fd_16=NULL;
  43. int *fd_ptr=NULL;
  44. int fsize;
  45. if(hiByte<0xa0 || loByte<0xa0) return -1;
  46. Q=hiByte-0xa0;
  47. W=loByte-0xa0;
  48. if(FONT_MODE_12X12==fontMode){
  49. fsize = 24;
  50. hzk_ptr=(char *)hzk_12x12;
  51. fd_ptr=&fd_12;
  52. }else if(FONT_MODE_16X16==fontMode){
  53. fsize = 32;
  54. hzk_ptr=(char *)hzk_16x16;
  55. fd_ptr=&fd_16;
  56. }else return -2;
  57. offset=(94 * (Q-1) + (W-1))* fsize;
  58. //′ò?a???t
  59. if(*fd_ptr==NULL){
  60. int fd=nwy_sdk_fopen(hzk_ptr, NWY_RDONLY);
  61. if(fd==NULL){
  62. MSG_ERR(1, "%s open failed", hzk_ptr);
  63. return -3;
  64. }
  65. *fd_ptr=fd;
  66. }
  67. //?áè??úèY
  68. nwy_sdk_fseek(*fd_ptr, offset, NWY_SEEK_SET);
  69. offset=nwy_sdk_fread(*fd_ptr, pHzk,fsize);
  70. #ifndef KEEP_HZK_OPENED
  71. nwy_sdk_fclose(*fd_ptr);
  72. *fd_ptr=NULL;
  73. #endif
  74. return offset;
  75. }
  76. static void guiShowChars(unsigned short x,unsigned short y,unsigned char hiByte, unsigned char loByte,
  77. FONT_MODEENUM fontMode,
  78. unsigned int frontColorID,//????
  79. unsigned int backColorID,//????
  80. REV_ENUM rev){//??
  81. unsigned short i,j,k,m,n,fcolor,bcolor;
  82. unsigned short width,height;
  83. unsigned char ByteWidth,data;
  84. const unsigned char *l_pucFont,*l_pucLetter;
  85. unsigned char *p;
  86. unsigned char sucHzk16[32];
  87. unsigned char sucHzk12[24];
  88. int ret;
  89. //===?¨¨?¨′?Y?á??¤?¨|¨¨????¨o?|ì??¨a??é????é|ì??¨?¨oy?Y????|쨨===
  90. k = 0;
  91. if(hiByte==0){//english
  92. if(fontMode==FONT_MODE_12X12){//8* 12
  93. width=8;//8
  94. height = 12; //12
  95. ByteWidth = 1;
  96. l_pucFont = g_apucFonts12;
  97. l_pucLetter = g_apucLetter12;
  98. }else{ //8*16
  99. width=8;
  100. height = 16;
  101. ByteWidth = 1;
  102. l_pucFont = g_apucFonts16;
  103. l_pucLetter = g_apucLetter16;
  104. }
  105. while(*l_pucLetter){
  106. if(loByte == *l_pucLetter)break;
  107. l_pucLetter ++;
  108. k ++;
  109. }
  110. //2¨|?¨°|ì??á??a|ì??a¨o?????
  111. l_pucFont += k * ByteWidth * height;
  112. }else{//chinese
  113. if(fontMode==FONT_MODE_12X12){//12*16
  114. width=16;
  115. height = 12;
  116. ByteWidth = 2;
  117. l_pucFont = sucHzk12;
  118. l_pucLetter = 0;
  119. k=0;
  120. ret=guiGetHzk(hiByte,loByte,sucHzk12,fontMode);
  121. if(sizeof(sucHzk12)!=ret){//??¨?sFlash|ì?HZK???t?D?¨¢¨¨??á??a¨oy?Y
  122. //?¨°2?|ì??á??a?ê???¨o?"?¨2"
  123. l_pucFont=g_apucCLetter16;
  124. MSG_ERR(1, "guiGetHzk12Err:%02x,%02x,%d",hiByte,loByte,ret);
  125. }
  126. }else{//16*16
  127. width=16;
  128. height = 16;
  129. ByteWidth = 2;
  130. l_pucFont = sucHzk16;
  131. l_pucLetter = 0;
  132. k=0;
  133. ret=guiGetHzk(hiByte,loByte,sucHzk16,fontMode);
  134. if(sizeof(sucHzk16)!=ret){//??¨?sFlash|ì?HZK16???t?D?¨¢¨¨??á??a¨oy?Y
  135. //?¨°2?|ì??á??a?ê???¨o?"?¨2"
  136. l_pucFont=g_apucCLetter16;
  137. MSG_ERR(1, "guiGetHzk16Err:%02x,%02x,%d",hiByte,loByte,ret);
  138. }
  139. }
  140. }
  141. //====================?¨′??|¨¤¨a|ì??¨?¨oy?Y==========
  142. if(y+height>GLCD_HEIGHT)return;
  143. if(x+width>GLCD_WIDTH)return;
  144. guiBlockSet(x,y,x+width-1,y+height-1);//8,12
  145. fcolor=RGB888_2_RGB565(frontColorID);
  146. bcolor=RGB888_2_RGB565(backColorID);
  147. //¨°?????à?¨???|ì??¨???é|ì?LCD?ê?2?¨?????????ê¨??¨?¨22a¨o??ê??á?騰a|ì??¨??2?¨°a|ì??¨??PaintBufToLcd?ê??¤??¨°¨°2??é?¨¢¨¢?
  148. //?¨′?Y?á???ê?¨2??????D?-|ì?
  149. for(j=0;j<height;j++){
  150. m=y+j;
  151. for(i=0;i<ByteWidth;i++){
  152. n=x+i*8;
  153. data=*l_pucFont++;
  154. for(k=0;k<8;k++){
  155. if(data & (0x80>>k)){//??,??frontColorID
  156. if(REVERSED_NO==rev) guiDrawDot(fcolor);
  157. else guiDrawDot(bcolor);
  158. }else{//??,??backColorID
  159. if(REVERSED_NO==rev) guiDrawDot(bcolor);
  160. else guiDrawDot(fcolor);
  161. }
  162. }
  163. }
  164. }
  165. }
  166. short guiGetStrXLen(char *str, FONT_MODEENUM fontMode){
  167. unsigned char ch;
  168. unsigned short len=0;
  169. unsigned char bsize;
  170. if(FONT_MODE_12X12==fontMode) bsize=6;
  171. else if(FONT_MODE_16X16==fontMode) bsize=8;
  172. else return 0;
  173. while ((ch=*str++)){
  174. if(ch=='M')len+=12;
  175. else if(ch>='A'&&ch<='Z')len+=8;
  176. else if(ch>='a'&&ch<='z')len+=8;
  177. else if(ch>='0'&&ch<='9')len+=8;
  178. else if(ch>=0x20&& ch<=0x60)len+=8; //¨??¨oa?á??¤?
  179. else len+=bsize;
  180. }
  181. return len;
  182. }
  183. //ò????ó?ú??ía?a·?
  184. void guiShowStr(unsigned short x, unsigned short y,const char *string, FONT_MODEENUM fontMode,
  185. REV_ENUM rev, //????
  186. unsigned int frontColorID,//???
  187. unsigned int backColorID){//???
  188. unsigned char width1,width2,heigh;
  189. unsigned char *p;
  190. unsigned short i;
  191. if(fontMode == FONT_MODE_12X12){
  192. width1 = 12;
  193. width2 = 1;
  194. heigh=12;
  195. }else if(fontMode == FONT_MODE_16X16){
  196. width1 = 16;
  197. width2 = 1;
  198. heigh=16;
  199. }else return;
  200. p = (unsigned char *)string;
  201. i = 0;
  202. while(*p){
  203. if (*p > 0x9f){//chinese letter
  204. guiShowChars(x+i,y,*p, *(p+1),fontMode,frontColorID,backColorID,rev);
  205. i += width1 ;
  206. p += 2;
  207. }else{//english letter
  208. guiShowChars(x + i,y,0,*p,fontMode,frontColorID,backColorID,rev);
  209. if(fontMode==0) i += width2 * 8;
  210. else i += width2 * 8;
  211. p ++;
  212. }
  213. }
  214. }
  215. void guiShowButton(unsigned short x, unsigned short y,const char *str, FONT_MODEENUM fontMode,unsigned int buttonColorID, unsigned int strColorID){
  216. int heigh,len;
  217. if(FONT_MODE_12X12==fontMode) heigh=12+4;
  218. else if(FONT_MODE_16X16==fontMode) heigh=16+4;
  219. else return;
  220. len=guiGetStrXLen((char *)str, fontMode);
  221. guiFillRect(x,y,x+len+8,y+heigh,buttonColorID);
  222. guiShowStr(x+4, y+1, str, fontMode,REVERSED_NO,strColorID,buttonColorID);
  223. }
  224. void guiFillRect(unsigned short x1, unsigned short y1, unsigned short x2, unsigned short y2,unsigned int colorData){
  225. unsigned short x=x2,y=y2,w,h,i,m,k,j,data;
  226. if(x1>x2 || y1>y2) return;
  227. if(x1>=GLCD_WIDTH || y1>=GLCD_HEIGHT) return;
  228. if(x>GLCD_WIDTH) x=GLCD_WIDTH;
  229. if(y>GLCD_HEIGHT) y=GLCD_HEIGHT;
  230. w=x-x1+1;
  231. h=y-y1+1;
  232. data=RGB888_2_RGB565(colorData);
  233. guiBlockSet(x1,y1,x,y);
  234. for(i=0;i<h;i++){
  235. m=y1+i;
  236. for(j=0;j<w;j++){
  237. k=x1+j;
  238. guiDrawDot(data);
  239. }
  240. }
  241. }
  242. void guiClearRect(unsigned short x1, unsigned short y1, unsigned short x2, unsigned short y2, unsigned int colorData){
  243. guiFillRect(x1,y1,x2,y2,colorData);
  244. }
  245. //??????
  246. void guiDrawHLine(unsigned short x1, unsigned short x2, unsigned short y, unsigned short ysize, unsigned int colorID){
  247. if(ysize==0) return;
  248. if(x2<x1) return;
  249. guiFillRect(x1,y,x2,y+ysize-1,colorID);
  250. }
  251. //′1?±??
  252. void guiDrawVLine(unsigned short y1, unsigned short y2, unsigned short x, unsigned short xsize, unsigned int colorID){
  253. if(xsize==0) return;
  254. if(y2<y1) return;
  255. guiFillRect(x,y1,x+xsize-1,y2,colorID);
  256. }
  257. void guiDrawRect(unsigned short x1, unsigned short y1, unsigned short x2, unsigned short y2,unsigned char width, unsigned int colorID){
  258. if(x1>x2||y1>y2) return;
  259. guiDrawHLine(x1,x2,y1,width,colorID);
  260. guiDrawHLine(x1,x2,y2,width,colorID);
  261. guiDrawVLine(y1,y2,x1,width,colorID);
  262. guiDrawVLine(y1,y2+width-1,x2,width,colorID);
  263. }
  264. //óé·??ò?aμ?(x,y)?aê??-
  265. void guiDrawArrow(unsigned short x, unsigned short y,unsigned short len, ARROW_ENUM direction, unsigned int colorID){
  266. unsigned short i;
  267. switch(direction){
  268. case ARROW_LEFT:
  269. for(i=0;i<len;i++) guiDrawRect(x+i,y-i,x+i,y+i,1,colorID);
  270. break;
  271. case ARROW_RIGHT:
  272. for(i=0;i<len;i++) guiDrawRect(x-i,y-i,x-i,y+i,1,colorID);
  273. break;
  274. case ARROW_UP:
  275. for(i=0;i<len;i++) guiDrawRect(x-i,y+i,x+i,y+i,1,colorID);
  276. break;
  277. case ARROW_DOWN:
  278. for(i=0;i<len;i++) guiDrawRect(x-i,y-i,x+i,y-i,1,colorID);
  279. break;
  280. }
  281. }
  282. static void guiFillBmpBuf(unsigned short x, unsigned short y, unsigned short width, unsigned short height, unsigned char *bmpBuf){
  283. int i,j,k,t;
  284. unsigned short RGB565,r,g,b;
  285. unsigned char R,G,B;
  286. for(j=0;j<height;j++){
  287. t=y+j;
  288. k=j*width;
  289. for(i=0;i<width;i++){
  290. R=bmpBuf[3*i];
  291. G=bmpBuf[3*i+1];
  292. B=bmpBuf[3*i+2];
  293. r=R>>3;
  294. g=G>>2;
  295. b=B>>3;
  296. RGB565=((r<<11)+(g<<5)+(b<<0));
  297. guiDrawDot(RGB565);
  298. }
  299. }
  300. }
  301. void guiShowBmp(unsigned short x, unsigned short y, char *bmp){
  302. int fd,l,w,i;
  303. unsigned short type;
  304. unsigned int width,height,bfOffBits;
  305. unsigned char tbuf[GLCD_WIDTH*4];
  306. fd=nwy_sdk_fopen(bmp, NWY_RDONLY);
  307. if(fd<0){
  308. MSG_ERR(1, "%s open failed", bmp);
  309. return;
  310. }
  311. //?áè????tààDí
  312. nwy_sdk_fseek(fd, 0x1c, NWY_SEEK_SET);
  313. nwy_sdk_fread(fd, (unsigned char *)&type, 2);
  314. if(type != 0x0018){
  315. MSG_ERR(1, "%s is not 24bits bmp",bmp);
  316. nwy_sdk_fclose(fd);
  317. return;
  318. }
  319. //?áè?í????í?è
  320. nwy_sdk_fseek(fd, 0x12, NWY_SEEK_SET);
  321. nwy_sdk_fread(fd, (unsigned char *)&width, 4);
  322. //?áè?í??????è
  323. nwy_sdk_fseek(fd, 0x16, NWY_SEEK_SET);
  324. nwy_sdk_fread(fd, (unsigned char *)&height, 4);
  325. //?D??3?′?
  326. if(width>GLCD_WIDTH || height>GLCD_HEIGHT){
  327. MSG_ERR(1, "%s size overflow",bmp);
  328. nwy_sdk_fclose(fd);
  329. return;
  330. }
  331. //?á3?êy?Y?eê?μ??·
  332. nwy_sdk_fseek(fd, 0x0a, NWY_SEEK_SET);
  333. nwy_sdk_fread(fd, (unsigned char *)&bfOffBits, 4);
  334. l=width%4;
  335. w=width*3+l;
  336. l=height-1;
  337. guiBlockSet(x,y,x+width-1,y+height-1);
  338. for(i=0;i<height;i++){
  339. nwy_sdk_fseek(fd, bfOffBits+l*w, NWY_SEEK_SET);
  340. nwy_sdk_fread(fd, tbuf, w);
  341. guiFillBmpBuf(x,y+i,width,1,tbuf);
  342. l--;
  343. }
  344. nwy_sdk_fclose(fd);
  345. }
  346. /*?ó?D??ê?*/
  347. void guiShowCaption(unsigned short xType, const char *str, unsigned short y, unsigned int frontColorID, unsigned int backColorID, FONT_MODEENUM fontMode){
  348. int len,sy;
  349. unsigned short x;
  350. if(xType==0){
  351. len=guiGetStrXLen((char *)str,fontMode);
  352. x=(GLCD_WIDTH-len)/2;
  353. }else x=xType;
  354. guiShowStr(x, y, str, fontMode, REVERSED_NO, frontColorID, backColorID);
  355. }
  356. void guiClearArea(unsigned short x, unsigned short y, unsigned short width, unsigned short height,unsigned int colorID){
  357. guiFillRect(x,y,x+width,y+height,colorID);
  358. }
  359. void guiInit(void){
  360. g_backColor=COLOR_BACKGROUND;
  361. g_foreColor=COLOR_FOREGROUND;
  362. guiFillRect(0,0,GLCD_WIDTH-1,GLCD_HEIGHT-1,guiGetBackColor());
  363. SetMessageConfi();
  364. guiShowBmp(0,0,"welcome.bmp");//开机图片
  365. CTL_LCD_BL(1);//清屏后打开,否则会看到关机前的画面
  366. }
  367. void guiClearAll(unsigned int colorID){//只清第二,三区
  368. guiFillRect(0, UI_STATUS_ITEM_Y, GLCD_WIDTH-1, UI_CONTENT_SHOW_Y-1, guiGetForeColor());//清除第二区
  369. guiFillRect(0,UI_CONTENT_SHOW_Y,GLCD_WIDTH-1,GLCD_HEIGHT-1,colorID);
  370. }
  371. void guiShowMessageBox(char *msg){
  372. char buf[70];
  373. short len,x,y;
  374. y=GLCD_HEIGHT/2;
  375. guiClearRect(0,y-20,GLCD_WIDTH-1,y+20,guiGetBackColor());
  376. guiDrawRect(2,y-18,GLCD_WIDTH-3,y+18,1,COLOR_BLACK);
  377. StrIntercept(buf,msg,18);
  378. len=guiGetStrXLen(buf,FONT_MODE_16X16);
  379. x=(GLCD_WIDTH-len)/2;
  380. guiShowStr(x,y-8,buf,FONT_MODE_16X16,REVERSED_NO,COLOR_BLACK,guiGetBackColor());
  381. }
  382. void guiShowTwoMessage(char *msg1, char *msg2){
  383. char buf[70];
  384. short len, x, y;
  385. y=GLCD_HEIGHT/2;
  386. guiClearRect(0,y-20,GLCD_WIDTH-1,y+20,guiGetBackColor());
  387. guiDrawRect(1,y-19,GLCD_WIDTH-2,y+19,1,COLOR_BLACK);
  388. StrIntercept(buf,msg1,18);
  389. len=guiGetStrXLen(buf,FONT_MODE_16X16);
  390. x=(GLCD_WIDTH-len)/2;
  391. guiShowStr(x,y-18,buf,FONT_MODE_16X16,REVERSED_NO,COLOR_BLACK,guiGetBackColor());
  392. StrIntercept(buf,msg2,18);
  393. len=guiGetStrXLen(buf,FONT_MODE_16X16);
  394. x=(GLCD_WIDTH-len)/2;
  395. guiShowStr(x,y+2,buf,FONT_MODE_16X16,REVERSED_NO,COLOR_BLACK,guiGetBackColor());
  396. }
  397. //////////////////////////////////////测试接口///////////////////////////////////
  398. void guiTest(void){
  399. return;
  400. //char testbuf[5]={0xB2,0xE2,0xCA,0xD4,0x00};
  401. //??ê?DD??
  402. guiDrawHLine(1, GLCD_WIDTH-2, 1, 1, guiGetForeColor());
  403. //??ê?áD??
  404. guiDrawVLine(3, GLCD_HEIGHT-2, 1, 1, guiGetForeColor());
  405. //??ê???D???D?
  406. guiDrawRect(3,3,3+10,3+10,1,guiGetForeColor());
  407. //??ê?êμD???D?
  408. guiFillRect(15, 3, 15+10, 3+10,guiGetForeColor());
  409. //??ê??yí·
  410. guiDrawArrow(50, 3,10, ARROW_UP, guiGetForeColor());
  411. guiDrawArrow(70, 3+10,10, ARROW_DOWN, guiGetForeColor());
  412. guiDrawArrow(90, 3+8,10, ARROW_LEFT, guiGetForeColor());
  413. guiDrawArrow(110, 3+8,10, ARROW_RIGHT, guiGetForeColor());
  414. //??ê?12X12×?·?′?
  415. guiShowStr(3, 30,"123测试45中,国AW", FONT_MODE_12X12, REVERSED_NO,guiGetForeColor(),guiGetBackColor());
  416. //??ê?16X16×?·?′?
  417. guiShowStr(3, 50,"123测试45你。好AW", FONT_MODE_16X16, REVERSED_NO,guiGetForeColor(),guiGetBackColor());
  418. //??ê?í???
  419. guiShowBmp(3, 80, "Palace2.bmp");
  420. //??ê?°′?ü
  421. guiShowButton(100, 80,"B12", FONT_MODE_12X12,guiGetForeColor(), guiGetBackColor());
  422. }