4180 Final Project

Dependencies:   4DGL-uLCD-SE mbed Servo

Committer:
ecarrick
Date:
Sun Apr 24 21:50:19 2016 +0000
Revision:
17:632c56db3c0a
Parent:
16:fb2a50f0ecde
Child:
18:107fe4158212
LEDs

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