|
@@ -2,9 +2,10 @@
|
2
|
2
|
#include <Keypad/Keypad.h>
|
3
|
3
|
#include <LiquidCrystal.h>
|
4
|
4
|
|
|
5
|
+#include "config.h"
|
|
6
|
+
|
|
7
|
+#include "HostCommunication.h"
|
5
|
8
|
|
6
|
|
-#define KP_ROWS 4
|
7
|
|
-#define KP_COLS 4
|
8
|
9
|
char keys[KP_ROWS][KP_COLS] = {
|
9
|
10
|
{'1','2','3','A'},
|
10
|
11
|
{'4','5','6','B'},
|
|
@@ -16,21 +17,9 @@ byte colPins[KP_COLS] = {8, 9, 10, 11};
|
16
|
17
|
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, KP_ROWS, KP_COLS);
|
17
|
18
|
|
18
|
19
|
|
19
|
|
-#define LCD_ROWS 2
|
20
|
|
-#define LCD_COLS 16
|
21
|
20
|
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
|
22
|
21
|
|
23
|
22
|
|
24
|
|
-#define SELF_TEST_LABEL "Self testing..."
|
25
|
|
-#define SELF_TEST_ERROR_LABEL "Error: "
|
26
|
|
-#define UID_LABEL "UID:"
|
27
|
|
-#define PWD_LABEL "PWD:"
|
28
|
|
-#define LOGIN_IN_PROGRESS "Login..."
|
29
|
|
-#define LOGIN_FAILED "Login failed"
|
30
|
|
-#define LOGIN_SUCCESS "Access granted"
|
31
|
|
-
|
32
|
|
-#define UID_MAX_LEN 12
|
33
|
|
-#define PWD_MAX_LEN 12
|
34
|
23
|
String uid;
|
35
|
24
|
String password;
|
36
|
25
|
enum Status {
|
|
@@ -43,21 +32,16 @@ enum Status {
|
43
|
32
|
};
|
44
|
33
|
Status status = Uid;
|
45
|
34
|
|
46
|
|
-#define MAX_IDLE_TIME 20
|
47
|
35
|
unsigned long lastActivity = 0;
|
48
|
36
|
|
49
|
|
-#define LOGIN_FAILED_TIME 2
|
50
|
|
-
|
51
|
|
-#define SELF_TEST_INTERVAL 60
|
52
|
|
-#define SELF_TEST_ERROR_INTERVAL 2
|
53
|
|
-#define MAX_SELF_TEST_TIME 5
|
54
|
37
|
unsigned long lastSelfTest = 0;
|
55
|
|
-#define SELF_TEST_NO_ERROR '0'
|
56
|
|
-int lastSelfTestResult = SELF_TEST_NO_ERROR;
|
|
38
|
+int lastSelfTestResult = ERROR_NONE;
|
|
39
|
+
|
|
40
|
+HostCommunication hostCommunication;
|
57
|
41
|
|
58
|
42
|
void setup() {
|
59
|
|
- Serial.begin(9600);
|
60
|
|
- Serial.println("Starting...");
|
|
43
|
+
|
|
44
|
+ hostCommunication.init();
|
61
|
45
|
|
62
|
46
|
lcd.begin(LCD_COLS, LCD_ROWS);
|
63
|
47
|
lcd.blink();
|
|
@@ -140,7 +124,7 @@ bool handleKeyPad()
|
140
|
124
|
|
141
|
125
|
void updateLcd()
|
142
|
126
|
{
|
143
|
|
- Serial.println("Update LCD");
|
|
127
|
+ hostCommunication.debug("Update LCD");
|
144
|
128
|
lcd.clear();
|
145
|
129
|
if (status == Uid || status == Password)
|
146
|
130
|
{
|
|
@@ -159,7 +143,7 @@ void updateLcd()
|
159
|
143
|
}
|
160
|
144
|
else if (status == SelfTest) {
|
161
|
145
|
lcd.print(SELF_TEST_LABEL);
|
162
|
|
- if (lastSelfTestResult != SELF_TEST_NO_ERROR) {
|
|
146
|
+ if (lastSelfTestResult != ERROR_NONE) {
|
163
|
147
|
lcd.setCursor(0, 1);
|
164
|
148
|
lcd.print(SELF_TEST_ERROR_LABEL);
|
165
|
149
|
lcd.print(lastSelfTestResult);
|
|
@@ -191,8 +175,7 @@ void login(String uid, String password)
|
191
|
175
|
|
192
|
176
|
delay(LOGIN_FAILED_TIME * 1000);
|
193
|
177
|
|
194
|
|
- Serial.println(uid);
|
195
|
|
- Serial.println(password);
|
|
178
|
+ hostCommunication.login(uid, password);
|
196
|
179
|
|
197
|
180
|
status = LoginSuccess;
|
198
|
181
|
updateLcd();
|
|
@@ -204,24 +187,35 @@ void login(String uid, String password)
|
204
|
187
|
|
205
|
188
|
void selfTest()
|
206
|
189
|
{
|
207
|
|
- lastSelfTest = SELF_TEST_NO_ERROR;
|
|
190
|
+ lastSelfTest = ERROR_NONE;
|
208
|
191
|
status = SelfTest;
|
209
|
192
|
updateLcd();
|
210
|
193
|
|
211
|
194
|
bool test = true;
|
212
|
195
|
while (test) {
|
213
|
|
- unsigned long secs = millis() / 1000;
|
214
|
|
- Serial.println("Self Testing");
|
215
|
|
- while (Serial.available() == 0 && (millis() / 1000) - secs < MAX_SELF_TEST_TIME);
|
216
|
|
- if (Serial.available() > 0) {
|
217
|
|
- lastSelfTestResult = Serial.read();
|
218
|
|
- if (lastSelfTestResult == SELF_TEST_NO_ERROR) {
|
219
|
|
- test = false;
|
220
|
|
- }
|
221
|
|
- else {
|
222
|
|
- updateLcd();
|
223
|
|
- delay(SELF_TEST_ERROR_INTERVAL * 1000);
|
224
|
|
- }
|
|
196
|
+ hostCommunication.debug("Self Testing");
|
|
197
|
+ hostCommunication.selfTest();
|
|
198
|
+
|
|
199
|
+ HostCommunicationResult res = hostCommunication.read(MAX_SELF_TEST_TIME * 1000);
|
|
200
|
+
|
|
201
|
+ if (!res.isSuccess) {
|
|
202
|
+ lastSelfTestResult = ERROR_HOST_TIMEOUT;
|
|
203
|
+ }
|
|
204
|
+ else if (res.length != sizeof(SERIAL_PACKET_TYPE_INT)) {
|
|
205
|
+ lastSelfTestResult = ERROR_HOST_PROTOCOL;
|
|
206
|
+ }
|
|
207
|
+ else {
|
|
208
|
+ lastSelfTestResult = *(SERIAL_PACKET_TYPE_INT*)res.data;
|
|
209
|
+ }
|
|
210
|
+
|
|
211
|
+ delete res.data;
|
|
212
|
+
|
|
213
|
+ if (lastSelfTestResult == ERROR_NONE) {
|
|
214
|
+ test = false;
|
|
215
|
+ }
|
|
216
|
+ else {
|
|
217
|
+ updateLcd();
|
|
218
|
+ delay(SELF_TEST_ERROR_INTERVAL * 1000);
|
225
|
219
|
}
|
226
|
220
|
}
|
227
|
221
|
lastSelfTest = millis() / 1000;
|