targets/Win32/plc_Win32_main.c
changeset 205 ee8d1f4528ef
child 229 8bc65076e290
equal deleted inserted replaced
204:f572ab819769 205:ee8d1f4528ef
       
     1 #include <stdio.h>
       
     2 #include <sys/timeb.h>
       
     3 #include <time.h>
       
     4 #include <windows.h>
       
     5 
       
     6 long AtomicCompareExchange(long* atomicvar,long exchange, long compared)
       
     7 {
       
     8     return InterlockedCompareExchange(atomicvar, exchange, compared);
       
     9 }
       
    10 
       
    11 //long AtomicExchange(long* atomicvar,long exchange)
       
    12 //{
       
    13 //    return InterlockedExchange(atomicvar, exchange);    
       
    14 //}
       
    15 
       
    16 struct _timeb timetmp;
       
    17 void PLC_GetTime(IEC_TIME *CURRENT_TIME)
       
    18 {
       
    19 	_ftime(&timetmp);
       
    20 	
       
    21 	(*CURRENT_TIME).tv_sec = timetmp.time;
       
    22 	(*CURRENT_TIME).tv_nsec = timetmp.millitm * 1000000;
       
    23 }
       
    24 
       
    25 void PLC_timer_notify()
       
    26 {
       
    27     PLC_GetTime(&__CURRENT_TIME);
       
    28     __run();
       
    29 }
       
    30 
       
    31 HANDLE PLC_timer = NULL;
       
    32 void PLC_SetTimer(long long next, long long period)
       
    33 {
       
    34 	LARGE_INTEGER liDueTime;		
       
    35 	/* arg 2 of SetWaitableTimer take 100 ns interval*/
       
    36 	liDueTime.QuadPart =  next / (-100);
       
    37 	
       
    38 	/*
       
    39 	printf("SetTimer(%lld,%lld)\n",next, period);
       
    40 	*/
       
    41 	
       
    42 	if (!SetWaitableTimer(PLC_timer, &liDueTime, common_ticktime__, NULL, NULL, 0))
       
    43     {
       
    44         printf("SetWaitableTimer failed (%d)\n", GetLastError());
       
    45     }
       
    46 	if (WaitForSingleObject(PLC_timer, INFINITE) != WAIT_OBJECT_0)
       
    47 	{
       
    48 		printf("WaitForSingleObject failed (%d)\n", GetLastError());
       
    49 	}
       
    50 	PLC_timer_notify();
       
    51 }
       
    52 
       
    53 int main(int argc,char **argv)
       
    54 {
       
    55 	/* Translate PLC's microseconds to Ttick nanoseconds */
       
    56 	Ttick = 1000000 * maxval(common_ticktime__,1);
       
    57 
       
    58 	/* Create a waitable timer */
       
    59     PLC_timer = CreateWaitableTimer(NULL, FALSE, "WaitableTimer");
       
    60     if(NULL == PLC_timer)
       
    61     {
       
    62         printf("CreateWaitableTimer failed (%d)\n", GetLastError());
       
    63         return 1;
       
    64     }
       
    65 
       
    66     if( __init(argc,argv) == 0 )
       
    67     {
       
    68     	printf("Tick Time : %d ms\n", common_ticktime__);
       
    69     	while(1)
       
    70     	{
       
    71     		// Set a timer
       
    72     		PLC_SetTimer(Ttick,Ttick);
       
    73     		if (kbhit())
       
    74     		{
       
    75     			printf("Finishing\n");
       
    76     		    break;
       
    77             }
       
    78     	}
       
    79     	PLC_SetTimer(0,0);
       
    80     }
       
    81     __cleanup();
       
    82     CloseHandle(PLC_timer);
       
    83     		
       
    84     return 0;
       
    85 }