Examples


The following two examples show how to use the timer device to read the current system time and make it wait a certain amount of time. See Portfolio Devices, for a complete description of the timer device.

Reading the System Time

Example 1 demonstrates how to use the timer device to read the current system time. The example does the following:

Portfolio provides convenience calls that make using the timer device easier, for example, CreateTimerIOReq() and DeleteTimerIOReq(). This example shows how to communicate with devices in the Portfolio environment. Note that the SampleSystemTime() and SampleSystemTimeTV() calls provide a more accurate reading of the system time than the calls used in this example.

Example 1: Reading the current time (timerread.c).

#include "types.h"
#include "string.h"
#include "io.h"
#include "device.h"
#include "item.h"
#include "time.h"
#include "stdio.h"
#include "operror.h"

int main(int32 argc, char **argv)
{
Item    deviceItem;
Item    ioreqItem;
IOReq  *ior;
IOInfo  ioInfo;
TimeVal tv;
Err     err;

    deviceItem = OpenNamedDevice("timer",0);
    if (deviceItem >= 0)
    {
        ioreqItem = CreateIOReq(0,0,deviceItem,0);
        if (ioreqItem >= 0)
        {
            ior = (IOReq *)LookupItem(ioreqItem);

            memset(&ioInfo,0,sizeof(ioInfo));
            ioInfo.ioi_Command         = CMD_READ;
            ioInfo.ioi_Unit            = TIMER_UNIT_USEC;
            ioInfo.ioi_Recv.iob_Buffer = &tv;
            ioInfo.ioi_Recv.iob_Len    = sizeof(tv);

            err = DoIO(ioreqItem,&ioInfo);
            if (err >= 0)
            {
                printf("Seconds %u, microseconds 

                      %u\n",tv.tv_Seconds,tv.tv_Microseconds);
            }
            else
            {
                printf("DoIO() failed: ");
                PrintfSysErr(err);
            }
            DeleteIOReq(ioreqItem);
        }
        else
        {
            printf("CreateIOReq() failed: ");
            PrintfSysErr(ioreqItem);
        }
        CloseNamedDevice(deviceItem);
    }
    else
    {
        printf("OpenNamedDevice() failed: ");
        PrintfSysErr(deviceItem);
    }

    return 0;
}

Using the Timer Device to Wait

Example 2 demonstrates how to use the timer device to wait a certain amount of time. The program does the following:

Note that Portfolio provides convenience calls to make using the timer device easier, for example, CreateTimerIOReq(), DeleteTimerIOReq(), and WaitTime(). This example shows how to communicate with devices in the Portfolio environment.

Example 2: Waiting for a specified time (timersleep.c).

#include "types.h"
#include "string.h"
#include "io.h"
#include "device.h"
#include "item.h"
#include "time.h"
#include "stdio.h"
#include "operror.h"


int main(int32 argc, char **argv)
{
Item    deviceItem;
Item    ioreqItem;
IOReq  *ior;
IOInfo  ioInfo;
TimeVal tv;
Err     err;

    if (argc == 3)
    {
        tv.tv_Seconds      = strtoul(argv[1],0,0);
        tv.tv_Microseconds = strtoul(argv[2],0,0);

        deviceItem = OpenNamedDevice("timer",0);
        if (deviceItem >= 0)
        {
            ioreqItem = CreateIOReq(0,0,deviceItem,0);
            if (ioreqItem >= 0)
            {
                ior = (IOReq *)LookupItem(ioreqItem);

                memset(&ioInfo,0,sizeof(ioInfo));
                ioInfo.ioi_Command         = TIMERCMD_DELAY;
                ioInfo.ioi_Unit            = TIMER_UNIT_USEC;
                ioInfo.ioi_Send.iob_Buffer = &tv;
                ioInfo.ioi_Send.iob_Len    = sizeof(tv);

                err = DoIO(ioreqItem,&ioInfo);
                if (err >= 0)
                {
                    printf("slept\n");
                }
                else
                {
                    printf("DoIO() failed: ");
                    PrintfSysErr(err);
                }
                DeleteIOReq(ioreqItem);
            }
            else
            {
                printf("CreateIOReq() failed: ");
                PrintfSysErr(ioreqItem);
            }
            CloseNamedDevice(deviceItem);
        }
        else
        {
            printf("OpenNamedDevice() failed: ");
            PrintfSysErr(deviceItem);
        }
    }
    else
    {
        printf("Usage: timersleep <num seconds> <num microseconds>\n");
    }

    return 0;
}