newMessage.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #include "newMessage.h"
  2. #include "log.h"
  3. #define MAX_PAGES 20 //最多显示20页
  4. #define START_SHOW_Y (UI_CONTENT_SHOW_Y+3) //开始显示的Y坐标
  5. #define END_SHOW_Y (UI_BOTTOM_LINE_Y) //显示结束的Y坐标
  6. #define START_SHOW_X 3
  7. #define MAX_ROW_NUM 5 //每页最多显示多少行
  8. typedef struct{
  9. unsigned short SY;//开始显示的Y坐标
  10. unsigned char font;//显示字体
  11. unsigned char xlen;//根据font配置固定的单字x长度->显示中文时占的宽度
  12. unsigned char ylen;//根据font配置固定的单字y高度
  13. unsigned char validIndexNum;//有效页数目
  14. unsigned short indexTable[MAX_PAGES];//一页最多显示5行,每行最多30字节,一页最多150字节
  15. unsigned char showIndex;//当前显示表的索引
  16. char *info;//显示的缓存
  17. }SUT_NEW_MESSAGE;
  18. SUT_NEW_MESSAGE sutNewMessage={
  19. .info=NULL,
  20. };
  21. #define MAX_INFO_NUM 3
  22. char newMessageInit(FONT_MODEENUM fontMode,char *info1, char *info2, char *info3){
  23. int i,len=0;
  24. char *infos[MAX_INFO_NUM]={info1,info2,info3};
  25. char spacesBuf[2]={0x0a,0x00};
  26. unsigned char unNullNum=0;
  27. char strcatAlready;
  28. sutNewMessage.SY=START_SHOW_Y;
  29. sutNewMessage.font=fontMode;
  30. if(fontMode==FONT_MODE_16X16){
  31. sutNewMessage.ylen=12;
  32. sutNewMessage.xlen=13;
  33. }else{
  34. sutNewMessage.ylen=16;
  35. sutNewMessage.xlen=16;
  36. }
  37. //申请下显示的缓存
  38. for(i=0;i<MAX_INFO_NUM;i++){
  39. if(infos[i] != NULL){
  40. len += strlen(infos[i]);
  41. unNullNum++;
  42. }
  43. }
  44. char *p=NULL;
  45. if(sutNewMessage.info!=NULL){
  46. free(sutNewMessage.info);
  47. sutNewMessage.info=NULL;
  48. }
  49. if(len>0 && unNullNum){
  50. len+=(unNullNum-1)+1;//申请多三个字节(unNullNum个0x0a和多一个空字节位置)
  51. p=(char *)malloc(sizeof(char)*len);
  52. if(p==NULL) return 1;
  53. sutNewMessage.info=p;
  54. sutNewMessage.info[0]=0;
  55. strcatAlready=0;
  56. for(i=0;i<MAX_INFO_NUM;i++){
  57. if(infos[i] != NULL){
  58. if(strcatAlready==0){
  59. strcatAlready=1;
  60. strcat(sutNewMessage.info, infos[i]);
  61. }else{
  62. strcat(sutNewMessage.info, spacesBuf);
  63. strcat(sutNewMessage.info, infos[i]);
  64. }
  65. }
  66. }
  67. }else sutNewMessage.info=NULL;
  68. newMessageShow(SHOW_INIT);
  69. return 0;
  70. }
  71. void newMessageShow(SHOW_ENUM direction)
  72. {
  73. //英文/12中文显示, 动态计算每行显示,尽可能每行显示多内容 遇到0x0a则换行显示
  74. short leftDot;
  75. char *ptr=sutNewMessage.info,ch;
  76. unsigned char validWidthDot=GLCD_WIDTH-START_SHOW_X;//每行可显示的点数目
  77. unsigned char num=0;
  78. unsigned short showIndex,fillindex;
  79. char showBuf[20];//一直最多显示30个字符,1个中文占两个字符
  80. char nextLine=0;
  81. unsigned char page=0;
  82. unsigned char yHeight=sutNewMessage.ylen+4;//每行字上下间隔5
  83. SHOW_ENUM thisDir=direction;
  84. if(sutNewMessage.info[0]=='\0') return;
  85. if(thisDir==SHOW_INIT){//计算出分段索引表
  86. sutNewMessage.indexTable[num++]=0;
  87. //然后一直往下做预显示得到分面索引表
  88. leftDot=validWidthDot;
  89. showIndex=0;
  90. while(*ptr !=0){
  91. ch=*ptr;
  92. if(ch > 0x80){//中文
  93. if(leftDot>=sutNewMessage.xlen){//还可以显示
  94. showIndex += 2;//移动源串索引
  95. ptr += 2;//移动源串地址
  96. leftDot -= sutNewMessage.xlen;//减去剩下可显示点数
  97. }else{//不足够显示了
  98. nextLine=1;
  99. }
  100. }else if(ch==0x0a){//换行符
  101. showIndex ++;
  102. ptr ++;
  103. nextLine=1;
  104. }else{//非中文
  105. if(leftDot>=8){//还可以显示
  106. showIndex ++;
  107. ptr ++;
  108. leftDot -= 8;
  109. }else{//不足够显示了
  110. nextLine=1;
  111. }
  112. }
  113. if(nextLine!=0){
  114. page++;
  115. nextLine=0;
  116. leftDot=validWidthDot;
  117. if(((sutNewMessage.SY+(page+1)*yHeight) >= END_SHOW_Y) || //Y 已不够显示了(有可能还不够显示MAX_ROW_NUM行)
  118. (page>=MAX_ROW_NUM)){//显示完一页了
  119. if(num < MAX_PAGES){
  120. page=0;
  121. sutNewMessage.indexTable[num++]=showIndex;
  122. }
  123. }
  124. }
  125. }
  126. sutNewMessage.validIndexNum=num;
  127. thisDir=SHOW_DOWN;
  128. sutNewMessage.showIndex=0;
  129. }else if(thisDir==SHOW_DOWN){
  130. if(sutNewMessage.validIndexNum<=1) return;//只有一页的就不用再重显示了
  131. if(++sutNewMessage.showIndex>=sutNewMessage.validIndexNum) sutNewMessage.showIndex=0;
  132. }else if(thisDir==SHOW_UP){
  133. if(sutNewMessage.validIndexNum<=1) return;//只有一页的就不用再重显示了
  134. if(sutNewMessage.showIndex==0) sutNewMessage.showIndex=sutNewMessage.validIndexNum-1;
  135. else sutNewMessage.showIndex--;
  136. }else if(thisDir==SHOW_HEAD){//显示开头
  137. sutNewMessage.showIndex=0;
  138. }
  139. guiClearRect(0,sutNewMessage.SY,GLCD_WIDTH-1,END_SHOW_Y-1,guiGetBackColor());
  140. //下面按实际显示
  141. leftDot=validWidthDot;
  142. showIndex=sutNewMessage.indexTable[sutNewMessage.showIndex];//获取源串显示开始索引
  143. page=0;
  144. fillindex=0;
  145. nextLine=0;
  146. ptr=sutNewMessage.info+showIndex;
  147. while(*ptr !=0){
  148. ch=*ptr;
  149. if(ch > 0x9f){//中文
  150. if(leftDot>=sutNewMessage.xlen){//还可以显示
  151. memcpy(showBuf+fillindex, ptr, 2);
  152. fillindex += 2;//移动源串索引
  153. ptr += 2;//移动源串地址
  154. leftDot -= sutNewMessage.xlen;//减去剩下可显示点数
  155. }else{//不足够显示了
  156. nextLine=1;
  157. }
  158. }else if(ch==0x0a){//换行符
  159. ptr ++;
  160. nextLine=1;
  161. }else{//非中文
  162. if(leftDot>=8){//还可以显示
  163. showBuf[fillindex++] = ch;
  164. ptr ++;
  165. leftDot -= 8;
  166. }else{//不足够显示了
  167. nextLine=1;
  168. }
  169. }
  170. if(nextLine!=0){
  171. nextLine=0;
  172. if(fillindex<sizeof(showBuf)) showBuf[fillindex]=0;
  173. fillindex=0;
  174. guiShowStr(START_SHOW_X, sutNewMessage.SY+page++*yHeight, showBuf, sutNewMessage.font, REVERSED_NO, COLOR_BLACK, guiGetBackColor());
  175. leftDot=validWidthDot;
  176. if(((sutNewMessage.SY+(page+1)*yHeight) >= END_SHOW_Y) || //Y 已不够显示了(有可能还不够显示MAX_ROW_NUM行)
  177. (page>=MAX_ROW_NUM)){//显示完一页了
  178. return;
  179. }
  180. }
  181. }
  182. if(fillindex>0){
  183. if(fillindex<sizeof(showBuf)) showBuf[fillindex]=0;
  184. guiShowStr(START_SHOW_X, sutNewMessage.SY+page++*yHeight, showBuf, sutNewMessage.font, REVERSED_NO, COLOR_BLACK, guiGetBackColor());
  185. }
  186. }