runtime/plc_Win32_main.c
author greg
Mon, 07 Jul 2008 13:37:23 +0200
changeset 196 93d06827e31b
parent 110 a05e8b30c024
child 203 cb9901076a21
permissions -rw-r--r--
changes in plc_Win32_main to match with new api (alignment feature)
49
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
     1
#include <stdio.h>
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
     2
#include <sys/timeb.h>
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
     3
#include <time.h>
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
     4
#include <windows.h>
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
     5
110
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 79
diff changeset
     6
int localcount = 0;
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 79
diff changeset
     7
196
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
     8
struct _timeb timetmp;
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
     9
void PLC_GetTime(IEC_TIME *CURRENT_TIME)
49
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    10
{
196
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    11
	_ftime(&timetmp);
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    12
	
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    13
	(*CURRENT_TIME).tv_sec = timetmp.time;
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    14
	(*CURRENT_TIME).tv_nsec = timetmp.millitm * 1000000;
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    15
}
49
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    16
196
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    17
void PLC_timer_notify()
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    18
{
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    19
    PLC_GetTime(&__CURRENT_TIME);
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    20
    __run();
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    21
}
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    22
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    23
HANDLE PLC_timer = NULL;
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    24
void PLC_SetTimer(long long next, long long period)
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    25
{
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    26
	LARGE_INTEGER liDueTime;		
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    27
	/* arg 2 of SetWaitableTimer take 100 ns interval*/
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    28
	liDueTime.QuadPart =  next / (-100);
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    29
	
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    30
	/*
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    31
	printf("SetTimer(%lld,%lld)\n",next, period);
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    32
	*/
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    33
	
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    34
	if (!SetWaitableTimer(PLC_timer, &liDueTime, common_ticktime__, NULL, NULL, 0))
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    35
    {
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    36
        printf("SetWaitableTimer failed (%d)\n", GetLastError());
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    37
    }
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    38
	if (WaitForSingleObject(PLC_timer, INFINITE) != WAIT_OBJECT_0)
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    39
	{
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    40
		printf("WaitForSingleObject failed (%d)\n", GetLastError());
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    41
	}
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    42
	PLC_timer_notify();
49
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    43
}
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    44
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    45
int main(int argc,char **argv)
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    46
{
196
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    47
	/* Translate PLC's microseconds to Ttick nanoseconds */
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    48
	Ttick = 1000000 * maxval(common_ticktime__,1);
49
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    49
196
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    50
	/* Create a waitable timer */
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    51
    PLC_timer = CreateWaitableTimer(NULL, FALSE, "WaitableTimer");
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    52
    if(NULL == PLC_timer)
49
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    53
    {
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    54
        printf("CreateWaitableTimer failed (%d)\n", GetLastError());
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    55
        return 1;
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    56
    }
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    57
196
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    58
    if( __init(argc,argv) == 0 )
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    59
    {
75
9ad18a387a96 Windows related enhancements
etisserant
parents: 57
diff changeset
    60
    	printf("Tick Time : %d ms\n", common_ticktime__);
196
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    61
    	while(1)
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    62
    	{
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    63
    		// Set a timer
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    64
    		PLC_SetTimer(Ttick,Ttick);
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    65
    		if (kbhit())
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    66
    		{
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    67
    			printf("Finishing\n");
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    68
    		    break;
57
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 49
diff changeset
    69
            }
196
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    70
    	}
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    71
    	PLC_SetTimer(0,0);
49
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    72
    }
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    73
    __cleanup();
196
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    74
    CloseHandle(PLC_timer);
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    75
    		
49
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    76
    return 0;
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    77
}