-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.c
142 lines (120 loc) · 3.19 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
#include "tm4c123gh6pm.h"
#include "ps2_keyboard.h"
#include "lcd.h"
#include "motor.h"
#include "EEPROM.h"
#define MAX_PASSWORD_LENGTH 32
char read_keyboard_char_sync() {
while (1) {
if (KEYBOARD_availableChars())
return KEYBOARD_readChar();
}
}
char* start_enter_password_dialogue() {
LCD_clear();
LCD_string("Press [Enter] to");
LCD_command(0xC0);
LCD_string("type password");
while (read_keyboard_char_sync() != '\n');
LCD_clear();
int i = 0;
static char password[33];
while (1) {
char c = read_keyboard_char_sync();
if (c == '\n')
break;
if (i == MAX_PASSWORD_LENGTH)
continue;
if (c == 0x66) {
LCD_clear();
i = 0;
} else if (c >= ' ' && c <= '~') {
LCD_clear();
int k = 0;
while (k != i) {
LCD_data('*');
k++;
if (k == 16)
LCD_command(0xC0);
}
LCD_data(c);
password[i] = c;
i++;
}
}
password[i] = '\0';
return password;
}
void open_incorrect_passwd_dialogue() {
LCD_clear();
LCD_string("Incorrect Secret");
LCD_command(0xC0);
LCD_string("[Enter] try again");
while (read_keyboard_char_sync() != '\n');
}
int open_on_door_closed_menu() {
while (1) {
LCD_clear();
LCD_string("1. Change password");
LCD_command(0xC0);
LCD_string("2. Open door");
char c = read_keyboard_char_sync();
if (c == '\n') {
LCD_clear();
LCD_string("3. Logout");
c = read_keyboard_char_sync();
if (c == '\n')
continue;
}
if (c == '1') {
char* passwd = start_enter_password_dialogue();
setPassword(passwd);
} else if (c == '2') {
LCD_string("Opening door..");
door_open();
return 0;
} else if (c == '3') {
return 1;
}
}
}
void open_main_menu() {
while (1) {
LCD_clear();
LCD_string("1. Change password");
LCD_command(0xC0);
LCD_string("2. Close door");
char c = read_keyboard_char_sync();
if (c == '1') {
char* passwd = start_enter_password_dialogue();
setPassword(passwd);
} else if (c == '2') {
LCD_string("Closing door..");
door_close();
int return_code = open_on_door_closed_menu();
if (return_code == 0) {
continue;
} else {
break;
}
}
}
}
int main(void)
{
KEYBOARD_init();
LCD_init();
LCD_start();
motor_init();
eeprom_init();
while (1) {
char* password = start_enter_password_dialogue();
int valid_password = checkPassword(password);
if (valid_password) {
door_open();
open_main_menu();
} else {
open_incorrect_passwd_dialogue();
}
}
}