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