4180 Final Project

Dependencies:   4DGL-uLCD-SE mbed Servo

Committer:
mbedTrent
Date:
Sun Apr 24 19:38:28 2016 +0000
Revision:
15:3d43fe8e108b
Parent:
14:dcfef939427d
Child:
16:fb2a50f0ecde
pushed changes from last meeting

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