4180 Final Project

Dependencies:   4DGL-uLCD-SE mbed Servo

Committer:
mbedTrent
Date:
Thu Apr 21 13:26:51 2016 +0000
Revision:
10:cf127bd9b857
Parent:
9:11a6f6c8c54b
Child:
11:1ca1f07896b0
Changed password entry code to include arbitrary size password of at least minLength and up to maxLength. Using vector.push_back/pop_back method. Some debugging print statements are still left. All code together not currently working?

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 0:5d35ff3dc9a5 36
mbedTrent 10:cf127bd9b857 37 vector<char> password;
mbedTrent 7:52b51f507ee5 38 int index=0;
mbedTrent 10:cf127bd9b857 39 int maxLength = 18; //random length chosen
mbedTrent 10:cf127bd9b857 40 int minLength = 5; //random length chosen
mbedTrent 10:cf127bd9b857 41
mbedTrent 10:cf127bd9b857 42 //function for debugging
mbedTrent 10:cf127bd9b857 43 void printPW() {
mbedTrent 10:cf127bd9b857 44 for (int i=0; i<password.size(); i++) {
mbedTrent 10:cf127bd9b857 45 pc.printf("%d", int(password[i]));
mbedTrent 10:cf127bd9b857 46 }
mbedTrent 10:cf127bd9b857 47 pc.printf("\r\n");
mbedTrent 10:cf127bd9b857 48 }
mbedTrent 7:52b51f507ee5 49
pshabbaki3 9:11a6f6c8c54b 50 void fallInterrupt()
pshabbaki3 9:11a6f6c8c54b 51 {
mbedTrent 7:52b51f507ee5 52 int key_code=0;
mbedTrent 7:52b51f507ee5 53 int i=0;
mbedTrent 7:52b51f507ee5 54 int value=mpr121.read(0x00);
mbedTrent 7:52b51f507ee5 55 value +=mpr121.read(0x01)<<8;
mbedTrent 10:cf127bd9b857 56
mbedTrent 10:cf127bd9b857 57 //checks every key for press
mbedTrent 7:52b51f507ee5 58 for (i=0; i<=11; i++) {
mbedTrent 10:cf127bd9b857 59
mbedTrent 10:cf127bd9b857 60 //if key 10 (clr) is pressed, clear the last value on the vector will be popped off
mbedTrent 10:cf127bd9b857 61 if ((((value>>i)&0x01)==1)&&(i==10)) {
mbedTrent 10:cf127bd9b857 62 if (!password.empty()) {
mbedTrent 10:cf127bd9b857 63 password.pop_back(); //pop off last value on the vector
mbedTrent 10:cf127bd9b857 64 printPW();//for debugging
mbedTrent 10:cf127bd9b857 65 pc.printf("container size: %d\r\n", password.size());//for debugging
mbedTrent 10:cf127bd9b857 66 }
mbedTrent 10:cf127bd9b857 67 }
mbedTrent 10:cf127bd9b857 68
mbedTrent 10:cf127bd9b857 69 //if key 11 (ENTER) is pressed, the password will be submitted IF it is long enough
mbedTrent 10:cf127bd9b857 70 else if ((((value>>i)&0x01)==1)&&(i==11)) {
mbedTrent 10:cf127bd9b857 71
mbedTrent 10:cf127bd9b857 72 //if the password is long enough, check passwords, and clear vector.
mbedTrent 10:cf127bd9b857 73 if (password.size() > minLength) {
mbedTrent 10:cf127bd9b857 74 //CALL checkResult() FUNCTION, OR SET A FLAG INDICATING THAT PASSWORD IS COMPLETE AND AVAILABLE TO BE CHECKED!
mbedTrent 10:cf127bd9b857 75 password.clear(); //only use this if the checkResult is called above. If a flag is set, do not call this here - it should be called from within the checkResult function!
mbedTrent 10:cf127bd9b857 76 }
mbedTrent 10:cf127bd9b857 77
mbedTrent 10:cf127bd9b857 78 else {
mbedTrent 10:cf127bd9b857 79 pc.printf("The password must be at least %d characters long\r\n", minLength);
mbedTrent 10:cf127bd9b857 80 }
mbedTrent 10:cf127bd9b857 81 }
mbedTrent 10:cf127bd9b857 82
mbedTrent 10:cf127bd9b857 83 //if keys 0 through 9 are pressed
mbedTrent 10:cf127bd9b857 84 else if (((value>>i)&0x01)==1) {
mbedTrent 7:52b51f507ee5 85
mbedTrent 10:cf127bd9b857 86 //if max pw length has not been reached, push onto password vector
mbedTrent 10:cf127bd9b857 87 if (password.size() < maxLength) {
mbedTrent 7:52b51f507ee5 88 key_code=i;
mbedTrent 10:cf127bd9b857 89 password.push_back((char)key_code);
mbedTrent 10:cf127bd9b857 90 printPW(); //for debugging
mbedTrent 10:cf127bd9b857 91 }
mbedTrent 10:cf127bd9b857 92
mbedTrent 10:cf127bd9b857 93 //if max password length has been reached
mbedTrent 10:cf127bd9b857 94 else {
mbedTrent 10:cf127bd9b857 95 pc.printf("Maximum password length reached.\r\n");
mbedTrent 7:52b51f507ee5 96 }
mbedTrent 7:52b51f507ee5 97 }
mbedTrent 7:52b51f507ee5 98 }
mbedTrent 7:52b51f507ee5 99 }
mbedTrent 7:52b51f507ee5 100
pshabbaki3 9:11a6f6c8c54b 101 void save_to_file()
pshabbaki3 9:11a6f6c8c54b 102 {
ecarrick 5:a2fbe5bd2be0 103 FILE * fp = fopen("/local/users.txt", "w");
pshabbaki3 9:11a6f6c8c54b 104 if(fp != NULL) {
pshabbaki3 9:11a6f6c8c54b 105 for(unsigned i=0; i< im.size(); i++) {
ecarrick 6:50a82641d07b 106 fprintf(fp,"%c",im[i]);
ecarrick 5:a2fbe5bd2be0 107 }
ecarrick 5:a2fbe5bd2be0 108 }
ecarrick 5:a2fbe5bd2be0 109 fclose(fp);
ecarrick 5:a2fbe5bd2be0 110 }
ecarrick 5:a2fbe5bd2be0 111
pshabbaki3 9:11a6f6c8c54b 112 void load_from_file()
pshabbaki3 9:11a6f6c8c54b 113 {
ecarrick 5:a2fbe5bd2be0 114 FILE * fp = fopen("/local/users.txt", "r");
pshabbaki3 9:11a6f6c8c54b 115 if(fp != NULL) {
ecarrick 5:a2fbe5bd2be0 116 im.clear();
pshabbaki3 9:11a6f6c8c54b 117 do {
ecarrick 6:50a82641d07b 118 im.push_back(fgetc(fp));
pshabbaki3 9:11a6f6c8c54b 119 } while(im.back() != '#');
ecarrick 5:a2fbe5bd2be0 120 }
ecarrick 5:a2fbe5bd2be0 121 fclose(fp);
ecarrick 5:a2fbe5bd2be0 122 }
pshabbaki3 0:5d35ff3dc9a5 123
pshabbaki3 0:5d35ff3dc9a5 124 int i = 0;
pshabbaki3 0:5d35ff3dc9a5 125
pshabbaki3 4:e6b198a22216 126 void checkResult(vector<char> pass, vector<char> name)
pshabbaki3 0:5d35ff3dc9a5 127 {
pshabbaki3 0:5d35ff3dc9a5 128 // This function will check the keypad values and check if the passcode exists or not
pshabbaki3 0:5d35ff3dc9a5 129
mbedTrent 10:cf127bd9b857 130 vector<char> ans(password.size()); //This vector should be the values coming from keypad
pshabbaki3 9:11a6f6c8c54b 131 ans = password; // password from fallInterrupt function
pshabbaki3 9:11a6f6c8c54b 132
pshabbaki3 9:11a6f6c8c54b 133
pshabbaki3 4:e6b198a22216 134 if (pass.size()==6) {
pshabbaki3 0:5d35ff3dc9a5 135 i++; //use "i" to find which person logged in.
pshabbaki3 4:e6b198a22216 136 if (ans == pass) {
pshabbaki3 0:5d35ff3dc9a5 137 // pc.printf("Found");
pshabbaki3 0:5d35ff3dc9a5 138 final = name;
pshabbaki3 4:e6b198a22216 139 final.insert(name.end(),pass.begin(),pass.end());
pshabbaki3 0:5d35ff3dc9a5 140
pshabbaki3 0:5d35ff3dc9a5 141 } else {
pshabbaki3 0:5d35ff3dc9a5 142 // pc.printf("not found");
pshabbaki3 4:e6b198a22216 143 //lcd.printf("\nnot found\n");
pshabbaki3 0:5d35ff3dc9a5 144 final.clear();
pshabbaki3 0:5d35ff3dc9a5 145 }
pshabbaki3 0:5d35ff3dc9a5 146 }
pshabbaki3 0:5d35ff3dc9a5 147 }
pshabbaki3 0:5d35ff3dc9a5 148
pshabbaki3 0:5d35ff3dc9a5 149
pshabbaki3 2:693a3af5af49 150 void checkKeyboard(vector<char> im)
pshabbaki3 0:5d35ff3dc9a5 151 {
pshabbaki3 2:693a3af5af49 152 for (int i = 0; i<im.size(); i++) {
pshabbaki3 4:e6b198a22216 153 // find space
pshabbaki3 2:693a3af5af49 154 if (im[i] ==' ') {
pshabbaki3 4:e6b198a22216 155 // Check the result
pshabbaki3 4:e6b198a22216 156 checkResult(pass,name);
pshabbaki3 9:11a6f6c8c54b 157 // clear pass, name and final
pshabbaki3 4:e6b198a22216 158 pass.clear();
pshabbaki3 2:693a3af5af49 159 name.clear();
pshabbaki3 0:5d35ff3dc9a5 160 // pc.printf("clear");
pshabbaki3 4:e6b198a22216 161 //lcd.printf("clear");
pshabbaki3 2:693a3af5af49 162 } else {
pshabbaki3 2:693a3af5af49 163 if (isdigit(im[i])) {
pshabbaki3 4:e6b198a22216 164 // write numbers (char) to vector pass
pshabbaki3 4:e6b198a22216 165 pass.push_back(im[i]);
pshabbaki3 9:11a6f6c8c54b 166
pshabbaki3 2:693a3af5af49 167 } else if (isalpha(im[i])) {
pshabbaki3 2:693a3af5af49 168 // write letters (char) to vector name
pshabbaki3 2:693a3af5af49 169 name.push_back(im[i]);
pshabbaki3 2:693a3af5af49 170
pshabbaki3 2:693a3af5af49 171 }
pshabbaki3 0:5d35ff3dc9a5 172 }
pshabbaki3 0:5d35ff3dc9a5 173 }
pshabbaki3 0:5d35ff3dc9a5 174 }
pshabbaki3 0:5d35ff3dc9a5 175
pshabbaki3 0:5d35ff3dc9a5 176
pshabbaki3 0:5d35ff3dc9a5 177
pshabbaki3 0:5d35ff3dc9a5 178 int main()
pshabbaki3 0:5d35ff3dc9a5 179 {
mbedTrent 7:52b51f507ee5 180 interrupt.fall(&fallInterrupt);
mbedTrent 7:52b51f507ee5 181 interrupt.mode(PullUp);
pshabbaki3 2:693a3af5af49 182 //load_from_file();
pshabbaki3 2:693a3af5af49 183
pshabbaki3 0:5d35ff3dc9a5 184 //interupt driven read from serial port
pshabbaki3 0:5d35ff3dc9a5 185 // calls save_to_file();
pshabbaki3 0:5d35ff3dc9a5 186 while(1) {
pshabbaki3 0:5d35ff3dc9a5 187 char c = pc.getc();
pshabbaki3 0:5d35ff3dc9a5 188 if (c!='#') {
pshabbaki3 0:5d35ff3dc9a5 189 im.push_back(c); //writing all the characters into vector
pshabbaki3 2:693a3af5af49 190 checkKeyboard(im);
pshabbaki3 0:5d35ff3dc9a5 191 } else {
pshabbaki3 0:5d35ff3dc9a5 192 for (int cnt = 0; cnt<im.size(); cnt++) {
pshabbaki3 0:5d35ff3dc9a5 193 pc.printf("%c",im[cnt]);
pshabbaki3 4:e6b198a22216 194 //lcd.printf("%c",im[cnt]);
pshabbaki3 0:5d35ff3dc9a5 195 }
pshabbaki3 0:5d35ff3dc9a5 196 }
pshabbaki3 0:5d35ff3dc9a5 197 }
pshabbaki3 0:5d35ff3dc9a5 198
pshabbaki3 0:5d35ff3dc9a5 199 }
pshabbaki3 0:5d35ff3dc9a5 200
pshabbaki3 0:5d35ff3dc9a5 201