Hello.c
上传用户:liaojie
上传日期:2021-10-23
资源大小:21k
文件大小:13k
- /*===========================================================================
- FILE: Hello.c
- ===========================================================================*/
- /*===============================================================================
- INCLUDES AND VARIABLE DEFINITIONS
- =============================================================================== */
- #include "AEEModGen.h" // Module interface definitions
- #include "AEEAppGen.h" // Applet interface definitions
- #include "AEEShell.h" // Shell interface definitions
- #include "AEEstdlib.h"
- #include "Hello.bid"
- #include "res.brh"
- #include "hello.h"
- #define TIMER 1000 //1秒=1000毫秒,按1秒计时
- //12个月,每个月的天数
- static uint8 g_arrMonthDay[12]
- = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
- /*-------------------------------------------------------------------
- Function Prototypes
- -------------------------------------------------------------------*/
- static boolean CKeyApp_HandleEvent(
- IApplet* pMe, AEEEvent eCode, uint16 wParam, uint32 dwParam);
- boolean CKeyApp_InitAppData(CKeyApp* pMe);
- static void CKeyApp_DrawWeekText(CKeyApp * pMe); //画日 一 二 。。。
- static void CKeyApp_DrawDay(CKeyApp *pMe); //画日期的布局
- static void CKeyApp_DrawRect(CKeyApp *pMe); //画选中日期的框位置
- static void CKeyApp_ViewYearMonth(CKeyApp *pMe); //在左上角显示年-月
- static void CKeyApp_ViewHourMinSec(CKeyApp *pMe);//在右上角显示时:分:秒
- static void CKeyApp_CalculateDay(CKeyApp * pMe); //计算年月日是星期几
- static void CKeyApp_Draw(CKeyApp *pMe); //绘制界面
- static void CKeyApp_UpEvent(CKeyApp * pMe); //向上移动
- static void CKeyApp_DownEvent(CKeyApp * pMe); //向下移动
- static void CKeyApp_LeftEvent(CKeyApp * pMe); //向左移动
- static void CKeyApp_RightEvent(CKeyApp * pMe);//向右移动
- /*===========================================================================
- FUNCTION: AEEClsCreateInstance
- ===========================================================================*/
- int AEEClsCreateInstance(AEECLSID ClsId, IShell *pIShell, IModule *po, void **ppObj)
- {
- *ppObj = NULL;
-
- if (ClsId == AEECLSID_HELLO)
- {
- // Create the applet and make room for the applet structure
- if ( AEEApplet_New(sizeof(CKeyApp),
- ClsId, pIShell, po,
- (IApplet**)ppObj,
- (AEEHANDLER)CKeyApp_HandleEvent,
- NULL) == TRUE )
- {
- if (CKeyApp_InitAppData((CKeyApp*)*ppObj))
- {
- //Data initialized successfully
- return(AEE_SUCCESS);
- }
- else
- {
- // AEEApplet_New was called.
- IAPPLET_Release((IApplet*)*ppObj);
- return EFAILED;
- }
- } // end AEEApplet_New
- }
-
- return(EFAILED);
- }
- boolean CKeyApp_InitAppData(CKeyApp* pMe)
- {
- JulianType mJulianDate;
- // Get the device information for this handset.
- // Reference all the data by looking at the pMe->DeviceInfo structure
- // Check the API reference guide for all the handy device info you can get
- pMe->m_DeviceInfo.wStructSize = sizeof(pMe->m_DeviceInfo);
- ISHELL_GetDeviceInfo(pMe->a.m_pIShell, &pMe->m_DeviceInfo);
- GETJULIANDATE(0, &mJulianDate);
- pMe->m_nYear = mJulianDate.wYear;
- pMe->m_nMonth = mJulianDate.wMonth;
- pMe->m_nDay = mJulianDate.wDay;
- pMe->m_nToday = mJulianDate.wDay;
- pMe->m_nWeek = 0; //一号是星期几
- pMe->m_nToday = 0 ; //今天是几号
- pMe->m_nFirstDay = 0; //一号是今年的第几天
- IDisplay_ClearScreen(pMe->a.m_pIDisplay) ;
- // Insert your code here for initializing or allocating resources...
- // if there have been no failures up to this point then return success
- return TRUE;
- }
- /*===========================================================================
- FUNCTION SampleAppWizard_HandleEvent
- ===========================================================================*/
- static boolean CKeyApp_HandleEvent(IApplet* pMe, AEEEvent eCode, uint16 wParam, uint32 dwParam)
- {
- // AEEDeviceInfo deviceInfo;
- CKeyApp *pApp = (CKeyApp *)pMe;
-
- switch (eCode)
- {
- // App is told it is starting up
- case EVT_APP_START:
- CKeyApp_DrawWeekText (pApp); //画日 一 二 。。。
- CKeyApp_Draw (pApp);
- ISHELL_SetTimer (pApp->a.m_pIShell, TIMER,
- (PFNNOTIFY) CKeyApp_ViewHourMinSec, (void *)pApp );
- return(TRUE);
-
- case EVT_APP_STOP:
- ISHELL_CancelTimer (pApp->a.m_pIShell,
- (PFNNOTIFY) CKeyApp_ViewHourMinSec, pApp );
- return(TRUE);
-
- // App is told it is KEY
- case EVT_KEY:
- switch(wParam)
- {
- case AVK_UP:
- CKeyApp_UpEvent (pApp);
- break;
-
- case AVK_DOWN:
- CKeyApp_DownEvent (pApp);
- break;
-
- case AVK_LEFT:
- CKeyApp_LeftEvent (pApp);
- break;
-
- case AVK_RIGHT:
- CKeyApp_RightEvent (pApp);
- break;
-
- default:
- return FALSE;
- }
- CKeyApp_Draw (pApp);
- return TRUE;
-
- default:
- break;
- }
-
- return FALSE;
- }
- // 重刷整个屏幕
- static void CKeyApp_Draw(CKeyApp *pMe)
- {
- pMe->m_nFirstDay = 0;
- CKeyApp_ViewYearMonth (pMe); //在左上角显示年-月
- CKeyApp_CalculateDay (pMe); //计算年月日是星期几
- CKeyApp_DrawDay (pMe); //画日期的布局
- CKeyApp_DrawRect (pMe); //画选中日期的框位置
- }
- static void CKeyApp_DrawWeekText(CKeyApp * pMe)
- {
- // print header
- RGBVAL clr ;
- uint8 i;
- AECHAR sTextBuf[100]={0};
-
- clr = IDisplay_SetColor (
- pMe->a.m_pIDisplay, CLR_USER_TEXT, MAKE_RGB(0, 0, 255));
-
- for (i=0; i<7; i++)
- {
- MEMSET (sTextBuf, 0, sizeof(sTextBuf));
- ISHELL_LoadResString (pMe->a.m_pIShell,
- RES_RES_FILE,
- (int16)(IDS_WEEK_TITLE+i),
- sTextBuf,
- ARRAY_SIZE(sTextBuf));
- IDISPLAY_DrawText (pMe->a.m_pIDisplay, // Display instance
- AEE_FONT_BOLD, // Use BOLD font
- sTextBuf, // Text - Normally comes from resource
- -1, // -1 = Use full string length
- (pMe->m_DeviceInfo.cxScreen/7)*i+2, // Ignored - IDF_ALIGN_CENTER
- pMe->m_DeviceInfo.cyScreen/8, // Ignored - IDF_ALIGN_MIDDLE
- NULL, // No clipping
- NULL);
-
- }
-
- IDisplay_SetColor (pMe->a.m_pIDisplay, CLR_USER_TEXT, clr);
- IDISPLAY_Update (pMe->a.m_pIDisplay);
- }
- //确定这一个月的一号所显示的位置,根据这个月的天数来往后一次显示日期。
- static void CKeyApp_DrawDay(CKeyApp *pMe)
- {
- uint16 i;
- AECHAR* psTextBuf;
- AEERect day = {
- 0, pMe->m_DeviceInfo.cyScreen/4,
- pMe->m_DeviceInfo.cxScreen,
- pMe->m_DeviceInfo.cyScreen-pMe->m_DeviceInfo.cyScreen/4
- };
- IDISPLAY_EraseRect(pMe->a.m_pIDisplay,&day);
- psTextBuf = (AECHAR*) MALLOC( 100 * sizeof( AECHAR ) );
- for (i=1; i<=g_arrMonthDay[pMe->m_nMonth-1]; i++)
- {
- //初始化内存为0
- MEMSET(psTextBuf, 0,100 * sizeof( AECHAR ) );
- WSPRINTF(psTextBuf, 100 * sizeof( AECHAR ), L"%d", i);
-
- IDISPLAY_DrawText(pMe->a.m_pIDisplay, // Display instance
- AEE_FONT_BOLD, // Use BOLD font
- psTextBuf, // Text - Normally comes from resource
- -1, // -1 = Use full string length
- (((i-1+pMe->m_nWeek)%7))*(pMe->m_DeviceInfo.cxScreen/7)+2, // Ignored - IDF_ALIGN_CENTER
- (((i-1+pMe->m_nWeek)/7)+2)*(pMe->m_DeviceInfo.cyScreen/8), // Ignored - IDF_ALIGN_MIDDLE
- NULL, // No clipping
- NULL);
- }
-
- IDISPLAY_Update(pMe->a.m_pIDisplay);
- }
- static void CKeyApp_DrawRect(CKeyApp *pMe)
- {
- CKeyApp *pApp = (CKeyApp *)pMe;
- AEERect rc = {
- ((pMe->m_nDay - 1 + pMe->m_nWeek)%7) * (pMe->m_DeviceInfo.cxScreen/7),
- ((pMe->m_nDay - 1 + pMe->m_nWeek)/7 + 2) * (pMe->m_DeviceInfo.cyScreen/8),
- 15,
- 15
- };
-
- IDISPLAY_DrawRect (pApp->a.m_pIDisplay, &rc, 0xff000000, 0x00ff0000, IDF_RECT_FRAME);
- IDISPLAY_Update (pApp->a.m_pIDisplay);
- }
- //显示时间年月在手机左上角
- static void CKeyApp_ViewYearMonth(CKeyApp *pMe)
- {
- AECHAR sTextBuf[20] = {0};
- AEERect ym = { 0, 0, 50, pMe->m_DeviceInfo.cyScreen/8 };
- WSPRINTF(sTextBuf, sizeof(sTextBuf), L"%d-%d",pMe->m_nYear,pMe->m_nMonth);
- IDISPLAY_EraseRect(pMe->a.m_pIDisplay,&ym);
- IDISPLAY_DrawText (pMe->a.m_pIDisplay, // Display instance
- AEE_FONT_BOLD, // Use BOLD font
- sTextBuf, // Text - Normally comes from resource
- -1, // -1 = Use full string length
- 0, // Ignored - IDF_ALIGN_CENTER
- 0, // Ignored - IDF_ALIGN_MIDDLE
- NULL, // No clipping
- NULL);
-
- IDISPLAY_Update(pMe->a.m_pIDisplay);
- }
- static void CKeyApp_ViewHourMinSec(CKeyApp *pMe)
- {
- CKeyApp *pApp = (CKeyApp *)pMe;
- JulianType mJulianDate;
- AECHAR sTextBuf[20] = {0};
- AEERect hms = {
- pMe->m_DeviceInfo.cxScreen-50, 0,
- 50, pMe->m_DeviceInfo.cyScreen/8
- };
- GETJULIANDATE(0, &mJulianDate);
- IDISPLAY_EraseRect(pMe->a.m_pIDisplay, &hms);
-
- if (pMe->m_nToday != mJulianDate.wDay)
- {
- pMe->m_nYear = mJulianDate.wYear;
- pMe->m_nMonth = mJulianDate.wMonth;
- pMe->m_nDay = mJulianDate.wDay;
- pMe->m_nToday = mJulianDate.wDay;
- }
- WSPRINTF (sTextBuf, sizeof(sTextBuf),
- L"%02d:%02d:%02d",mJulianDate.wHour,
- mJulianDate.wMinute,mJulianDate.wSecond);
-
- IDISPLAY_DrawText (pMe->a.m_pIDisplay, // Display instance
- AEE_FONT_BOLD, // Use BOLD font
- sTextBuf, // Text - Normally comes from resource
- -1, // -1 = Use full string length
- pMe->m_DeviceInfo.cxScreen-50, // Ignored - IDF_ALIGN_CENTER
- 0, // Ignored - IDF_ALIGN_MIDDLE
- NULL, // No clipping
- NULL);
- IDISPLAY_Update(pMe->a.m_pIDisplay);
- ISHELL_SetTimer( pApp->a.m_pIShell, TIMER,
- (PFNNOTIFY) CKeyApp_ViewHourMinSec, (void *)pApp );
- }
- //计算年月日是星期几
- static void CKeyApp_CalculateDay(CKeyApp * pMe)
- {
- //1,判断这一年是否为闰年,是设flg=1,否flg=0
- uint16 i;
- if ((pMe->m_nYear%400 == 0)
- || ((pMe->m_nYear%4==0) && (pMe->m_nYear%100!=0)))
- {
- g_arrMonthDay[1] = 29;//闰年,二月有29天
- }
- else
- {
- g_arrMonthDay[1] = 28;
- }
-
- //2,计算显示月份的一号为这一年的第几天pMe->m_nDay
- for ( i=0; i<pMe->m_nMonth-1; i++)
- {
- pMe->m_nFirstDay += g_arrMonthDay[i];
- }
- pMe->m_nFirstDay = pMe->m_nFirstDay+1;//一号为这一年的第几天pMe->m_nDay
-
- /*3 计算1号是周几
- pMe->m_nYear是年份数,pMe->m_nFirstDay是这一天在这一年中的累积天数,
- 也就是这一天在这一年是第几天。算出来的pMe->m_nDurday除以7,
- 余数是几就是星期几(pMe->m_nWeek),如果余数是0,则为星期日*/
- i = (pMe->m_nYear-1) + (pMe->m_nYear-1)/4 - (pMe->m_nYear-1)/100
- + (pMe->m_nYear-1)/400 + pMe->m_nFirstDay;
- pMe->m_nWeek = i%7;
- }
- // 向上按键时计算年月日,移至上一周
- static void CKeyApp_UpEvent(CKeyApp * pMe)
- {
- pMe->m_nDay -= 7;
- // 换月份
- if (pMe->m_nDay <= 0)
- {
- pMe->m_nMonth--;
-
- if (pMe->m_nMonth <= 0)
- {
- pMe->m_nYear--;
- pMe->m_nMonth = 12;
- }
- pMe->m_nDay = g_arrMonthDay[pMe->m_nMonth-1] + pMe->m_nDay;
- }
- }
- // 向下按键时计算年月日,移至下一周
- static void CKeyApp_DownEvent(CKeyApp * pMe)
- {
- uint16 NowMonth=pMe->m_nMonth;
- pMe->m_nDay += 7;
- // 换月份
- if (pMe->m_nDay > g_arrMonthDay[pMe->m_nMonth-1])
- {
- pMe->m_nMonth ++;
- if (pMe->m_nMonth > 12)
- {
- pMe->m_nYear++;
- pMe->m_nMonth = 1;
- }
- pMe->m_nDay = pMe->m_nDay%g_arrMonthDay[NowMonth-1];
- }
- }
- // 向左按键时计算年月日,移至上一日
- static void CKeyApp_LeftEvent(CKeyApp * pMe)
- {
- pMe->m_nDay --;
- // 换月份
- if (pMe->m_nDay <= 0)
- {
- pMe->m_nMonth--;
- if (pMe->m_nMonth <= 0)
- {
- pMe->m_nYear--;
- pMe->m_nMonth = 12;
- }
- pMe->m_nDay = g_arrMonthDay[pMe->m_nMonth-1];
- }
- }
- //向右按键时计算年月日,移至下一日
- static void CKeyApp_RightEvent(CKeyApp * pMe)
- {
- pMe->m_nDay ++;
- // 换月份
- if (pMe->m_nDay > g_arrMonthDay[pMe->m_nMonth-1])
- {
- pMe->m_nMonth ++;
- if (pMe->m_nMonth > 12)
- {
- pMe->m_nYear ++;
- pMe->m_nMonth = 1;
- }
- pMe->m_nDay = 1;
- }
- }