tests/main.c
changeset 41 8998c8b24b60
child 43 37dd4e2fd2ec
equal deleted inserted replaced
40:873a5b60a7ea 41:8998c8b24b60
       
     1 #include <time.h>
       
     2 #include <signal.h>
       
     3 
       
     4 #include "iec_std_lib.h"
       
     5 
       
     6 /*
       
     7  * Functions and variables provied by generated C softPLC
       
     8  **/ 
       
     9 void config_run__(int tick);
       
    10 void config_init__(void);
       
    11 extern int common_ticktime__;
       
    12 
       
    13 /*
       
    14  *  Functions and variables to export to generated C softPLC
       
    15  **/
       
    16  
       
    17 TIME __CURRENT_TIME;
       
    18 
       
    19 #define __LOCATED_VAR(type, name) type name;
       
    20 #include "LOCATED_VARIABLES.h"
       
    21 #undef __LOCATED_VAR
       
    22 
       
    23 #define print_BOOL(name) printf("  %s = (BOOL) %s\n",#name, name?"TRUE":"FALSE");
       
    24 #define print_SINT(name) printf("  %s = (SINT) %d\n",#name, name);
       
    25 #define print_INT(name) printf("  %s = (INT) %d\n",#name, name);
       
    26 #define print_DINT(name) printf("  %s = (DINT) %d\n",#name, name);
       
    27 #define print_LINT(name) printf("  %s = (LINT) %d\n",#name, name);
       
    28 #define print_USINT(name) printf("  %s = (USINT) %u\n",#name, name);
       
    29 #define print_UINT(name) printf("  %s = (UINT) %u\n",#name, name);
       
    30 #define print_UDINT(name) printf("  %s = (UDINT) %u\n",#name, name);
       
    31 #define print_ULINT(name) printf("  %s = (ULINT) %lu\n",#name, name);
       
    32 #define print_REAL(name) printf("  %s = (REAL) %f\n",#name, (double)name);
       
    33 #define print_LREAL(name) printf("  %s = (LREAL) %f\n",#name, (double)name);
       
    34 #define print_TIME(name) {tmp STRING = __time_to_string(name);tmp.body[tmp.len] = 0; printf("  %s = (TIME) %s*s\n",#name, tmp.len, &tmp.body);}
       
    35 #define print_DATE(name) {tmp STRING = __date_to_string(name);tmp.body[tmp.len] = 0; printf("  %s = (TIME) %*s\n",#name, tmp.len, &tmp.body);}
       
    36 #define print_TOD(name) {tmp STRING = __tod_to_string(name);tmp.body[tmp.len] = 0; printf("  %s = (TIME) %*s\n",#name, tmp.len, &tmp.body);}
       
    37 #define print_DT(name) {tmp STRING = __dt_to_string(name);tmp.body[tmp.len] = 0; printf("  %s = (TIME) %*s\n",#name, tmp.len, &tmp.body);}
       
    38 #define print_STRING(name) printf("  %s = (STRING) {%d, \"%*s\"}\n",#name, name.len, name.len, &name.body);
       
    39 #define print_BYTE(name) printf("  %s = (BYTE) 0x%2.2x\n",#name, name);
       
    40 #define print_WORD(name) printf("  %s = (WORD) 0x%d4.4\n",#name, name);
       
    41 #define print_DWORD(name) printf("  %s = (DWORD) 0x%d8.8\n",#name, name);
       
    42 #define print_LWORD(name) printf("  %s = (LWORD) 0x%d16.16\n",#name, name);
       
    43 
       
    44 static int tick = 0;
       
    45 void timer_notify(sigval_t val)
       
    46 {
       
    47     clock_gettime(CLOCK_REALTIME, &__CURRENT_TIME);
       
    48     printf("Tick %d\n",tick);
       
    49     config_run__(tick++);
       
    50     printf("  Located variables : \n");
       
    51 #define __LOCATED_VAR(type, name) print_##type(name);
       
    52 #include "LOCATED_VARIABLES.h"
       
    53 #undef __LOCATED_VAR
       
    54 }
       
    55 
       
    56 void catch_signal(int sig)
       
    57 {
       
    58   signal(SIGTERM, catch_signal);
       
    59   signal(SIGINT, catch_signal);
       
    60   printf("Got Signal %d\n",sig);
       
    61 }
       
    62 
       
    63 #define maxval(a,b) ((a>b)?a:b)
       
    64 int main(int argc,char **argv)
       
    65 {
       
    66     timer_t timer;
       
    67     struct sigevent sigev;
       
    68     long tv_nsec = 1000000 * (maxval(common_ticktime__,1)%1000);
       
    69     time_t tv_sec = common_ticktime__/1000;
       
    70     struct itimerspec timerValues;
       
    71     
       
    72     memset (&sigev, 0, sizeof (struct sigevent));
       
    73     memset (&timerValues, 0, sizeof (struct itimerspec));
       
    74     sigev.sigev_value.sival_int = 0;
       
    75     sigev.sigev_notify = SIGEV_THREAD;
       
    76     sigev.sigev_notify_attributes = NULL;
       
    77     sigev.sigev_notify_function = timer_notify;
       
    78     timerValues.it_value.tv_sec = tv_sec;
       
    79     timerValues.it_value.tv_nsec = tv_nsec;
       
    80     timerValues.it_interval.tv_sec = tv_sec;
       
    81     timerValues.it_interval.tv_nsec = tv_nsec;
       
    82 
       
    83     config_init__();
       
    84 
       
    85     timer_create (CLOCK_REALTIME, &sigev, &timer);
       
    86     timer_settime (timer, 0, &timerValues, NULL);
       
    87     
       
    88     /* install signal handler for manual break */
       
    89     signal(SIGTERM, catch_signal);
       
    90     signal(SIGINT, catch_signal);
       
    91     
       
    92     pause();
       
    93     
       
    94     timer_delete (timer);
       
    95     
       
    96     return 0;
       
    97 }