-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyscanSecurity.cpp
More file actions
executable file
·409 lines (353 loc) · 10.4 KB
/
Copy pathKeyscanSecurity.cpp
File metadata and controls
executable file
·409 lines (353 loc) · 10.4 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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
// ARDUINO MEGA, NOT UNO!
const byte ROWS = 4; // four rows
const byte COLS = 4; // four columns
byte c = 0;
char curChar = 0;
bool keyPressDetected = false;
unsigned long alarmCurWaitTime, alarmLastSentTime;
unsigned long keyStartHoldTime, keyCurHoldTime;
const short armedDetectionTime_ms = 2000;
bool armed = false;
bool alarm = false;
#define _IO_Mem_reg(address) *((volatile byte*) (address))
// B is for the onboard LED
#define DataDirReg_B _IO_Mem_reg(0x24)
#define DataDirReg_D _IO_Mem_reg(0x2A)
#define DataDirReg_H _IO_Mem_reg(0x101)
#define DataDirReg_J _IO_Mem_reg(0x104)
#define PORT_B_REG _IO_Mem_reg(0x25)
// Pins 18-21
#define PORT_D_REG _IO_Mem_reg(0x2B)
// PORT is for outputs. PIN is for reading inputs!
#define PIN_D_REG _IO_Mem_reg(0x29)
// Pins 16-17
#define PORT_H_REG _IO_Mem_reg(0x102)
#define PIN_H_REG _IO_Mem_reg(0x100)
// Pins 14-15
#define PORT_J_REG _IO_Mem_reg(0x105)
#define PIN_J_REG _IO_Mem_reg(0x103)
// The compiler actually doesn't allow these addresses. Interesting
// Must use the predefined macros
// #define Extern_Inp_Ctrl_Reg(0x69)
// #define Extern_Inp_Mask_Reg(0x3D)
char keyMap[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
char silentAlarmCode[] = {'*','8','6','A'};
byte alarmCodeCounter = 0;
/*
MEGA2560 Interrupt pins:
18–21
*/
void setup() {
// DataDirReg_B |= (1 << 7); // Same thing
DataDirReg_B |= 0x80;
// Default LED OFF
PORT_B_REG &= ~0x80;
DataDirReg_D &= ~0x0F;
// Set PD0-3 as 1
PORT_D_REG |= 0x0F;
// Set DDR[H/J] bits 0-1 to 1
DataDirReg_H |= 0x03;
DataDirReg_J |= 0x03;
// Set ports to 0 for Output LOW
PORT_H_REG &= ~0x03;
PORT_J_REG &= ~0x03;
EICRA = 0xAA; // 0b10101010 -> Falling edge for INT0-INT3
// We only want to enable INT0-4, not all 8
EIMSK |= 0x0F;
Serial.begin(9600);
}
ISR(INT0_vect) {
// Serial.println("Something happened!" + (String)c + (String)(3-c));
/*
Set ports to 1 for Output HIGH. Now, the pressed column is
also high, and will only be LOW when the row physically connected
to the column (i.e. where the press/contact is) is driven low
(i.e. we should still see HIGH if we drive a row not connected
to the column to LOW)
*/
c = 0;
curChar = 0;
PORT_H_REG |= 0x03;
PORT_J_REG |= 0x03;
// PORT_J_REG &= ~(1 << 1); // Same thing
PORT_J_REG &= ~0x2;
/*
This was the trickiest part. The logic, without this for
loop, is correct. However, without the for loop, the CPU
actually runs faster than the pin has time to discharge.
The pin seems to discharge at approximately the time it takes
to get to the next row, so it thinks that the preceeding row
was pressed!
This for loop is a busy-wait, but necessary for correct row detection
The reason this works is because PORT_J_REG is a volatile variable.
If any variable that is not marked volatile is used, this loop will
be optimized out by the compiler because the state is determined
and unchanged
*/
for (int i = 0; i < 1000; i++) { PORT_J_REG = PORT_J_REG; }
if ( !((PIN_D_REG >> c) & 0x1) ) {
// 3-c because I want to keep the array mapped with how the
// physical keypad looks; changing the mapping thru software
curChar = keyMap[0][3-c];
PORT_H_REG &= ~0x03;
PORT_J_REG &= ~0x03;
// 2. Clear any pending interrupt flags caused by dropping the rows to LOW!
// Writing 1 to INTF0, INTF1, INTF2, and INTF3 clears them.
// Also, this MUST happen right after driving the lines low; doing it after
// the very next line of code didn't make it work
EIFR = 0x0F;
keyPressDetected = true;
return;
}
PORT_J_REG &= ~0x1;
for (int i = 0; i < 1000; i++) { PORT_J_REG = PORT_J_REG; }
if ( !((PIN_D_REG >> c) & 0x1) ) {
curChar = keyMap[1][3-c];
PORT_H_REG &= ~0x03;
PORT_J_REG &= ~0x03;
EIFR = 0x0F;
keyPressDetected = true;
return;
}
PORT_H_REG &= ~0x2;
for (int i = 0; i < 1000; i++) { PORT_J_REG = PORT_J_REG; }
if ( !((PIN_D_REG >> c) & 0x1) ) {
curChar = keyMap[2][3-c];
PORT_H_REG &= ~0x03;
PORT_J_REG &= ~0x03;
EIFR = 0x0F;
keyPressDetected = true;
return;
}
PORT_H_REG &= ~0x1;
for (int i = 0; i < 1000; i++) { PORT_J_REG = PORT_J_REG; }
if ( !((PIN_D_REG >> c) & 0x1) ) {
curChar = keyMap[3][3-c];
PORT_H_REG &= ~0x03;
PORT_J_REG &= ~0x03;
EIFR = 0x0F;
keyPressDetected = true;
return;
}
// If we're here, somehow we didn't find a valid key. Don't give one
curChar = 0;
}
ISR(INT1_vect) {
// Serial.println("Something happened!" + (String)c + (String)(3-c));
c = 1;
curChar = 0;
PORT_H_REG |= 0x03;
PORT_J_REG |= 0x03;
PORT_J_REG &= ~0x2;
for (int i = 0; i < 1000; i++) { PORT_J_REG = PORT_J_REG; }
if ( !((PIN_D_REG >> c) & 0x1) ) {
curChar = keyMap[0][3-c];
PORT_H_REG &= ~0x03;
PORT_J_REG &= ~0x03;
EIFR = 0x0F;
keyPressDetected = true;
return;
}
PORT_J_REG &= ~0x1;
for (int i = 0; i < 1000; i++) { PORT_J_REG = PORT_J_REG; }
if ( !((PIN_D_REG >> c) & 0x1) ) {
curChar = keyMap[1][3-c];
PORT_H_REG &= ~0x03;
PORT_J_REG &= ~0x03;
EIFR = 0x0F;
keyPressDetected = true;
return;
}
PORT_H_REG &= ~0x2;
for (int i = 0; i < 1000; i++) { PORT_J_REG = PORT_J_REG; }
if ( !((PIN_D_REG >> c) & 0x1) ) {
curChar = keyMap[2][3-c];
PORT_H_REG &= ~0x03;
PORT_J_REG &= ~0x03;
EIFR = 0x0F;
keyPressDetected = true;
return;
}
PORT_H_REG &= ~0x1;
for (int i = 0; i < 1000; i++) { PORT_J_REG = PORT_J_REG; }
if ( !((PIN_D_REG >> c) & 0x1) ) {
curChar = keyMap[3][3-c];
PORT_H_REG &= ~0x03;
PORT_J_REG &= ~0x03;
EIFR = 0x0F;
keyPressDetected = true;
return;
}
curChar = 0;
}
ISR(INT2_vect) {
// Serial.println("Something happened!" + (String)c + (String)(3-c));
c = 2;
curChar = 0;
PORT_H_REG |= 0x03;
PORT_J_REG |= 0x03;
PORT_J_REG &= ~0x2;
for (int i = 0; i < 1000; i++) { PORT_J_REG = PORT_J_REG; }
if ( !((PIN_D_REG >> c) & 0x1) ) {
curChar = keyMap[0][3-c];
PORT_H_REG &= ~0x03;
PORT_J_REG &= ~0x03;
EIFR = 0x0F;
keyPressDetected = true;
return;
}
PORT_J_REG &= ~0x1;
for (int i = 0; i < 1000; i++) { PORT_J_REG = PORT_J_REG; }
if ( !((PIN_D_REG >> c) & 0x1) ) {
curChar = keyMap[1][3-c];
PORT_H_REG &= ~0x03;
PORT_J_REG &= ~0x03;
EIFR = 0x0F;
keyPressDetected = true;
return;
}
PORT_H_REG &= ~0x2;
for (int i = 0; i < 1000; i++) { PORT_J_REG = PORT_J_REG; }
if ( !((PIN_D_REG >> c) & 0x1) ) {
curChar = keyMap[2][3-c];
PORT_H_REG &= ~0x03;
PORT_J_REG &= ~0x03;
EIFR = 0x0F;
keyPressDetected = true;
return;
}
PORT_H_REG &= ~0x1;
for (int i = 0; i < 1000; i++) { PORT_J_REG = PORT_J_REG; }
if ( !((PIN_D_REG >> c) & 0x1) ) {
curChar = keyMap[3][3-c];
PORT_H_REG &= ~0x03;
PORT_J_REG &= ~0x03;
EIFR = 0x0F;
keyPressDetected = true;
return;
}
curChar = 0;
}
ISR(INT3_vect) {
// Serial.println("Something happened!" + (String)c + (String)(3-c));
c = 3;
curChar = 0;
PORT_H_REG |= 0x03;
PORT_J_REG |= 0x03;
PORT_J_REG &= ~0x2;
for (int i = 0; i < 1000; i++) { PORT_J_REG = PORT_J_REG; }
if ( !((PIN_D_REG >> c) & 0x1) ) {
curChar = keyMap[0][3-c];
PORT_H_REG &= ~0x03;
PORT_J_REG &= ~0x03;
EIFR = 0x0F;
keyPressDetected = true;
return;
}
PORT_J_REG &= ~0x1;
for (int i = 0; i < 1000; i++) { PORT_J_REG = PORT_J_REG; }
if ( !((PIN_D_REG >> c) & 0x1) ) {
curChar = keyMap[1][3-c];
PORT_H_REG &= ~0x03;
PORT_J_REG &= ~0x03;
EIFR = 0x0F;
keyPressDetected = true;
return;
}
PORT_H_REG &= ~0x2;
for (int i = 0; i < 1000; i++) { PORT_J_REG = PORT_J_REG; }
if ( !((PIN_D_REG >> c) & 0x1) ) {
curChar = keyMap[2][3-c];
PORT_H_REG &= ~0x03;
PORT_J_REG &= ~0x03;
EIFR = 0x0F;
keyPressDetected = true;
return;
}
PORT_H_REG &= ~0x1;
for (int i = 0; i < 1000; i++) { PORT_J_REG = PORT_J_REG; }
if ( !((PIN_D_REG >> c) & 0x1) ) {
curChar = keyMap[3][3-c];
PORT_H_REG &= ~0x03;
PORT_J_REG &= ~0x03;
EIFR = 0x0F;
keyPressDetected = true;
return;
}
}
void trip_alarm() {
alarmCurWaitTime = millis();
if (alarmCurWaitTime-alarmLastSentTime >= 200) {
Serial.println("ALARM");
PORT_B_REG ^= (1 << 7); // Trigger LED state
alarmLastSentTime = alarmCurWaitTime;
}
// PORT_B_REG |= (1 << 7); // Keep it on at the end
if (curChar == 'D') {
alarm = false;
Serial.println("\nALARM OFF.\n");
// A security system would want redundancy in setting armed states
PORT_B_REG |= 0x80;
armed = true;
Serial.print("BACK TO ARMED STATE\n");
}
}
void loop() {
if (alarm) {
trip_alarm();
}
// If all PULLUP pins are HIGH, no button is being pressed
// This is how we detect holds; much better than timing
if ((PIN_D_REG & 0x0F) == 0x0F) {
keyStartHoldTime = millis();
} else { // Any pin is low, means button is actively pressed
if (!alarm && !armed && curChar == 'A') {
if (millis() - keyStartHoldTime >= armedDetectionTime_ms) {
PORT_B_REG |= 0x80;
armed = true;
Serial.println("\nARMED\n");
// Update the timer so we don't trigger this again until released
keyStartHoldTime = millis();
}
}
}
if (keyPressDetected) { // Key was either pressed or being held down
keyPressDetected = false;
if (alarm) {
if (curChar == 'D') {
alarm = false;
Serial.println("\nALARM OFF.\n");
// A security system would want redundancy in setting armed states
PORT_B_REG |= 0x80;
armed = true;
Serial.print("BACK TO ARMED STATE\n");
}
} else { // Alarm is not on, check for secret security code
if (curChar == silentAlarmCode[alarmCodeCounter]) {
++alarmCodeCounter;
if (curChar == silentAlarmCode[(sizeof(silentAlarmCode)/sizeof(silentAlarmCode[0]))-1]) {
alarm = true;
armed = true;
alarmCodeCounter = 0;
trip_alarm();
}
} else { // Incorrect character in sequence
// For security purposes, always start over, even if the key pressed was the start of the sequence
alarmCodeCounter = 0;
}
}
if (!alarm && armed && curChar == 'C') {
PORT_B_REG &= ~0x80;
armed = false;
Serial.println("\nIDLE\n");
}
// Printing pressed key
PRINT_KEY:
Serial.println(curChar);
}
}