-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSource.c
More file actions
354 lines (277 loc) · 11.8 KB
/
Copy pathSource.c
File metadata and controls
354 lines (277 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
// ============================================================================== //
// Project Name : Vehicle Control System - C - M.T. Diploma 65 //
// File Name : Source.c //
// Author : Hossam Nasr //
// Version : Final Version (20 / 04 / 2022) //
// ============================================================================== //
#include <stdio.h>
#include "Functions_Prototypes.h"
// ========================== Main Function ==================================== //
/* function main begins program execution */
int main(void)
{
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);
get_the_user_choice(); /* get the the user choice when needed */
check_mainMENU_option(); /* check which option the user choose */
} /* end main function */
// ========================= Functions Definitions ============================= //
/* function definition: get the the user choice when needed */
void get_the_user_choice(void)
{
/* represent the options to the user to choose from */
printf("Choose what you want:\n");
printf("=====================\n");
printf("a. Turn on the vehicle engine\n");
printf("b. Turn off the vehicle engine\n");
printf("c. Quit the system\n");
/* prompt the user to enter a choice and read it from the console */
printf("\nEnter a character indicating what you want: ");
scanf(" %c", &choice);
} /* end get_the_user_choice function */
/* function definition: check which option at the beginning the user choose */
void check_mainMENU_option(void)
{
/* use switch cases to choose the proper set of operations to apply */
switch( choice )
{
case 'a':
case 'A':
engine_state = ON;
printf("\n<<< The vehicle engine is ON >>>\n\n"); /* print the system current state */
/* while the engine is ON, menu always displayed and waits for an answer */
while( engine_state == ON )
{
sensors_set_menu(); /* print the sensor set menu and let the user decide what to do */
check_sensorMENU_option(); /* check which option at the sensor menu the user choose */
if( choice == 'A' || choice == 'a' )
{
print_state(); /* print the state of the vehicle to the console */
}
} /* end while */
break;
case 'b':
case 'B':
printf("\n<<< The vehicle engine is OFF >>>\n\n"); /* print the system current state */
/* go back to the choice phase to let the user choose again */
while( choice == 'b' || choice == 'B')
{
get_the_user_choice(); /* get the the user choice when needed */
check_mainMENU_option(); /* check which option at the beginning the user choose, this function call itself */
} /* end while */
break;
case 'c':
case 'C':
printf("\n<<< Quit System >>>\n"); /* print the system current state */
return; /* quit the program by return any number to the main function of return type void */
break; /* optional: not necessary as the program will end directly after return */
default: /* user input undefined symbol */
printf("\nInvalid Input. Try to input a valid one.\n\n"); /* inform the user that he/she has inputed an invalid input */
/* prompt the user to input a valid choice */
get_the_user_choice(); /* get the the user choice when needed */
check_mainMENU_option(); /* check which option at the beginning the user choose, this function call itself */
break; /* optional: will exit switch anyway */
} /* end switch */
} /* end check_which_option function */
/* function definition: print the sensor set menu and let the user decide what to do */
void sensors_set_menu(void)
{
/* represent the options to the user to choose from */
printf("Choose the right option:\n");
printf("========================\n");
printf("a. Turn off the engine\n");
printf("b. Set the traffic light color\n");
printf("c. Set the room temperature\n");
printf("d. Set the engine temperature\n\n");
/* prompt the user to enter a choice and read it from the console */
printf("Enter a character indicating what you want: ");
scanf(" %c", &sensor_choice);
} /* end sensors_set_menu function */
/* function definition: check which option at the sensor menu the user choose */
void check_sensorMENU_option(void)
{
/* use switch cases to choose the proper set of operations to apply */
switch( sensor_choice )
{
case 'a':
case 'A':
engine_state = OFF; /* make the engine state OFF */
choice = 'b'; /* manipulate the choice variable to be (b) to loop again on the main menu */
/* go back to the choice phase to let the user choose again */
printf("\n");
get_the_user_choice();
check_mainMENU_option();
break;
case 'b':
case 'B':
traffic_light(); /* get the traffic light data and decide what to do */
/* check the special case of the speed of 30 to set the proper values */
if( vehicle_speed == 30 )
{
speed_30_case(); /* put proper values for the special case of the speed of 30 */
return;
}
break;
case 'c':
case 'C':
/* check the special case of the speed of 30 to set the proper values */
if( vehicle_speed == 30 )
{
speed_30_case(); /* put proper values for the special case of the speed of 30 */
/* do not call the function of room temperature as it is already set at the special case of the speed 30 */
return; /* return blank as it is a void function */
}
room_temperature();
break;
case 'd':
case 'D':
/* check the special case of the speed of 30 to set the proper values */
if( vehicle_speed == 30 )
{
speed_30_case(); /* put proper values for the special case of the speed of 30 */
/* do not call the function of engine temperature as it is already set at the special case of the speed 30 */
return; /* return blank as it is a void function */
}
engine_temperature();
break;
default:
/* inform the user that his input is invalid and make him choose again */
printf("\nInvalid Input. Try to input a valid one.\n\n");
sensors_set_menu();
check_sensorMENU_option();
break; /* optional: will exit switch anyway */
} /* end switch */
} /* end check_sensor_option function */
/* function definition: get the traffic light data and decide what to do */
void traffic_light(void)
{
char traffic_light_data; /* variable to store the data of the traffic light */
/* represent the options of the traffic light to the user to choose from */
printf("\nChoose from the traffic light options:\n");
printf("======================================\n");
printf("G. Green\n");
printf("O. Yellow\n");
printf("R. Red\n\n");
/* prompt the user to enter the traffic light data from the console */
printf("Enter the current traffic light color: ");
scanf(" %c", &traffic_light_data);
/* use switch cases to choose the proper set of operations to apply */
switch( traffic_light_data )
{
/* check if the traffic light is green */
case 'g':
case 'G':
vehicle_speed = 100; /* set the vehicle speed to 100 km/hr */
break;
/* check if the traffic light is yellow */
case 'o':
case 'O':
vehicle_speed = 30; /* set the vehicle speed to 30 km/hr */
break;
/* check if the traffic light is red */
case 'r':
case 'R':
vehicle_speed = 0; /* set the vehicle speed to 0 km/hr */
break;
default: /* user input undefined symbol */
printf("\nInvalid Input.\n"); /* inform the user that he/she has inputed an invalid input */
break; /* optional: will exit switch anyway */
} /* end switch */
} /* end traffic_light function */
/* function definition: get the room temperature data and decide what to do */
void room_temperature(void)
{
/* prompt the user to enter the room temperature data from the console */
printf("\nEnter the current room temperature: ");
scanf("%f", &curr_room_temperature);
if( curr_room_temperature < 10 ) /* check if the room temperature is less than 10 */
{
/* turn the AC state to ON, and set the room temperature to 20 */
ac_state = ON;
room_temp = 20;
}
else if( curr_room_temperature > 30 ) /* check if the room temperature is greater than 30 */
{
/* turn the AC state to ON, and set the room temperature to 20 */
ac_state = ON;
room_temp = 20;
}
else /* otherwise */
{
/* turn the AC state to OFF */
ac_state = OFF;
}
} /* end room_temperature function */
/* function definition: get the engine_temperature data and decide what to do */
void engine_temperature(void)
{
/* prompt the user to enter the engine temperature data from the console */
printf("\nEnter the current engine temperature: ");
scanf("%f", &curr_engine_temperature);
if( curr_engine_temperature < 100 ) /* check if the engine temperature is less than 10 */
{
/* turn the Engine Temperature Controller state to ON, and set the engine temperature to 20 */
etc_state = ON;
engine_temp = 125;
}
else if( curr_engine_temperature > 150 ) /* check if the room temperature is greater than 30 */
{
/* turn the Engine Temperature Controller state to ON, and set the engine temperature to 20 */
etc_state = ON;
engine_temp = 125;
}
else /* otherwise */
{
/* turn the Engine Temperature Controller state to OFF */
etc_state = OFF;
}
} /* end room_temperature function */
/* function definition: put proper values for the special case of the speed of 30 */
void speed_30_case(void)
{
/* inform the user that he entered a special case so it will need to provide some additional data */
printf("\nThe speed is 30 Km/Hr and this is special case\n");
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
/* set the values of the system by calling special functions */
adjust_AC(); /* do the special case needed operations for AC */
adjust_ETC(); /* do the special case needed operations for ETC */
} /* end speed_30_case function */
/* function definition: do the special case needed operations for AC */
void adjust_AC(void)
{
/* prompt the user to enter the room temperature data from the console */
printf("\nEnter the current room temperature: ");
scanf("%f", &curr_room_temperature);
ac_state = ON; /* turn the AC state to ON */
room_temp = ( curr_room_temperature * 5/4 ) + 1; /* set the room temperature */
printf("\n"); /* print double of new line for readability */
} /* end adjust_AC */
/* function definition: do the special case needed operations for ETC */
void adjust_ETC(void)
{
/* prompt the user to enter the engine temperature data from the console */
printf("Enter the current engine temperature: ");
scanf("%f", &curr_engine_temperature);
etc_state = ON; /* turn the Engine Temperature Controller state to ON */
engine_temp = ( curr_engine_temperature * 5/4 ) + 1; /* set the engine temperature */
} /* end adjust_ETC */
/* function definition: print the state of the vehicle to the console */
void print_state(void)
{
/* print a header for the state print */
printf("\nCurrent vehicle state\n");
printf("*********************\n");
/* print the engine state */
printf("Engine State: ");
engine_state == ON ? printf("ON\n"): printf("OFF\n");
/* print the AC state */
printf("AC State: ");
ac_state == ON ? printf("ON\n"): printf("OFF\n");
printf("Vehicle Speed: %d Km/Hr\n", vehicle_speed); /* print the vehicle state */
printf("Room Temperature: %.3f C\n", room_temp); /* print the room temperature */
/* print the Engine Temperature Controller state */
printf("Engine Temperature Controller: ");
etc_state == ON ? printf("ON\n"): printf("OFF\n");
printf("Engine Temperature: %.3f C\n\n", engine_temp); /* print the engine temperature */
}
/* end print_state function */