本帖最后由 zhanddkk 于 2015-9-13 00:47 编辑
最近公司里的废弃 电路板比较多就是拿回来自己折腾玩了,
以前大学的时候用51做过数字万年历,但是51那可怜的资源真是设计起来麻烦的要死,现在在ARM7
上搞,简直很随意了。
其实原理也很简单没啥技术含量,纯属娱乐,就当做对ucos ii的学习了。
- #include "Menu.h"
- //Common
- //extern const stStateInfo NormalClock;
- const stMenuList *pMenuListEntry = NULL;
- //Home
- const char clockStr[] = {
- MENU_ZH_CODE_START, 43,
- MENU_ZH_CODE_START, 44,
- };
- static const stHomeIcon clockIcon = {
- BMP_ICON_ID_CLOCK,
- {clockStr, sizeof(clockStr)},
- &NormalClock,
- 0x00000000,
- };
- const char calendarStr[] = {
- MENU_ZH_CODE_START, 0,
- MENU_ZH_CODE_START, 45,
- };
- static const stHomeIcon calendarIcon = {
- BMP_ICON_ID_CALENDAR,
- {calendarStr, sizeof(calendarStr)},
- &NormalClock,
- 0x00000000,
- };
- const char gameStr[] = {
- MENU_ZH_CODE_START, 46,
- MENU_ZH_CODE_START, 47,
- };
- static const stHomeIcon gameIcon = {
- BMP_ICON_ID_GAME,
- {gameStr, sizeof(gameStr)},
- &NormalClock,
- 0x00000000,
- };
- const char settingStr[] = {
- MENU_ZH_CODE_START, 48,
- MENU_ZH_CODE_START, 49,
- };
- static const stHomeIcon settingIcon = {
- BMP_ICON_ID_SETTING,
- {settingStr, sizeof(settingStr)},
- &Menu,
- (INT32U)&MenuSetList,
- };
- const char systemStr[] = {
- MENU_ZH_CODE_START, 50,
- MENU_ZH_CODE_START, 51,
- };
- static const stHomeIcon systemIcon = {
- BMP_ICON_ID_SYSTEM,
- {systemStr, sizeof(systemStr)},
- &NormalClock,
- 0x00000000,
- };
- const stHomeIcon *HomeIcon[] = {
- &clockIcon,
- &calendarIcon,
- &gameIcon,
- &settingIcon,
- &systemIcon,
- };
- const INT8U HomeIconTotalNum = sizeof(HomeIcon) / sizeof(stHomeIcon *);
- //Set menu
- static INT8U menuSetInputData[2] = {1, 1};
- #define MENU_SET_ITEM_ID 0u
- #define MENU_SET_TIME_ITEM_ID MENU_SET_ITEM_ID + 0u
- static const char menuSetTimeStr[] = {
- MENU_ZH_CODE_START, 43,
- MENU_ZH_CODE_START, 56,
- MENU_ZH_CODE_START, 48,
- MENU_ZH_CODE_START, 49
- };
- const stMenuItem MenuSetTime = {
- 0u,
- MENU_SET_TIME_ITEM_ID,
- {menuSetTimeStr, sizeof(menuSetTimeStr)},
- &NormalClock,
- };
- #define MENU_SET_DATE_ITEM_ID MENU_SET_TIME_ITEM_ID + 1u
- static const char menuSetDateStr[] = {
- MENU_ZH_CODE_START, 0,
- MENU_ZH_CODE_START, 13,
- MENU_ZH_CODE_START, 48,
- MENU_ZH_CODE_START, 49
- };
- const stMenuItem MenuSetDate = {
- 0u,
- MENU_SET_DATE_ITEM_ID,
- {menuSetDateStr, sizeof(menuSetDateStr)},
- &SetDate,
- };
- #define MENU_SYSTEM_SET_ITEM_ID MENU_SET_DATE_ITEM_ID + 1u
- static const char menuSystemSetStr[] = {
- MENU_ZH_CODE_START, 50,
- MENU_ZH_CODE_START, 51,
- MENU_ZH_CODE_START, 48,
- MENU_ZH_CODE_START, 49
- };
- const stMenuItem MenuSystemSet = {
- 3u,
- MENU_SYSTEM_SET_ITEM_ID,
- {menuSystemSetStr, sizeof(menuSystemSetStr)},
- &MenuSystemSetList,
- };
- const stMenuItem *menuSetListGroup[] = {
- &MenuSetTime,
- &MenuSetDate,
- &MenuSystemSet,
- };
- const stMenuList MenuSetList = {
- sizeof(menuSetListGroup) / sizeof(stMenuItem *),
- menuSetInputData,
- &settingIcon.str,
- NULL,
- NULL,
- menuSetListGroup,
- };
- //System set menu
- static INT8U menuSystemSetInputData[2] = {1, 1};
- #define MENU_SET_SYSTEM_ID 0u
- #define MENU_BACK_LIGHT_TIME_ITEM_ID MENU_SET_SYSTEM_ID + 0u
- static const char menuBackLightTimeStr[] = {
- MENU_ZH_CODE_START, 57,
- MENU_ZH_CODE_START, 58,
- MENU_ZH_CODE_START, 43,
- MENU_ZH_CODE_START, 56
- };
- const stMenuItem MenuBackLightTime = {
- 0u,
- MENU_BACK_LIGHT_TIME_ITEM_ID,
- {menuBackLightTimeStr, sizeof(menuBackLightTimeStr)},
- &NormalClock,
- };
- #define MENU_MENU_OVERTIME_ITEM_ID MENU_BACK_LIGHT_TIME_ITEM_ID + 0u
- static const char menuOvertimeStr[] = {
- MENU_ZH_CODE_START, 59,
- MENU_ZH_CODE_START, 60,
- MENU_ZH_CODE_START, 61,
- MENU_ZH_CODE_START, 43,
- MENU_ZH_CODE_START, 43,
- MENU_ZH_CODE_START, 56
- };
- const stMenuItem MenuOvertime = {
- 0u,
- MENU_MENU_OVERTIME_ITEM_ID,
- {menuOvertimeStr, sizeof(menuOvertimeStr)},
- &NormalClock,
- };
- #define MENU_LCD_CONTRAST_ITEM_ID MENU_MENU_OVERTIME_ITEM_ID + 0u
- static const char menuLcdContrastStr[] = {
- 'L',
- 'C',
- 'D',
- MENU_ZH_CODE_START, 64,
- MENU_ZH_CODE_START, 65,
- MENU_ZH_CODE_START, 66
- };
- const stMenuItem MenuLcdContrast = {
- 0u,
- MENU_LCD_CONTRAST_ITEM_ID,
- {menuLcdContrastStr, sizeof(menuLcdContrastStr)},
- &NormalClock,
- };
- const stMenuItem *menuSystemSetListGroup[] = {
- &MenuBackLightTime,
- &MenuOvertime,
- &MenuLcdContrast,
- };
- const stMenuList MenuSystemSetList = {
- sizeof(menuSystemSetListGroup) / sizeof(stMenuItem *),
- menuSystemSetInputData,
- &MenuSystemSet.itemName,
- &MenuSystemSet,
- &MenuSetList,
- menuSystemSetListGroup,
- };
复制代码
- #ifndef MENU_H_
- #define MENU_H_
- #include "Platform.h"
- #include "ucos_ii.h"
- #include "State.h"
- #define MENU_ZH_CODE_START 0xA1
- #define HOME_ICON_ID_TYPE INT8U
- struct ST_MENU_ITEM;
- struct ST_MENU_ITEM_STR;
- struct ST_MENU_LIST;
- typedef struct ST_MENU_ITEM_STR{
- const char *pstr;
- INT8U len;
- }stMenuItemStr;
- typedef struct ST_MENU_LIST {
- INT8U itemNum;
- INT8U *pdata;
- const stMenuItemStr *pname;
- const struct ST_MENU_ITEM *pparentItem;
- const struct ST_MENU_LIST *pparentItemList;
- const struct ST_MENU_ITEM **pItem;
- }stMenuList;
- typedef struct ST_MENU_ITEM {
- INT8U subItemNum;
- INT8U thisItemID;
- stMenuItemStr itemName;
- const void *pData;
- }stMenuItem;
- typedef struct {
- const HOME_ICON_ID_TYPE iconID;
- const stMenuItemStr str;
- const struct ST_STATE_INFO *pstate;
- INT32U data;
- }stHomeIcon;
- extern const stMenuList MenuSetList;
- extern const stMenuList MenuSystemSetList;
- extern const stMenuList *pMenuListEntry;
- extern const stHomeIcon *HomeIcon[];
- extern const INT8U HomeIconTotalNum;
- #endif /* MENU_H_ */
复制代码
- #include "State.h"
- #include "CalendarCalculated.h"
- //NormalClock data
- void State_NormalClock(void *ptr);
- const stStateInfo NormalClock = {
- 0x00000000,
- State_NormalClock,
- NULL,
- };
- //Home data
- void State_Home(void *ptr);
- static INT8U homeInputData[2] = {1, 1};
- const stStateInfo Home = {
- 0x00000000,
- State_Home,
- homeInputData,
- };
- //Power off data
- void State_PowerOff(void *ptr);
- static INT32U lastStateAddr = 0;
- const stStateInfo PowerOff = {
- 0x00000000,
- State_PowerOff,
- &lastStateAddr,
- };
- //Menu data
- void State_Menu(void *ptr);
- const stStateInfo Menu = {
- (INT32U)&pMenuListEntry,
- State_Menu,
- NULL,
- };
- //Set date
- void State_SetDate(void *ptr);
- const stStateInfo SetDate = {
- (INT32U)&pMenuListEntry,
- State_SetDate,
- (void *)&MenuSetList,
- };
- //Common data
- const stStateInfo *pState = &NormalClock;
- static void (*pTimeAction)(void);
- OS_EVENT *pStateQ;
- static stStateMsg stateMsg[STATE_MSG_SIZE];
- void *StateMsg[STATE_MSG_SIZE];
- static void __defaultEventProcess(INT8U *pexitFlag, stMSG *pmsg, void *ptr);
- //NormalClock
- static void __lunarCalendarDisplay(void)
- {
- INT8U x = 48;
- switch (lunaCalendarData.month)
- {
- case 1:
- LCD_PutZH16x16(x, 4, 17, 1);
- break;
- case 2:
- LCD_PutZH16x16(x, 4, 2, 1);
- break;
- case 3:
- LCD_PutZH16x16(x, 4, 3, 1);
- break;
- case 4:
- LCD_PutZH16x16(x, 4, 4, 1);
- break;
- case 5:
- LCD_PutZH16x16(x, 4, 5, 1);
- break;
- case 6:
- LCD_PutZH16x16(x, 4, 6, 1);
- break;
- case 7:
- LCD_PutZH16x16(x, 4, 7, 1);
- break;
- case 8:
- LCD_PutZH16x16(x, 4, 8, 1);
- break;
- case 9:
- LCD_PutZH16x16(x, 4, 9, 1);
- break;
- case 10:
- LCD_PutZH16x16(x, 4, 10, 1);
- break;
- case 11:
- LCD_PutZH16x16(x, 4, 10, 1);
- x += 16;
- LCD_PutZH16x16(x, 4, 1, 1);
- break;
- case 12:
- LCD_PutZH16x16(x, 4, 10, 1);
- x += 16;
- LCD_PutZH16x16(x, 4, 2, 1);
- break;
- default:
- break;
- }
- x += 16;
- LCD_PutZH16x16(x, 4, 19, 1);
- if(lunaCalendarData.day < 11)
- {
- //初
- x += 16;
- LCD_PutZH16x16(x, 4, 18, 1);
- x += 16;
- LCD_PutZH16x16(x, 4, lunaCalendarData.day, 1);
- }
- else if(lunaCalendarData.day < 20)
- {
- //十
- x += 16;
- LCD_PutZH16x16(x, 4, 10, 1);
- x += 16;
- LCD_PutZH16x16(x, 4, lunaCalendarData.day % 10, 1);
- }
- else if(lunaCalendarData.day < 30)
- {
- //廿
- x += 16;
- LCD_PutZH16x16(x, 4, 11, 1);
- x += 16;
- if(lunaCalendarData.day % 10)
- LCD_PutZH16x16(x, 4, lunaCalendarData.day % 10, 1);
- else
- LCD_PutZH16x16(x, 4, 10, 1);
- }
- else
- {
- //三十
- x += 16;
- LCD_PutZH16x16(x, 4, 3, 1);
- x += 16;
- LCD_PutZH16x16(x, 4, 10, 1);
- }
- x += 16;
- if(x < 128)
- {
- LCD_PutMiddleString(x, 4, " ", 1);
- }
- x = 64;
- LCD_PutZH16x16(x, 2, lunaCalendarData.tianGan + 20, 1);
- x += 16;
- LCD_PutZH16x16(x, 2, lunaCalendarData.diZhi + 30, 1);
- }
- static void __NormalClock_TimeAction(void)
- {
- INT8U err, x = 0;
- INT8U h = 0;
- INT8U m = 0;
- INT8U s = 0;
- INT8U d = 0;
- INT8U month = 0;
- INT8U date = 0;
- INT8U year = 0;
- stStateMsg *pStateMsg;
- while(1)
- {
- pStateMsg = (stStateMsg *)OSQPend(pStateQ, 10, &err);
- switch(err)
- {
- case OS_ERR_NONE:
- switch(pStateMsg->cmd)
- {
- case NORMAL_CLOCK_TIMEUPDATE:
- x = 0;
- LCD_PutLargeNumber(x, 0, ((h >> 4) & 0x0f), 1);
- x += 12;
- LCD_PutLargeNumber(x, 0, (h & 0x0f), 1);
- x += 12;
- if(s % 2)
- LCD_PutLargeNumber(x, 0, 10, 1);
- else
- LCD_PutLargeNumber(x, 0, 11, 1);
- x += 12;
- LCD_PutLargeNumber(x, 0, ((m >> 4) & 0x0f), 1);
- x += 12;
- LCD_PutLargeNumber(x, 0, (m & 0x0f), 1);
- break;
- case NORMAL_CLOCK_DATEUPDATE:
- x = 0;
- LCD_PutSmallChar(x, 3, '2', 1);
- x += 6;
- LCD_PutSmallChar(x, 3, '0', 1);
- x += 6;
- LCD_PutSmallChar(x, 3, '0' + ((year >> 4) & 0x0f), 1);
- x += 6;
- LCD_PutSmallChar(x, 3, '0' + (year & 0x0f), 1);
- x += 12;
- LCD_PutSmallChar(x, 3, '0' + ((month >> 4) & 0x0f), 1);
- x += 6;
- LCD_PutSmallChar(x, 3, '0' + (month & 0x0f), 1);
- x += 12;
- LCD_PutSmallChar(x, 3, '0' + ((date >> 4) & 0x0f), 1);
- x += 6;
- LCD_PutSmallChar(x, 3, '0' + (date & 0x0f), 1);
- if(d == 7)
- LCD_PutZH16x16(96, 0, 0, 1);
- else
- LCD_PutZH16x16(96, 0, d, 1);
- solaCalendarToLunarCalendar(
- ((year >> 4) & 0x0f) * 10 +
- (year & 0x0f) + 2000,
- ((month >> 4) & 0x0f) * 10 +
- (month & 0x0f),
- ((date >> 4) & 0x0f) * 10 +
- (date & 0x0f)
- );
- __lunarCalendarDisplay();
- break;
- case NORMAL_CLOCK_INIT:
- LCD_Clear(0);
- LCD_PutSmallChar(24, 3, '/', 1);
- LCD_PutSmallChar(42, 3, '/', 1);
- LCD_PutZH16x16(64, 0, 12, 1);
- LCD_PutZH16x16(80, 0, 13, 1);
- LCD_PutZH16x16(96, 2, 20, 1);
- LCD_PutBmp(48, 6, BMP_BUTTON_ID_HOME, 1);
- LCD_PutBmp(48, 7, BMP_BUTTON_ID_LAMP, 1);
- DS1337_Getc(RTC_HOUR, &h);
- DS1337_Getc(RTC_MIN, &m);
- DS1337_Getc(RTC_SEC, &s);
- DS1337_Getc(RTC_MONTH, &month);
- DS1337_Getc(RTC_DATE, &date);
- DS1337_Getc(RTC_YEAR, &year);
- DS1337_Getc(RTC_DAY, &d);
- if((h < 6) || (h > 18))
- {
- LCD_PutBmp(0, 4, BMP_PIC_ID_NIGHT, 1);
- }
- else if((h >= 6) && (h < 10))
- {
- LCD_PutBmp(0, 4, BMP_PIC_ID_MORNING, 1);
- }
- else if((h >= 10) && (h < 14))
- {
- LCD_PutBmp(0, 4, BMP_PIC_ID_NOON, 1);
- }
- else
- {
- LCD_PutBmp(0, 4, BMP_PIC_ID_AFTERNOON, 1);
- }
- pStateMsg = &stateMsg[1];
- pStateMsg->cmd = NORMAL_CLOCK_TIMEUPDATE;
- OSQPost(pStateQ, (void *)pStateMsg);
- pStateMsg = &stateMsg[2];
- pStateMsg->cmd = NORMAL_CLOCK_DATEUPDATE;
- OSQPost(pStateQ, (void *)pStateMsg);
- break;
- case NORMAL_CLOCK_PICUPDATE:
- LCD_PutBmp(0, 4, (INT8U)(INT32U)pStateMsg->pData, 1);
- break;
- case STATE_EXIT:
- return;
- default:
- break;
- }
- break;
- case OS_ERR_TIMEOUT:
- {
- INT8U tmpSec = s, tmpDate = date, tmpHour = h;
- DS1337_Getc(RTC_HOUR, &h);
- DS1337_Getc(RTC_MIN, &m);
- DS1337_Getc(RTC_SEC, &s);
- DS1337_Getc(RTC_MONTH, &month);
- DS1337_Getc(RTC_DATE, &date);
- DS1337_Getc(RTC_YEAR, &year);
- if(s != tmpSec)
- {
- pStateMsg = &stateMsg[1];
- pStateMsg->cmd = NORMAL_CLOCK_TIMEUPDATE;
- OSQPost(pStateQ, (void *)pStateMsg);
- }
- if(date != tmpDate)
- {
- pStateMsg = &stateMsg[2];
- pStateMsg->cmd = NORMAL_CLOCK_DATEUPDATE;
- OSQPost(pStateQ, (void *)pStateMsg);
- }
- if((tmpHour != h) &&
- ((h == 0x06) || (h == 0x10) || (h == 0x14) || (h == 0x18))
- )
- {
- pStateMsg = &stateMsg[3];
- pStateMsg->cmd = NORMAL_CLOCK_PICUPDATE;
- switch(h)
- {
- case 0x06:
- pStateMsg->pData = (void *)BMP_PIC_ID_MORNING;
- break;
- case 0x10:
- pStateMsg->pData = (void *)BMP_PIC_ID_NOON;
- break;
- case 0x14:
- pStateMsg->pData = (void *)BMP_PIC_ID_AFTERNOON;
- break;
- case 0x18:
- pStateMsg->pData = (void *)BMP_PIC_ID_NIGHT;
- break;
- default:
- pStateMsg->pData = (void *)BMP_PIC_ID_MORNING;
- break;
- }
- OSQPost(pStateQ, (void *)pStateMsg);
- }
- }
- break;
- default:
- OSTimeDly(10);
- break;
- }
- }
- }
- static void __NormalClock_EventProcess(void *ptr)
- {
- INT8U err, exitFlag = 1u;
- stMSG *pmsg;
- stStateMsg *pStateMsg = &stateMsg[0];
- while(exitFlag)
- {
- pmsg = (stMSG *)OSMboxPend(pMsgEvent, 0, &err);
- switch(pmsg->keyValue)
- {
- case KEY_VAL_LEFT:
- break;
- case KEY_VAL_RIGHT:
- break;
- case KEY_VAL_UP:
- pState = &Home;
- ((INT8U *)pState->pdata)[0] = 1;
- ((INT8U *)pState->pdata)[1] = 1;
- pStateMsg->cmd = STATE_EXIT;
- OSQPost(pStateQ, (void *)pStateMsg);
- return;
- case KEY_VAL_DOWN | KEY_VAL_LONG:
- case KEY_VAL_DOWN:
- FLASH_POWER? FLASH_POWER_OFF : FLASH_POWER_ON;
- break;
- default:
- __defaultEventProcess(&exitFlag, pmsg, ptr);
- break;
- }
- }
- }
- void State_NormalClock(void *ptr)
- {
- stStateMsg *pStateMsg = &stateMsg[0];
- pTimeAction = __NormalClock_TimeAction;
- pStateMsg->cmd = NORMAL_CLOCK_INIT;
- OSQPost(pStateQ, (void *)pStateMsg);
- __NormalClock_EventProcess(ptr);
- }
- //Home
- static void __Home_TimeAction(void)
- {
- INT8U err, x = 0;
- INT8U h = 0;
- INT8U m = 0;
- INT8U s = 0;
- stStateMsg *pStateMsg;
- INT8U iconIndex, dspIndex;
- INT8U dspMaxIndex = HomeIconTotalNum > 4 ? 4 : HomeIconTotalNum;
- while(1)
- {
- pStateMsg = (stStateMsg *)OSQPend(pStateQ, 10, &err);
- switch(err)
- {
- case OS_ERR_NONE:
- switch(pStateMsg->cmd)
- {
- case HOME_INIT:
- LCD_Clear(0);
- LCD_PutBmp(0, 6, BMP_BUTTON_ID_ENTER, 1);
- LCD_PutBmp(48, 6, BMP_BUTTON_ID_UP, 1);
- LCD_PutBmp(48, 7, BMP_BUTTON_ID_DOWN, 1);
- LCD_PutBmp(96, 6, BMP_BUTTON_ID_BACK, 1);
- DS1337_Getc(RTC_HOUR, &h);
- DS1337_Getc(RTC_MIN, &m);
- DS1337_Getc(RTC_SEC, &s);
- dspIndex = ((INT8U *)pStateMsg->pData)[0];
- iconIndex = ((INT8U *)pStateMsg->pData)[1];
- pStateMsg = &stateMsg[1];
- pStateMsg->cmd = HOME_ICONUPDATE;
- pStateMsg->pData = (void *)&iconIndex;
- OSQPost(pStateQ, (void *)pStateMsg);
- pStateMsg = &stateMsg[2];
- pStateMsg->cmd = HOME_TIMEUPDATE;
- OSQPost(pStateQ, (void *)pStateMsg);
- break;
- case HOME_ICONMOVELEFT:
- iconIndex = (*(INT8U *)pStateMsg->pData);
- if(iconIndex == HomeIconTotalNum)
- dspIndex = dspMaxIndex;
- else if(dspIndex > 1)
- dspIndex--;
- pStateMsg = &stateMsg[1];
- pStateMsg->cmd = HOME_ICONUPDATE;
- pStateMsg->pData = (void *)&iconIndex;
- OSQPost(pStateQ, (void *)pStateMsg);
- break;
- case HOME_ICONMOVERIGHT:
- iconIndex = (*(INT8U *)pStateMsg->pData);
- if(iconIndex == 1)
- dspIndex = 1;
- else if(dspIndex < dspMaxIndex)
- dspIndex++;
- pStateMsg = &stateMsg[1];
- pStateMsg->cmd = HOME_ICONUPDATE;
- pStateMsg->pData = (void *)&iconIndex;
- OSQPost(pStateQ, (void *)pStateMsg);
- break;
- case HOME_ICONUPDATE:
- {
- INT8U i, j;
- LCD_PutMenuStr(0, 64, 0, &HomeIcon[iconIndex - 1]->str, 1);
- i = iconIndex - dspIndex;
- x = 0;
- for(j = 1; j < dspMaxIndex + 1; j++)
- {
- if(j == dspIndex)
- LCD_PutBmp(x, 2, HomeIcon[i]->iconID, 0);
- else
- LCD_PutBmp(x, 2, HomeIcon[i]->iconID, 1);
- i++;
- x += 32;
- }
- }
- break;
- case HOME_TIMEUPDATE:
- {
- char strBuf[6], *str = strBuf;
- *str = '0' + (h >> 4), str++;
- *str = '0' + (h &0x0f), str++;
- if(s % 2)
- *str = ':', str++;
- else
- *str = ' ', str++;
- *str = '0' + (m >> 4), str++;
- *str = '0' + (m & 0x0f), str++;
- *str = 0;
- LCD_PutMiddleString(88, 0, strBuf, 1);
- }
- break;
- case STATE_EXIT:
- ((INT8U *)Home.pdata)[0] = dspIndex;
- return;
- default:
- break;
- }
- case OS_ERR_TIMEOUT:
- {
- INT8U tmpSec = s;
- DS1337_Getc(RTC_HOUR, &h);
- DS1337_Getc(RTC_MIN, &m);
- DS1337_Getc(RTC_SEC, &s);
- if(s != tmpSec)
- {
- pStateMsg = &stateMsg[2];
- pStateMsg->cmd = HOME_TIMEUPDATE;
- OSQPost(pStateQ, (void *)pStateMsg);
- }
- }
- break;
- default:
- OSTimeDly(10);
- break;
- }
- }
- }
- static void __Home_EventProcess(void *ptr)
- {
- INT8U err, exitFlag = 1u;
- stMSG *pmsg;
- stStateMsg *pStateMsg = &stateMsg[0];
- INT8U iconIndex;
- stStateInfo *pthisState = (stStateInfo *)ptr;
- pTimeAction = __Home_TimeAction;
- pStateMsg->cmd = HOME_INIT;
- pStateMsg->pData = pthisState->pdata;
- iconIndex = ((INT8U *)pthisState->pdata)[1];
- OSQPost(pStateQ, (void *)pStateMsg);
- while(exitFlag)
- {
- pmsg = (stMSG *)OSMboxPend(pMsgEvent,
- CONFIG_MENU_TIME_OUT * 100u,
- &err);
- switch(err)
- {
- case OS_ERR_NONE:
- switch(pmsg->keyValue)
- {
- case KEY_VAL_LEFT:
- if(iconIndex)
- {
- pState = HomeIcon[iconIndex - 1]->pstate;
- pStateMsg->cmd = STATE_EXIT;
- if(pState == &Menu)
- {
- pMenuListEntry =
- (const stMenuList *)HomeIcon[iconIndex - 1]->data;
- ((INT8U *)pMenuListEntry->pdata)[0] = 1;
- ((INT8U *)pMenuListEntry->pdata)[1] = 1;
- }
- OSQPost(pStateQ, (void *)pStateMsg);
- ((INT8U *)Home.pdata)[1] = iconIndex;
- return;
- }
- else
- break;
- case KEY_VAL_RIGHT:
- pState = &NormalClock;
- pStateMsg->cmd = STATE_EXIT;
- OSQPost(pStateQ, (void *)pStateMsg);
- return;
- case KEY_VAL_UP | KEY_VAL_LONG:
- case KEY_VAL_UP:
- if(iconIndex <= 1)
- iconIndex = HomeIconTotalNum;
- else
- iconIndex --;
- pStateMsg->cmd = HOME_ICONMOVELEFT;
- pStateMsg->pData = (void *)&iconIndex;
- OSQPost(pStateQ, (void *)pStateMsg);
- break;
- case KEY_VAL_DOWN | KEY_VAL_LONG:
- case KEY_VAL_DOWN:
- iconIndex++;
- if(iconIndex > HomeIconTotalNum)
- iconIndex = 1;
- pStateMsg->cmd = HOME_ICONMOVERIGHT;
- pStateMsg->pData = (void *)&iconIndex;
- OSQPost(pStateQ, (void *)pStateMsg);
- break;
- default:
- __defaultEventProcess(&exitFlag, pmsg, ptr);
- break;
- }
- break;
- case OS_ERR_TIMEOUT:
- pState = &NormalClock;
- pStateMsg->cmd = STATE_EXIT;
- OSQPost(pStateQ, (void *)pStateMsg);
- return;
- default:
- OSTimeDly(10);
- break;
- }
- }
- }
- void State_Home(void *ptr)
- {
- __Home_EventProcess(ptr);
- }
- //Power off
- static void __PowerOff_TimeAction(void)
- {
- INT8U err, x, count, sec;
- stStateMsg *pStateMsg;
- while(1)
- {
- pStateMsg = (stStateMsg *)OSQPend(pStateQ, 50, &err);
- switch(err)
- {
- case OS_ERR_NONE:
- switch(pStateMsg->cmd)
- {
- case POWER_OFF_INIT:
- LCD_Clear(0);
- LED_ON;
- x = 32;
- LCD_PutZH16x16(x, 2, 53, 1), x += 16;
- LCD_PutZH16x16(x, 2, 54, 1), x += 16;
- LCD_PutZH16x16(x, 2, 55, 1), x += 16;
- LCD_PutSmallString(x, 3, "...", 1);
- sec = CONFIG_POWER_OFF_KEEP_TIME;
- count = sec << 1;
- pStateMsg = &stateMsg[1];
- pStateMsg->cmd = POWER_OFF_TIMESECUPDATE;
- OSQPost(pStateQ, (void *)pStateMsg);
- break;
- case POWER_OFF_TIMESECUPDATE:
- x = 56;
- LCD_PutMiddleChar(x, 4, '0' + sec / 10, 0);
- x += 8;
- LCD_PutMiddleChar(x, 4, '0' + sec % 10, 0);
- break;
- case STATE_EXIT:
- LED_OFF;
- return;
- default:
- break;
- }
- break;
- case OS_ERR_TIMEOUT:
- if(count == 1)
- {
- DIGITAL_POWER_OFF;
- while(IO2PIN_bit.P2_25)
- {
- OSTimeDly(10);
- }
- }
- else
- {
- count--;
- sec = count >> 1;
- LED_POWER? LED_OFF : LED_ON;
- pStateMsg = &stateMsg[2];
- pStateMsg->cmd = POWER_OFF_TIMESECUPDATE;
- OSQPost(pStateQ, (void *)pStateMsg);
- }
- break;
- default:
- OSTimeDly(10);
- break;
- }
- }
- }
- static void __PowerOff_EventProcess(void *ptr)
- {
- INT8U err;
- stMSG *pmsg;
- stStateMsg *pStateMsg = &stateMsg[0];
- stStateInfo *plastState = (stStateInfo *)ptr;
- plastState = (stStateInfo *)(*(INT32U *)plastState->pdata);
- while(1)
- {
- pmsg = (stMSG *)OSMboxPend(pMsgEvent, 51, &err);
- switch(err)
- {
- case OS_ERR_NONE:
- {
- switch(pmsg->keyValue)
- {
- case KEY_VAL_UP | KEY_VAL_LONG:
- break;
- default:
- goto POWEROFF_EXIT;
- break;
- }
- }
- break;
- case OS_ERR_TIMEOUT:
- {
- POWEROFF_EXIT: if(plastState)
- {
- pState = plastState;
- }
- else
- {
- pState = &Home;//&NormalClock;
- }
- pStateMsg->cmd = STATE_EXIT;
- OSQPost(pStateQ, (void *)pStateMsg);
- return;
- }
- break;
- default:
- OSTimeDly(10);
- break;
- }
- }
- }
- void State_PowerOff(void *ptr)
- {
- stStateMsg *pStateMsg = &stateMsg[0];
- pTimeAction = __PowerOff_TimeAction;
- pStateMsg->cmd = POWER_OFF_INIT;
- OSQPost(pStateQ, (void *)pStateMsg);
- __PowerOff_EventProcess(ptr);
- }
- //Menu
- static void __Menu_TimeAction(void)
- {
- INT8U err, y;
- INT8U h = 0;
- INT8U m = 0;
- INT8U s = 0;
- stStateMsg *pStateMsg;
- const stMenuList *pthisMenuList = NULL;
- const stMenuItem *pMenuItem;
- INT8U itemIndex = 1, dspIndex = 1;
- INT8U dspMaxIndex;
- while(1)
- {
- pStateMsg = (stStateMsg *)OSQPend(pStateQ, 50, &err);
- switch(err)
- {
- case OS_ERR_NONE:
- switch(pStateMsg->cmd)
- {
- case MENU_INIT:
- LCD_Clear(0);
- LCD_PutBmp(0, 6, BMP_BUTTON_ID_ENTER, 1);
- LCD_PutBmp(48, 6, BMP_BUTTON_ID_UP, 1);
- LCD_PutBmp(48, 7, BMP_BUTTON_ID_DOWN, 1);
- LCD_PutBmp(96, 6, BMP_BUTTON_ID_BACK, 1);
- DS1337_Getc(RTC_HOUR, &h);
- DS1337_Getc(RTC_MIN, &m);
- DS1337_Getc(RTC_SEC, &s);
- stateMsg[1].pData = pStateMsg->pData;
- pStateMsg = &stateMsg[1];
- pStateMsg->cmd = MENU_LISTUPDATE;
- OSQPost(pStateQ, (void *)pStateMsg);
- pStateMsg = &stateMsg[2];
- pStateMsg->cmd = MENU_ITEMUPDATE;
- OSQPost(pStateQ, (void *)pStateMsg);
- pStateMsg = &stateMsg[3];
- pStateMsg->cmd = MENU_TIMEUPDATE;
- OSQPost(pStateQ, (void *)pStateMsg);
- case MENU_TIMEUPDATE:
- {
- char strBuf[6], *str = strBuf;
- *str = '0' + (h >> 4), str++;
- *str = '0' + (h &0x0f), str++;
- if(s % 2)
- *str = ':', str++;
- else
- *str = ' ', str++;
- *str = '0' + (m >> 4), str++;
- *str = '0' + (m & 0x0f), str++;
- *str = 0;
- LCD_PutMiddleString(88, 0, strBuf, 1);
- }
- break;
- case MENU_ITEMMOVEUP:
- itemIndex = (*(INT8U *)pStateMsg->pData);
- if(itemIndex == pthisMenuList->itemNum)
- dspIndex = dspMaxIndex;
- else if(dspIndex > 1)
- dspIndex--;
- pStateMsg = &stateMsg[2];
- pStateMsg->cmd = MENU_ITEMUPDATE;
- pStateMsg->pData = (void *)&itemIndex;
- OSQPost(pStateQ, (void *)pStateMsg);
- break;
- case MENU_ITEMMOVEDOWN:
- itemIndex = (*(INT8U *)pStateMsg->pData);
- if(itemIndex == 1)
- dspIndex = 1;
- else if(dspIndex < dspMaxIndex)
- dspIndex++;
- pStateMsg = &stateMsg[2];
- pStateMsg->cmd = MENU_ITEMUPDATE;
- pStateMsg->pData = (void *)&itemIndex;
- OSQPost(pStateQ, (void *)pStateMsg);
- break;
- case MENU_ITEMUPDATE:
- {
- INT8U i, j;
- LCD_PutMenuStr(0, 64, 0, pthisMenuList->pname, 1);
- i = itemIndex - dspIndex;
- y = 2;
- for(j = 1; j < dspMaxIndex + 1; j++)
- {
- pMenuItem = pthisMenuList->pItem[i];
- if(j == dspIndex)
- LCD_PutMenuStr(0, 128, y, &pMenuItem->itemName, 0);
- else
- LCD_PutMenuStr(0, 128, y, &pMenuItem->itemName, 1);
- i++;
- y += 2;
- }
- }
- break;
- case MENU_LISTUPDATE:
- if(pthisMenuList)
- ((INT8U *)pthisMenuList->pdata)[0] = dspIndex;
- pthisMenuList=
- (const stMenuList *)(*(INT32U *)pStateMsg->pData);
- itemIndex = ((INT8U *)pthisMenuList->pdata)[1];
- dspMaxIndex =
- pthisMenuList->itemNum > 2 ? 2 : pthisMenuList->itemNum;
- dspIndex = ((INT8U *)pthisMenuList->pdata)[0];
- break;
- case STATE_EXIT:
- ((INT8U *)pthisMenuList->pdata)[0] = dspIndex;
- return;
- default:
- break;
- }
- case OS_ERR_TIMEOUT:
- {
- INT8U tmpSec = s;
- DS1337_Getc(RTC_HOUR, &h);
- DS1337_Getc(RTC_MIN, &m);
- DS1337_Getc(RTC_SEC, &s);
- if(s != tmpSec)
- {
- pStateMsg = &stateMsg[3];
- pStateMsg->cmd = MENU_TIMEUPDATE;
- OSQPost(pStateQ, (void *)pStateMsg);
- }
- }
- break;
- default:
- OSTimeDly(10);
- break;
- }
- }
- }
- static void __Menu_EventProcess(void *ptr)
- {
- INT8U err, exitFlag = 1u;
- INT8U itemIndex;
- stMSG *pmsg;
- stStateMsg *pStateMsg = &stateMsg[0];
- stStateInfo *pthisState = (stStateInfo *)ptr;
- const stMenuList *pthisMenuList;
- const stMenuItem *pMenuItem;
- pthisMenuList = (const stMenuList *)(*((INT32U *)pthisState->stateMask));
- itemIndex = ((INT8U *)pthisMenuList->pdata)[1];
- pTimeAction = __Menu_TimeAction;
- pStateMsg->cmd = MENU_INIT;
- pStateMsg->pData = (void *)&pthisMenuList;
- OSQPost(pStateQ, (void *)pStateMsg);
- while(exitFlag)
- {
- pmsg = (stMSG *)OSMboxPend(pMsgEvent,
- CONFIG_MENU_TIME_OUT * 100u,
- &err);
- switch(err)
- {
- case OS_ERR_NONE:
- {
- switch(pmsg->keyValue)
- {
- case KEY_VAL_LEFT:
- if(pthisMenuList->pItem[itemIndex - 1]->subItemNum)
- {
- ((INT8U *)pthisMenuList->pdata)[1] = itemIndex;
- pMenuItem = pthisMenuList->pItem[itemIndex - 1];
- pthisMenuList = (const stMenuList *)(pMenuItem->pData);
- itemIndex = 1;
- ((INT8U *)pthisMenuList->pdata)[0] = 1;
- ((INT8U *)pthisMenuList->pdata)[1] = 1;
- pStateMsg = &stateMsg[0];
- pStateMsg->cmd = MENU_LISTUPDATE;
- pStateMsg->pData = (void *)&pthisMenuList;
- OSQPost(pStateQ, (void *)pStateMsg);
- pStateMsg = &stateMsg[2];
- pStateMsg->cmd = MENU_ITEMUPDATE;
- OSQPost(pStateQ, (void *)pStateMsg);
- break;
- }
- else
- {
- pMenuItem = pthisMenuList->pItem[itemIndex - 1];
- pState = (const stStateInfo *)(pMenuItem->pData);
- pStateMsg = &stateMsg[0];
- pStateMsg->cmd = STATE_EXIT;
- OSQPost(pStateQ, (void *)pStateMsg);
- ((INT8U *)pthisMenuList->pdata)[1] = itemIndex;
- return;
- }
- case KEY_VAL_RIGHT:
- if(pthisMenuList->pparentItemList)
- {
- itemIndex = pthisMenuList->pparentItem->thisItemID + 1;
- pthisMenuList = pthisMenuList->pparentItemList;
- pStateMsg = &stateMsg[0];
- pStateMsg->cmd = MENU_LISTUPDATE;
- pStateMsg->pData = (void *)&pthisMenuList;
- OSQPost(pStateQ, (void *)pStateMsg);
- pStateMsg = &stateMsg[2];
- pStateMsg->cmd = MENU_ITEMUPDATE;
- OSQPost(pStateQ, (void *)pStateMsg);
- break;
- }
- else
- {
- pState = &Home;
- ((INT8U *)pthisMenuList->pdata)[0] = 1;
- ((INT8U *)pthisMenuList->pdata)[1] = 1;
- pStateMsg = &stateMsg[0];
- pStateMsg->cmd = STATE_EXIT;
- OSQPost(pStateQ, (void *)pStateMsg);
- return;
- }
- case KEY_VAL_UP | KEY_VAL_LONG:
- case KEY_VAL_UP:
- if(itemIndex <= 1)
- itemIndex = pthisMenuList->itemNum;
- else
- itemIndex--;
- pStateMsg = &stateMsg[0];
- pStateMsg->cmd = MENU_ITEMMOVEUP;
- pStateMsg->pData = (void *)&itemIndex;
- OSQPost(pStateQ, (void *)pStateMsg);
- break;
- case KEY_VAL_DOWN | KEY_VAL_LONG:
- case KEY_VAL_DOWN:
- itemIndex++;
- if(itemIndex > pthisMenuList->itemNum)
- itemIndex = 1;
- pStateMsg = &stateMsg[0];
- pStateMsg->cmd = MENU_ITEMMOVEDOWN;
- pStateMsg->pData = (void *)&itemIndex;
- OSQPost(pStateQ, (void *)pStateMsg);
- break;
- default:
- __defaultEventProcess(&exitFlag, pmsg, ptr);
- break;
- }
- }
- break;
- case OS_ERR_TIMEOUT:
- pState = &NormalClock;
- pStateMsg = &stateMsg[0];
- pStateMsg->cmd = STATE_EXIT;
- OSQPost(pStateQ, (void *)pStateMsg);
- return;
- default:
- OSTimeDly(10);
- break;
- }
- }
- }
- void State_Menu(void *ptr)
- {
- __Menu_EventProcess(ptr);
- }
- //Set date
- static void __SetDate_TimeAction(void)
- {
- INT8U err;
- stStateMsg *pStateMsg;
- while(1)
- {
- pStateMsg = (stStateMsg *)OSQPend(pStateQ, 0, &err);
- switch(err)
- {
- case OS_ERR_NONE:
- switch(pStateMsg->cmd)
- {
- case SETDATE_INIT:
- LCD_Clear(0);
- LCD_PutBmp(0, 6, BMP_BUTTON_ID_ENTER, 1);
- LCD_PutBmp(48, 6, BMP_BUTTON_ID_UP, 1);
- LCD_PutBmp(48, 7, BMP_BUTTON_ID_DOWN, 1);
- LCD_PutBmp(96, 6, BMP_BUTTON_ID_BACK, 1);
- break;
- case STATE_EXIT:
- return;
- default:
- break;
- }
- break;
- case OS_ERR_TIMEOUT:
- default:
- OSTimeDly(10);
- break;
- }
- }
- }
- static void __SetDate_EventProcess(void *ptr)
- {
- INT8U err, exitFlag = 1u;
- stMSG *pmsg;
- stStateMsg *pStateMsg = &stateMsg[0];
- while(exitFlag)
- {
- pmsg = (stMSG *)OSMboxPend(pMsgEvent,
- CONFIG_MENU_TIME_OUT * 100u,
- &err);
- switch(err)
- {
- case OS_ERR_NONE:
- {
- switch(pmsg->keyValue)
- {
- case KEY_VAL_LEFT:
- break;
- case KEY_VAL_RIGHT:
- *(INT32U *)pState->stateMask = (INT32U)pState->pdata;
- pState = &Menu;
- pStateMsg = &stateMsg[0];
- pStateMsg->cmd = STATE_EXIT;
- OSQPost(pStateQ, (void *)pStateMsg);
- return;
- case KEY_VAL_UP | KEY_VAL_LONG:
- case KEY_VAL_UP:
- break;
- case KEY_VAL_DOWN | KEY_VAL_LONG:
- case KEY_VAL_DOWN:
- break;
- default:
- __defaultEventProcess(&exitFlag, pmsg, ptr);
- break;
- }
- }
- break;
- case OS_ERR_TIMEOUT:
- pState = &NormalClock;
- pStateMsg = &stateMsg[0];
- pStateMsg->cmd = STATE_EXIT;
- OSQPost(pStateQ, (void *)pStateMsg);
- return;
- default:
- OSTimeDly(10);
- break;
- }
- }
- }
- void State_SetDate(void *ptr)
- {
- stStateMsg *pStateMsg = &stateMsg[0];
- pTimeAction = __SetDate_TimeAction;
- pStateMsg->cmd = SETDATE_INIT;
- OSQPost(pStateQ, (void *)pStateMsg);
- __SetDate_EventProcess(ptr);
- }
- //Common function
- static void __defaultEventProcess(INT8U *pexitFlag, stMSG *pmsg, void *ptr)
- {
- stStateMsg *pStateMsg = &stateMsg[0];
- switch(pmsg->keyValue)
- {
- case KEY_VAL_UP | KEY_VAL_LONG:
- *pexitFlag = 0;
- *(INT32U *)PowerOff.pdata = (INT32U)ptr;
- pState = &PowerOff;
- pStateMsg->cmd = STATE_EXIT;
- OSQPost(pStateQ, (void *)pStateMsg);
- break;
- }
- }
- void State_RunMachine(void)
- {
- pState->pfun((void *)pState);
- }
- void State_TimeAction(void)
- {
- pTimeAction();
- }
复制代码
- #ifndef STATE_H_
- #define STATE_H_
- #include "Interrupt.h"
- #include "ucos_ii.h"
- #include "Task.h"
- #include "Config.h"
- #include "Menu.h"
- struct ST_STATE_INFO;
- #define NORMAL_CLOCK_INIT 0u
- #define NORMAL_CLOCK_TIMEUPDATE 1u
- #define NORMAL_CLOCK_DATEUPDATE 2u
- #define NORMAL_CLOCK_PICUPDATE 3u
- #define HOME_INIT 0u
- #define HOME_ICONUPDATE 1u
- #define HOME_ICONMOVELEFT 2u
- #define HOME_ICONMOVERIGHT 3u
- #define HOME_TIMEUPDATE 4u
- #define POWER_OFF_INIT 0u
- #define POWER_OFF_TIMESECUPDATE 1u
- #define MENU_INIT 0u
- #define MENU_ITEMUPDATE 1u
- #define MENU_ITEMMOVEUP 2u
- #define MENU_ITEMMOVEDOWN 3u
- #define MENU_TIMEUPDATE 4u
- #define MENU_LISTUPDATE 5u
- #define SETDATE_INIT 0u
- #define STATE_EXIT 10u
- #define STATE_MSG_SIZE 5u
- typedef void (* tStateFun)(void *ptr);
- #define STATE_FUN tStateFun
- typedef struct {
- INT8U cmd;
- void *pData;
- }stStateMsg;
- typedef struct ST_STATE_INFO{
- INT32U stateMask;
- STATE_FUN pfun;
- void *pdata;
- }stStateInfo;
- extern OS_EVENT *pStateQ;
- extern void *StateMsg[STATE_MSG_SIZE];
- extern const stStateInfo *pState;
- extern const stStateInfo NormalClock;
- extern const stStateInfo Home;
- extern const stStateInfo PowerOff;
- extern const stStateInfo Menu;
- extern const stStateInfo SetDate;
- extern void State_RunMachine(void);
- extern void State_TimeAction(void);
- #endif /* STATE_H_ */
复制代码
1
已退回1积分
|
4个回答
|
|
|