-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
172 lines (156 loc) · 4.3 KB
/
main.c
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
#include <stdio.h>
#include <stdlib.h>
#define WITH_ENGINE_TEMP_CONTROLLER 0
struct flags{
int engineState; // bool
int ac; // bool
int speed; // velocity
int roomTemp; // int
#if WITH_ENGINE_TEMP_CONTROLLER
int engineTempController; // bool
int engineTemp; // int
#endif
};
struct flags states={0,0,0,20,0,0};
void vehicleSpeedAction()
{
if(states.speed==30)
{
states.ac=1;
states.roomTemp=states.roomTemp*(5/4)+1;
#if WITH_ENGINE_TEMP_CONTROLLER
states.engineTempController=1;
states.engineTemp=states.engineTemp*(5/4)+1;
#endif
}
}
void trafficLightAction(char traffic)
{
switch (traffic) {
case 'G':
states.speed=100;
break;
case 'O':
states.speed=30;
break;
case 'R':
states.speed=0;
break;
}
vehicleSpeedAction();
return;
}
void roomTempAction(int temperature)
{
if(temperature<10 || temperature>30)
{
states.ac=1;
states.roomTemp=20;
}
else
{
states.ac=0;
states.roomTemp=temperature;
}
}
#if WITH_ENGINE_TEMP_CONTROLLER
void engineTempAction(int engineTemp_)
{
if(engineTemp_<100 ||engineTemp_>150 )
{
states.engineTempController=1;
states.engineTemp=125;
}
else
{
states.engineTempController=0;
states.engineTemp=engineTemp_;
}
}
#endif
void actionsMenu()
{
puts("a. Turn off the engine"); // display the welcome menu
puts("b. Set the traffic light color."); // change the
puts("c. Set the room temperature (Temperature Sensor)");
#if WITH_ENGINE_TEMP_CONTROLLER
puts("d. Set the engine temperature (Engine Temperature Sensor)");
#endif
}
void printVehicleStates()
{
system("cls");
printf("Engine is %s\n",states.engineState?"ON":"OFF");
printf("AC is %s\n",states.ac?"ON":"OFF");
printf("Vehicle Speed: %d Km/Hr\n",states.speed);
printf("Room Temperature: %d C\n",states.roomTemp);
#if WITH_ENGINE_TEMP_CONTROLLER
printf("Engine Temp Controller is %s\n",states.engineTempController?"ON":"OFF");
printf("Engine temperature: %d C\n",states.engineTemp);
#endif
}
void welcomePrompt()
{
system("cls");
puts("\ta. Turn on the vehicle engine"); // open Actions menu
puts("\tb. Turn off the vehicle engine"); // display the welcome menu again
puts("\tc. Quit the system"); // exit program
}
int main(){
setvbuf(stdout, NULL, _IONBF,0);
setvbuf(stderr, NULL, _IONBF, 0);
while (1) {
welcomePrompt();
char userInput;
scanf(" %c",&userInput);
switch (userInput)
{
case 'a':
states.engineState=1;
printVehicleStates();
actionsMenu();
break;
case 'c':
puts("bye!!");
return 1;
break;
default:
continue;
}
int breakLoop=0;
while (!breakLoop)
{
scanf(" %c",&userInput);
switch (userInput)
{
char input;
int inputTemp;
case 'a':
states.engineState=0;
breakLoop=1;
break;
case 'b':
puts(" enter traffic light:");
scanf(" %c",&input);
trafficLightAction(input);
break;
case 'c':
puts(" enter room temperature:");
scanf(" %d",&inputTemp);
roomTempAction(inputTemp);
break;
#if WITH_ENGINE_TEMP_CONTROLLER
case 'd':
puts(" enter engine temperature:");
scanf(" %d",&inputTemp);
engineTempAction(inputTemp);
break;
#endif
default:
continue;
}
printVehicleStates();
actionsMenu();
}
}
}