Join Now
Quality Rating:
  • Currently 0.0 / 5
(0.0 / 5 - 0 votes cast)
This page was last modified 09:08, 14 October 2008.

CS001140 - Using periodic timer in RGA

From Forum Nokia Wiki



ID CS001140 Creation date October 14, 2008
Platform S60 3rd Edition Tested on devices Nokia N93, Nokia N95
Category Open C/C++ Subcategory RGA


Keywords (APIs, classes, methods, functions): ...

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.

Related Discussions
Thread Thread Starter Forum Replies Last Post
Displaying file names one by one.. jobin.tech General Symbian C++ 13 2007-09-08 09:49
call timer mkiranmca General Discussion 0 2005-05-06 07:45
Unique identifier for timer object anilgeorge General Symbian C++ 2 2004-02-24 09:58
wlan API is not working properly. Digish Wired and Wireless interfaces 1 2007-12-18 11:24
Call RunL without Tel event Bill_Murray General Symbian C++ 6 2006-04-07 22:19
 
Powered by MediaWiki
RDF Facets: qfnZtypeQUqfnTypeZCommunityContentQ qfnZtypeQUqfnTypeZWebpageQ qfnZtypeQUqfnTypeZWikiContentQ qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX