4180 Final Project

Dependencies:   4DGL-uLCD-SE mbed Servo

main.cpp

Committer:
pshabbaki3
Date:
2016-04-21
Revision:
12:22444d5f5920
Parent:
11:1ca1f07896b0
Child:
13:88d8408f6e60

File content as of revision 12:22444d5f5920:

#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);
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;
int index=0;
int maxLength = 18; //random length chosen
int minLength = 5;  //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 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()) {
                password.pop_back(); //pop off last value on the vector
                printPW();//for debugging
                pc.printf("container size: %d\r\n", password.size());//for debugging
            }
        }

        //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) { // --------- password.size() == 6 -------------- PAYMAN
                ans = password; // ------checkResult() , passing password to ans (vector used in checkResult() ------ PAYMAN
                //CALL checkResult() FUNCTION, OR SET A FLAG INDICATING THAT PASSWORD IS COMPLETE AND AVAILABLE TO BE CHECKED!
                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!
            }

            else {
                pc.printf("The password must be at least %d characters long\r\n", minLength);
            }
        }

        //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) {
                key_code=i;
                password.push_back((char)key_code);
                printPW(); //for debugging
            }

            //if max password length has been reached
            else {
                pc.printf("Maximum password length reached.\r\n");
            }
        }
    }
}

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;

void checkResult(vector<char> pass, vector<char> name)
{
    // This function will check the keypad values and check if the passcode exists or not

    // vector<char> ans(password.size()); //This vector should be the values coming from keypad
    //ans = password; // password from fallInterrupt function


    if (pass.size()==6) {
        i++; //use "i" to find which person logged in.
        if (ans == pass) {
//            pc.printf("Found");
            final = name;
            final.insert(name.end(),pass.begin(),pass.end());

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


void checkKeyboard(vector<char> im)
{
    for (int i = 0; i<im.size(); i++) {
        // find space
        if (im[i] ==' ') {
            // Check the result
            checkResult(pass,name);
            // clear pass, name and final
            pass.clear();
            name.clear();
//        pc.printf("clear");
            //lcd.printf("clear");
        } else {
            if (isdigit(im[i])) {
                // write numbers (char) to vector pass
                pass.push_back(im[i]);

            } else if (isalpha(im[i])) {
                // write letters (char) to vector name
                name.push_back(im[i]);

            }
        }
    }
}


//Interrupt
void Rx_interrupt()
{
    while (pc.readable()) {
        char c = pc.getc();
        if (c!='#') {
            im.push_back(c); //writing all the characters into vector
            checkKeyboard(im);
        } else {
            for (int cnt = 0; cnt<im.size(); cnt++) {
                pc.printf("%c",im[cnt]);
                //lcd.printf("%c",im[cnt]);
            }
        }

    }
    return;
}


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