-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.c
More file actions
119 lines (101 loc) · 3.62 KB
/
Copy pathmain.c
File metadata and controls
119 lines (101 loc) · 3.62 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
#include "IOModule.h"
#include "Timer.h"
int BUTTONS[BUTTON_SIZE] = {0};
void initialize(struct order *currentOrder);
int main() {
// Initialize hardware
if (!elev_init()) {
printf("Unable to initialize elevator hardware!\n");
return 1;
}
clearQueueAndButtons();
typedef enum {MOVING, IDLE, SERVICE, STOP} STATE;
STATE state = IDLE;
struct order currentOrder;
initialize(¤tOrder);
int currentFloor;
pollFloorsAndSetLights(¤tFloor);
while(1){
switch(state){
case IDLE:
elev_set_door_open_lamp(0);
pollAndUpdateButtonsAndLights();
createOrdersAndUpdateQueue(¤tOrder);
if (!(currentOrder.isEnabled)){
fetchOrder(¤tOrder);
}
if(currentOrder.isEnabled){
state = MOVING;
}
else if(elev_get_stop_signal()){
state = STOP;
}
break;
case MOVING:
setMotorDirection(¤tFloor, ¤tOrder);
while(elev_get_floor_sensor_signal() != currentOrder.floor){
if (elev_get_stop_signal()){
state = STOP;
break;
}
pollAndUpdateButtonsAndLights();
createOrdersAndUpdateQueue(¤tOrder);
pollFloorsAndSetLights(¤tFloor);
if ((ordersOnCurrentFloor(¤tFloor, getDirection())) && (elev_get_floor_sensor_signal() != -1)){
state = SERVICE;
break;
}
}
if (state != STOP){
state = SERVICE;
}
break;
case SERVICE:
elev_set_motor_direction(DIRN_STOP);
pollFloorsAndSetLights(¤tFloor);
setDirection(-1);
elev_set_door_open_lamp(1);
startTimer();
while(!(timeOut())){
clearOrdersOnCurrentFloor(currentFloor);
pollAndUpdateButtonsAndLights();
createOrdersAndUpdateQueue(¤tOrder);
if(elev_get_stop_signal()){
state = STOP;
break;
}
}
if(currentFloor == currentOrder.floor){
currentOrder.isEnabled = 0;
}
if (state != STOP){
state = IDLE;
}
break;
case STOP:
elev_set_motor_direction(DIRN_STOP);
clearQueueAndButtons();
pollAndUpdateButtonsAndLights();
currentOrder.isEnabled = 0;
if(!(elev_get_floor_sensor_signal() == -1)){
state = SERVICE;
}
else if(!(elev_get_stop_signal())){
state = IDLE;
}
break;
default:
printf("INVALID STATE!\n");
}
}
return 0;
}
void initialize(struct order *currentOrder){
currentOrder->isEnabled = 0;
currentOrder->buttonType = -1;
currentOrder->floor = -1;
while (elev_get_floor_sensor_signal() == -1){
elev_set_motor_direction(DIRN_DOWN);
}
elev_set_motor_direction(DIRN_STOP);
}