Trung Nguyen
/
FINAL_PROJECT_4180
asdasdasd
Fork of FINAL_PROJECT_4180 by
main.cpp@12:5cb9ffad1ad7, 2016-05-06 (annotated)
- Committer:
- nyengele
- Date:
- Fri May 06 13:10:20 2016 +0000
- Revision:
- 12:5cb9ffad1ad7
- Parent:
- 11:1d7021c0739d
- Child:
- 13:3d521aa61e6a
fixed magnetic card bug
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nyengele | 0:f0e40bf44ffe | 1 | #include "mbed.h" |
nyengele | 12:5cb9ffad1ad7 | 2 | #include "keypad.h" |
nyengele | 12:5cb9ffad1ad7 | 3 | #include "cryst_lcd.h" |
nyengele | 12:5cb9ffad1ad7 | 4 | #include "FPScanner.h" |
nyengele | 1:0e5e9821d89d | 5 | #include "lib.h" |
nyengele | 12:5cb9ffad1ad7 | 6 | #include <cstring> |
nyengele | 9:48e93bcd1d5c | 7 | |
nyengele | 12:5cb9ffad1ad7 | 8 | Serial mag_card(p9, p10); |
nyengele | 12:5cb9ffad1ad7 | 9 | Serial android(USBTX, USBRX); |
nyengele | 12:5cb9ffad1ad7 | 10 | FPScanner fp(p28, p27); |
nyengele | 12:5cb9ffad1ad7 | 11 | Cryst_LCD lcd(p20, p19, p18, p17, p16, p15, Cryst_LCD::LCD20x4); |
nyengele | 12:5cb9ffad1ad7 | 12 | PwmOut speaker(p21); |
nyengele | 12:5cb9ffad1ad7 | 13 | //Create the keypad ky values |
nyengele | 12:5cb9ffad1ad7 | 14 | char mat[][3] = {{'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}, {'*', '0', '#'}}; |
nyengele | 12:5cb9ffad1ad7 | 15 | // Create the bus for use with the keypad's columns |
nyengele | 12:5cb9ffad1ad7 | 16 | BusInOut colPins(p24, p22, p26); // increasing order (column 1 ....n) |
nyengele | 12:5cb9ffad1ad7 | 17 | // Create the bus for use with the keypad's rows |
nyengele | 12:5cb9ffad1ad7 | 18 | BusInOut rowPins(p23, p30, p29, p25); // increasing order (row 1 ....n) |
nyengele | 12:5cb9ffad1ad7 | 19 | // Create the keypad object |
nyengele | 12:5cb9ffad1ad7 | 20 | Keypad<4, 3> pad(mat, &rowPins, &colPins); |
nyengele | 12:5cb9ffad1ad7 | 21 | LocalFileSystem local_fs("local"); |
nyengele | 12:5cb9ffad1ad7 | 22 | DigitalOut door_lock(p8); |
nyengele | 12:5cb9ffad1ad7 | 23 | DigitalIn sys_reset(p14); |
nyengele | 0:f0e40bf44ffe | 24 | |
nyengele | 12:5cb9ffad1ad7 | 25 | enum STATE {STANDBY, ENROLL, AUTH, FP_ENROLL, FP_AUTH, MC_ENROLL, MC_AUTH, FACE_ENROLL, FACE_AUTH, SPEECH_ENROLL, SPEECH_AUTH, |
nyengele | 12:5cb9ffad1ad7 | 26 | ENROLL_SUCCESS, AUTH_SUCCESS, VOICE_ENROLL, VOICE_AUTH |
nyengele | 11:1d7021c0739d | 27 | }; |
nyengele | 11:1d7021c0739d | 28 | |
nyengele | 11:1d7021c0739d | 29 | char user_id[4]; |
nyengele | 11:1d7021c0739d | 30 | char mag_card_data[250]; |
nyengele | 12:5cb9ffad1ad7 | 31 | char keyboard_input[20]; |
nyengele | 11:1d7021c0739d | 32 | int mc_bytes = 0; |
nyengele | 12:5cb9ffad1ad7 | 33 | char temp_char; |
nyengele | 12:5cb9ffad1ad7 | 34 | int temp_int; |
nyengele | 12:5cb9ffad1ad7 | 35 | bool flag; |
nyengele | 0:f0e40bf44ffe | 36 | |
nyengele | 1:0e5e9821d89d | 37 | int main() |
nyengele | 1:0e5e9821d89d | 38 | { |
nyengele | 12:5cb9ffad1ad7 | 39 | sys_reset.mode(PullUp); |
nyengele | 12:5cb9ffad1ad7 | 40 | if (sys_reset == 0) fp_clearAll(&fp); |
nyengele | 12:5cb9ffad1ad7 | 41 | speaker.period(1.0/440); |
nyengele | 12:5cb9ffad1ad7 | 42 | door_lock = 0; |
nyengele | 11:1d7021c0739d | 43 | STATE current_state = STANDBY; |
nyengele | 11:1d7021c0739d | 44 | while (true) { |
nyengele | 11:1d7021c0739d | 45 | switch (current_state) { |
nyengele | 12:5cb9ffad1ad7 | 46 | case STANDBY: { |
nyengele | 12:5cb9ffad1ad7 | 47 | clear_serial_buffer(&android); |
nyengele | 11:1d7021c0739d | 48 | greeting(&lcd); |
nyengele | 12:5cb9ffad1ad7 | 49 | temp_char = pad.getKeyChar(); |
nyengele | 12:5cb9ffad1ad7 | 50 | while (temp_char != '#' && temp_char != '*') { |
nyengele | 12:5cb9ffad1ad7 | 51 | temp_char = pad.getKeyChar(); |
nyengele | 11:1d7021c0739d | 52 | } |
nyengele | 12:5cb9ffad1ad7 | 53 | if (temp_char == '*') current_state = AUTH; |
nyengele | 12:5cb9ffad1ad7 | 54 | else current_state = ENROLL; |
nyengele | 11:1d7021c0739d | 55 | break; |
nyengele | 12:5cb9ffad1ad7 | 56 | } |
nyengele | 12:5cb9ffad1ad7 | 57 | case ENROLL: { |
nyengele | 12:5cb9ffad1ad7 | 58 | if (read_pin(user_id, 4, &lcd, &pad)) { |
nyengele | 12:5cb9ffad1ad7 | 59 | if (is_valid_user(user_id)) { |
nyengele | 12:5cb9ffad1ad7 | 60 | display_msg(&lcd, "USER ALREADY\nENROLLED."); |
nyengele | 12:5cb9ffad1ad7 | 61 | wait(3); |
nyengele | 12:5cb9ffad1ad7 | 62 | current_state = STANDBY; |
nyengele | 12:5cb9ffad1ad7 | 63 | } else current_state = FACE_ENROLL; |
nyengele | 12:5cb9ffad1ad7 | 64 | } else { |
nyengele | 12:5cb9ffad1ad7 | 65 | display_msg(&lcd, "INVALID PIN."); |
nyengele | 12:5cb9ffad1ad7 | 66 | wait(3); |
nyengele | 12:5cb9ffad1ad7 | 67 | current_state = STANDBY; |
nyengele | 12:5cb9ffad1ad7 | 68 | } |
nyengele | 11:1d7021c0739d | 69 | break; |
nyengele | 12:5cb9ffad1ad7 | 70 | } |
nyengele | 12:5cb9ffad1ad7 | 71 | case FP_ENROLL: { |
nyengele | 12:5cb9ffad1ad7 | 72 | display_msg(&lcd, "PLACE YOUR FINGER\nON SCANNER."); |
nyengele | 11:1d7021c0739d | 73 | if (fp_enroll(&fp)) { |
nyengele | 11:1d7021c0739d | 74 | current_state = MC_ENROLL; |
nyengele | 11:1d7021c0739d | 75 | } else { |
nyengele | 12:5cb9ffad1ad7 | 76 | display_msg(&lcd, "FINGERPRINT FAILED."); |
nyengele | 12:5cb9ffad1ad7 | 77 | wait(3); |
nyengele | 12:5cb9ffad1ad7 | 78 | current_state = STANDBY; |
nyengele | 12:5cb9ffad1ad7 | 79 | } |
nyengele | 12:5cb9ffad1ad7 | 80 | break; |
nyengele | 12:5cb9ffad1ad7 | 81 | } |
nyengele | 12:5cb9ffad1ad7 | 82 | case MC_ENROLL: { |
nyengele | 12:5cb9ffad1ad7 | 83 | flag = false; |
nyengele | 12:5cb9ffad1ad7 | 84 | while (!flag) { |
nyengele | 12:5cb9ffad1ad7 | 85 | memset(mag_card_data, 0, 250); |
nyengele | 12:5cb9ffad1ad7 | 86 | display_msg(&lcd, "SWIPE CARD NOW"); |
nyengele | 12:5cb9ffad1ad7 | 87 | read_mag_card(&mag_card, mag_card_data, &mc_bytes); |
nyengele | 12:5cb9ffad1ad7 | 88 | if (prompt("SUCCESSFUL SWIPE?", &lcd, &pad)) flag = true; |
nyengele | 12:5cb9ffad1ad7 | 89 | } |
nyengele | 12:5cb9ffad1ad7 | 90 | temp_int = hashcode(mag_card_data, mc_bytes); |
nyengele | 12:5cb9ffad1ad7 | 91 | if (write_hash_to_file(user_id, temp_int)) current_state = ENROLL_SUCCESS; |
nyengele | 12:5cb9ffad1ad7 | 92 | else { |
nyengele | 12:5cb9ffad1ad7 | 93 | display_msg(&lcd, "AN ERROR OCCURED."); |
nyengele | 12:5cb9ffad1ad7 | 94 | wait(3); |
nyengele | 12:5cb9ffad1ad7 | 95 | current_state = STANDBY; |
nyengele | 12:5cb9ffad1ad7 | 96 | } |
nyengele | 12:5cb9ffad1ad7 | 97 | break; |
nyengele | 12:5cb9ffad1ad7 | 98 | } |
nyengele | 12:5cb9ffad1ad7 | 99 | case FACE_ENROLL: { |
nyengele | 12:5cb9ffad1ad7 | 100 | display_msg(&lcd, "PROCEED TO\nFACE ENROLLMENT."); |
nyengele | 12:5cb9ffad1ad7 | 101 | wait(2); |
nyengele | 12:5cb9ffad1ad7 | 102 | if (face_enroll(user_id, &android)) { |
nyengele | 12:5cb9ffad1ad7 | 103 | current_state = SPEECH_ENROLL; |
nyengele | 12:5cb9ffad1ad7 | 104 | } else { |
nyengele | 12:5cb9ffad1ad7 | 105 | display_msg(&lcd, "FACE ENROLLMENT\nFAILED."); |
nyengele | 12:5cb9ffad1ad7 | 106 | wait(3); |
nyengele | 12:5cb9ffad1ad7 | 107 | current_state = STANDBY; |
nyengele | 12:5cb9ffad1ad7 | 108 | } |
nyengele | 12:5cb9ffad1ad7 | 109 | break; |
nyengele | 12:5cb9ffad1ad7 | 110 | } |
nyengele | 12:5cb9ffad1ad7 | 111 | case SPEECH_ENROLL: { |
nyengele | 12:5cb9ffad1ad7 | 112 | display_msg(&lcd, "PROCEED TO\nSPEECH ENROLLMENT."); |
nyengele | 12:5cb9ffad1ad7 | 113 | wait(2); |
nyengele | 12:5cb9ffad1ad7 | 114 | if (speech_enroll(user_id, &android)) { |
nyengele | 12:5cb9ffad1ad7 | 115 | current_state = FP_ENROLL; |
nyengele | 12:5cb9ffad1ad7 | 116 | } else { |
nyengele | 12:5cb9ffad1ad7 | 117 | display_msg(&lcd, "SPEECH ENROLLMENT\nFAILED."); |
nyengele | 12:5cb9ffad1ad7 | 118 | wait(3); |
nyengele | 12:5cb9ffad1ad7 | 119 | current_state = STANDBY; |
nyengele | 12:5cb9ffad1ad7 | 120 | } |
nyengele | 12:5cb9ffad1ad7 | 121 | break; |
nyengele | 12:5cb9ffad1ad7 | 122 | } |
nyengele | 12:5cb9ffad1ad7 | 123 | //case VOICE_ENROLL: { |
nyengele | 12:5cb9ffad1ad7 | 124 | // display_msg(&lcd, "PROCEED TO\nVOICE ENROLLMENT."); |
nyengele | 12:5cb9ffad1ad7 | 125 | // wait(3); |
nyengele | 12:5cb9ffad1ad7 | 126 | // if (voice_enroll(user_id, &android)) { |
nyengele | 12:5cb9ffad1ad7 | 127 | // current_state = FP_ENROLL; |
nyengele | 12:5cb9ffad1ad7 | 128 | // } else { |
nyengele | 12:5cb9ffad1ad7 | 129 | // display_msg(&lcd, "VOICE ENROLLMENT\nFAILED."); |
nyengele | 12:5cb9ffad1ad7 | 130 | // wait(3); |
nyengele | 12:5cb9ffad1ad7 | 131 | // current_state = STANDBY; |
nyengele | 12:5cb9ffad1ad7 | 132 | // } |
nyengele | 12:5cb9ffad1ad7 | 133 | // break; |
nyengele | 12:5cb9ffad1ad7 | 134 | // } |
nyengele | 12:5cb9ffad1ad7 | 135 | case ENROLL_SUCCESS: { |
nyengele | 12:5cb9ffad1ad7 | 136 | display_msg(&lcd, "ENROLLMENT\nSUCCESSFUL."); |
nyengele | 12:5cb9ffad1ad7 | 137 | wait(3); |
nyengele | 12:5cb9ffad1ad7 | 138 | current_state = STANDBY; |
nyengele | 12:5cb9ffad1ad7 | 139 | break; |
nyengele | 12:5cb9ffad1ad7 | 140 | } |
nyengele | 12:5cb9ffad1ad7 | 141 | case AUTH: { |
nyengele | 12:5cb9ffad1ad7 | 142 | if (read_pin(user_id, 4, &lcd, &pad) && is_valid_user(user_id)) { |
nyengele | 12:5cb9ffad1ad7 | 143 | current_state = FP_AUTH; |
nyengele | 12:5cb9ffad1ad7 | 144 | } else { |
nyengele | 12:5cb9ffad1ad7 | 145 | display_msg(&lcd, "INVALID PIN."); |
nyengele | 12:5cb9ffad1ad7 | 146 | wait(3); |
nyengele | 12:5cb9ffad1ad7 | 147 | current_state = STANDBY; |
nyengele | 11:1d7021c0739d | 148 | } |
nyengele | 11:1d7021c0739d | 149 | break; |
nyengele | 12:5cb9ffad1ad7 | 150 | } |
nyengele | 12:5cb9ffad1ad7 | 151 | case FP_AUTH: { |
nyengele | 12:5cb9ffad1ad7 | 152 | display_msg(&lcd, "PLACE YOUR FINGER\nON SCANNER."); |
nyengele | 12:5cb9ffad1ad7 | 153 | if (fp_auth(&fp)) { |
nyengele | 12:5cb9ffad1ad7 | 154 | current_state = MC_AUTH; |
nyengele | 12:5cb9ffad1ad7 | 155 | } else { |
nyengele | 12:5cb9ffad1ad7 | 156 | display_msg(&lcd, "FINGERPRINT FAILED."); |
nyengele | 12:5cb9ffad1ad7 | 157 | wait(3); |
nyengele | 12:5cb9ffad1ad7 | 158 | current_state = STANDBY; |
nyengele | 12:5cb9ffad1ad7 | 159 | } |
nyengele | 12:5cb9ffad1ad7 | 160 | break; |
nyengele | 12:5cb9ffad1ad7 | 161 | } |
nyengele | 12:5cb9ffad1ad7 | 162 | case MC_AUTH: { |
nyengele | 12:5cb9ffad1ad7 | 163 | flag = false; |
nyengele | 12:5cb9ffad1ad7 | 164 | while (!flag) { |
nyengele | 12:5cb9ffad1ad7 | 165 | memset(mag_card_data, 0, 250); |
nyengele | 12:5cb9ffad1ad7 | 166 | display_msg(&lcd, "SWIPE CARD NOW"); |
nyengele | 12:5cb9ffad1ad7 | 167 | read_mag_card(&mag_card, mag_card_data, &mc_bytes); |
nyengele | 12:5cb9ffad1ad7 | 168 | if (prompt("SUCCESSFUL SWIPE?", &lcd, &pad)) flag = true; |
nyengele | 12:5cb9ffad1ad7 | 169 | } |
nyengele | 12:5cb9ffad1ad7 | 170 | temp_int = hashcode(mag_card_data, mc_bytes); |
nyengele | 12:5cb9ffad1ad7 | 171 | int saved_hash = 0; |
nyengele | 12:5cb9ffad1ad7 | 172 | if (read_hash_from_file(user_id, &saved_hash) && temp_int == saved_hash) { |
nyengele | 12:5cb9ffad1ad7 | 173 | current_state = FACE_AUTH; |
nyengele | 12:5cb9ffad1ad7 | 174 | } else { |
nyengele | 12:5cb9ffad1ad7 | 175 | display_msg(&lcd, "AUTH FAILED."); |
nyengele | 12:5cb9ffad1ad7 | 176 | wait(3); |
nyengele | 12:5cb9ffad1ad7 | 177 | current_state = STANDBY; |
nyengele | 12:5cb9ffad1ad7 | 178 | } |
nyengele | 11:1d7021c0739d | 179 | break; |
nyengele | 12:5cb9ffad1ad7 | 180 | } |
nyengele | 12:5cb9ffad1ad7 | 181 | case FACE_AUTH: { |
nyengele | 12:5cb9ffad1ad7 | 182 | display_msg(&lcd, "PROCEED TO FACE\nAUTHENTICATION."); |
nyengele | 12:5cb9ffad1ad7 | 183 | wait(2); |
nyengele | 12:5cb9ffad1ad7 | 184 | if (face_auth(user_id, &android)) { |
nyengele | 12:5cb9ffad1ad7 | 185 | current_state = SPEECH_AUTH; |
nyengele | 12:5cb9ffad1ad7 | 186 | } else { |
nyengele | 12:5cb9ffad1ad7 | 187 | display_msg(&lcd, "FACE AUTH FAILED."); |
nyengele | 12:5cb9ffad1ad7 | 188 | wait(3); |
nyengele | 12:5cb9ffad1ad7 | 189 | current_state = STANDBY; |
nyengele | 12:5cb9ffad1ad7 | 190 | } |
nyengele | 12:5cb9ffad1ad7 | 191 | break; |
nyengele | 12:5cb9ffad1ad7 | 192 | } |
nyengele | 12:5cb9ffad1ad7 | 193 | case SPEECH_AUTH: { |
nyengele | 12:5cb9ffad1ad7 | 194 | display_msg(&lcd, "PROCEED TO SPEECH\nAUTHENTICATION."); |
nyengele | 12:5cb9ffad1ad7 | 195 | wait(2); |
nyengele | 12:5cb9ffad1ad7 | 196 | if (speech_auth(user_id, &android)) { |
nyengele | 12:5cb9ffad1ad7 | 197 | current_state = AUTH_SUCCESS; |
nyengele | 12:5cb9ffad1ad7 | 198 | } else { |
nyengele | 12:5cb9ffad1ad7 | 199 | display_msg(&lcd, "SPEECH AUTH FAILED."); |
nyengele | 12:5cb9ffad1ad7 | 200 | wait(3); |
nyengele | 12:5cb9ffad1ad7 | 201 | current_state = STANDBY; |
nyengele | 12:5cb9ffad1ad7 | 202 | } |
nyengele | 11:1d7021c0739d | 203 | break; |
nyengele | 12:5cb9ffad1ad7 | 204 | } |
nyengele | 12:5cb9ffad1ad7 | 205 | //case VOICE_AUTH: { |
nyengele | 12:5cb9ffad1ad7 | 206 | // display_msg(&lcd, "PROCEED TO VOICE\nAUTHENTICATION."); |
nyengele | 12:5cb9ffad1ad7 | 207 | // wait(2); |
nyengele | 12:5cb9ffad1ad7 | 208 | // if (voice_auth(user_id, &android)) { |
nyengele | 12:5cb9ffad1ad7 | 209 | // current_state = AUTH_SUCCESS; |
nyengele | 12:5cb9ffad1ad7 | 210 | // } else { |
nyengele | 12:5cb9ffad1ad7 | 211 | // display_msg(&lcd, "VOICE AUTH FAILED."); |
nyengele | 12:5cb9ffad1ad7 | 212 | // wait(3); |
nyengele | 12:5cb9ffad1ad7 | 213 | // current_state = STANDBY; |
nyengele | 12:5cb9ffad1ad7 | 214 | // } |
nyengele | 12:5cb9ffad1ad7 | 215 | // break; |
nyengele | 12:5cb9ffad1ad7 | 216 | // } |
nyengele | 12:5cb9ffad1ad7 | 217 | case AUTH_SUCCESS: { |
nyengele | 12:5cb9ffad1ad7 | 218 | speaker = 0.5; |
nyengele | 12:5cb9ffad1ad7 | 219 | door_lock = 1; |
nyengele | 12:5cb9ffad1ad7 | 220 | display_msg(&lcd, "ACCESS GRANTED!"); |
nyengele | 12:5cb9ffad1ad7 | 221 | wait(3); |
nyengele | 12:5cb9ffad1ad7 | 222 | door_lock = 0; |
nyengele | 12:5cb9ffad1ad7 | 223 | speaker = 0; |
nyengele | 11:1d7021c0739d | 224 | current_state = STANDBY; |
nyengele | 12:5cb9ffad1ad7 | 225 | memset(user_id, 0, 4); |
nyengele | 12:5cb9ffad1ad7 | 226 | } |
nyengele | 11:1d7021c0739d | 227 | } |
nyengele | 11:1d7021c0739d | 228 | } |
nyengele | 0:f0e40bf44ffe | 229 | } |