-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinside.h
163 lines (143 loc) · 3.2 KB
/
inside.h
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
#include "floor.h"
// #define DEBUG_INSIDE
// 엘리베이터 내부에 해당되는 기능들
struct INSIDE {
int floor_button[TOTAL_FLOORS]; // 각 층에 해당되는 버튼들
int open_button; // 열림버튼
int close_button; // 닫힘버튼
};
struct INSIDE inside;
// 엘리베이터 내부 버튼 on/off 여부
void inside_button_on_off(int floor) {
if (inside.floor_button[floor - 1] == 1)
inside.floor_button[floor - 1] = 0;
else
inside.floor_button[floor - 1] = 1;
}
// 엘리베이터 내부 버튼 동작 함수 (현재 20층 까지 지원)
int inside_button_press(void) {
char input[2]; // 사용자 입력
int input_floor;
int ok = 1;
#ifdef DEBUG_INSIDE
int i;
#endif
input[0] = '\0';
// 키보드 입력 시간 제한
while (1) {
if (_kbhit()) {
// _getch() 함수 반환형이 int 이므로 char 로 캐스팅 해줌
// input[1] 은 NULL 문자 채워줌
input[0] = (char)_getch();
input[1] = '\0';
}
if (wait(1000 / ELEVATOR_SPEED)) // 시간 만료되면 루프 빠져나옴
break;
}
switch (input[0]) {
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
input_floor = atoi(input);
break;
case '0':
input_floor = 10;
break;
case '!':
input_floor = 11;
break;
case '@':
input_floor = 12;
break;
case '#':
input_floor = 13;
break;
case '$':
input_floor = 14;
break;
case '%':
input_floor = 15;
break;
case '^':
input_floor = 16;
break;
case '&':
input_floor = 17;
break;
case '*':
input_floor = 18;
break;
case '(':
input_floor = 19;
break;
case ')':
input_floor = 20;
break; // 1~20 층에 해당되는 버튼들
case 'z':
case 'Z':
inside.open_button = 1;
return ok;
case 'x':
case 'X':
inside.close_button = 1;
return ok;
default:
#ifdef DEBUG_INSIDE
//printf("Error: inside_button_press input failed!\n");
#endif
ok = 0;
return ok;
}
// 지정된 건물 층 수 보다 큰 값의 버튼 눌렸을 경우에 대한 처리
if (input_floor > TOTAL_FLOORS) {
#ifdef DEBUG_INSIDE
printf("Error: inside_button_press floor too big!\n");
#endif
ok = 0;
return ok;
}
inside_button_on_off(input_floor);
/* Pressed test code */
#ifdef DEBUG_INSIDE
printf("\n");
printf("pressed_button = %s\n", input);
for (i = 0; i < TOTAL_FLOORS; i++) {
if (inside.floor_button[i])
printf("internal_button[%d] = true\n", i + 1);
else
printf("internal_button[%d] = false\n", i + 1);
}
if (inside.open_button)
printf("internal_button[open_button] = true\n");
else
printf("internal_button[open_button] = false\n");
if (inside.close_button)
printf("internal_button[close_button] = true\n");
else
printf("internal_button[close_button] = false\n");
#endif
return ok;
}
// 올라가거나 내려가면서 남은 버튼 있는지 확인하는 함수
int inside_button_check(int cur_floor, int is_up) {
int i;
int found = 0;
if (is_up) { // 올라가는 방향
for (i = cur_floor - 1; i < TOTAL_FLOORS; i++) {
if (inside.floor_button[i])
found = 1;
}
} else { // 내려가는 방향
for (i = cur_floor - 1; i >= 0; i--) {
if (inside.floor_button[i])
found = 1;
}
}
return found;
}