|
@@ -0,0 +1,170 @@
|
|
1
|
+#include <Arduino.h>
|
|
2
|
+#include <Keypad/Keypad.h>
|
|
3
|
+#include <LiquidCrystal.h>
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+#define KP_ROWS 4
|
|
7
|
+#define KP_COLS 4
|
|
8
|
+char keys[KP_ROWS][KP_COLS] = {
|
|
9
|
+ {'1','2','3','A'},
|
|
10
|
+ {'4','5','6','B'},
|
|
11
|
+ {'7','8','9','C'},
|
|
12
|
+ {'*','0','#','.'}
|
|
13
|
+};
|
|
14
|
+byte rowPins[KP_ROWS] = {A3, A2, A0, A1};
|
|
15
|
+byte colPins[KP_COLS] = {8, 9, 10, 11};
|
|
16
|
+Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, KP_ROWS, KP_COLS);
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+#define LCD_ROWS 2
|
|
20
|
+#define LCD_COLS 16
|
|
21
|
+LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+#define SELF_TEST_LABEL "Self testing..."
|
|
25
|
+#define UID_LABEL "UID:"
|
|
26
|
+#define PWD_LABEL "PWD:"
|
|
27
|
+#define UID_MAX_LEN 12
|
|
28
|
+#define PWD_MAX_LEN 12
|
|
29
|
+String uid;
|
|
30
|
+String password;
|
|
31
|
+enum Status {
|
|
32
|
+ Uid,
|
|
33
|
+ Password,
|
|
34
|
+ SelfTest
|
|
35
|
+};
|
|
36
|
+Status status = Uid;
|
|
37
|
+
|
|
38
|
+#define MAX_IDLE_TIME 20
|
|
39
|
+unsigned long lastActivity = 0;
|
|
40
|
+#define SELF_TEST_INTERVAL 10
|
|
41
|
+unsigned long lastSelfTest = 0;
|
|
42
|
+
|
|
43
|
+void setup() {
|
|
44
|
+ Serial.begin(9600);
|
|
45
|
+ Serial.println("Starting...");
|
|
46
|
+
|
|
47
|
+ lcd.begin(LCD_COLS, LCD_ROWS);
|
|
48
|
+ lcd.blink();
|
|
49
|
+ lcd.noAutoscroll();
|
|
50
|
+
|
|
51
|
+ reset();
|
|
52
|
+}
|
|
53
|
+
|
|
54
|
+String makeChars(char c, int count) {
|
|
55
|
+ char str[count + 1];
|
|
56
|
+ memset(str, c, count);
|
|
57
|
+ str[count] = 0;
|
|
58
|
+ return String(str);
|
|
59
|
+}
|
|
60
|
+
|
|
61
|
+bool handleUidPassword(String& str, int maxLength, char key) {
|
|
62
|
+ int len = str.length();
|
|
63
|
+ if (key >= '0' && key <= '9') {
|
|
64
|
+ if (len < maxLength) {
|
|
65
|
+ str += key;
|
|
66
|
+ return true;
|
|
67
|
+ }
|
|
68
|
+ }
|
|
69
|
+ else if (key == 'C' || key == '*') {
|
|
70
|
+ if (len > 0) {
|
|
71
|
+ str = str.substring(0, len - 2);
|
|
72
|
+ return true;
|
|
73
|
+ }
|
|
74
|
+ }
|
|
75
|
+ return false;
|
|
76
|
+}
|
|
77
|
+
|
|
78
|
+void loop() {
|
|
79
|
+ unsigned long secs = millis() / 1000;
|
|
80
|
+ if (handleKeyPad()) {
|
|
81
|
+ lastActivity = secs;
|
|
82
|
+ updateLcd();
|
|
83
|
+ }
|
|
84
|
+ if ((uid.length() != 0 || password.length() != 0) && secs - lastActivity >= MAX_IDLE_TIME) {
|
|
85
|
+ reset();
|
|
86
|
+ }
|
|
87
|
+ if (uid.length() == 0 && password.length() == 0 && secs - lastSelfTest >= SELF_TEST_INTERVAL) {
|
|
88
|
+ selfTest();
|
|
89
|
+ }
|
|
90
|
+}
|
|
91
|
+
|
|
92
|
+bool handleKeyPad()
|
|
93
|
+{
|
|
94
|
+ char key = keypad.getKey();
|
|
95
|
+ if (key != NO_KEY) {
|
|
96
|
+ if (status == Uid) {
|
|
97
|
+ if (key == '#') {
|
|
98
|
+ if (uid.length() != 0) {
|
|
99
|
+ status = Password;
|
|
100
|
+ return true;
|
|
101
|
+ }
|
|
102
|
+ }
|
|
103
|
+ else {
|
|
104
|
+ return handleUidPassword(uid, UID_MAX_LEN, key);
|
|
105
|
+ }
|
|
106
|
+ }
|
|
107
|
+ else if (status == Password) {
|
|
108
|
+ if (key == '#') {
|
|
109
|
+ if (password.length() != 0) {
|
|
110
|
+ login(uid, password);
|
|
111
|
+ return true;
|
|
112
|
+ }
|
|
113
|
+ }
|
|
114
|
+ else {
|
|
115
|
+ return handleUidPassword(password, PWD_MAX_LEN, key);
|
|
116
|
+ }
|
|
117
|
+ }
|
|
118
|
+ }
|
|
119
|
+ return false;
|
|
120
|
+}
|
|
121
|
+
|
|
122
|
+void updateLcd()
|
|
123
|
+{
|
|
124
|
+ Serial.println("Update LCD");
|
|
125
|
+ lcd.clear();
|
|
126
|
+ if (status == Uid || status == Password)
|
|
127
|
+ {
|
|
128
|
+ lcd.setCursor(0, 0);
|
|
129
|
+ lcd.print(UID_LABEL);
|
|
130
|
+ lcd.print(uid);
|
|
131
|
+ lcd.setCursor(0, 1);
|
|
132
|
+ lcd.print(PWD_LABEL);
|
|
133
|
+ lcd.print(makeChars('*', password.length()));
|
|
134
|
+ if (status == Uid) {
|
|
135
|
+ lcd.setCursor(strlen(UID_LABEL) + uid.length(), 0);
|
|
136
|
+ }
|
|
137
|
+ else {
|
|
138
|
+ lcd.setCursor(strlen(PWD_LABEL) + password.length(), 1);
|
|
139
|
+ }
|
|
140
|
+ }
|
|
141
|
+ else if (status == SelfTest) {
|
|
142
|
+ lcd.print(SELF_TEST_LABEL);
|
|
143
|
+ }
|
|
144
|
+}
|
|
145
|
+
|
|
146
|
+void reset()
|
|
147
|
+{
|
|
148
|
+ status = Uid;
|
|
149
|
+ uid = "";
|
|
150
|
+ password = "";
|
|
151
|
+ updateLcd();
|
|
152
|
+}
|
|
153
|
+
|
|
154
|
+void login(String uid, String password)
|
|
155
|
+{
|
|
156
|
+ Serial.println(uid);
|
|
157
|
+ Serial.println(password);
|
|
158
|
+ reset();
|
|
159
|
+}
|
|
160
|
+
|
|
161
|
+void selfTest()
|
|
162
|
+{
|
|
163
|
+ status = SelfTest;
|
|
164
|
+ updateLcd();
|
|
165
|
+ Serial.println("Self Testing");
|
|
166
|
+// while (Serial.available() == 0);
|
|
167
|
+ Serial.read();
|
|
168
|
+ lastSelfTest = millis() / 1000;
|
|
169
|
+ reset();
|
|
170
|
+}
|