tests/main.c
changeset 57 db9cadaab2ea
parent 43 37dd4e2fd2ec
child 150 398cfd6e8e4a
equal deleted inserted replaced
56:6e87bbc2abe9 57:db9cadaab2ea
       
     1 #ifdef __WIN32__
       
     2 #include <stdio.h>
       
     3 #include <sys/timeb.h>
       
     4 #include <time.h>
       
     5 #include <windows.h>
       
     6 #else
       
     7 #include <stdio.h>
       
     8 #include <string.h>
     1 #include <time.h>
     9 #include <time.h>
     2 #include <signal.h>
    10 #include <signal.h>
     3 
    11 #endif
     4 #include "iec_std_lib.h"
       
     5 
    12 
     6 /*
    13 /*
     7  * Functions and variables provied by generated C softPLC
    14  * Functions and variables provied by generated C softPLC
     8  **/ 
    15  **/ 
     9 void config_run__(int tick);
       
    10 void config_init__(void);
       
    11 extern int common_ticktime__;
    16 extern int common_ticktime__;
    12 
    17 
    13 /*
    18 /*
    14  *  Functions and variables to export to generated C softPLC
    19  * Functions and variables provied by plc.c
    15  **/
    20  **/ 
    16  
    21 void run(long int tv_sec, long int tv_nsec);
    17 TIME __CURRENT_TIME;
       
    18 
    22 
    19 #define __LOCATED_VAR(type, name) type name;
    23 #define maxval(a,b) ((a>b)?a:b)
    20 #include "LOCATED_VARIABLES.h"
       
    21 #undef __LOCATED_VAR
       
    22 
    24 
    23 static int tick = 0;
    25 #ifdef __WIN32__
       
    26 void timer_notify()
       
    27 {
       
    28    struct _timeb timebuffer;
       
    29 
       
    30    _ftime( &timebuffer );
       
    31    run(timebuffer.time, timebuffer.millitm * 1000000);
       
    32 }
       
    33 
       
    34 int main(int argc,char **argv)
       
    35 {
       
    36     HANDLE hTimer = NULL;
       
    37     LARGE_INTEGER liDueTime;
       
    38 
       
    39     liDueTime.QuadPart = -10000 * maxval(common_ticktime__,1);;
       
    40 
       
    41     // Create a waitable timer.
       
    42     hTimer = CreateWaitableTimer(NULL, TRUE, "WaitableTimer");
       
    43     if (NULL == hTimer)
       
    44     {
       
    45         printf("CreateWaitableTimer failed (%d)\n", GetLastError());
       
    46         return 1;
       
    47     }
       
    48 
       
    49     config_init__();
       
    50 
       
    51     // Set a timer to wait for 10 seconds.
       
    52     if (!SetWaitableTimer(hTimer, &liDueTime, common_ticktime__, NULL, NULL, 0))
       
    53     {
       
    54         printf("SetWaitableTimer failed (%d)\n", GetLastError());
       
    55         return 2;
       
    56     }
       
    57 
       
    58     while(1){
       
    59     // Wait for the timer.
       
    60         if (WaitForSingleObject(hTimer, INFINITE) != WAIT_OBJECT_0)
       
    61         {
       
    62             printf("WaitForSingleObject failed (%d)\n", GetLastError());
       
    63             break;
       
    64         }
       
    65         timer_notify();
       
    66     }
       
    67     return 0;
       
    68 }
       
    69 #else
    24 void timer_notify(sigval_t val)
    70 void timer_notify(sigval_t val)
    25 {
    71 {
    26     clock_gettime(CLOCK_REALTIME, &__CURRENT_TIME);
    72     struct timespec CURRENT_TIME;
    27     printf("Tick %d\n",tick);
    73     clock_gettime(CLOCK_REALTIME, &CURRENT_TIME);
    28     config_run__(tick++);
    74     run(CURRENT_TIME.tv_sec, CURRENT_TIME.tv_nsec);
    29     printf("  Located variables : \n");
       
    30 #define __LOCATED_VAR(type, name) __print_##type(name);
       
    31 #include "LOCATED_VARIABLES.h"
       
    32 #undef __LOCATED_VAR
       
    33 }
    75 }
    34 
    76 
    35 void catch_signal(int sig)
    77 void catch_signal(int sig)
    36 {
    78 {
    37   signal(SIGTERM, catch_signal);
    79   signal(SIGTERM, catch_signal);
    38   signal(SIGINT, catch_signal);
    80   signal(SIGINT, catch_signal);
    39   printf("Got Signal %d\n",sig);
    81   printf("Got Signal %d\n",sig);
    40 }
    82 }
    41 
    83 
    42 #define maxval(a,b) ((a>b)?a:b)
       
    43 int main(int argc,char **argv)
    84 int main(int argc,char **argv)
    44 {
    85 {
    45     timer_t timer;
    86     timer_t timer;
    46     struct sigevent sigev;
    87     struct sigevent sigev;
    47     long tv_nsec = 1000000 * (maxval(common_ticktime__,1)%1000);
    88     long tv_nsec = 1000000 * (maxval(common_ticktime__,1)%1000);
    72     
   113     
    73     timer_delete (timer);
   114     timer_delete (timer);
    74     
   115     
    75     return 0;
   116     return 0;
    76 }
   117 }
       
   118 #endif