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