changes in plc_Win32_main to match with new api (alignment feature)
authorgreg
Mon, 07 Jul 2008 13:37:23 +0200
changeset 196 93d06827e31b
parent 195 26a39a90a332
child 197 a50b5fa04c57
changes in plc_Win32_main to match with new api (alignment feature)
runtime/plc_Win32_main.c
--- a/runtime/plc_Win32_main.c	Mon Jul 07 11:34:12 2008 +0200
+++ b/runtime/plc_Win32_main.c	Mon Jul 07 13:37:23 2008 +0200
@@ -5,61 +5,73 @@
 
 int localcount = 0;
 
-void timer_notify()
+struct _timeb timetmp;
+void PLC_GetTime(IEC_TIME *CURRENT_TIME)
 {
-   struct _timeb timebuffer;
-   if(++localcount % 50 == 0){
-	   printf("PLC tick : %d\n",localcount);
-	   fflush(stdout);
-	   }
+	_ftime(&timetmp);
+	
+	(*CURRENT_TIME).tv_sec = timetmp.time;
+	(*CURRENT_TIME).tv_nsec = timetmp.millitm * 1000000;
+}
 
-   _ftime( &timebuffer );
-   __CURRENT_TIME.tv_sec = timebuffer.time;
-   __CURRENT_TIME.tv_nsec = timebuffer.millitm * 1000000;
-   __run();
+void PLC_timer_notify()
+{
+    PLC_GetTime(&__CURRENT_TIME);
+    __run();
+}
+
+HANDLE PLC_timer = NULL;
+void PLC_SetTimer(long long next, long long period)
+{
+	LARGE_INTEGER liDueTime;		
+	/* arg 2 of SetWaitableTimer take 100 ns interval*/
+	liDueTime.QuadPart =  next / (-100);
+	
+	/*
+	printf("SetTimer(%lld,%lld)\n",next, period);
+	*/
+	
+	if (!SetWaitableTimer(PLC_timer, &liDueTime, common_ticktime__, NULL, NULL, 0))
+    {
+        printf("SetWaitableTimer failed (%d)\n", GetLastError());
+    }
+	if (WaitForSingleObject(PLC_timer, INFINITE) != WAIT_OBJECT_0)
+	{
+		printf("WaitForSingleObject failed (%d)\n", GetLastError());
+	}
+	PLC_timer_notify();
 }
 
 int main(int argc,char **argv)
 {
-    HANDLE hTimer = NULL;
-    LARGE_INTEGER liDueTime;
+	/* Translate PLC's microseconds to Ttick nanoseconds */
+	Ttick = 1000000 * maxval(common_ticktime__,1);
 
-    liDueTime.QuadPart = -10000 * maxval(common_ticktime__,1);
-
-    // Create a waitable timer.
-    hTimer = CreateWaitableTimer(NULL, FALSE, "WaitableTimer");
-    if (NULL == hTimer)
+	/* Create a waitable timer */
+    PLC_timer = CreateWaitableTimer(NULL, FALSE, "WaitableTimer");
+    if(NULL == PLC_timer)
     {
         printf("CreateWaitableTimer failed (%d)\n", GetLastError());
         return 1;
     }
 
-    if( __init(argc,argv) == 0 ){
-
+    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;
+    	while(1)
+    	{
+    		// Set a timer
+    		PLC_SetTimer(Ttick,Ttick);
+    		if (kbhit())
+    		{
+    			printf("Finishing\n");
+    		    break;
             }
-            if (kbhit())
-            {
-                printf("Finishing\n");
-                break;
-            }
-            timer_notify();
-        }
+    	}
+    	PLC_SetTimer(0,0);
     }
     __cleanup();
-
+    CloseHandle(PLC_timer);
+    		
     return 0;
 }