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