×
Namespaces

Variants
Actions

Using periodic timer in RGA

From Nokia Developer Wiki
Jump to: navigation, search

Archived.pngArchived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.

The article is believed to be still valid for the original topic scope.


Article Metadata
Tested with
Devices(s): Nokia N93, Nokia N95
Compatibility
Platform(s): S60 3rd Edition
Article
Keywords: ...
Created: stolik (30 Sep 2008)
Last edited: lpvalente (18 Sep 2012)

Contents

Overview

This snippet shows how to use IPeriodicTimer from the RGA plugin's ngi framework. It is shown inside the page class which inherits from examplefw CPage and ITimerObserver classes. It shows and updates the string with the current time on the screen. It is also possible to pause/unpause the timer.


Note: In order to use this code, you need to install the Open C plug-in.

Preconditions

This example relies on RGA API ngi and examplefw frameworks.

To run this snippet you need to use the ngi and examplefw frameworks provided in theOpen C/C++ Plug-ins for S60 3rd Edition. Examplefw sources are located in the \nokia_plugin\rga\s60rgaex\common\gameexfw\src\ folder after the plug-in has been installed.

MMP file

The following capabilities and libraries are required:

CAPABILITY SwEvent
STATICLIBRARY libcrt0.lib
LIBRARY euser.lib
LIBRARY runtime.lib
LIBRARY libc.lib
LIBRARY libm.lib
LIBRARY libpthread.lib

Note: The snippet itself does not require any special capabilities, but because the RGA API ngi framework is used, the resulting application requires SwEvent capability. See also Open C/C++ DLL.

Header file

// ...
#include "page.h" //examplefw
#include <timing.h> // ngi
 
using namespace ngi;
using examplefw::CPage;
 
class CExamplePage : public CPage, public ITimerObserver
{
//...
/**
* Method called by ITimerObserver.
* This method will be called when a timer exceeds.
* @param aTimerID ID of the timer
*/

virtual void HandleTimer( uint32 aTimerID ) NO_THROW;
//...
/// Pointer to the periodic timer.
IPeriodicTimer* mPeriodicTimer;
/// timer state
bool bPause;
/// Pointer to time string buffer
wchar_t* mBuffer;
};

Source file

#include <time.h>
#include <wchar.h> // libc (wcsrchr, wcslen, wmemcpy)
#include <runtime.h> // ngi
#include <standardtypes.h> // ngi
#include <standarddefines.h> // ngi
 
const char16 *const LABEL_BUTTON_PAUSE = L"Pause refreshing";
const char16 *const LABEL_BUTTON_UNPAUSE = L"Unpause refreshing";
const char16 *const LABEL_BUTTON_BACK = L"Back";
 
const uint32 TIMER_PERIOD = 100000;
 
CExamplePage::CExamplePage():
CPage(LABEL_BUTTON_PAUSE, LABEL_BUTTON_BACK, NULL, NULL),
mPeriodicTimer( NULL ),
bPause(true),
mBuffer(NULL)
{
}
// ------------------------------------------------------------------------
// Initialize()
// Creates an instance of IPeriodicTimer and sets the observer.
// ------------------------------------------------------------------------
//
bool32 CExamplePage::Initialize()
{
//...
 
if( !efCheck( CRuntime::CreateInstance( mPeriodicTimer ) ) )
{
return FALSE;
}
 
mPeriodicTimer->SetPeriod( TIMER_PERIOD );
mPeriodicTimer->SetObserver( this );
// ...
}
 
CExamplePage::~CExamplePage()
{
if( mPeriodicTimer )
{
mPeriodicTimer->Stop();
mPeriodicTimer->SetObserver( NULL );
mPeriodicTimer->Release();
}
if (mBuffer)
{
delete mBuffer;
mBuffer = NULL;
}
// ...
}
 
// ------------------------------------------------------------------------
// This function is called when the user switches to this page.
// ------------------------------------------------------------------------
void CHighscoresPage::OnShow( CPage* /*aPrevPage*/ )
{
mPeriodicTimer->Start();
bPause = false;
// ...
}
 
// ------------------------------------------------------------------------
// HandleTimer()
// This method will be called when an event occurs.
// ------------------------------------------------------------------------
//
void CExamplePage::HandleTimer( uint32 aTimerID ) NO_THROW
{
if( aTimerID == mPeriodicTimer->GetTimerID() )
{
Invalidate();
}
}
 
// ------------------------------------------------------------------------
// HandleInput()
// This method will be called when an input event occurs.
// ------------------------------------------------------------------------
//
CPage* CExamplePage::HandleInput( const InputType aInputType,
const IInputDevice& aDevice,
const uint64 aTimeStamp,
const uint32 aData1,
const int32 aData2 )
{
CPage* nextPage = this;
/* check for pressed keys */
if( aInputType == INPUT_TYPE_KEYPRESSED )
{
if(( aData1 & INPUT_KEY_OK ) || ( aData1 & INPUT_KEY_LSK ))
{
if (bPause == TRUE)
{
mPeriodicTimer->Start();
SetLeftButtonString(LABEL_BUTTON_PAUSE);
bPause = FALSE;
}
 
else
{
mPeriodicTimer->Stop();
SetLeftButtonString(LABEL_BUTTON_UNPAUSE);
bPause = TRUE;
}
}
else if( aData1 & INPUT_KEY_RSK )
{
mPeriodicTimer->Stop();
//exiting
nextPage = GetParentPage();
}
 
}
return nextPage;
 
}
 
// ------------------------------------------------------------------------
// Draw()
// This method draws this page onto the specified graphics context.
// ------------------------------------------------------------------------
//
void CExamplePage::Draw( IGraphicsContext& aGraphicsContext )
{
CPage::Draw( aGraphicsContext );
 
timespec seedTime;
clock_gettime(CLOCK_REALTIME, &seedTime);
 
if (!mBuffer)
mBuffer = new wchar_t[32];
 
tm* time = localtime(&seedTime.tv_sec);
 
swprintf(mBuffer, 32, L"%04d-%02d-%02d %02d:%02d.%02d",
time->tm_year+1900, time->tm_mon+1, time->tm_mday,
time->tm_hour, time->tm_min, time->tm_sec);
 
PrintString(buf1, CPoint(100, 100), aGraphicsContext, COLOR_WHITE,
FONT_STYLE_NORMAL, 0);
 
}

Postconditions

This example application will show the time string updated every TIMER_PERIOD. The example application pauses/unpauses when the middle (ok) or left softkey is pressed (the left softkey label is also changed according to the timer state). The example application returns to the previous page when the right softkey pressed.

This page was last modified on 18 September 2012, at 21:07.
59 page views in the last 30 days.
×