📄 stopwatch.c
字号:
/*===========================================================================
FILE: stopwatch.c
===========================================================================*/
/*===============================================================================
INCLUDES AND VARIABLE DEFINITIONS
=============================================================================== */
#include "AEEModGen.h" // Module interface definitions
#include "AEEAppGen.h" // Applet interface definitions
#include "AEEShell.h" // Shell interface definitions
#include "stopwatch.bid"
#include "stopwatch.brh"
//using by app
#include "AEEStdLib.h" // AEE Stb Lib Services
#include "AEEMenu.h" // AEE Menu Services
#include "AEEFile.h" // AEE File Manager Services
#include "AEEImageCtl.h" // AEE ImageCtl Services
#include "AEEMimeTypes.h" // AEE MIME definitions
#include "AEETime.h" //AEE TIME definitions
/*-------------------------------------------------------------------
Applet structure. All variables in here are reference via "pMe->"
-------------------------------------------------------------------*/
typedef struct _stopwatch {
AEEApplet a ; // First element of this structure must be AEEApplet
AEEDeviceInfo DeviceInfo; // always have access to the hardware device information
// add your own variables here...
IDisplay *pIDisplay;// give a standard way to access the Display interface
IShell *pIShell;// give a standard way to access the Shell interface
int sw_cxWidth; // Stores the device screen width
int sw_cyHeight; // Stores the device screen height
IImage * pIImage; // IImage interface pointer
ITimeCtl * sw_TimeCtl;
IMenuCtl * pIMenuCtl;
IMenuCtl* m_pSK; // Softkey Menu
uint32 destime;
uint32 curtime;
uint32 bistime;
int flag;
int flagStartStop;
} stopwatch;
/*-------------------------------------------------------------------
Function Prototypes
-------------------------------------------------------------------*/
static boolean stopwatch_HandleEvent(stopwatch* pMe, AEEEvent eCode,
uint16 wParam, uint32 dwParam);
boolean stopwatch_InitAppData(stopwatch* pMe);
void stopwatch_FreeAppData(stopwatch* pMe);
static void SW_DrawScreen(stopwatch * pMe);
static void SWtime_Start(stopwatch *pMe);
void SWtime_Update(pMe);
void SWtime_Stop(pMe);
void SWTime_Reset(pMe);
void SWTime_Lap(stopwatch*pMe);
/*===============================================================================
FUNCTION DEFINITIONS
=============================================================================== */
/*===========================================================================
FUNCTION: AEEClsCreateInstance
===========================================================================*/
int AEEClsCreateInstance(AEECLSID ClsId, IShell *pIShell, IModule *po, void **ppObj)
{
*ppObj = NULL;
if( ClsId == AEECLSID_STOPWATCH )
{
// Create the applet and make room for the applet structure
if( AEEApplet_New(sizeof(stopwatch),
ClsId,
pIShell,
po,
(IApplet**)ppObj,
(AEEHANDLER)stopwatch_HandleEvent,
(PFNFREEAPPDATA)stopwatch_FreeAppData) ) // the FreeAppData function is called after sending EVT_APP_STOP to the HandleEvent function
{
//Initialize applet data, this is called before sending EVT_APP_START
// to the HandleEvent function
if(stopwatch_InitAppData((stopwatch*)*ppObj))
{
//Data initialized successfully
return(AEE_SUCCESS);
}
else
{
//Release the applet. This will free the memory allocated for the applet when
// AEEApplet_New was called.
IAPPLET_Release((IApplet*)*ppObj);
return EFAILED;
}
} // end AEEApplet_New
}
return(EFAILED);
}
/*===========================================================================
FUNCTION SampleAppWizard_HandleEvent
===========================================================================*/
static boolean stopwatch_HandleEvent(stopwatch* pMe, AEEEvent eCode, uint16 wParam, uint32 dwParam)
{
AEERect rec;
// static int lapflag;
switch (eCode)
{
// App is told it is starting up
case EVT_APP_START:
// Add your code here..
// lapflag=1;
if(!pMe->a.m_pIShell)
return FALSE;
if(ISHELL_CreateInstance(pMe->a.m_pIShell, AEECLSID_STOPWATCHCTL, (void**) &(pMe->sw_TimeCtl)) != SUCCESS)
{
return FALSE;
}
ISHELL_CreateInstance(pMe->a.m_pIShell,AEECLSID_SOFTKEYCTL,(void**)&(pMe->m_pSK));
SWtime_Start(pMe);
SW_DrawScreen( pMe);
return(TRUE);
// App is told it is exiting
case EVT_APP_STOP:
// Add your code here...
return(TRUE);
// App is being suspended
case EVT_APP_SUSPEND:
// Add your code here...
return(TRUE);
// App is being resumed
case EVT_APP_RESUME:
// Add your code here...
return(TRUE);
// An SMS message has arrived for this app. Message is in the dwParam above as (char *)
// sender simply uses this format "//BREW:ClassId:Message", example //BREW:0x00000001:Hello World
case EVT_APP_MESSAGE:
// Add your code here...
return(TRUE);
case EVT_COMMAND:
switch(wParam)
{
case IDC_LAP:
if(pMe->flag==0)
{
SWTime_Lap(pMe);
}
break;
case IDC_START:
if(pMe->flagStartStop==1)
{
SWtime_Update(pMe);
}
else
{
// IMENUCTL_SetItemText(pMe->pIMenuCtl,IDC_START,STOPWATCH_RES_FILE,NULL,L"Stop");
SWtime_Stop(pMe);
SWTime_Reset(pMe);
}
pMe->flag=0;
pMe->flagStartStop=-pMe->flagStartStop;
break;
}
return(TRUE);
// A key was pressed. Look at the wParam above to see which key was pressed. The key
// codes are in AEEVCodes.h. Example "AVK_1" means that the "1" key was pressed.
case EVT_KEY:
// Add your code here...
SETAEERECT(&rec,0,75,128,90);
ITIMECTL_HandleEvent(pMe->sw_TimeCtl,eCode,wParam,dwParam);
IMENUCTL_HandleEvent(pMe->m_pSK,eCode,wParam,dwParam);
IDisplay_SetClipRect(pMe->pIDisplay,&rec);
//IMENUCTL_HandleEvent(pMe->m_pSK,eCode,wParam,dwParam);
/* switch(wParam)
{
case AVK_UP:
SWtime_Start(pMe);
SWtime_Update(pMe);
pMe->flag=0;
break;
case AVK_DOWN:
SWtime_Stop(pMe);
SWTime_Reset(pMe);
break;
//case AVK_SELECT:
case AVK_SOFT1:
if(pMe->flag==0)
{
SWTime_Lap(pMe);
}
break;
case AVK_CLR:
break;
default:
break;
} */
return(TRUE);
// If nothing fits up to this point then we'll just break out
default:
break;
}
return FALSE;
}
// this function is called when your application is starting up
boolean stopwatch_InitAppData(stopwatch* pMe)
{
// 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->DeviceInfo.wStructSize = sizeof(pMe->DeviceInfo);
ISHELL_GetDeviceInfo(pMe->a.m_pIShell,&pMe->DeviceInfo);
// Insert your code here for initializing or allocating resources...
pMe->pIDisplay=pMe->a.m_pIDisplay;
pMe->pIShell=pMe->a.m_pIShell;
pMe->sw_cxWidth = pMe->DeviceInfo.cxScreen; // Cache the width of the device screen
pMe->sw_cyHeight = pMe->DeviceInfo.cyScreen; // Cache the height of the device screen
// if there have been no failures up to this point then return success
return TRUE;
}
// this function is called when your application is exiting
void stopwatch_FreeAppData(stopwatch* pMe)
{
// insert your code here for freeing any resources you have allocated...
// example to use for releasing each interface:
// if ( pMe->pIMenuCtl != NULL ) // check for NULL first
// {
// IMENUCTL_Release(pMe->pIMenuCtl) // release the interface
// pMe->pIMenuCtl = NULL; // set to NULL so no problems trying to free later
// }
//
if(pMe->sw_TimeCtl)
{
ITIMECTL_Release(pMe->sw_TimeCtl);
}
if ( pMe->m_pSK != NULL ) // check for NULL first
{
IMENUCTL_Release(pMe->m_pSK); // release the interface
pMe->m_pSK = NULL; // set to NULL so no problems trying to free later
}
}
static void SW_DrawScreen(stopwatch * pMe)
{
AECHAR szText[30] = {0};
//Add softkey
//ISHELL_CreateInstance(pMe->a.m_pIShell,AEECLSID_SOFTKEYCTL,(void**)&(pMe->m_pSK));
IMENUCTL_AddItem(pMe->m_pSK, STOPWATCH_RES_FILE, IDC_START, IDC_START, NULL, 0);
IMENUCTL_AddItem(pMe->m_pSK, STOPWATCH_RES_FILE, IDC_LAP, IDC_LAP, NULL, 0);
IMENUCTL_Redraw(pMe->m_pSK);//keep softkey appear
IMENUCTL_SetActive(pMe->m_pSK, TRUE);
//load strings
ISHELL_LoadResString(pMe->pIShell,STOPWATCH_RES_FILE,IDS_TITLE,szText,sizeof( szText));
//draw strings
IDISPLAY_DrawText(pMe->pIDisplay, // Display instance
AEE_FONT_BOLD, // Use BOLD font
szText, // Text - Normally comes from resource
-1, // -1 = Use full string length
pMe->DeviceInfo.cxScreen/2,
pMe->DeviceInfo.cyScreen/2,
NULL, // No clipping
IDF_ALIGN_CENTER | IDF_ALIGN_TOP);
IDISPLAY_DrawHLine(pMe->a.m_pIDisplay, 0, 20, pMe->sw_cxWidth);
//icon=ISHELL_LoadResImage( pMe->pIShell,STOPWATCH_RES_FILE,IDI_ICON );
//IIMAGE_Draw(icon,30,30);
//IIMAGE_Release(icon);
}
static void SWtime_Start(stopwatch *pMe)
{
static int cntr = 0; //(for testing) specify time passed since start time in milliseconds.
//int STAT_INSET=100;
AEERect r;
pMe->flag=1;
pMe->flagStartStop=1;
ISHELL_GetDeviceInfo(pMe->a.m_pIShell, &pMe->DeviceInfo);
//r.x = r.y = STAT_INSET;
//r.dx = pMe->DeviceInfo.cxScreen - 2*STAT_INSET;
//r.dy = pMe->DeviceInfo.cyScreen - 2*STAT_INSET;
SETAEERECT(&r,20,100,90,20);
ITIMECTL_SetRect(pMe->sw_TimeCtl, &r);
//IMECTL_SetProperties(pMe->sw_TimeCtl, TP_NO_MSECONDS|TP_AUTOREDRAW);
//shows the starting time which I set to zero, cntr = 0
ITIMECTL_SetTimeEx(pMe->sw_TimeCtl, cntr, TRUE);
pMe->bistime = GETTIMEMS();
}
void SWtime_Update(stopwatch* pMe)
{
pMe->curtime = GETTIMEMS();
pMe->destime = pMe->curtime - pMe->bistime;
ITIMECTL_SetTime(pMe->sw_TimeCtl,pMe->destime);
ISHELL_SetTimer(pMe->pIShell,10,(PFNNOTIFY)SWtime_Update,pMe);
SW_DrawScreen( pMe);
};
void SWtime_Stop(stopwatch* pMe)
{
static int cntr = 0;
ISHELL_CancelTimer(pMe->pIShell, (PFNNOTIFY)SWtime_Update, pMe);
//Set to zero
ITIMECTL_SetTimeEx(pMe->sw_TimeCtl, cntr, FALSE);
//is same to ITIMECTL_SetTime(pMe->sw_TimeCtl,cntr);
SW_DrawScreen( pMe);
}
void SWTime_Reset(stopwatch*pMe)
{
pMe->bistime = GETTIMEMS();
}
void SWTime_Lap(stopwatch*pMe)
{
AECHAR pDest[20]={0};
AEERect rec;
//ISHELL_CancelTimer(pMe->pIShell, (PFNNOTIFY)SWtime_Update, pMe);
ITIMECTL_SetTime(pMe->sw_TimeCtl,pMe->destime);
ITIMECTL_GetTimeString(pMe->sw_TimeCtl, pMe->destime, pDest, sizeof(pDest),GTS_MSECS);
//set lap display area
SETAEERECT(&rec,20,50,90,20);
IDisplay_SetClipRect(pMe->pIDisplay,NULL);
SW_DrawScreen( pMe);
IDISPLAY_DrawText(pMe->pIDisplay,
AEE_FONT_BOLD,
pDest,
-1,
rec.x,
rec.y,
&rec,
IDF_ALIGN_CENTER | IDF_ALIGN_MIDDLE);
SETAEERECT(&rec,20,100,90,20);
IDisplay_SetClipRect(pMe->pIDisplay,&rec);
//IDisplay_SetClipRect(pMe->pIDisplay,NULL);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -