4180 Final Project
Dependencies: 4DGL-uLCD-SE mbed Servo
main.cpp@26:f08205fa22d3, 2016-05-01 (annotated)
- Committer:
- ecarrick
- Date:
- Sun May 01 15:19:50 2016 +0000
- Revision:
- 26:f08205fa22d3
- Parent:
- 25:2e95a5731412
- Child:
- 27:2bd737c0a8af
to push to repo
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" |
ecarrick | 25:2e95a5731412 | 11 | #include "Servo.h" |
pshabbaki3 | 0:5d35ff3dc9a5 | 12 | |
mbedTrent | 7:52b51f507ee5 | 13 | #include <list> |
mbedTrent | 7:52b51f507ee5 | 14 | #include <mpr121.h> |
mbedTrent | 7:52b51f507ee5 | 15 | |
ecarrick | 1:dd048edb2716 | 16 | LocalFileSystem local("local"); |
ecarrick | 25:2e95a5731412 | 17 | Servo lock(p25); // a pwm pin, 0 for unlocked; 1 for locked; |
pshabbaki3 | 0:5d35ff3dc9a5 | 18 | |
pshabbaki3 | 0:5d35ff3dc9a5 | 19 | DigitalOut myled(LED1); |
ecarrick | 17:632c56db3c0a | 20 | DigitalOut ledr(p5); |
ecarrick | 17:632c56db3c0a | 21 | DigitalOut ledg(p6); |
ecarrick | 17:632c56db3c0a | 22 | DigitalOut ledb(p7); |
ecarrick | 17:632c56db3c0a | 23 | |
pshabbaki3 | 0:5d35ff3dc9a5 | 24 | Serial pc(USBTX,USBRX); |
ecarrick | 16:fb2a50f0ecde | 25 | //uLCD_4DGL lcd(p28,p27,p30); // serial tx, serial rx, reset pin; |
pshabbaki3 | 0:5d35ff3dc9a5 | 26 | |
mbedTrent | 7:52b51f507ee5 | 27 | // Create the interrupt receiver object on pin 26 |
mbedTrent | 7:52b51f507ee5 | 28 | InterruptIn interrupt(p26); |
mbedTrent | 7:52b51f507ee5 | 29 | |
mbedTrent | 7:52b51f507ee5 | 30 | // Setup the i2c bus on pins 28 and 27 |
mbedTrent | 7:52b51f507ee5 | 31 | I2C i2c(p9, p10); |
mbedTrent | 7:52b51f507ee5 | 32 | |
mbedTrent | 7:52b51f507ee5 | 33 | // Setup the Mpr121: |
mbedTrent | 7:52b51f507ee5 | 34 | // constructor(i2c object, i2c address of the mpr121) |
mbedTrent | 7:52b51f507ee5 | 35 | Mpr121 mpr121(&i2c, Mpr121::ADD_VSS); |
mbedTrent | 7:52b51f507ee5 | 36 | |
ecarrick | 25:2e95a5731412 | 37 | // log for web page |
ecarrick | 25:2e95a5731412 | 38 | vector<string> accessLog; |
ecarrick | 25:2e95a5731412 | 39 | unsigned logLength = 10; |
ecarrick | 25:2e95a5731412 | 40 | volatile bool updateAccessLog = false; |
ecarrick | 25:2e95a5731412 | 41 | |
ecarrick | 25:2e95a5731412 | 42 | // save to file flag |
ecarrick | 25:2e95a5731412 | 43 | volatile bool saveToFileFlag = false; |
ecarrick | 25:2e95a5731412 | 44 | volatile bool newData = false; |
ecarrick | 25:2e95a5731412 | 45 | |
ecarrick | 25:2e95a5731412 | 46 | // time |
ecarrick | 25:2e95a5731412 | 47 | time_t rtc; |
pshabbaki3 | 0:5d35ff3dc9a5 | 48 | |
pshabbaki3 | 0:5d35ff3dc9a5 | 49 | vector<char> im; |
ecarrick | 26:f08205fa22d3 | 50 | vector<char> tempim; |
pshabbaki3 | 4:e6b198a22216 | 51 | vector<char> pass; |
pshabbaki3 | 0:5d35ff3dc9a5 | 52 | vector<char> name; |
pshabbaki3 | 0:5d35ff3dc9a5 | 53 | vector<char> final; |
pshabbaki3 | 11:1ca1f07896b0 | 54 | vector<char> ans; |
pshabbaki3 | 0:5d35ff3dc9a5 | 55 | |
ecarrick | 25:2e95a5731412 | 56 | string print_vector(vector<char>& vec){ |
ecarrick | 25:2e95a5731412 | 57 | string str; |
ecarrick | 25:2e95a5731412 | 58 | str.append(vec.begin(),vec.end()); |
ecarrick | 25:2e95a5731412 | 59 | return str; |
ecarrick | 25:2e95a5731412 | 60 | } |
ecarrick | 25:2e95a5731412 | 61 | |
mbedTrent | 10:cf127bd9b857 | 62 | vector<char> password; |
mbedTrent | 15:3d43fe8e108b | 63 | unsigned maxLength = 18; //random length chosen |
mbedTrent | 15:3d43fe8e108b | 64 | unsigned minLength = 6; //random length chosen |
mbedTrent | 10:cf127bd9b857 | 65 | |
ecarrick | 24:94e8b3e00c24 | 66 | void print_to_serial() |
ecarrick | 24:94e8b3e00c24 | 67 | { |
ecarrick | 24:94e8b3e00c24 | 68 | for(unsigned i=0; i< im.size(); i++) { |
ecarrick | 24:94e8b3e00c24 | 69 | pc.printf("%c",im[i]); |
ecarrick | 24:94e8b3e00c24 | 70 | } |
ecarrick | 24:94e8b3e00c24 | 71 | pc.printf("\r\n"); |
ecarrick | 24:94e8b3e00c24 | 72 | } |
pshabbaki3 | 12:22444d5f5920 | 73 | |
mbedTrent | 10:cf127bd9b857 | 74 | //function for debugging |
pshabbaki3 | 12:22444d5f5920 | 75 | void printPW() |
pshabbaki3 | 12:22444d5f5920 | 76 | { |
mbedTrent | 10:cf127bd9b857 | 77 | for (int i=0; i<password.size(); i++) { |
ecarrick | 24:94e8b3e00c24 | 78 | pc.printf("%c", password[i]); |
mbedTrent | 10:cf127bd9b857 | 79 | } |
mbedTrent | 10:cf127bd9b857 | 80 | pc.printf("\r\n"); |
mbedTrent | 10:cf127bd9b857 | 81 | } |
mbedTrent | 7:52b51f507ee5 | 82 | |
ecarrick | 25:2e95a5731412 | 83 | bool foundResult = false; |
ecarrick | 16:fb2a50f0ecde | 84 | void checkResult(vector<char> pass, vector<char> name) |
ecarrick | 16:fb2a50f0ecde | 85 | { |
ecarrick | 25:2e95a5731412 | 86 | /* |
ecarrick | 24:94e8b3e00c24 | 87 | for (int i=0; i<pass.size(); i++) { |
ecarrick | 24:94e8b3e00c24 | 88 | pc.printf("%d", int(pass[i])); |
ecarrick | 24:94e8b3e00c24 | 89 | } |
ecarrick | 25:2e95a5731412 | 90 | */ |
ecarrick | 25:2e95a5731412 | 91 | |
ecarrick | 25:2e95a5731412 | 92 | //pc.printf("\r\n"); |
ecarrick | 16:fb2a50f0ecde | 93 | // This function will check the keypad values and check if the passcode exists or not |
ecarrick | 16:fb2a50f0ecde | 94 | |
ecarrick | 16:fb2a50f0ecde | 95 | if (pass.size()==6) { |
ecarrick | 24:94e8b3e00c24 | 96 | if (password == pass) { |
ecarrick | 25:2e95a5731412 | 97 | //pc.printf("Found"); |
ecarrick | 16:fb2a50f0ecde | 98 | final = name; |
ecarrick | 16:fb2a50f0ecde | 99 | final.push_back(' '); |
ecarrick | 16:fb2a50f0ecde | 100 | final.insert(final.end(),pass.begin(),pass.end()); |
ecarrick | 25:2e95a5731412 | 101 | foundResult=true; |
ecarrick | 25:2e95a5731412 | 102 | ledb =0; |
ecarrick | 25:2e95a5731412 | 103 | ledr =0; |
ecarrick | 25:2e95a5731412 | 104 | ledg =1; |
ecarrick | 25:2e95a5731412 | 105 | lock = 0; |
ecarrick | 25:2e95a5731412 | 106 | //wait(0.01); |
ecarrick | 25:2e95a5731412 | 107 | //ledg =0; |
ecarrick | 16:fb2a50f0ecde | 108 | |
ecarrick | 16:fb2a50f0ecde | 109 | } else { |
ecarrick | 25:2e95a5731412 | 110 | //pc.printf("not found"); |
ecarrick | 16:fb2a50f0ecde | 111 | //lcd.printf("\nnot found\n"); |
ecarrick | 16:fb2a50f0ecde | 112 | final.clear(); |
ecarrick | 16:fb2a50f0ecde | 113 | } |
ecarrick | 16:fb2a50f0ecde | 114 | } |
ecarrick | 16:fb2a50f0ecde | 115 | } |
ecarrick | 16:fb2a50f0ecde | 116 | |
ecarrick | 16:fb2a50f0ecde | 117 | |
ecarrick | 16:fb2a50f0ecde | 118 | void checkKeyboard() |
ecarrick | 16:fb2a50f0ecde | 119 | { |
pshabbaki3 | 23:e23578000e42 | 120 | //load_from_file(); |
ecarrick | 25:2e95a5731412 | 121 | //pc.printf("CHECKING PASSWORD\n\r"); |
ecarrick | 25:2e95a5731412 | 122 | //pc.printf("\n\r"); |
pshabbaki3 | 19:6fce7b88de5e | 123 | bool colon = false; |
ecarrick | 25:2e95a5731412 | 124 | vector<char> empty; |
ecarrick | 25:2e95a5731412 | 125 | swap(empty, pass); |
ecarrick | 25:2e95a5731412 | 126 | vector<char> empty2; |
ecarrick | 25:2e95a5731412 | 127 | swap(empty2, name); |
ecarrick | 25:2e95a5731412 | 128 | //pass.clear(); |
ecarrick | 25:2e95a5731412 | 129 | //name.clear(); |
ecarrick | 16:fb2a50f0ecde | 130 | for (int i = 0; i<im.size(); i++) { |
pshabbaki3 | 19:6fce7b88de5e | 131 | |
ecarrick | 16:fb2a50f0ecde | 132 | // find space |
ecarrick | 16:fb2a50f0ecde | 133 | if (im[i] ==' ') { |
ecarrick | 16:fb2a50f0ecde | 134 | checkResult(pass,name); |
pshabbaki3 | 19:6fce7b88de5e | 135 | colon = false; |
ecarrick | 25:2e95a5731412 | 136 | if(foundResult){ |
ecarrick | 25:2e95a5731412 | 137 | //foundResult=false; |
ecarrick | 25:2e95a5731412 | 138 | break; |
ecarrick | 25:2e95a5731412 | 139 | } |
ecarrick | 25:2e95a5731412 | 140 | vector<char> empty; |
ecarrick | 25:2e95a5731412 | 141 | swap(empty, pass); |
ecarrick | 25:2e95a5731412 | 142 | vector<char> empty2; |
ecarrick | 25:2e95a5731412 | 143 | swap(empty2, name); |
ecarrick | 25:2e95a5731412 | 144 | //pass.clear(); |
ecarrick | 25:2e95a5731412 | 145 | //name.clear(); |
pshabbaki3 | 19:6fce7b88de5e | 146 | |
pshabbaki3 | 19:6fce7b88de5e | 147 | } else if (im[i] == ':') { |
pshabbaki3 | 19:6fce7b88de5e | 148 | colon = true; |
ecarrick | 16:fb2a50f0ecde | 149 | } else { |
pshabbaki3 | 19:6fce7b88de5e | 150 | if(colon) |
ecarrick | 24:94e8b3e00c24 | 151 | pass.push_back(unsigned(im[i])); |
pshabbaki3 | 19:6fce7b88de5e | 152 | else |
ecarrick | 24:94e8b3e00c24 | 153 | name.push_back(char(im[i])); |
ecarrick | 16:fb2a50f0ecde | 154 | } |
ecarrick | 16:fb2a50f0ecde | 155 | } |
ecarrick | 16:fb2a50f0ecde | 156 | } |
ecarrick | 16:fb2a50f0ecde | 157 | |
pshabbaki3 | 9:11a6f6c8c54b | 158 | void fallInterrupt() |
pshabbaki3 | 9:11a6f6c8c54b | 159 | { |
mbedTrent | 7:52b51f507ee5 | 160 | int key_code=0; |
mbedTrent | 7:52b51f507ee5 | 161 | int i=0; |
mbedTrent | 7:52b51f507ee5 | 162 | int value=mpr121.read(0x00); |
mbedTrent | 7:52b51f507ee5 | 163 | value +=mpr121.read(0x01)<<8; |
mbedTrent | 10:cf127bd9b857 | 164 | |
mbedTrent | 10:cf127bd9b857 | 165 | //checks every key for press |
mbedTrent | 7:52b51f507ee5 | 166 | for (i=0; i<=11; i++) { |
pshabbaki3 | 12:22444d5f5920 | 167 | |
mbedTrent | 10:cf127bd9b857 | 168 | //if key 10 (clr) is pressed, clear the last value on the vector will be popped off |
mbedTrent | 10:cf127bd9b857 | 169 | if ((((value>>i)&0x01)==1)&&(i==10)) { |
ecarrick | 25:2e95a5731412 | 170 | |
mbedTrent | 10:cf127bd9b857 | 171 | if (!password.empty()) { |
ecarrick | 17:632c56db3c0a | 172 | ledb = 1; |
mbedTrent | 10:cf127bd9b857 | 173 | password.pop_back(); //pop off last value on the vector |
ecarrick | 25:2e95a5731412 | 174 | //printPW();//for debugging |
ecarrick | 25:2e95a5731412 | 175 | //pc.printf("container size: %d\r\n", password.size());//for dubugging |
mbedTrent | 10:cf127bd9b857 | 176 | } |
mbedTrent | 15:3d43fe8e108b | 177 | break; |
mbedTrent | 10:cf127bd9b857 | 178 | } |
pshabbaki3 | 12:22444d5f5920 | 179 | |
mbedTrent | 10:cf127bd9b857 | 180 | //if key 11 (ENTER) is pressed, the password will be submitted IF it is long enough |
mbedTrent | 10:cf127bd9b857 | 181 | else if ((((value>>i)&0x01)==1)&&(i==11)) { |
ecarrick | 25:2e95a5731412 | 182 | if(lock == 0){ |
ecarrick | 25:2e95a5731412 | 183 | lock = 1.0; |
ecarrick | 25:2e95a5731412 | 184 | break; |
ecarrick | 25:2e95a5731412 | 185 | } |
mbedTrent | 10:cf127bd9b857 | 186 | //if the password is long enough, check passwords, and clear vector. |
mbedTrent | 15:3d43fe8e108b | 187 | if (password.size() >= minLength) { |
mbedTrent | 10:cf127bd9b857 | 188 | //CALL checkResult() FUNCTION, OR SET A FLAG INDICATING THAT PASSWORD IS COMPLETE AND AVAILABLE TO BE CHECKED! |
ecarrick | 25:2e95a5731412 | 189 | //ledb = 1; |
ecarrick | 24:94e8b3e00c24 | 190 | //ans = password; |
ecarrick | 25:2e95a5731412 | 191 | //pc.printf("ENTER KEY PRESSED\n\r"); |
ecarrick | 25:2e95a5731412 | 192 | //printPW(); |
ecarrick | 25:2e95a5731412 | 193 | //for(unsigned k =0; k<password.size(); k++) |
ecarrick | 25:2e95a5731412 | 194 | // ans.push_back(password[k]); |
ecarrick | 16:fb2a50f0ecde | 195 | checkKeyboard(); |
ecarrick | 25:2e95a5731412 | 196 | rtc = time(NULL); |
ecarrick | 25:2e95a5731412 | 197 | if(foundResult){ |
ecarrick | 25:2e95a5731412 | 198 | foundResult=false; |
ecarrick | 25:2e95a5731412 | 199 | char buf[128]; |
ecarrick | 25:2e95a5731412 | 200 | sprintf(buf,"%s %s SUCCESS", print_vector(name).c_str(), ctime(&rtc)); |
ecarrick | 25:2e95a5731412 | 201 | if(accessLog.size()<logLength){ |
ecarrick | 25:2e95a5731412 | 202 | accessLog.push_back(buf); |
ecarrick | 25:2e95a5731412 | 203 | } else{ |
ecarrick | 25:2e95a5731412 | 204 | vector<string> empty; |
ecarrick | 25:2e95a5731412 | 205 | swap(accessLog,empty); |
ecarrick | 25:2e95a5731412 | 206 | //accessLog.clear(); |
ecarrick | 25:2e95a5731412 | 207 | accessLog.push_back(buf); |
ecarrick | 25:2e95a5731412 | 208 | } |
ecarrick | 25:2e95a5731412 | 209 | } else{ |
ecarrick | 25:2e95a5731412 | 210 | ledr = 1; |
ecarrick | 25:2e95a5731412 | 211 | char buf[128]; |
ecarrick | 25:2e95a5731412 | 212 | sprintf(buf,"%s %s FAILURE", print_vector(password).c_str(), ctime(&rtc)); |
ecarrick | 25:2e95a5731412 | 213 | if(accessLog.size()<logLength){ |
ecarrick | 25:2e95a5731412 | 214 | accessLog.push_back(buf); |
ecarrick | 25:2e95a5731412 | 215 | } else{ |
ecarrick | 25:2e95a5731412 | 216 | vector<string> empty; |
ecarrick | 25:2e95a5731412 | 217 | swap(accessLog,empty); |
ecarrick | 25:2e95a5731412 | 218 | //accessLog.clear(); |
ecarrick | 25:2e95a5731412 | 219 | accessLog.push_back(buf); |
ecarrick | 25:2e95a5731412 | 220 | } |
ecarrick | 25:2e95a5731412 | 221 | } |
ecarrick | 25:2e95a5731412 | 222 | //update_log(accessLog); |
ecarrick | 25:2e95a5731412 | 223 | updateAccessLog = true; |
ecarrick | 25:2e95a5731412 | 224 | //wait(0.005); |
ecarrick | 25:2e95a5731412 | 225 | ledg=0; |
ecarrick | 25:2e95a5731412 | 226 | ledr=0; |
mbedTrent | 15:3d43fe8e108b | 227 | vector<char> empty; |
mbedTrent | 15:3d43fe8e108b | 228 | swap(empty, password); |
ecarrick | 25:2e95a5731412 | 229 | //password.clear(); |
mbedTrent | 10:cf127bd9b857 | 230 | } |
pshabbaki3 | 12:22444d5f5920 | 231 | |
mbedTrent | 10:cf127bd9b857 | 232 | else { |
ecarrick | 25:2e95a5731412 | 233 | //pc.printf("The password must be at least %d characters long\r\n", minLength); |
mbedTrent | 10:cf127bd9b857 | 234 | } |
mbedTrent | 15:3d43fe8e108b | 235 | break; |
pshabbaki3 | 12:22444d5f5920 | 236 | } |
pshabbaki3 | 12:22444d5f5920 | 237 | |
mbedTrent | 10:cf127bd9b857 | 238 | //if keys 0 through 9 are pressed |
mbedTrent | 10:cf127bd9b857 | 239 | else if (((value>>i)&0x01)==1) { |
mbedTrent | 7:52b51f507ee5 | 240 | |
mbedTrent | 10:cf127bd9b857 | 241 | //if max pw length has not been reached, push onto password vector |
mbedTrent | 10:cf127bd9b857 | 242 | if (password.size() < maxLength) { |
ecarrick | 17:632c56db3c0a | 243 | ledb = 1; |
ecarrick | 18:107fe4158212 | 244 | |
ecarrick | 17:632c56db3c0a | 245 | |
mbedTrent | 7:52b51f507ee5 | 246 | key_code=i; |
ecarrick | 24:94e8b3e00c24 | 247 | password.push_back((char)(key_code+48)); |
ecarrick | 25:2e95a5731412 | 248 | //printPW(); //for debugging |
ecarrick | 25:2e95a5731412 | 249 | //pc.printf("times: %d\r\n", password.size()); |
mbedTrent | 15:3d43fe8e108b | 250 | |
mbedTrent | 15:3d43fe8e108b | 251 | if (password.size() == maxLength) { |
mbedTrent | 15:3d43fe8e108b | 252 | //pc.printf("Max password length reached. Press clr to delete character. Press any number key to reset.\r\n"); |
ecarrick | 25:2e95a5731412 | 253 | ledb = 0; |
ecarrick | 25:2e95a5731412 | 254 | ledr = 1; |
ecarrick | 25:2e95a5731412 | 255 | wait(0.05); |
ecarrick | 25:2e95a5731412 | 256 | ledr = 0; |
mbedTrent | 15:3d43fe8e108b | 257 | vector<char> empty; |
mbedTrent | 15:3d43fe8e108b | 258 | swap(empty, password); |
mbedTrent | 15:3d43fe8e108b | 259 | //password.clear(); |
mbedTrent | 15:3d43fe8e108b | 260 | } |
mbedTrent | 15:3d43fe8e108b | 261 | } |
mbedTrent | 15:3d43fe8e108b | 262 | |
mbedTrent | 15:3d43fe8e108b | 263 | //if max password length has been reached |
mbedTrent | 15:3d43fe8e108b | 264 | else { |
ecarrick | 25:2e95a5731412 | 265 | //pc.printf("Maximum password length reached, password has been cleared. \r\n"); |
ecarrick | 25:2e95a5731412 | 266 | //password.clear(); |
ecarrick | 25:2e95a5731412 | 267 | vector<char> empty; |
ecarrick | 25:2e95a5731412 | 268 | swap(empty, password); |
mbedTrent | 15:3d43fe8e108b | 269 | } |
mbedTrent | 15:3d43fe8e108b | 270 | break; |
mbedTrent | 15:3d43fe8e108b | 271 | } |
mbedTrent | 15:3d43fe8e108b | 272 | |
mbedTrent | 15:3d43fe8e108b | 273 | } |
ecarrick | 25:2e95a5731412 | 274 | wait(0.005); |
ecarrick | 18:107fe4158212 | 275 | ledb = 0; |
mbedTrent | 15:3d43fe8e108b | 276 | |
mbedTrent | 15:3d43fe8e108b | 277 | } |
mbedTrent | 15:3d43fe8e108b | 278 | |
ecarrick | 16:fb2a50f0ecde | 279 | |
ecarrick | 24:94e8b3e00c24 | 280 | |
mbedTrent | 7:52b51f507ee5 | 281 | |
pshabbaki3 | 9:11a6f6c8c54b | 282 | void save_to_file() |
pshabbaki3 | 9:11a6f6c8c54b | 283 | { |
ecarrick | 5:a2fbe5bd2be0 | 284 | FILE * fp = fopen("/local/users.txt", "w"); |
pshabbaki3 | 9:11a6f6c8c54b | 285 | if(fp != NULL) { |
pshabbaki3 | 9:11a6f6c8c54b | 286 | for(unsigned i=0; i< im.size(); i++) { |
ecarrick | 6:50a82641d07b | 287 | fprintf(fp,"%c",im[i]); |
ecarrick | 5:a2fbe5bd2be0 | 288 | } |
ecarrick | 5:a2fbe5bd2be0 | 289 | } |
ecarrick | 5:a2fbe5bd2be0 | 290 | fclose(fp); |
ecarrick | 5:a2fbe5bd2be0 | 291 | } |
ecarrick | 5:a2fbe5bd2be0 | 292 | |
pshabbaki3 | 9:11a6f6c8c54b | 293 | void load_from_file() |
pshabbaki3 | 9:11a6f6c8c54b | 294 | { |
ecarrick | 5:a2fbe5bd2be0 | 295 | FILE * fp = fopen("/local/users.txt", "r"); |
pshabbaki3 | 9:11a6f6c8c54b | 296 | if(fp != NULL) { |
ecarrick | 5:a2fbe5bd2be0 | 297 | im.clear(); |
pshabbaki3 | 9:11a6f6c8c54b | 298 | do { |
ecarrick | 6:50a82641d07b | 299 | im.push_back(fgetc(fp)); |
pshabbaki3 | 9:11a6f6c8c54b | 300 | } while(im.back() != '#'); |
ecarrick | 5:a2fbe5bd2be0 | 301 | } |
ecarrick | 5:a2fbe5bd2be0 | 302 | fclose(fp); |
ecarrick | 5:a2fbe5bd2be0 | 303 | } |
pshabbaki3 | 0:5d35ff3dc9a5 | 304 | |
pshabbaki3 | 0:5d35ff3dc9a5 | 305 | int i = 0; |
pshabbaki3 | 0:5d35ff3dc9a5 | 306 | |
pshabbaki3 | 0:5d35ff3dc9a5 | 307 | |
pshabbaki3 | 0:5d35ff3dc9a5 | 308 | |
pshabbaki3 | 12:22444d5f5920 | 309 | //Interrupt |
pshabbaki3 | 12:22444d5f5920 | 310 | void Rx_interrupt() |
pshabbaki3 | 0:5d35ff3dc9a5 | 311 | { |
ecarrick | 26:f08205fa22d3 | 312 | if(!newData){ |
ecarrick | 26:f08205fa22d3 | 313 | tempim.clear(); |
ecarrick | 26:f08205fa22d3 | 314 | tempim.push_back(pc.getc()); |
ecarrick | 26:f08205fa22d3 | 315 | newData = true; |
ecarrick | 26:f08205fa22d3 | 316 | } |
ecarrick | 26:f08205fa22d3 | 317 | else{ |
ecarrick | 26:f08205fa22d3 | 318 | char c = pc.getc(); |
ecarrick | 26:f08205fa22d3 | 319 | if (c!='#') { |
ecarrick | 26:f08205fa22d3 | 320 | tempim.push_back(c); //writing all the characters into vector |
ecarrick | 26:f08205fa22d3 | 321 | |
ecarrick | 26:f08205fa22d3 | 322 | //checkKeyboard(im); |
ecarrick | 26:f08205fa22d3 | 323 | } else { |
ecarrick | 26:f08205fa22d3 | 324 | tempim.push_back(c); |
ecarrick | 26:f08205fa22d3 | 325 | saveToFileFlag = true; |
ecarrick | 26:f08205fa22d3 | 326 | im = tempim; |
ecarrick | 26:f08205fa22d3 | 327 | newData = false; |
ecarrick | 26:f08205fa22d3 | 328 | } |
ecarrick | 26:f08205fa22d3 | 329 | } |
pshabbaki3 | 12:22444d5f5920 | 330 | return; |
pshabbaki3 | 0:5d35ff3dc9a5 | 331 | } |
pshabbaki3 | 0:5d35ff3dc9a5 | 332 | |
pshabbaki3 | 0:5d35ff3dc9a5 | 333 | |
pshabbaki3 | 12:22444d5f5920 | 334 | int main() |
pshabbaki3 | 12:22444d5f5920 | 335 | { |
ecarrick | 25:2e95a5731412 | 336 | |
ecarrick | 25:2e95a5731412 | 337 | pc.printf("\r\n\r\nMBED RESET\r\n"); |
ecarrick | 25:2e95a5731412 | 338 | lock = 1.0; |
ecarrick | 25:2e95a5731412 | 339 | pc.printf("Setting up WIFI\r\n"); |
ecarrick | 26:f08205fa22d3 | 340 | //setup_wifi(); |
ecarrick | 25:2e95a5731412 | 341 | pc.printf("Setting Up HTTP\r\n"); |
ecarrick | 26:f08205fa22d3 | 342 | //setup_http(); |
ecarrick | 25:2e95a5731412 | 343 | // pc.printf("updating access log\r\n"); |
ecarrick | 25:2e95a5731412 | 344 | // accessLog.push_back("test 1"); |
ecarrick | 25:2e95a5731412 | 345 | // accessLog.push_back("Hey Edison"); |
ecarrick | 25:2e95a5731412 | 346 | // accessLog.push_back("Great job!"); |
ecarrick | 25:2e95a5731412 | 347 | //update_log(accessLog); |
ecarrick | 25:2e95a5731412 | 348 | pc.printf("Setting up NTP\r\n"); |
ecarrick | 26:f08205fa22d3 | 349 | //setup_ntp(); |
ecarrick | 25:2e95a5731412 | 350 | pc.printf("Updating NTP\r\n"); |
ecarrick | 26:f08205fa22d3 | 351 | //update_ntp(); |
ecarrick | 25:2e95a5731412 | 352 | rtc = time(NULL); |
ecarrick | 25:2e95a5731412 | 353 | pc.printf("%s\r\n", ctime(&rtc)); |
ecarrick | 25:2e95a5731412 | 354 | |
ecarrick | 25:2e95a5731412 | 355 | |
ecarrick | 24:94e8b3e00c24 | 356 | load_from_file(); |
ecarrick | 16:fb2a50f0ecde | 357 | print_to_serial(); |
ecarrick | 25:2e95a5731412 | 358 | |
pshabbaki3 | 12:22444d5f5920 | 359 | interrupt.fall(&fallInterrupt); |
pshabbaki3 | 12:22444d5f5920 | 360 | interrupt.mode(PullUp); |
pshabbaki3 | 12:22444d5f5920 | 361 | //load_from_file(); |
pshabbaki3 | 12:22444d5f5920 | 362 | |
pshabbaki3 | 12:22444d5f5920 | 363 | //interupt driven read from serial port |
pshabbaki3 | 12:22444d5f5920 | 364 | // calls save_to_file(); |
pshabbaki3 | 12:22444d5f5920 | 365 | pc.attach(&Rx_interrupt, Serial::RxIrq); |
ecarrick | 25:2e95a5731412 | 366 | |
ecarrick | 25:2e95a5731412 | 367 | ledg = 1; |
ecarrick | 25:2e95a5731412 | 368 | wait(0.3); |
ecarrick | 25:2e95a5731412 | 369 | ledg = 0; |
ecarrick | 25:2e95a5731412 | 370 | wait(0.3); |
ecarrick | 25:2e95a5731412 | 371 | ledg = 1; |
ecarrick | 25:2e95a5731412 | 372 | wait(0.3); |
ecarrick | 25:2e95a5731412 | 373 | ledg = 0; |
ecarrick | 25:2e95a5731412 | 374 | |
ecarrick | 25:2e95a5731412 | 375 | while(1){ |
ecarrick | 25:2e95a5731412 | 376 | if(updateAccessLog){ |
ecarrick | 25:2e95a5731412 | 377 | update_log(accessLog); |
ecarrick | 25:2e95a5731412 | 378 | updateAccessLog = false; |
ecarrick | 25:2e95a5731412 | 379 | } |
ecarrick | 25:2e95a5731412 | 380 | |
ecarrick | 25:2e95a5731412 | 381 | if(saveToFileFlag){ |
ecarrick | 25:2e95a5731412 | 382 | save_to_file(); |
ecarrick | 25:2e95a5731412 | 383 | saveToFileFlag = false; |
ecarrick | 25:2e95a5731412 | 384 | } |
ecarrick | 26:f08205fa22d3 | 385 | /* |
ecarrick | 25:2e95a5731412 | 386 | if(newData){ |
ecarrick | 26:f08205fa22d3 | 387 | pc.printf("read in from serial\r\n"); |
ecarrick | 26:f08205fa22d3 | 388 | |
ecarrick | 25:2e95a5731412 | 389 | ledb = 1; |
ecarrick | 25:2e95a5731412 | 390 | vector<char> empty; |
ecarrick | 25:2e95a5731412 | 391 | swap(empty, im); |
ecarrick | 25:2e95a5731412 | 392 | while (1) { |
ecarrick | 25:2e95a5731412 | 393 | char c = pc.getc(); |
ecarrick | 25:2e95a5731412 | 394 | if (c!='#') { |
ecarrick | 25:2e95a5731412 | 395 | im.push_back(c); //writing all the characters into vector |
ecarrick | 25:2e95a5731412 | 396 | |
ecarrick | 25:2e95a5731412 | 397 | //checkKeyboard(im); |
ecarrick | 25:2e95a5731412 | 398 | } else { |
ecarrick | 25:2e95a5731412 | 399 | im.push_back(c); |
ecarrick | 25:2e95a5731412 | 400 | saveToFileFlag = true; |
ecarrick | 25:2e95a5731412 | 401 | newData = false; |
ecarrick | 25:2e95a5731412 | 402 | break; |
ecarrick | 25:2e95a5731412 | 403 | } |
ecarrick | 25:2e95a5731412 | 404 | |
ecarrick | 25:2e95a5731412 | 405 | } |
ecarrick | 25:2e95a5731412 | 406 | ledb = 0; |
ecarrick | 25:2e95a5731412 | 407 | |
ecarrick | 25:2e95a5731412 | 408 | } |
ecarrick | 26:f08205fa22d3 | 409 | */ |
ecarrick | 25:2e95a5731412 | 410 | wait(0.1); |
ecarrick | 25:2e95a5731412 | 411 | //sleep(); |
ecarrick | 25:2e95a5731412 | 412 | |
ecarrick | 25:2e95a5731412 | 413 | } |
ecarrick | 25:2e95a5731412 | 414 | |
pshabbaki3 | 12:22444d5f5920 | 415 | } |
pshabbaki3 | 12:22444d5f5920 | 416 | |
pshabbaki3 | 12:22444d5f5920 | 417 |