123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- #include "newMessage.h"
- #include "log.h"
- #define MAX_PAGES 20 //最多显示20页
- #define START_SHOW_Y (UI_CONTENT_SHOW_Y+3) //开始显示的Y坐标
- #define END_SHOW_Y (UI_BOTTOM_LINE_Y) //显示结束的Y坐标
- #define START_SHOW_X 3
- #define MAX_ROW_NUM 5 //每页最多显示多少行
- typedef struct{
- unsigned short SY;//开始显示的Y坐标
- unsigned char font;//显示字体
- unsigned char xlen;//根据font配置固定的单字x长度->显示中文时占的宽度
- unsigned char ylen;//根据font配置固定的单字y高度
- unsigned char validIndexNum;//有效页数目
- unsigned short indexTable[MAX_PAGES];//一页最多显示5行,每行最多30字节,一页最多150字节
- unsigned char showIndex;//当前显示表的索引
- char *info;//显示的缓存
- }SUT_NEW_MESSAGE;
- SUT_NEW_MESSAGE sutNewMessage={
- .info=NULL,
- };
- #define MAX_INFO_NUM 3
- char newMessageInit(FONT_MODEENUM fontMode,char *info1, char *info2, char *info3){
- int i,len=0;
- char *infos[MAX_INFO_NUM]={info1,info2,info3};
- char spacesBuf[2]={0x0a,0x00};
- unsigned char unNullNum=0;
- char strcatAlready;
- sutNewMessage.SY=START_SHOW_Y;
- sutNewMessage.font=fontMode;
- if(fontMode==FONT_MODE_12X12){
- sutNewMessage.ylen=12;
- sutNewMessage.xlen=13;
- }else{
- sutNewMessage.ylen=16;
- sutNewMessage.xlen=16;
- }
- //申请下显示的缓存
- for(i=0;i<MAX_INFO_NUM;i++){
- if(infos[i] != NULL){
- len += strlen(infos[i]);
- unNullNum++;
- }
- }
- char *p=NULL;
- if(sutNewMessage.info!=NULL){
- free(sutNewMessage.info);
- sutNewMessage.info=NULL;
- }
- if(len>0 && unNullNum){
- len+=(unNullNum-1)+1;//申请多三个字节(unNullNum个0x0a和多一个空字节位置)
- p=(char *)malloc(sizeof(char)*len);
- if(p==NULL) return 1;
- sutNewMessage.info=p;
- sutNewMessage.info[0]=0;
- strcatAlready=0;
- for(i=0;i<MAX_INFO_NUM;i++){
- if(infos[i] != NULL){
- if(strcatAlready==0){
- strcatAlready=1;
- strcat(sutNewMessage.info, infos[i]);
- }else{
- strcat(sutNewMessage.info, spacesBuf);
- strcat(sutNewMessage.info, infos[i]);
- }
- }
- }
- }else sutNewMessage.info=NULL;
- newMessageShow(SHOW_INIT);
- return 0;
- }
- void newMessageShow(SHOW_ENUM direction)
- {
- //英文/12中文显示, 动态计算每行显示,尽可能每行显示多内容 遇到0x0a则换行显示
- short leftDot;
- char *ptr=sutNewMessage.info,ch;
- unsigned char validWidthDot=GLCD_WIDTH-START_SHOW_X;//每行可显示的点数目
- unsigned char num=0;
- unsigned short showIndex,fillindex;
- char showBuf[30];//一直最多显示30个字符,1个中文占两个字符
- char nextLine=0;
- unsigned char page=0;
- unsigned char yHeight=sutNewMessage.ylen+4;//每行字上下间隔5
- SHOW_ENUM thisDir=direction;
- if(sutNewMessage.info[0]=='\0') return;
- if(thisDir==SHOW_INIT){//计算出分段索引表
- sutNewMessage.indexTable[num++]=0;
- //然后一直往下做预显示得到分面索引表
- leftDot=validWidthDot;
- showIndex=0;
- while(*ptr !=0){
- ch=*ptr;
- if(ch > 0x9f){//中文
- if(leftDot>=sutNewMessage.xlen){//还可以显示
- showIndex += 2;//移动源串索引
- ptr += 2;//移动源串地址
- leftDot -= sutNewMessage.xlen;//减去剩下可显示点数
- }else{//不足够显示了
- nextLine=1;
- }
- }else if(ch==0x0a){//换行符
- showIndex ++;
- ptr ++;
- nextLine=1;
- }else{//非中文
- if(leftDot>=8){//还可以显示
- showIndex ++;
- ptr ++;
- leftDot -= 8;
- }else{//不足够显示了
- nextLine=1;
- }
- }
- if(nextLine!=0){
- page++;
- nextLine=0;
- leftDot=validWidthDot;
- if(((sutNewMessage.SY+(page+1)*yHeight) >= END_SHOW_Y) || //Y 已不够显示了(有可能还不够显示MAX_ROW_NUM行)
- (page>=MAX_ROW_NUM)){//显示完一页了
- if(num < MAX_PAGES){
- page=0;
- sutNewMessage.indexTable[num++]=showIndex;
- }
- }
- }
- }
- sutNewMessage.validIndexNum=num;
- thisDir=SHOW_DOWN;
- sutNewMessage.showIndex=0;
- }else if(thisDir==SHOW_DOWN){
- if(sutNewMessage.validIndexNum<=1) return;//只有一页的就不用再重显示了
- if(++sutNewMessage.showIndex>=sutNewMessage.validIndexNum) sutNewMessage.showIndex=0;
- }else if(thisDir==SHOW_UP){
- if(sutNewMessage.validIndexNum<=1) return;//只有一页的就不用再重显示了
- if(sutNewMessage.showIndex==0) sutNewMessage.showIndex=sutNewMessage.validIndexNum-1;
- else sutNewMessage.showIndex--;
- }else if(thisDir==SHOW_HEAD){//显示开头
- sutNewMessage.showIndex=0;
- }
- guiClearRect(0,sutNewMessage.SY,GLCD_WIDTH-1,END_SHOW_Y-1,guiGetBackColor());
- //下面按实际显示
- leftDot=validWidthDot;
- showIndex=sutNewMessage.indexTable[sutNewMessage.showIndex];//获取源串显示开始索引
- page=0;
- fillindex=0;
- nextLine=0;
- ptr=sutNewMessage.info+showIndex;
- while(*ptr !=0){
- ch=*ptr;
- if(ch > 0x9f){//中文
- if(leftDot>=sutNewMessage.xlen){//还可以显示
- memcpy(showBuf+fillindex, ptr, 2);
- fillindex += 2;//移动源串索引
- ptr += 2;//移动源串地址
- leftDot -= sutNewMessage.xlen;//减去剩下可显示点数
- }else{//不足够显示了
- nextLine=1;
- }
- }else if(ch==0x0a){//换行符
- ptr ++;
- nextLine=1;
- }else{//非中文
- if(leftDot>=8){//还可以显示
- showBuf[fillindex++] = ch;
- ptr ++;
- leftDot -= 8;
- }else{//不足够显示了
- nextLine=1;
- }
- }
- if(nextLine!=0){
- nextLine=0;
- if(fillindex<sizeof(showBuf)) showBuf[fillindex]=0;
- fillindex=0;
- guiShowStr(START_SHOW_X, sutNewMessage.SY+page++*yHeight, showBuf, sutNewMessage.font, REVERSED_NO, COLOR_BLACK, guiGetBackColor());
- leftDot=validWidthDot;
- if(((sutNewMessage.SY+(page+1)*yHeight) >= END_SHOW_Y) || //Y 已不够显示了(有可能还不够显示MAX_ROW_NUM行)
- (page>=MAX_ROW_NUM)){//显示完一页了
- return;
- }
- }
- }
- if(fillindex>0){
- if(fillindex<sizeof(showBuf)) showBuf[fillindex]=0;
- guiShowStr(START_SHOW_X, sutNewMessage.SY+page++*yHeight, showBuf, sutNewMessage.font, REVERSED_NO, COLOR_BLACK, guiGetBackColor());
- }
- }
|