4180 Final Project

Dependencies:   4DGL-uLCD-SE mbed Servo

Committer:
pshabbaki3
Date:
Sun Apr 24 22:01:23 2016 +0000
Revision:
23:e23578000e42
Parent:
22:0dfc46f6ba93
Child:
24:94e8b3e00c24
small fixes

Who changed what in which revision?

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