4180 Final Project

Dependencies:   4DGL-uLCD-SE mbed Servo

Revision:
25:2e95a5731412
Parent:
24:94e8b3e00c24
Child:
26:f08205fa22d3
--- a/main.cpp	Wed Apr 27 19:33:54 2016 +0000
+++ b/main.cpp	Thu Apr 28 19:26:59 2016 +0000
@@ -8,11 +8,13 @@
 #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);
@@ -32,6 +34,17 @@
 // 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> pass;
@@ -39,6 +52,12 @@
 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
@@ -60,25 +79,34 @@
     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");
+    */
+    
+    //pc.printf("\r\n");
     // This function will check the keypad values and check if the passcode exists or not
 
     if (pass.size()==6) {
-        //i++; //use "i" to find which person logged in.
         if (password == pass) {
-            pc.printf("Found");
+            //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");
+            //pc.printf("not found");
             //lcd.printf("\nnot found\n");
             final.clear();
         }
@@ -89,17 +117,31 @@
 void checkKeyboard()
 {
     //load_from_file();
-    pc.printf("CHECKING PASSWORD\n\r");
-    pc.printf("\n\r");
+    //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;
-            pass.clear();
-            name.clear();
+            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;
@@ -124,34 +166,70 @@
 
         //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
+                //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;
+                //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]);
+                //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);
+                //pc.printf("The password must be at least %d characters long\r\n", minLength);
             }
             break;
         }
@@ -166,11 +244,15 @@
                 
                 key_code=i;
                 password.push_back((char)(key_code+48));
-                printPW(); //for debugging
-                pc.printf("times: %d\r\n", password.size());
+                //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();
@@ -179,13 +261,16 @@
 
             //if max password length has been reached
             else {
-                pc.printf("Maximum password length reached, password has been cleared. \r\n");
-                password.clear();
+                //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;
 
 }
@@ -223,25 +308,7 @@
 //Interrupt
 void Rx_interrupt()
 {
-    //pc.printf("read in from serial\r\n");
-    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 {
-            save_to_file();
-            for (int cnt = 0; cnt<im.size(); cnt++) {
-                pc.printf("%c",im[cnt]);
-                //lcd.printf("%c",im[cnt]);
-            }
-            break;
-        }
-
-    }
+    newData = true;
 
     return;
 }
@@ -249,21 +316,29 @@
 
 int main()
 {
-    pc.printf("MBED RESET\r\n");
+    
+    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();
-    /*
-    ledr = 1;
-    wait(1);
-    ledr = 0;
-    ledg = 1;
-    wait(1);
-    ledg = 0;
-    ledb = 1;
-    wait(1);
-    ledb = 0;
-    /**/    
-    
+ 
     interrupt.fall(&fallInterrupt);
     interrupt.mode(PullUp);
     //load_from_file();
@@ -271,9 +346,53 @@
     //interupt driven read from serial port
     // calls save_to_file();
     pc.attach(&Rx_interrupt, Serial::RxIrq);
-
-    while(1)
-        sleep();
+    
+    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();
+    
+    }
+    
 }