Rev 1226 | Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1225 | giacomo | 1 | /* Event Generator |
2 | * |
||
3 | * Giacomo Guidi |
||
4 | */ |
||
5 | |||
6 | #include <stdio.h> |
||
7 | #include <stdlib.h> |
||
8 | |||
9 | #include "event_gen.h" |
||
10 | |||
11 | #define ACT_SINGLE 0 |
||
12 | #define ACT_PERIODIC 1 |
||
13 | #define ACT_MEAN 2 |
||
14 | #define ACT_INVALID 3 |
||
15 | |||
16 | int select_act_type(void) |
||
17 | { |
||
18 | char act_type[10]; |
||
19 | |||
20 | printf("\nInsert the activation type\n"); |
||
21 | printf(" S - Single\n"); |
||
22 | printf(" P - Periodic\n"); |
||
23 | printf(" M - Sporadic with constant distribution (Mean,Delta)\n"); |
||
24 | printf("> "); |
||
25 | scanf("%s",act_type); |
||
26 | |||
27 | switch (act_type[0]) { |
||
28 | |||
29 | case 's': |
||
30 | case 'S': |
||
31 | |||
32 | return ACT_SINGLE; |
||
33 | break; |
||
34 | |||
35 | case 'p': |
||
36 | case 'P': |
||
37 | |||
38 | return ACT_PERIODIC; |
||
39 | break; |
||
40 | |||
41 | case 'm': |
||
42 | case 'M': |
||
43 | |||
44 | return ACT_MEAN; |
||
45 | break; |
||
46 | |||
47 | default: |
||
48 | |||
49 | return ACT_INVALID; |
||
50 | |||
51 | } |
||
52 | |||
53 | return ACT_INVALID; |
||
54 | |||
55 | } |
||
56 | |||
57 | int write_single_act(char *task_name) |
||
58 | { |
||
59 | |||
60 | FILE *file_act_header; |
||
61 | int time_usec; |
||
62 | |||
63 | file_act_header = fopen("test_header.h","a+"); |
||
64 | if (file_act_header == NULL) return 1; |
||
65 | |||
66 | printf("\nInsert single activation time [us]\n"); |
||
67 | printf("> "); |
||
68 | scanf("%d",&time_usec); |
||
69 | |||
70 | fprintf(file_act_header,"\nstruct timespec %s_act_event[1] = {%d,%d};\n", |
||
71 | task_name,time_usec/1000000,time_usec%1000000); |
||
72 | |||
73 | fclose(file_act_header); |
||
74 | |||
75 | return 0; |
||
76 | |||
77 | } |
||
78 | |||
79 | int write_periodic_act(char *task_name) |
||
80 | { |
||
81 | |||
82 | FILE *file_act_header; |
||
83 | int i,first_time_usec,per_time_usec,act_num; |
||
84 | long long tot_time_usec; |
||
85 | |||
86 | file_act_header = fopen("test_header.h","a+"); |
||
87 | if (file_act_header == NULL) return 1; |
||
88 | |||
89 | printf("\nInsert the number of activations\n"); |
||
90 | printf("> "); |
||
91 | scanf("%d",&act_num); |
||
92 | printf("Insert first activation time [us]\n"); |
||
93 | printf("> "); |
||
94 | scanf("%d",&first_time_usec); |
||
95 | printf("Insert periodic activation time [us]\n"); |
||
96 | printf("> "); |
||
97 | scanf("%d",&per_time_usec); |
||
98 | |||
99 | fprintf(file_act_header,"\nstruct timespec %s_act_event[%d] = {{%d,%d},\n", |
||
100 | task_name,act_num,first_time_usec/1000000,first_time_usec%1000000); |
||
101 | |||
102 | tot_time_usec = first_time_usec; |
||
103 | for (i=0;i<act_num-1;i++) { |
||
104 | tot_time_usec += per_time_usec; |
||
105 | fprintf(file_act_header," {%d,%d},\n", |
||
106 | (int)tot_time_usec/1000000,(int)tot_time_usec%1000000); |
||
107 | } |
||
108 | |||
109 | fprintf(file_act_header,"};\n"); |
||
110 | |||
111 | fclose(file_act_header); |
||
112 | |||
113 | return 0; |
||
114 | |||
115 | } |
||
116 | |||
117 | int write_mean_act(char *task_name) |
||
118 | { |
||
119 | |||
120 | FILE *file_act_header; |
||
121 | int i,first_time_usec,mean_time_usec,act_num,delta_time_usec; |
||
122 | long long tot_time_usec; |
||
123 | |||
124 | file_act_header = fopen("test_header.h","a+"); |
||
125 | if (file_act_header == NULL) return 1; |
||
126 | |||
127 | printf("\nInsert the number of activations\n"); |
||
128 | printf("> "); |
||
129 | scanf("%d",&act_num); |
||
130 | printf("Insert first activation time [us]\n"); |
||
131 | printf("> "); |
||
132 | scanf("%d",&first_time_usec); |
||
133 | printf("Insert mean peridic time [us]\n"); |
||
134 | printf("> "); |
||
135 | scanf("%d",&mean_time_usec); |
||
136 | printf("Insert delta time [us]\n"); |
||
137 | printf("> "); |
||
138 | scanf("%d",&delta_time_usec); |
||
139 | |||
140 | fprintf(file_act_header,"\nstruct timespec %s_act_event[%d] = {{%d,%d},\n", |
||
141 | task_name,act_num,first_time_usec/1000000,first_time_usec%1000000); |
||
142 | |||
143 | tot_time_usec = first_time_usec; |
||
144 | for (i=0;i<act_num-1;i++) { |
||
145 | tot_time_usec += mean_time_usec + random() % delta_time_usec - delta_time_usec/2; |
||
146 | fprintf(file_act_header," {%d,%d},\n", |
||
147 | (int)tot_time_usec/1000000,(int)tot_time_usec%1000000); |
||
148 | } |
||
149 | |||
150 | fprintf(file_act_header,"};\n"); |
||
151 | |||
152 | fclose(file_act_header); |
||
153 | |||
154 | return 0; |
||
155 | |||
156 | } |
||
157 | |||
158 | int main(void) { |
||
159 | |||
160 | char task_name[100]; |
||
161 | char act_type; |
||
162 | int err; |
||
163 | |||
164 | printf("\nEvent Generator\n\n"); |
||
165 | |||
166 | srandom(12354132); |
||
167 | |||
168 | printf("Insert the name of the task\n"); |
||
169 | printf("> "); |
||
170 | scanf("%s",task_name); |
||
171 | |||
172 | while((act_type = select_act_type()) == ACT_INVALID) { |
||
173 | printf("Error: Invalid Act Type\n"); |
||
174 | } |
||
175 | |||
176 | switch (act_type) { |
||
177 | case ACT_SINGLE: |
||
178 | err = write_single_act(task_name); |
||
179 | if (err != 0) { |
||
180 | printf("Error writing activation header\n"); |
||
181 | exit(1); |
||
182 | } |
||
183 | break; |
||
184 | case ACT_PERIODIC: |
||
185 | err = write_periodic_act(task_name); |
||
186 | if (err != 0) { |
||
187 | printf("Error writing activation header\n"); |
||
188 | exit(1); |
||
189 | } |
||
190 | break; |
||
191 | case ACT_MEAN: |
||
192 | err = write_mean_act(task_name); |
||
193 | if (err != 0) { |
||
194 | printf("Error writing activation header\n"); |
||
195 | exit(1); |
||
196 | } |
||
197 | break; |
||
198 | } |
||
199 | |||
200 | return 0; |
||
201 | |||
202 | } |