2.5. Creating a Timer interface

The following steps describe how to create a Timer object:

  1. Open the main.cpp file from the 04 - Using the Timer Interface project.

    Example 2.16 shows how to create a System and Timer interface:

    Example 2.16. Create a timer

    #include <mde/mde.h>
    #include <cstdio>
    
    using namespace MDE;
    
    /* Portable sleep function */
    #ifdef MDE_PLATFORM_ARM_LINUX 
            #include <unistd.h>
        void Sleep(unsigned int time)
        {
            usleep(time * 1000);
        }
    #endif
    
    #ifdef MDE_OS_PC_LINUX 
            #include <unistd.h>
        void Sleep(unsigned int time)
        {
            usleep(time * 1000);
        }
    #endif
    
    int main()
    {
        try
        {
            Managed<System> system = create_system_backend();
            Managed<Timer> timer = system->createTimer();
    ...
    

  2. Example 2.17 shows how to track changes in the timer.

    Example 2.17. Timer sleep calls

    ...
            printf("Reseting timer\n");
            timer->reset();
            printf("Time since object reset is: %f\n", timer->getTime());
    
            printf("\nSleeping 1000 milliseconds\n");
            Sleep(1000);
            printf("Time since object creation is: %f\n", timer->getTime());
            
            printf("\nReseting timer\n");
            timer->reset();
            printf("Time since timer reset is: %f\n", timer->getTime());
    
            printf("\nSleeping 1000 milliseconds\n");
            Sleep(1000);
            printf("Time since timer reset is: %f\n", timer->getTime());
    ...    
    

  3. Example 2.18 shows additional print statements that validate the timer behavior:

    Example 2.18. Printing timer values

            printf("\nTesting timer intevals:\n");
            printf("\nReseting timer\n");
            timer->reset();
            for(int i = 0; i < 4; i++)
            {
                printf("Interval: %f\n", timer->getInterval());
    
                printf("\nSleeping %d milliseconds\n", i*50*(i%2));
                Sleep(i*50*(i%2));
            }
    
            printf("Interval: %f\n", timer->getInterval());
    

  4. Example 2.19 shows the final catch block:

    Example 2.19. Exception catch block

    ...
    	catch(Exception& e)
    	{
    		printf("An unexpected exception was thrown:\n%s\n", e.getMessage().getCharString() );
    	}
    }
    

Copyright © 2010 ARM. All rights reserved.ARM DUI 0527A‑02a
Non-Confidential - Draft - BetaID070710