4180 Final Project
Dependencies: 4DGL-uLCD-SE mbed Servo
main.cpp
- Committer:
- ecarrick
- Date:
- 2016-05-01
- Revision:
- 26:f08205fa22d3
- Parent:
- 25:2e95a5731412
- Child:
- 27:2bd737c0a8af
File content as of revision 26:f08205fa22d3:
#include "mbed.h" #include "wifi.h" #include <string> #include <vector> #include <iostream> #include <sstream> #include <iterator> #include <stdio.h> #include <ctype.h> #include "uLCD_4DGL.h" #include "Servo.h" #include <list> #include <mpr121.h> LocalFileSystem local("local"); Servo lock(p25); // a pwm pin, 0 for unlocked; 1 for locked; DigitalOut myled(LED1); DigitalOut ledr(p5); DigitalOut ledg(p6); DigitalOut ledb(p7); Serial pc(USBTX,USBRX); //uLCD_4DGL lcd(p28,p27,p30); // serial tx, serial rx, reset pin; // Create the interrupt receiver object on pin 26 InterruptIn interrupt(p26); // Setup the i2c bus on pins 28 and 27 I2C i2c(p9, p10); // Setup the Mpr121: // constructor(i2c object, i2c address of the mpr121) Mpr121 mpr121(&i2c, Mpr121::ADD_VSS); // log for web page vector<string> accessLog; unsigned logLength = 10; volatile bool updateAccessLog = false; // save to file flag volatile bool saveToFileFlag = false; volatile bool newData = false; // time time_t rtc; vector<char> im; vector<char> tempim; vector<char> pass; vector<char> name; vector<char> final; vector<char> ans; string print_vector(vector<char>& vec){ string str; str.append(vec.begin(),vec.end()); return str; } vector<char> password; unsigned maxLength = 18; //random length chosen unsigned minLength = 6; //random length chosen void print_to_serial() { for(unsigned i=0; i< im.size(); i++) { pc.printf("%c",im[i]); } pc.printf("\r\n"); } //function for debugging void printPW() { for (int i=0; i<password.size(); i++) { pc.printf("%c", password[i]); } pc.printf("\r\n"); } bool foundResult = false; void checkResult(vector<char> pass, vector<char> name) { /* for (int i=0; i<pass.size(); i++) { pc.printf("%d", int(pass[i])); } */ //pc.printf("\r\n"); // This function will check the keypad values and check if the passcode exists or not if (pass.size()==6) { if (password == pass) { //pc.printf("Found"); final = name; final.push_back(' '); final.insert(final.end(),pass.begin(),pass.end()); foundResult=true; ledb =0; ledr =0; ledg =1; lock = 0; //wait(0.01); //ledg =0; } else { //pc.printf("not found"); //lcd.printf("\nnot found\n"); final.clear(); } } } void checkKeyboard() { //load_from_file(); //pc.printf("CHECKING PASSWORD\n\r"); //pc.printf("\n\r"); bool colon = false; vector<char> empty; swap(empty, pass); vector<char> empty2; swap(empty2, name); //pass.clear(); //name.clear(); for (int i = 0; i<im.size(); i++) { // find space if (im[i] ==' ') { checkResult(pass,name); colon = false; if(foundResult){ //foundResult=false; break; } vector<char> empty; swap(empty, pass); vector<char> empty2; swap(empty2, name); //pass.clear(); //name.clear(); } else if (im[i] == ':') { colon = true; } else { if(colon) pass.push_back(unsigned(im[i])); else name.push_back(char(im[i])); } } } void fallInterrupt() { int key_code=0; int i=0; int value=mpr121.read(0x00); value +=mpr121.read(0x01)<<8; //checks every key for press for (i=0; i<=11; i++) { //if key 10 (clr) is pressed, clear the last value on the vector will be popped off if ((((value>>i)&0x01)==1)&&(i==10)) { if (!password.empty()) { ledb = 1; password.pop_back(); //pop off last value on the vector //printPW();//for debugging //pc.printf("container size: %d\r\n", password.size());//for dubugging } break; } //if key 11 (ENTER) is pressed, the password will be submitted IF it is long enough else if ((((value>>i)&0x01)==1)&&(i==11)) { if(lock == 0){ lock = 1.0; break; } //if the password is long enough, check passwords, and clear vector. if (password.size() >= minLength) { //CALL checkResult() FUNCTION, OR SET A FLAG INDICATING THAT PASSWORD IS COMPLETE AND AVAILABLE TO BE CHECKED! //ledb = 1; //ans = password; //pc.printf("ENTER KEY PRESSED\n\r"); //printPW(); //for(unsigned k =0; k<password.size(); k++) // ans.push_back(password[k]); checkKeyboard(); rtc = time(NULL); if(foundResult){ foundResult=false; char buf[128]; sprintf(buf,"%s %s SUCCESS", print_vector(name).c_str(), ctime(&rtc)); if(accessLog.size()<logLength){ accessLog.push_back(buf); } else{ vector<string> empty; swap(accessLog,empty); //accessLog.clear(); accessLog.push_back(buf); } } else{ ledr = 1; char buf[128]; sprintf(buf,"%s %s FAILURE", print_vector(password).c_str(), ctime(&rtc)); if(accessLog.size()<logLength){ accessLog.push_back(buf); } else{ vector<string> empty; swap(accessLog,empty); //accessLog.clear(); accessLog.push_back(buf); } } //update_log(accessLog); updateAccessLog = true; //wait(0.005); ledg=0; ledr=0; vector<char> empty; swap(empty, password); //password.clear(); } else { //pc.printf("The password must be at least %d characters long\r\n", minLength); } break; } //if keys 0 through 9 are pressed else if (((value>>i)&0x01)==1) { //if max pw length has not been reached, push onto password vector if (password.size() < maxLength) { ledb = 1; key_code=i; password.push_back((char)(key_code+48)); //printPW(); //for debugging //pc.printf("times: %d\r\n", password.size()); if (password.size() == maxLength) { //pc.printf("Max password length reached. Press clr to delete character. Press any number key to reset.\r\n"); ledb = 0; ledr = 1; wait(0.05); ledr = 0; vector<char> empty; swap(empty, password); //password.clear(); } } //if max password length has been reached else { //pc.printf("Maximum password length reached, password has been cleared. \r\n"); //password.clear(); vector<char> empty; swap(empty, password); } break; } } wait(0.005); ledb = 0; } void save_to_file() { FILE * fp = fopen("/local/users.txt", "w"); if(fp != NULL) { for(unsigned i=0; i< im.size(); i++) { fprintf(fp,"%c",im[i]); } } fclose(fp); } void load_from_file() { FILE * fp = fopen("/local/users.txt", "r"); if(fp != NULL) { im.clear(); do { im.push_back(fgetc(fp)); } while(im.back() != '#'); } fclose(fp); } int i = 0; //Interrupt void Rx_interrupt() { if(!newData){ tempim.clear(); tempim.push_back(pc.getc()); newData = true; } else{ char c = pc.getc(); if (c!='#') { tempim.push_back(c); //writing all the characters into vector //checkKeyboard(im); } else { tempim.push_back(c); saveToFileFlag = true; im = tempim; newData = false; } } return; } int main() { pc.printf("\r\n\r\nMBED RESET\r\n"); lock = 1.0; pc.printf("Setting up WIFI\r\n"); //setup_wifi(); pc.printf("Setting Up HTTP\r\n"); //setup_http(); // pc.printf("updating access log\r\n"); // accessLog.push_back("test 1"); // accessLog.push_back("Hey Edison"); // accessLog.push_back("Great job!"); //update_log(accessLog); pc.printf("Setting up NTP\r\n"); //setup_ntp(); pc.printf("Updating NTP\r\n"); //update_ntp(); rtc = time(NULL); pc.printf("%s\r\n", ctime(&rtc)); load_from_file(); print_to_serial(); interrupt.fall(&fallInterrupt); interrupt.mode(PullUp); //load_from_file(); //interupt driven read from serial port // calls save_to_file(); pc.attach(&Rx_interrupt, Serial::RxIrq); ledg = 1; wait(0.3); ledg = 0; wait(0.3); ledg = 1; wait(0.3); ledg = 0; while(1){ if(updateAccessLog){ update_log(accessLog); updateAccessLog = false; } if(saveToFileFlag){ save_to_file(); saveToFileFlag = false; } /* if(newData){ pc.printf("read in from serial\r\n"); ledb = 1; vector<char> empty; swap(empty, im); while (1) { char c = pc.getc(); if (c!='#') { im.push_back(c); //writing all the characters into vector //checkKeyboard(im); } else { im.push_back(c); saveToFileFlag = true; newData = false; break; } } ledb = 0; } */ wait(0.1); //sleep(); } }