/******************************************************************************** * File Name: Spin.c * Function Describe: * Relate Module: lcd.c,lcd.h,Key.c,Key.h * Explain: * Writer: LiuYanZhi * Date: 2003-11-7 * Rewriter: * Date: *******************************************************************************/ #include #include #include "lcd.h" #include "KeyLed.h" #include "Spin.h" /******************************************************************************** * Function:SpinInit * initialize spin struct *******************************************************************************/ void SpinInit ( struct SUT_SPIN *p, unsigned short x, unsigned char y, char **item ) { unsigned char i; p->x = x; p->y = y; p->item = item; p->itemnum = 0; i = 0; p->width = 0; for(;;) { if(strlen(p->item[i]) == 0)break; if(strlen(p->item[i]) > p->width) p->width = strlen(p->item[i]); i++; p->itemnum ++; } p->top = 0; p->bottom = p->itemnum - 1; if(p->handle < p->top)p->handle = p->top; if(p->handle > p->bottom)p->handle = p->bottom; p->focus = 0; p->disable = 0; p->keyflag = 0; p->type = 0; p->step = 1; } /******************************************************************************** * Function:SpinInitData * initialize spin struct to data mode *******************************************************************************/ void SpinInitData ( struct SUT_SPIN *p, unsigned short x, unsigned char y, unsigned char width, short min, short max, short step, char *unit ) { p->x = x; p->y = y; p->width = width; p->top = min; p->bottom = max; p->unit = unit; p->step = step; if(p->handle < p->top || p->handle > p->bottom) p->handle = p->top; p->focus = 0; p->disable = 0; p->keyflag = 0; p->type = 1; } /******************************************************************************** * Function:SpinSetPage *******************************************************************************/ void SpinSetRange(struct SUT_SPIN *p,short top,short bottom) { p->top = top; p->bottom = bottom; if(p->type == 0) { if(p->top >= p->itemnum)p->top = p->itemnum - 1; if(p->bottom >= p->itemnum)p->bottom = p->itemnum - 1; if(p->bottom < p->top)p->bottom = p->top; } if(p->handle < p->top)p->handle = p->top; if(p->handle > p->bottom)p->handle = p->bottom; } /******************************************************************************** * Function:SpinSetFocus *******************************************************************************/ void SpinSetFocus(struct SUT_SPIN *p) { if(p->disable)return; if(p->focus)return; p->focus = 1; LcdReverseBar(p->x,p->y-1,p->x+(p->width-1)*8,p->y+16); } /******************************************************************************** * Function:SpinLoseFocus *******************************************************************************/ void SpinLoseFocus(struct SUT_SPIN *p) { if(p->disable)return; if(!p->focus)return; p->focus = 0; LcdReverseBar(p->x,p->y-1,p->x+(p->width-1)*8,p->y+16); } /******************************************************************************** * Function:SpinShow * show spin *******************************************************************************/ void SpinShow(struct SUT_SPIN *p) { unsigned char x; char buffer[20]; LcdClearArea(p->x,p->y,p->x+(p->width-1)*8,p->y+16); if(p->type == 0) { if(p->disable)LcdShowStr(p->x,p->y,p->item[p->handle],0x03); else LcdShowStr(p->x,p->y,p->item[p->handle],0x01); if(p->disable)return; } else { sprintf(buffer,"%d",p->handle); strcat(buffer,p->unit); x = strlen(buffer); if(x > p->width) { x = p->width; buffer[x] = 0; } x = (p->width - x) / 2; LcdShowStr(p->x+x*8,p->y,buffer,0x00); } if(p->focus)LcdReverseBar(p->x,p->y-1,p->x+(p->width-1)*8,p->y+16); } /******************************************************************************** * Function:SpinShowBorder *******************************************************************************/ void SpinShowBorder(struct SUT_SPIN *p) { unsigned short x; unsigned char width; x = (p->x >> 3) * 8; width = p->width * 8; LcdShowRect(x-1,p->y-2,x+width,p->y+17,0x01); } /******************************************************************************** * Function:SpinGetHandle * get spin's handle *******************************************************************************/ short SpinGetHandle(struct SUT_SPIN *p) { return p->handle; } /******************************************************************************** * Function:SpinResponse * spin response *******************************************************************************/ unsigned char SpinResponse(struct SUT_SPIN *p) { unsigned char task; if(p->disable)return 0; if(!p->focus)return 0; task = 0xFF; switch(g_ucKeyValue) { case KEY_UP: if(p->keyflag)task = 2; else task = 0; break; case KEY_DOWN: if(p->keyflag)task = 3; else task = 1; break; case KEY_LEFT: if(p->keyflag)task = 0; else task = 2; break; case KEY_RIGHT: if(p->keyflag)task = 1; else task = 3; break; case KEY_BACK: return 1; case KEY_ENTER: return 2; default: break; } switch(task) { case 0: LcdReverseBar(p->x,p->y-1,p->x+(p->width-1)*8,p->y+16); if((p->handle - p->step) < p->top)p->handle = p->bottom; else p->handle -= p->step; SpinShow(p); break; case 1: LcdReverseBar(p->x,p->y-1,p->x+(p->width-1)*8,p->y+16); if((p->handle + p->step) > p->bottom)p->handle = p->top; else p->handle += p->step; SpinShow(p); break; case 2: return 3; // break; case 3: return 4; //break; } return 0; } void SpinSetNext(struct SUT_SPIN *p) { LcdReverseBar(p->x,p->y-1,p->x+(p->width-1)*8,p->y+16); if(p->handle >= p->bottom)p->handle = p->top; else p->handle += p->step; SpinShow(p); } /******************************************************************************** * End of focus *******************************************************************************/