-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
403 lines (343 loc) · 12.4 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
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
/*******************************************************************************
* MSP432 Keypad Solenoid Lockbox Solution *
* *
* Author: Long Tran *
* Device: MSP432P401R LaunchPad *
* Program: Display input to keypad *
* *
* Important Ports: *
* P4 is OUTPUT for 7-seg display digit pattern *
* P8 is OUTPUT to control active digits in row *
* P9 is INPUT from keypad column *
* *
* P2 is OUTPUT to lock solenoid *
* P5 is OUTPUT to red LED (on during lock mode *
* *
* Demo: https://www.youtube.com/watch?v=xvXOEY5Ds3I *
* GitHub: https://github.com/Ltran0325 *
*******************************************************************************/
// Include header file(s) and define constants
#include "msp.h"
#include <stdbool.h>
#define N 100 // debounce loop count
#define BLANK 16 // digit-bit index to clear display
#define OPEN_KEY 10 // open key is 'A' key
#define LOCK_KEY 11 // lock key is 'B' key
#define FIVE_SEC 12000 // 5sec delay
// Define digit-bit lookup table
const uint8_t digit_array[17] = {
0b11000000, // 0
0b11111001, // 1
0b10100100, // 2
0b10110000, // 3
0b10011001, // 4
0b10010010, // 5
0b10000010, // 6
0b11111000, // 7
0b10000000, // 8
0b10010000, // 9
0b10001000, // A
0b10000011, // b
0b11000110, // C
0b10100001, // d
0b11000111, // L
0b11110111, // _
0b11111111, // BLANK
};
// Define 4x4 keypad layout
const uint8_t keypad_table[4][9] = {
{BLANK, OPEN_KEY, 3, BLANK, 2, BLANK, BLANK, BLANK, 1}, // 4x9 multiplixer keypad
{BLANK, LOCK_KEY, 6, BLANK, 5, BLANK, BLANK, BLANK, 4}, // only columns 1, 2, 4, 8 are valid for single keypres
{BLANK, 12, 9, BLANK, 8, BLANK, BLANK, BLANK, 7}, // A is open key
{BLANK, 13, 15, BLANK, 0, BLANK, BLANK, BLANK, 14}}; // B is LOCK key
// Define keypad structure
typedef struct{
enum {IDLE, PRESS, PROCESS, RELEASE} state; // keypad state variable
uint8_t x; // x position of pressed key
uint8_t y; // y position of pressed key
uint8_t display[4]; // array for keeping the last four pressed numbers
uint8_t display_count; // display array index
uint8_t k; // points to active row of keypad
uint32_t pulses; // debouncing pulses
} Keypad;
// Define lockbox structure
typedef struct{
enum {LOCK, SOLENOID, DOWN, NORMAL, PRELOCK} state; // lockbox state variable
uint32_t wait; // wait counter
uint32_t cnt; // password attempts
} Lockbox;
// Define prototypes
void keypad_fsm(Keypad *key);
void lockbox_fsm(Keypad *key, Lockbox *lock);
bool pw_check(uint8_t *arr);
void set_display(uint8_t *arr, uint8_t digit0, uint8_t digit1, uint8_t digit2, uint8_t digit3);
void set_password(uint8_t *arr);
void gpio_init(void);
void solenoid_off(void);
void solenoid_on(void);
void redLED_off(void);
void redLED_on(void);
void redLED_toggle(void);
void wait(uint32_t t);
// Define flags
bool open_flag = 0;
bool lock_flag = 0;
bool keypad_freeze_flag = 0;
uint8_t lock_pass[4] = {1,2,3,4}; // lock password
void main(void)
{
// Disable watchdog and initialize GPIO
WDT_A->CTL = WDT_A_CTL_PW | WDT_A_CTL_HOLD;
gpio_init();
// Initialize structure instances
Keypad key = {IDLE, 0, 0, {15,14,0,12}, 0, 0, 0 };
Lockbox lock = {LOCK, 0, 0};
while(1)
{
keypad_fsm(&key);
lockbox_fsm(&key, &lock);
}
}
// Call keypad FSM as a function
void keypad_fsm(Keypad *key){
static int temp;
// Display digit-k
P4->OUT = 0xFF; // blank display
P8->OUT = 0xFF & ~(BIT5 >> key->k); // enable k-th display
P4->OUT = digit_array[key->display[key->k]]; // display k-th char in array
wait(100);
// Scan for keypad input in row-k if keypad is not frozen
if(!keypad_freeze_flag)
{
temp = (P9->IN) & 0x0F; // scan input at row-k
if(temp > 0 ) // if key press detected,
{
key->x = temp; // acknowledge input x position
key->y = key->k; // acknowledge input y position
}
}
// Increment index-k
(key->k)++;
if (key->k >= 4){key->k = 0;}
// Return if keypad is frozen
if(keypad_freeze_flag){return;}
// Switch keypad debouncing state
switch (key->state){
// Wait for input
case IDLE:
{
if(key->x != 0){
key->state = PRESS;
key->pulses = 0;
}break;
}
// Accept input if N pulses of HIGH detected
case PRESS:
{
// Scan for input where input was previously found
P4->OUT = 0xFF; // blank display
P8->OUT = 0xFF & ~(BIT5 >> key->y); // switch to row where input was prev found
temp = (P9->IN) & 0x0F; // read column input
if(temp == key->x){
key->pulses++; // increment pulse if same input
}else{
key->pulses = 0;
key->state = IDLE; // input fail
}
// Process input if N pulses
if(key->pulses > N){
key->pulses = 0;
key->state = PROCESS; // input success
}break;
}
// Update display array with accepted input
case PROCESS:
{
// set open key flag if open key pressed
if(key->y == 0 && key->x == 1){
open_flag = 1;
key->state = RELEASE;
break;
}
// set lock key flag if lock key pressed
if(key->y == 1 && key->x == 1){
lock_flag = 1;
key->state = RELEASE;
break;
}
// update display if numerical key pressed
key->display[key->display_count] = keypad_table[key->y][key->x];
key->display_count++;
key->state = RELEASE;
if(key->display_count > 3){key->display_count = 0;}
break;
}
// Accept release if N pulses of LOW detected
case RELEASE:
{
// Scan for input where input was previously found
P4->OUT = 0xFF; // blank display
P8->OUT = 0xFF & ~(BIT5 >> key->y); // switch to row where input was prev found
temp = (P9->IN) & 0x0F; // read keypad column input
if(temp == 0){
key->pulses++; // increment pulse if no input
}else{
key->pulses = 0;
key->state = RELEASE; // release fail
}
if(key->pulses > N){
key->pulses = 0;
key->state = IDLE; // release successful
}break;
}
}
}
// Call lockbox FSM as a function
void lockbox_fsm(Keypad *key, Lockbox *lock){
// Switch keypad lockbox debouncing state
switch (lock->state){
// Wait for user to input 4 digits, then compare with password using open key. Red LED on.
case LOCK:
{
// Entering LOCK state:
if(lock->wait == 0){
redLED_on();
set_display(key->display, 15, 14, 0, 12); // display "_LOC"
lock->wait++;
}
// Test input if open key pressed
if(open_flag){
open_flag = 0;
lock->wait = 0;
if(pw_check(key->display)){
lock->cnt = 0;
lock->state = SOLENOID; // password success
redLED_off();
break;
}else{ // if password wrong, increment cnt
lock->cnt += 1;
if(lock->cnt >= 5){
lock->cnt = 0;
lock->state = DOWN; // if wrong 5 times, enter lockdown state
redLED_off();
}
}
}break;
}
// Energize solenoid to open door (5 sec).
case SOLENOID:
{
// Entering SOLENOID state:
solenoid_on();
lock->wait++;
// Return to NORMAL state after 5s
if(lock->wait > FIVE_SEC){
lock->wait = 0;
lock->state = NORMAL;
solenoid_off();
}break;
}
// Freeze keypad for 1 minute, then return to LOCK state
case DOWN:
{
// Entering DOWN state:
set_display(key->display, 15, 14, 13, 15); // Display "_Ld_"
keypad_freeze_flag = 1; // freeze keypad (no input)
lock->wait++;
// Return to LOCK state after 1 minute
if(lock->wait > FIVE_SEC*3){
lock->wait = 0;
lock->state = LOCK;
keypad_freeze_flag = 0;
}break;
}
case NORMAL:
{
// Entering NORMAL state:
if(lock->wait == 0){
set_display(key->display, 0, 0, 0, 0); // "0000"
lock->wait++;
}
// Re-energize lock solenoid if open key is pressed
if(open_flag){
open_flag = 0;
lock->wait = 0;
lock->state = SOLENOID;
}
// Go to PRELOCK state and set new user password
if(lock_flag){
lock_flag = 0;
lock->wait = 0;
set_password(key->display);
lock->state = PRELOCK;
}break;
}
// 5 second period before LOCK state. Blink red LED. Allow user option to return to normal state with locking.
case PRELOCK:
{
if(lock->wait%1000 == 0){ // Blink LED every 1000 loops
redLED_toggle();
}
lock->wait++;
if(key->display_count){ // if input detected, return to NORMAL state
lock->wait = 0;
key->display_count = 0;
lock->state = NORMAL;
redLED_off();
}
if(lock->wait == FIVE_SEC){ // if no input detected in 5s, lock the safe
lock->wait = 0;
lock->state = LOCK;
}break;
}
}
}
// Compare user input to password
bool pw_check(uint8_t *arr ){
int i = 0;
while(i < 4){
if(arr[i] != lock_pass[i]){return 0;}
i++;
}
return 1;
}
void set_password(uint8_t *arr){
lock_pass[0] = *(arr);
lock_pass[1] = *(arr+1);
lock_pass[2] = *(arr+2);
lock_pass[3] = *(arr+3);
}
// Set display array
void set_display(uint8_t *arr, uint8_t digit0, uint8_t digit1, uint8_t digit2, uint8_t digit3){
*(arr) = digit0;
*(arr+1) = digit1;
*(arr+2) = digit2;
*(arr+3) = digit3;
}
void gpio_init(void){
P2->DIR |= BIT5; // P2.5 is solenoid output
P4->DIR = 0xFF; // P4 is 7-Seg LED output
P5->DIR |= BIT0; // P5.0 is lock state LED
P8->DIR = 0xFF; // P8 is display output
P9->DIR = 0x00; // P9 is keypad input
redLED_off();
solenoid_off();
}
void redLED_off(void){
P5->OUT |= BIT0;
}
void redLED_on(void){
P5->OUT &= ~BIT0;
}
void redLED_toggle(void){
P5->OUT ^= BIT0;
}
void solenoid_on(void){
P2->OUT &= ~BIT5;
}
void solenoid_off(void){
P2->OUT |= BIT5;
}
void wait(uint32_t t){
while(t != 0){t--;} // Busy wait
}