runtime/plc_Win32_main.c
author etisserant
Tue, 26 Feb 2008 15:04:12 +0100
changeset 112 fa0eaeaa9012
parent 110 a05e8b30c024
child 196 93d06827e31b
permissions -rw-r--r--
Re-enabled stderr
#include <stdio.h>
#include <sys/timeb.h>
#include <time.h>
#include <windows.h>

int localcount = 0;

void timer_notify()
{
   struct _timeb timebuffer;
   if(++localcount % 50 == 0){
	   printf("PLC tick : %d\n",localcount);
	   fflush(stdout);
	   }

   _ftime( &timebuffer );
   __CURRENT_TIME.tv_sec = timebuffer.time;
   __CURRENT_TIME.tv_nsec = timebuffer.millitm * 1000000;
   __run();
}

int main(int argc,char **argv)
{
    HANDLE hTimer = NULL;
    LARGE_INTEGER liDueTime;

    liDueTime.QuadPart = -10000 * maxval(common_ticktime__,1);

    // Create a waitable timer.
    hTimer = CreateWaitableTimer(NULL, FALSE, "WaitableTimer");
    if (NULL == hTimer)
    {
        printf("CreateWaitableTimer failed (%d)\n", GetLastError());
        return 1;
    }

    if( __init(argc,argv) == 0 ){

    	printf("Tick Time : %d ms\n", common_ticktime__);
        // Set a timer
        if (!SetWaitableTimer(hTimer, &liDueTime, common_ticktime__, NULL, NULL, 0))
        {
            printf("SetWaitableTimer failed (%d)\n", GetLastError());
            return 2;
        }
    
        while(1){
        // Wait for the timer.
            if (WaitForSingleObject(hTimer, INFINITE) != WAIT_OBJECT_0)
            {
                printf("WaitForSingleObject failed (%d)\n", GetLastError());
                break;
            }
            if (kbhit())
            {
                printf("Finishing\n");
                break;
            }
            timer_notify();
        }
    }
    __cleanup();

    return 0;
}