4180 Final Project

Dependencies:   4DGL-uLCD-SE mbed Servo

main.cpp

Committer:
pshabbaki3
Date:
2016-04-24
Revision:
22:0dfc46f6ba93
Parent:
21:ff7bacd317fc
Child:
23:e23578000e42

File content as of revision 22:0dfc46f6ba93:

#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 <list>
#include <mpr121.h>

LocalFileSystem local("local");

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);


vector<char> im;
vector<char> pass;
vector<char> name;
vector<char> final;
vector<char> ans;

vector<char> password;
unsigned maxLength = 18; //random length chosen
unsigned minLength = 6;  //random length chosen


//function for debugging
void printPW()
{
    for (int i=0; i<password.size(); i++) {
        pc.printf("%d", int(password[i]));
    }
    pc.printf("\r\n");
}


void checkResult(vector<char> pass, vector<char> name)
{
    // 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 (ans == pass) {
            pc.printf("Found");
            final = name;
            final.push_back(' ');
            final.insert(final.end(),pass.begin(),pass.end());

        } else {
            pc.printf("not found");
            //lcd.printf("\nnot found\n");
            final.clear();
        }
    }
}


void checkKeyboard()
{
    load_from_file();
    pc.printf("CHECKING PASSWORD\n\r");
    bool colon = false;
    for (int i = 0; i<im.size(); i++) {

        // find space
        if (im[i] ==' ') {
            checkResult(pass,name);
            colon = false;
            pass.clear();
            name.clear();

        } else if (im[i] == ':') {
            colon = true;
        } else {
            if(colon)
                pass.push_back(im[i]);
            else
                name.push_back(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 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;
                checkKeyboard();
                vector<char> empty;
                swap(empty, password);
            }

            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);
                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");
                    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();
            }
            break;
        }

    }
    ledb = 0;

}


void print_to_serial()
{
    for(unsigned i=0; i< im.size(); i++) {
        pc.printf("%c",im[i]);
    }
}

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()
{
    //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;
        }

    }

    return;
}


int main()
{
    pc.printf("MBED RESET\r\n");
    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();

    //interupt driven read from serial port
    // calls save_to_file();
    pc.attach(&Rx_interrupt, Serial::RxIrq);

    while(1)
        sleep();
}