#include "mywin_inc.h" #include "timer.h" #include "mywin_user_timeout.h" #define WIN_TIMEOUT_TYPE "WIN_TimeOutStruct" //把毫秒数转化为时分秒 static void msToHour (u32 msint,int *hour,int *min,int *sec,int *ms) { *ms=msint%100; *sec=msint/100%60; *min=msint/60/100%60; *hour=msint/60/60/100%24; } static u32 hourToms (int hour,int min,int sec,int ms) { return hour*60*60*100+min*60*100+sec*100+ms; } static u32 g_ms; //获取毫秒数 static u32 timer_getMs (void) { return g_ms; } //1ms计时中断 static void timer_irq (void) { if (g_ms) g_ms--; } static void timer_init (u32 time) { g_ms=time; TIMER_InitStruct timer_init={0}; timer_init.Cycle=10000; //10毫秒计时器 timer_init.Tim=TIM3; timer_init.UpdataCall=timer_irq; TIMER_InitNormal(&timer_init); } static void timer_deInit (void) { TIMER_DeInit (TIM3); g_ms=0; } static void timer_spend (void) { TIM_Cmd(TIM3,DISABLE); } static void timer_continue (void) { TIM_Cmd(TIM3,ENABLE); } WIN_TimeOutStruct *WIN_CreatTimeOut (WIN_WindowStruct *base, void (*msgLoop)(struct _WIN_WindowStruct *win,WIN_MsgStruct *msg), int x,int y,int x_size,int y_size) { //重设消息循环 if (msgLoop==0) { msgLoop=(void (*)(struct _WIN_WindowStruct *win,WIN_MsgStruct *msg))TIMEOUT_defaultMsgLoop; } WIN_TimeOutStruct *ret=mymalloc (sizeof ( WIN_TimeOutStruct)); //调用父类的构造函数 if (ret) { mymemset (ret,0,sizeof ( WIN_TimeOutStruct)); // if (0==WIN_CreatWindowExt((WIN_WindowStruct *)ret,base,msgLoop,x,y,x_size,y_size)) if (0==WIN_CreatTouchEx((WIN_TouchWinStruct *)ret,base,msgLoop,x,y,x_size,y_size)) { //创建失败 myfree (ret); ret=0; } else { ((WIN_WindowStruct *)ret)->winType=WIN_TIMEOUT_TYPE; ((WIN_WindowStruct *)ret)->intercept=1; ((WIN_WindowStruct *)ret)->deleteWindow=(void (*)(struct _WIN_WindowStruct *win))WIN_DeleteTimeOut; //构造一个消息框 ret->title="倒计时"; ret->rectColor=0x221f18; ret->txtColor=0x342e27; ret->selectRectColor=0x342f2a; ret->selectTxtColor=0xc2ae9b; ret->timerId=WIN_CreatTimer ((WIN_WindowStruct *)ret,70); } } return ret; } void WIN_DeleteTimeOut (WIN_TimeOutStruct *timeout ) { //回收定时器 if (timeout->power) { timer_deInit (); } //调用父类的销毁函数 WIN_DeleteWindow ( (WIN_WindowStruct *)timeout); } void TIMEOUT_KeyEnter (WIN_TimeOutStruct *timeout) { if (timeout->power==0) { timeout->index++; if (timeout->index>=3) { timeout->power=1; timeout->fresh=0; timer_init(hourToms (timeout->hour,timeout->min,timeout->sec,0)); } } else { if (timer_getMs()) { if (timeout->spend==0) { timeout->spend=1; timer_spend(); }else { timeout->spend=0; timer_continue(); } } else { //重新设置倒计时 timeout->power=0; timer_deInit (); timeout->index=0; timeout->spend=0; } } } void TIMEOUT_KeyBack (WIN_TimeOutStruct *timeout) { if (timeout->power) { //重新设置倒计时 timeout->power=0; timer_deInit (); timeout->index=0; timeout->spend=0; } else { //返回 ((WIN_WindowStruct *)timeout)->deleteWindow((WIN_WindowStruct *)timeout); } } void TIMEOUT_KeyUp (WIN_TimeOutStruct *timeout) { if (timeout->power==0) { if (timeout->index==0) { if (timeout->hour<23) timeout->hour++; else timeout->hour=0; } else if (timeout->index==1) { if (timeout->min<59) timeout->min++; else timeout->min=0; } else if (timeout->index==2) { if (timeout->sec<59) timeout->sec++; else timeout->sec=0; } } } void TIMEOUT_KeyDown (WIN_TimeOutStruct *timeout) { if (timeout->power==0) { if (timeout->index==0) { if (timeout->hour>0) timeout->hour--; else timeout->hour=23; } else if (timeout->index==1) { if (timeout->min>0) timeout->min--; else timeout->min=59; } else if (timeout->index==2) { if (timeout->sec>0) timeout->sec--; else timeout->sec=59; } } } //消息框的绘制函数 void TIMEOUT_DefaultPaint (WIN_TimeOutStruct *timeout) { int x=0; int y=0; int x_size=((WIN_WindowStruct *)timeout)->x_size; int y_size=((WIN_WindowStruct *)timeout)->y_size; char txt_buff[20]={0}; WIN_PaintBackGround ((WIN_WindowStruct *)timeout); u32 font_type=WIN_SetFontType(24); //显示标题 int img_x=x+25; int img_y=y; int img_xsize=0; int img_ysize=0; int head_ysize=45; WIN_SetLcdColor (((WIN_WindowStruct *)timeout)->color); if (timeout->icon) { WIN_GetImageSize (timeout->icon,&img_xsize,&img_ysize); if (img_ysize>head_ysize) img_ysize=head_ysize; WIN_DrawImagByAlpha (img_x,img_y+head_ysize/2-img_ysize/2, img_xsize,img_ysize,timeout->icon,WIN_GetLcdColor16()); img_xsize+=10; } WIN_DrawTxtAt (timeout->title,img_x+img_xsize,y+head_ysize/2-WIN_GetFontHight()/2); x+=25;x_size-=25*2; y+=45;y_size-=45*2; //绘制矩形 int x_step=x_size/2; int y_step=y_size/3; x+=(x_size-x_step*2)/2; y+=(y_size-y_step*3)/2; x_size=x_step*2; y_size=y_step*3; WIN_SetLcdColor (timeout->rectColor); WIN_FillRectByColorAlpha (x,y,x_size-1,y_step*2-1,16); WIN_SetLcdColor (timeout->selectRectColor); WIN_FillRectByColorAlpha (x,y+y_step*2,x_step-1,y_step-1,16); WIN_FillRectByColorAlpha (x+x_step,y+y_step*2,x_step-1,y_step-1,16); //保存按钮位置 timeout->rect_ok.x=x,timeout->rect_ok.y=y+y_step*2, timeout->rect_ok.x_size=x_step,timeout->rect_ok.y_size=y_step; timeout->rect_back.x=x+x_step,timeout->rect_back.y=y+y_step*2, timeout->rect_back.x_size=x_step,timeout->rect_back.y_size=y_step; //显示当前秒数 int hour,min,sec,ms; msToHour (timer_getMs(),&hour,&min,&sec,&ms); sprintf (txt_buff,"%02d:%02d:%02d:%02d",hour,min,sec,ms); //显示选中框 int num_type=30; WIN_SetFontType(num_type); int num_x=x+x_size/2-strlen(txt_buff)*WIN_GetFontWidth()/2/2; int num_y=y+y_step-20; int num_xsize=strlen(txt_buff)*WIN_GetFontWidth()/2; int num_ysize=WIN_GetFontHight(); //保存数字显示的位置,自动刷新的时候要用 timeout->num_x=num_x; timeout->num_y=num_y; timeout->num_xsize=num_xsize; timeout->num_ysize=num_ysize; if (timeout->power==0) { if (timeout->index==0) { WIN_SetLcdColor (timeout->selectRectColor); WIN_FillRectByColorAlpha (num_x,num_y,num_type+4,35,16); } else if (timeout->index==1) { WIN_SetLcdColor (timeout->selectRectColor); WIN_FillRectByColorAlpha (num_x+num_type*3/2+1,num_y,num_type+4,35,16); } else if (timeout->index==2) { WIN_SetLcdColor (timeout->selectRectColor); WIN_FillRectByColorAlpha (num_x+num_type*6/2+3,num_y,num_type+4,35,16); } else if (timeout->index==3) { WIN_SetLcdColor (timeout->selectRectColor); WIN_FillRectByColorAlpha (num_x+num_type*5/2,num_y,num_type+4,35,16); } WIN_SetLcdColor (timeout->selectTxtColor); sprintf (txt_buff,"%02d:%02d:%02d:%02d",timeout->hour,timeout->min,timeout->sec,0); WIN_DrawTxtAt(txt_buff,num_x,num_y); } else { if (timer_getMs()) { WIN_SetLcdColor (timeout->selectTxtColor); WIN_DrawTxtAt(txt_buff,num_x,num_y); } else { WIN_SetLcdColor (timeout->selectTxtColor); int x_off=strlen(txt_buff)*WIN_GetFontWidth()/2/2; WIN_DrawTxtHCenterAt ("计时到",num_x+x_off,num_y); } } //显示按钮 WIN_SetFontType(24); WIN_SetLcdColor (timeout->selectTxtColor); if (timeout->power==0) { if (timeout->index<2) WIN_DrawTxtCenterAtRect ("下一步",x,y+y_step*2,x_step-1,y_step-1); else WIN_DrawTxtCenterAtRect ("开始",x,y+y_step*2,x_step-1,y_step-1); WIN_DrawTxtCenterAtRect ("返回",x+x_step,y+y_step*2,x_step-1,y_step-1); } else { if (timeout->spend==0) { if (timer_getMs()) { WIN_DrawTxtCenterAtRect ("暂停",x,y+y_step*2,x_step-1,y_step-1); } else { WIN_DrawTxtCenterAtRect ("再次",x,y+y_step*2,x_step-1,y_step-1); } WIN_DrawTxtCenterAtRect ("停止",x+x_step,y+y_step*2,x_step-1,y_step-1); } else { WIN_DrawTxtCenterAtRect ("继续",x,y+y_step*2,x_step-1,y_step-1); WIN_DrawTxtCenterAtRect ("停止",x+x_step,y+y_step*2,x_step-1,y_step-1); } } WIN_SetFontType(font_type); } //消息框的消息处理函数 void TIMEOUT_defaultMsgLoop (WIN_TimeOutStruct *timeout,WIN_MsgStruct *msg) { WIN_KeyStruct *k=0; WIN_MoveStruct *m=0; switch (msg->msg) { case WIN_MSG_PAINT: TIMEOUT_DefaultPaint(timeout); break; case WIN_MSG_TIMER: if (msg->data.v==timeout->timerId) { WIN_SetInvalidRectWhenTop ((WIN_WindowStruct *)timeout,timeout->num_x,timeout->num_y,timeout->num_xsize,timeout->num_ysize); if ((timer_getMs()==0)&&(timeout->power==1)&&(timeout->fresh==0)) { timeout->fresh=1; WIN_SetInvalid ((WIN_WindowStruct *)timeout); } } break; case WIN_MSG_KEY: k=msg->data.p; if (k->shortPress&KEY_VALUE_UP)//短按 { TIMEOUT_KeyUp (timeout); } else if (k->shortPress&KEY_VALUE_DOWN) { TIMEOUT_KeyDown (timeout); } else if (k->shortPress&KEY_VALUE_HOME) { TIMEOUT_KeyBack(timeout); } else if (k->shortPress&KEY_VALUE_ENTER) { TIMEOUT_KeyEnter(timeout); } if (k->shortPress) { WIN_SetInvalid ((WIN_WindowStruct *)timeout); } break;//case WIN_MSG_KEY: case WIN_MSG_MOVE: m=msg->data.p; switch (m->moveType) { case MOVE_DATA_SHORT: { if (POS_InRect(timeout->rect_ok.x,timeout->rect_ok.y, timeout->rect_ok.x_size,timeout->rect_ok.y_size,m->x_move,m->y_move)) { TIMEOUT_KeyEnter(timeout); } else if (POS_InRect(timeout->rect_back.x,timeout->rect_back.y, timeout->rect_back.x_size,timeout->rect_back.y_size,m->x_move,m->y_move)) { TIMEOUT_KeyBack(timeout); } WIN_SetInvalid ((WIN_WindowStruct *)timeout); } break; default: break; } break; default: WIN_DefaultMsgLoop((WIN_WindowStruct *)timeout,msg); break; } } WIN_TimeOutStruct *TIMEOUT_SaveTime (WIN_WindowStruct *base) { WIN_TimeOutStruct *ret=0; if (base) { ret=WIN_CreatTimeOut (base,0,0,0,base->x_size,base->y_size); } else { ret=WIN_CreatTimeOut (0,0,0,0,WIN_GetBaseWindow()->x_size,WIN_GetBaseWindow()->y_size); } WIN_SetBackPic ((WIN_WindowStruct *)ret,&base->pic); WIN_ShowWindow((WIN_WindowStruct*)ret); return ret; }