4180 Final Project
Dependencies: 4DGL-uLCD-SE mbed Servo
main.cpp@19:6fce7b88de5e, 2016-04-24 (annotated)
- Committer:
- pshabbaki3
- Date:
- Sun Apr 24 21:29:25 2016 +0000
- Revision:
- 19:6fce7b88de5e
- Parent:
- 16:fb2a50f0ecde
- Child:
- 20:2f0d29b66d37
checkResult
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pshabbaki3 | 0:5d35ff3dc9a5 | 1 | #include "mbed.h" |
ecarrick | 6:50a82641d07b | 2 | #include "wifi.h" |
pshabbaki3 | 0:5d35ff3dc9a5 | 3 | #include <string> |
pshabbaki3 | 0:5d35ff3dc9a5 | 4 | #include <vector> |
pshabbaki3 | 0:5d35ff3dc9a5 | 5 | #include <iostream> |
pshabbaki3 | 0:5d35ff3dc9a5 | 6 | #include <sstream> |
pshabbaki3 | 0:5d35ff3dc9a5 | 7 | #include <iterator> |
pshabbaki3 | 0:5d35ff3dc9a5 | 8 | #include <stdio.h> |
pshabbaki3 | 0:5d35ff3dc9a5 | 9 | #include <ctype.h> |
pshabbaki3 | 0:5d35ff3dc9a5 | 10 | #include "uLCD_4DGL.h" |
pshabbaki3 | 0:5d35ff3dc9a5 | 11 | |
mbedTrent | 7:52b51f507ee5 | 12 | #include <list> |
mbedTrent | 7:52b51f507ee5 | 13 | #include <mpr121.h> |
mbedTrent | 7:52b51f507ee5 | 14 | |
ecarrick | 1:dd048edb2716 | 15 | LocalFileSystem local("local"); |
pshabbaki3 | 0:5d35ff3dc9a5 | 16 | |
pshabbaki3 | 0:5d35ff3dc9a5 | 17 | DigitalOut myled(LED1); |
pshabbaki3 | 0:5d35ff3dc9a5 | 18 | Serial pc(USBTX,USBRX); |
ecarrick | 16:fb2a50f0ecde | 19 | //uLCD_4DGL lcd(p28,p27,p30); // serial tx, serial rx, reset pin; |
pshabbaki3 | 0:5d35ff3dc9a5 | 20 | |
mbedTrent | 7:52b51f507ee5 | 21 | // Create the interrupt receiver object on pin 26 |
mbedTrent | 7:52b51f507ee5 | 22 | InterruptIn interrupt(p26); |
mbedTrent | 7:52b51f507ee5 | 23 | |
mbedTrent | 7:52b51f507ee5 | 24 | // Setup the i2c bus on pins 28 and 27 |
mbedTrent | 7:52b51f507ee5 | 25 | I2C i2c(p9, p10); |
mbedTrent | 7:52b51f507ee5 | 26 | |
mbedTrent | 7:52b51f507ee5 | 27 | // Setup the Mpr121: |
mbedTrent | 7:52b51f507ee5 | 28 | // constructor(i2c object, i2c address of the mpr121) |
mbedTrent | 7:52b51f507ee5 | 29 | Mpr121 mpr121(&i2c, Mpr121::ADD_VSS); |
mbedTrent | 7:52b51f507ee5 | 30 | |
pshabbaki3 | 0:5d35ff3dc9a5 | 31 | |
pshabbaki3 | 0:5d35ff3dc9a5 | 32 | vector<char> im; |
pshabbaki3 | 4:e6b198a22216 | 33 | vector<char> pass; |
pshabbaki3 | 0:5d35ff3dc9a5 | 34 | vector<char> name; |
pshabbaki3 | 0:5d35ff3dc9a5 | 35 | vector<char> final; |
pshabbaki3 | 11:1ca1f07896b0 | 36 | vector<char> ans; |
pshabbaki3 | 0:5d35ff3dc9a5 | 37 | |
mbedTrent | 10:cf127bd9b857 | 38 | vector<char> password; |
mbedTrent | 15:3d43fe8e108b | 39 | unsigned maxLength = 18; //random length chosen |
mbedTrent | 15:3d43fe8e108b | 40 | unsigned minLength = 6; //random length chosen |
mbedTrent | 10:cf127bd9b857 | 41 | |
pshabbaki3 | 12:22444d5f5920 | 42 | |
mbedTrent | 10:cf127bd9b857 | 43 | //function for debugging |
pshabbaki3 | 12:22444d5f5920 | 44 | void printPW() |
pshabbaki3 | 12:22444d5f5920 | 45 | { |
mbedTrent | 10:cf127bd9b857 | 46 | for (int i=0; i<password.size(); i++) { |
mbedTrent | 10:cf127bd9b857 | 47 | pc.printf("%d", int(password[i])); |
mbedTrent | 10:cf127bd9b857 | 48 | } |
mbedTrent | 10:cf127bd9b857 | 49 | pc.printf("\r\n"); |
mbedTrent | 10:cf127bd9b857 | 50 | } |
mbedTrent | 7:52b51f507ee5 | 51 | |
ecarrick | 16:fb2a50f0ecde | 52 | |
ecarrick | 16:fb2a50f0ecde | 53 | void checkResult(vector<char> pass, vector<char> name) |
ecarrick | 16:fb2a50f0ecde | 54 | { |
ecarrick | 16:fb2a50f0ecde | 55 | // This function will check the keypad values and check if the passcode exists or not |
ecarrick | 16:fb2a50f0ecde | 56 | |
ecarrick | 16:fb2a50f0ecde | 57 | if (pass.size()==6) { |
ecarrick | 16:fb2a50f0ecde | 58 | //i++; //use "i" to find which person logged in. |
ecarrick | 16:fb2a50f0ecde | 59 | if (ans == pass) { |
ecarrick | 16:fb2a50f0ecde | 60 | pc.printf("Found"); |
ecarrick | 16:fb2a50f0ecde | 61 | final = name; |
ecarrick | 16:fb2a50f0ecde | 62 | final.push_back(' '); |
ecarrick | 16:fb2a50f0ecde | 63 | final.insert(final.end(),pass.begin(),pass.end()); |
ecarrick | 16:fb2a50f0ecde | 64 | |
ecarrick | 16:fb2a50f0ecde | 65 | } else { |
ecarrick | 16:fb2a50f0ecde | 66 | pc.printf("not found"); |
ecarrick | 16:fb2a50f0ecde | 67 | //lcd.printf("\nnot found\n"); |
ecarrick | 16:fb2a50f0ecde | 68 | final.clear(); |
ecarrick | 16:fb2a50f0ecde | 69 | } |
ecarrick | 16:fb2a50f0ecde | 70 | } |
ecarrick | 16:fb2a50f0ecde | 71 | } |
ecarrick | 16:fb2a50f0ecde | 72 | |
ecarrick | 16:fb2a50f0ecde | 73 | |
ecarrick | 16:fb2a50f0ecde | 74 | void checkKeyboard() |
ecarrick | 16:fb2a50f0ecde | 75 | { |
ecarrick | 16:fb2a50f0ecde | 76 | pc.printf("CHECKING PASSWORD\n\r"); |
pshabbaki3 | 19:6fce7b88de5e | 77 | bool colon = false; |
ecarrick | 16:fb2a50f0ecde | 78 | for (int i = 0; i<im.size(); i++) { |
pshabbaki3 | 19:6fce7b88de5e | 79 | |
ecarrick | 16:fb2a50f0ecde | 80 | // find space |
ecarrick | 16:fb2a50f0ecde | 81 | if (im[i] ==' ') { |
ecarrick | 16:fb2a50f0ecde | 82 | checkResult(pass,name); |
pshabbaki3 | 19:6fce7b88de5e | 83 | colon = false; |
ecarrick | 16:fb2a50f0ecde | 84 | pass.clear(); |
ecarrick | 16:fb2a50f0ecde | 85 | name.clear(); |
pshabbaki3 | 19:6fce7b88de5e | 86 | |
pshabbaki3 | 19:6fce7b88de5e | 87 | } else if (im[i] == ':') { |
pshabbaki3 | 19:6fce7b88de5e | 88 | colon = true; |
ecarrick | 16:fb2a50f0ecde | 89 | } else { |
pshabbaki3 | 19:6fce7b88de5e | 90 | if(colon) |
ecarrick | 16:fb2a50f0ecde | 91 | pass.push_back(im[i]); |
pshabbaki3 | 19:6fce7b88de5e | 92 | else |
ecarrick | 16:fb2a50f0ecde | 93 | name.push_back(im[i]); |
ecarrick | 16:fb2a50f0ecde | 94 | } |
ecarrick | 16:fb2a50f0ecde | 95 | } |
ecarrick | 16:fb2a50f0ecde | 96 | } |
ecarrick | 16:fb2a50f0ecde | 97 | |
pshabbaki3 | 9:11a6f6c8c54b | 98 | void fallInterrupt() |
pshabbaki3 | 9:11a6f6c8c54b | 99 | { |
mbedTrent | 7:52b51f507ee5 | 100 | int key_code=0; |
mbedTrent | 7:52b51f507ee5 | 101 | int i=0; |
mbedTrent | 7:52b51f507ee5 | 102 | int value=mpr121.read(0x00); |
mbedTrent | 7:52b51f507ee5 | 103 | value +=mpr121.read(0x01)<<8; |
mbedTrent | 10:cf127bd9b857 | 104 | |
mbedTrent | 10:cf127bd9b857 | 105 | //checks every key for press |
mbedTrent | 7:52b51f507ee5 | 106 | for (i=0; i<=11; i++) { |
pshabbaki3 | 12:22444d5f5920 | 107 | |
mbedTrent | 10:cf127bd9b857 | 108 | //if key 10 (clr) is pressed, clear the last value on the vector will be popped off |
mbedTrent | 10:cf127bd9b857 | 109 | if ((((value>>i)&0x01)==1)&&(i==10)) { |
mbedTrent | 10:cf127bd9b857 | 110 | if (!password.empty()) { |
mbedTrent | 10:cf127bd9b857 | 111 | password.pop_back(); //pop off last value on the vector |
mbedTrent | 10:cf127bd9b857 | 112 | printPW();//for debugging |
mbedTrent | 15:3d43fe8e108b | 113 | pc.printf("container size: %d\r\n", password.size());//for dubugging |
mbedTrent | 10:cf127bd9b857 | 114 | } |
mbedTrent | 15:3d43fe8e108b | 115 | break; |
mbedTrent | 10:cf127bd9b857 | 116 | } |
pshabbaki3 | 12:22444d5f5920 | 117 | |
mbedTrent | 10:cf127bd9b857 | 118 | //if key 11 (ENTER) is pressed, the password will be submitted IF it is long enough |
mbedTrent | 10:cf127bd9b857 | 119 | else if ((((value>>i)&0x01)==1)&&(i==11)) { |
pshabbaki3 | 12:22444d5f5920 | 120 | |
mbedTrent | 10:cf127bd9b857 | 121 | //if the password is long enough, check passwords, and clear vector. |
mbedTrent | 15:3d43fe8e108b | 122 | if (password.size() >= minLength) { |
mbedTrent | 10:cf127bd9b857 | 123 | //CALL checkResult() FUNCTION, OR SET A FLAG INDICATING THAT PASSWORD IS COMPLETE AND AVAILABLE TO BE CHECKED! |
ecarrick | 16:fb2a50f0ecde | 124 | ans = password; |
ecarrick | 16:fb2a50f0ecde | 125 | checkKeyboard(); |
mbedTrent | 15:3d43fe8e108b | 126 | vector<char> empty; |
mbedTrent | 15:3d43fe8e108b | 127 | swap(empty, password); |
mbedTrent | 10:cf127bd9b857 | 128 | } |
pshabbaki3 | 12:22444d5f5920 | 129 | |
mbedTrent | 10:cf127bd9b857 | 130 | else { |
mbedTrent | 10:cf127bd9b857 | 131 | pc.printf("The password must be at least %d characters long\r\n", minLength); |
mbedTrent | 10:cf127bd9b857 | 132 | } |
mbedTrent | 15:3d43fe8e108b | 133 | break; |
pshabbaki3 | 12:22444d5f5920 | 134 | } |
pshabbaki3 | 12:22444d5f5920 | 135 | |
mbedTrent | 10:cf127bd9b857 | 136 | //if keys 0 through 9 are pressed |
mbedTrent | 10:cf127bd9b857 | 137 | else if (((value>>i)&0x01)==1) { |
mbedTrent | 7:52b51f507ee5 | 138 | |
mbedTrent | 10:cf127bd9b857 | 139 | //if max pw length has not been reached, push onto password vector |
mbedTrent | 10:cf127bd9b857 | 140 | if (password.size() < maxLength) { |
mbedTrent | 7:52b51f507ee5 | 141 | key_code=i; |
mbedTrent | 10:cf127bd9b857 | 142 | password.push_back((char)key_code); |
mbedTrent | 10:cf127bd9b857 | 143 | printPW(); //for debugging |
mbedTrent | 15:3d43fe8e108b | 144 | pc.printf("times: %d\r\n", password.size()); |
mbedTrent | 15:3d43fe8e108b | 145 | |
mbedTrent | 15:3d43fe8e108b | 146 | if (password.size() == maxLength) { |
mbedTrent | 15:3d43fe8e108b | 147 | //pc.printf("Max password length reached. Press clr to delete character. Press any number key to reset.\r\n"); |
mbedTrent | 15:3d43fe8e108b | 148 | vector<char> empty; |
mbedTrent | 15:3d43fe8e108b | 149 | swap(empty, password); |
mbedTrent | 15:3d43fe8e108b | 150 | //password.clear(); |
mbedTrent | 15:3d43fe8e108b | 151 | } |
mbedTrent | 15:3d43fe8e108b | 152 | } |
mbedTrent | 15:3d43fe8e108b | 153 | |
mbedTrent | 15:3d43fe8e108b | 154 | //if max password length has been reached |
mbedTrent | 15:3d43fe8e108b | 155 | else { |
mbedTrent | 15:3d43fe8e108b | 156 | pc.printf("Maximum password length reached, password has been cleared. \r\n"); |
mbedTrent | 15:3d43fe8e108b | 157 | password.clear(); |
mbedTrent | 15:3d43fe8e108b | 158 | } |
mbedTrent | 15:3d43fe8e108b | 159 | break; |
mbedTrent | 15:3d43fe8e108b | 160 | } |
mbedTrent | 15:3d43fe8e108b | 161 | |
mbedTrent | 15:3d43fe8e108b | 162 | } |
mbedTrent | 15:3d43fe8e108b | 163 | |
mbedTrent | 15:3d43fe8e108b | 164 | } |
ecarrick | 16:fb2a50f0ecde | 165 | /* |
mbedTrent | 15:3d43fe8e108b | 166 | //if keys 0 through 9 are pressed |
mbedTrent | 15:3d43fe8e108b | 167 | else if (((value>>i)&0x01)==1) { |
mbedTrent | 15:3d43fe8e108b | 168 | |
mbedTrent | 15:3d43fe8e108b | 169 | //if max pw length has not been reached, push onto password vector |
mbedTrent | 15:3d43fe8e108b | 170 | if (password.size() < maxLength) { |
mbedTrent | 15:3d43fe8e108b | 171 | key_code=i; |
mbedTrent | 15:3d43fe8e108b | 172 | password.push_back((char)key_code); |
mbedTrent | 15:3d43fe8e108b | 173 | printPW(); //for debugging |
pshabbaki3 | 12:22444d5f5920 | 174 | } |
pshabbaki3 | 12:22444d5f5920 | 175 | |
mbedTrent | 10:cf127bd9b857 | 176 | //if max password length has been reached |
mbedTrent | 10:cf127bd9b857 | 177 | else { |
pshabbaki3 | 12:22444d5f5920 | 178 | pc.printf("Maximum password length reached.\r\n"); |
mbedTrent | 7:52b51f507ee5 | 179 | } |
mbedTrent | 7:52b51f507ee5 | 180 | } |
mbedTrent | 7:52b51f507ee5 | 181 | } |
mbedTrent | 7:52b51f507ee5 | 182 | } |
ecarrick | 16:fb2a50f0ecde | 183 | */ |
ecarrick | 16:fb2a50f0ecde | 184 | |
ecarrick | 16:fb2a50f0ecde | 185 | void print_to_serial() |
ecarrick | 16:fb2a50f0ecde | 186 | { |
ecarrick | 16:fb2a50f0ecde | 187 | for(unsigned i=0; i< im.size(); i++) { |
ecarrick | 16:fb2a50f0ecde | 188 | pc.printf("%c",im[i]); |
ecarrick | 16:fb2a50f0ecde | 189 | } |
ecarrick | 16:fb2a50f0ecde | 190 | } |
mbedTrent | 7:52b51f507ee5 | 191 | |
pshabbaki3 | 9:11a6f6c8c54b | 192 | void save_to_file() |
pshabbaki3 | 9:11a6f6c8c54b | 193 | { |
ecarrick | 5:a2fbe5bd2be0 | 194 | FILE * fp = fopen("/local/users.txt", "w"); |
pshabbaki3 | 9:11a6f6c8c54b | 195 | if(fp != NULL) { |
pshabbaki3 | 9:11a6f6c8c54b | 196 | for(unsigned i=0; i< im.size(); i++) { |
ecarrick | 6:50a82641d07b | 197 | fprintf(fp,"%c",im[i]); |
ecarrick | 5:a2fbe5bd2be0 | 198 | } |
ecarrick | 5:a2fbe5bd2be0 | 199 | } |
ecarrick | 5:a2fbe5bd2be0 | 200 | fclose(fp); |
ecarrick | 5:a2fbe5bd2be0 | 201 | } |
ecarrick | 5:a2fbe5bd2be0 | 202 | |
pshabbaki3 | 9:11a6f6c8c54b | 203 | void load_from_file() |
pshabbaki3 | 9:11a6f6c8c54b | 204 | { |
ecarrick | 5:a2fbe5bd2be0 | 205 | FILE * fp = fopen("/local/users.txt", "r"); |
pshabbaki3 | 9:11a6f6c8c54b | 206 | if(fp != NULL) { |
ecarrick | 5:a2fbe5bd2be0 | 207 | im.clear(); |
pshabbaki3 | 9:11a6f6c8c54b | 208 | do { |
ecarrick | 6:50a82641d07b | 209 | im.push_back(fgetc(fp)); |
pshabbaki3 | 9:11a6f6c8c54b | 210 | } while(im.back() != '#'); |
ecarrick | 5:a2fbe5bd2be0 | 211 | } |
ecarrick | 5:a2fbe5bd2be0 | 212 | fclose(fp); |
ecarrick | 5:a2fbe5bd2be0 | 213 | } |
pshabbaki3 | 0:5d35ff3dc9a5 | 214 | |
pshabbaki3 | 0:5d35ff3dc9a5 | 215 | int i = 0; |
pshabbaki3 | 0:5d35ff3dc9a5 | 216 | |
pshabbaki3 | 0:5d35ff3dc9a5 | 217 | |
pshabbaki3 | 0:5d35ff3dc9a5 | 218 | |
pshabbaki3 | 12:22444d5f5920 | 219 | //Interrupt |
pshabbaki3 | 12:22444d5f5920 | 220 | void Rx_interrupt() |
pshabbaki3 | 0:5d35ff3dc9a5 | 221 | { |
ecarrick | 16:fb2a50f0ecde | 222 | //pc.printf("read in from serial\r\n"); |
ecarrick | 16:fb2a50f0ecde | 223 | vector<char> empty; |
ecarrick | 16:fb2a50f0ecde | 224 | swap(empty, im); |
ecarrick | 16:fb2a50f0ecde | 225 | while (1) { |
pshabbaki3 | 0:5d35ff3dc9a5 | 226 | char c = pc.getc(); |
pshabbaki3 | 0:5d35ff3dc9a5 | 227 | if (c!='#') { |
pshabbaki3 | 0:5d35ff3dc9a5 | 228 | im.push_back(c); //writing all the characters into vector |
ecarrick | 16:fb2a50f0ecde | 229 | //checkKeyboard(im); |
pshabbaki3 | 0:5d35ff3dc9a5 | 230 | } else { |
pshabbaki3 | 0:5d35ff3dc9a5 | 231 | for (int cnt = 0; cnt<im.size(); cnt++) { |
pshabbaki3 | 0:5d35ff3dc9a5 | 232 | pc.printf("%c",im[cnt]); |
pshabbaki3 | 4:e6b198a22216 | 233 | //lcd.printf("%c",im[cnt]); |
pshabbaki3 | 0:5d35ff3dc9a5 | 234 | } |
ecarrick | 16:fb2a50f0ecde | 235 | break; |
pshabbaki3 | 0:5d35ff3dc9a5 | 236 | } |
pshabbaki3 | 12:22444d5f5920 | 237 | |
pshabbaki3 | 0:5d35ff3dc9a5 | 238 | } |
pshabbaki3 | 19:6fce7b88de5e | 239 | |
pshabbaki3 | 12:22444d5f5920 | 240 | return; |
pshabbaki3 | 0:5d35ff3dc9a5 | 241 | } |
pshabbaki3 | 0:5d35ff3dc9a5 | 242 | |
pshabbaki3 | 0:5d35ff3dc9a5 | 243 | |
pshabbaki3 | 12:22444d5f5920 | 244 | int main() |
pshabbaki3 | 12:22444d5f5920 | 245 | { |
ecarrick | 16:fb2a50f0ecde | 246 | pc.printf("MBED RESET\r\n"); |
ecarrick | 16:fb2a50f0ecde | 247 | print_to_serial(); |
pshabbaki3 | 19:6fce7b88de5e | 248 | |
ecarrick | 16:fb2a50f0ecde | 249 | |
pshabbaki3 | 12:22444d5f5920 | 250 | interrupt.fall(&fallInterrupt); |
pshabbaki3 | 12:22444d5f5920 | 251 | interrupt.mode(PullUp); |
pshabbaki3 | 12:22444d5f5920 | 252 | //load_from_file(); |
pshabbaki3 | 12:22444d5f5920 | 253 | |
pshabbaki3 | 12:22444d5f5920 | 254 | //interupt driven read from serial port |
pshabbaki3 | 12:22444d5f5920 | 255 | // calls save_to_file(); |
pshabbaki3 | 12:22444d5f5920 | 256 | pc.attach(&Rx_interrupt, Serial::RxIrq); |
ecarrick | 16:fb2a50f0ecde | 257 | |
ecarrick | 16:fb2a50f0ecde | 258 | while(1) |
ecarrick | 16:fb2a50f0ecde | 259 | sleep(); |
pshabbaki3 | 12:22444d5f5920 | 260 | } |
pshabbaki3 | 12:22444d5f5920 | 261 | |
pshabbaki3 | 12:22444d5f5920 | 262 |