4180 Final Project

Dependencies:   4DGL-uLCD-SE mbed Servo

Committer:
ecarrick
Date:
Sun Apr 24 21:53:39 2016 +0000
Revision:
18:107fe4158212
Parent:
17:632c56db3c0a
Child:
21:ff7bacd317fc
led fix

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;
mbedTrent 10:cf127bd9b857 119 password.pop_back(); //pop off last value on the vector
mbedTrent 10:cf127bd9b857 120 printPW();//for debugging
mbedTrent 15:3d43fe8e108b 121 pc.printf("container size: %d\r\n", password.size());//for dubugging
mbedTrent 10:cf127bd9b857 122 }
mbedTrent 15:3d43fe8e108b 123 break;
mbedTrent 10:cf127bd9b857 124 }
pshabbaki3 12:22444d5f5920 125
mbedTrent 10:cf127bd9b857 126 //if key 11 (ENTER) is pressed, the password will be submitted IF it is long enough
mbedTrent 10:cf127bd9b857 127 else if ((((value>>i)&0x01)==1)&&(i==11)) {
pshabbaki3 12:22444d5f5920 128
mbedTrent 10:cf127bd9b857 129 //if the password is long enough, check passwords, and clear vector.
mbedTrent 15:3d43fe8e108b 130 if (password.size() >= minLength) {
mbedTrent 10:cf127bd9b857 131 //CALL checkResult() FUNCTION, OR SET A FLAG INDICATING THAT PASSWORD IS COMPLETE AND AVAILABLE TO BE CHECKED!
ecarrick 17:632c56db3c0a 132 ledb = 1;
ecarrick 16:fb2a50f0ecde 133 ans = password;
ecarrick 16:fb2a50f0ecde 134 checkKeyboard();
mbedTrent 15:3d43fe8e108b 135 vector<char> empty;
mbedTrent 15:3d43fe8e108b 136 swap(empty, password);
mbedTrent 10:cf127bd9b857 137 }
pshabbaki3 12:22444d5f5920 138
mbedTrent 10:cf127bd9b857 139 else {
mbedTrent 10:cf127bd9b857 140 pc.printf("The password must be at least %d characters long\r\n", minLength);
mbedTrent 10:cf127bd9b857 141 }
mbedTrent 15:3d43fe8e108b 142 break;
pshabbaki3 12:22444d5f5920 143 }
pshabbaki3 12:22444d5f5920 144
mbedTrent 10:cf127bd9b857 145 //if keys 0 through 9 are pressed
mbedTrent 10:cf127bd9b857 146 else if (((value>>i)&0x01)==1) {
mbedTrent 7:52b51f507ee5 147
mbedTrent 10:cf127bd9b857 148 //if max pw length has not been reached, push onto password vector
mbedTrent 10:cf127bd9b857 149 if (password.size() < maxLength) {
ecarrick 17:632c56db3c0a 150 ledb = 1;
ecarrick 18:107fe4158212 151
ecarrick 17:632c56db3c0a 152
mbedTrent 7:52b51f507ee5 153 key_code=i;
mbedTrent 10:cf127bd9b857 154 password.push_back((char)key_code);
mbedTrent 10:cf127bd9b857 155 printPW(); //for debugging
mbedTrent 15:3d43fe8e108b 156 pc.printf("times: %d\r\n", password.size());
mbedTrent 15:3d43fe8e108b 157
mbedTrent 15:3d43fe8e108b 158 if (password.size() == maxLength) {
mbedTrent 15:3d43fe8e108b 159 //pc.printf("Max password length reached. Press clr to delete character. Press any number key to reset.\r\n");
mbedTrent 15:3d43fe8e108b 160 vector<char> empty;
mbedTrent 15:3d43fe8e108b 161 swap(empty, password);
mbedTrent 15:3d43fe8e108b 162 //password.clear();
mbedTrent 15:3d43fe8e108b 163 }
mbedTrent 15:3d43fe8e108b 164 }
mbedTrent 15:3d43fe8e108b 165
mbedTrent 15:3d43fe8e108b 166 //if max password length has been reached
mbedTrent 15:3d43fe8e108b 167 else {
mbedTrent 15:3d43fe8e108b 168 pc.printf("Maximum password length reached, password has been cleared. \r\n");
mbedTrent 15:3d43fe8e108b 169 password.clear();
mbedTrent 15:3d43fe8e108b 170 }
mbedTrent 15:3d43fe8e108b 171 break;
mbedTrent 15:3d43fe8e108b 172 }
mbedTrent 15:3d43fe8e108b 173
mbedTrent 15:3d43fe8e108b 174 }
ecarrick 18:107fe4158212 175 ledb = 0;
mbedTrent 15:3d43fe8e108b 176
mbedTrent 15:3d43fe8e108b 177 }
mbedTrent 15:3d43fe8e108b 178
ecarrick 16:fb2a50f0ecde 179
ecarrick 16:fb2a50f0ecde 180 void print_to_serial()
ecarrick 16:fb2a50f0ecde 181 {
ecarrick 16:fb2a50f0ecde 182 for(unsigned i=0; i< im.size(); i++) {
ecarrick 16:fb2a50f0ecde 183 pc.printf("%c",im[i]);
ecarrick 16:fb2a50f0ecde 184 }
ecarrick 16:fb2a50f0ecde 185 }
mbedTrent 7:52b51f507ee5 186
pshabbaki3 9:11a6f6c8c54b 187 void save_to_file()
pshabbaki3 9:11a6f6c8c54b 188 {
ecarrick 5:a2fbe5bd2be0 189 FILE * fp = fopen("/local/users.txt", "w");
pshabbaki3 9:11a6f6c8c54b 190 if(fp != NULL) {
pshabbaki3 9:11a6f6c8c54b 191 for(unsigned i=0; i< im.size(); i++) {
ecarrick 6:50a82641d07b 192 fprintf(fp,"%c",im[i]);
ecarrick 5:a2fbe5bd2be0 193 }
ecarrick 5:a2fbe5bd2be0 194 }
ecarrick 5:a2fbe5bd2be0 195 fclose(fp);
ecarrick 5:a2fbe5bd2be0 196 }
ecarrick 5:a2fbe5bd2be0 197
pshabbaki3 9:11a6f6c8c54b 198 void load_from_file()
pshabbaki3 9:11a6f6c8c54b 199 {
ecarrick 5:a2fbe5bd2be0 200 FILE * fp = fopen("/local/users.txt", "r");
pshabbaki3 9:11a6f6c8c54b 201 if(fp != NULL) {
ecarrick 5:a2fbe5bd2be0 202 im.clear();
pshabbaki3 9:11a6f6c8c54b 203 do {
ecarrick 6:50a82641d07b 204 im.push_back(fgetc(fp));
pshabbaki3 9:11a6f6c8c54b 205 } while(im.back() != '#');
ecarrick 5:a2fbe5bd2be0 206 }
ecarrick 5:a2fbe5bd2be0 207 fclose(fp);
ecarrick 5:a2fbe5bd2be0 208 }
pshabbaki3 0:5d35ff3dc9a5 209
pshabbaki3 0:5d35ff3dc9a5 210 int i = 0;
pshabbaki3 0:5d35ff3dc9a5 211
pshabbaki3 0:5d35ff3dc9a5 212
pshabbaki3 0:5d35ff3dc9a5 213
pshabbaki3 12:22444d5f5920 214 //Interrupt
pshabbaki3 12:22444d5f5920 215 void Rx_interrupt()
pshabbaki3 0:5d35ff3dc9a5 216 {
ecarrick 16:fb2a50f0ecde 217 //pc.printf("read in from serial\r\n");
ecarrick 16:fb2a50f0ecde 218 vector<char> empty;
ecarrick 16:fb2a50f0ecde 219 swap(empty, im);
ecarrick 16:fb2a50f0ecde 220 while (1) {
pshabbaki3 0:5d35ff3dc9a5 221 char c = pc.getc();
pshabbaki3 0:5d35ff3dc9a5 222 if (c!='#') {
pshabbaki3 0:5d35ff3dc9a5 223 im.push_back(c); //writing all the characters into vector
ecarrick 16:fb2a50f0ecde 224 //checkKeyboard(im);
pshabbaki3 0:5d35ff3dc9a5 225 } else {
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 }
ecarrick 16:fb2a50f0ecde 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;
ecarrick 17:632c56db3c0a 252
ecarrick 16:fb2a50f0ecde 253
ecarrick 16:fb2a50f0ecde 254
pshabbaki3 12:22444d5f5920 255 interrupt.fall(&fallInterrupt);
pshabbaki3 12:22444d5f5920 256 interrupt.mode(PullUp);
pshabbaki3 12:22444d5f5920 257 //load_from_file();
pshabbaki3 12:22444d5f5920 258
pshabbaki3 12:22444d5f5920 259 //interupt driven read from serial port
pshabbaki3 12:22444d5f5920 260 // calls save_to_file();
pshabbaki3 12:22444d5f5920 261 pc.attach(&Rx_interrupt, Serial::RxIrq);
ecarrick 16:fb2a50f0ecde 262
ecarrick 16:fb2a50f0ecde 263 while(1)
ecarrick 16:fb2a50f0ecde 264 sleep();
pshabbaki3 12:22444d5f5920 265 }
pshabbaki3 12:22444d5f5920 266
pshabbaki3 12:22444d5f5920 267