Jacob Smith / Mbed 2 deprecated ECE4180_Touchpad_V2

Dependencies:   PinDetect mbed

Dependents:   IoT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include <mbed.h>
00002 #include <mpr121.h>
00003 #include <stdlib.h>
00004 #include "PinDetect.h"
00005 
00006 /* CODE_LENGTH needs to be double the amount of numbers you
00007       want in your authenticator/passcode because of the way interrupt.fall(&fallInterrupt)
00008       works. It is called twice every time an interrupt is detected.
00009       The extra numbers in the array will just be filled with zeros and ignored in checking 
00010       code sequences, so they will not matter either way */
00011       /* i.e, you want a code with 7 numbers, CODE_LENGTH needs to be 14 */
00012 #define CODE_LENGTH 14
00013 
00014 DigitalOut led1(LED1);
00015 DigitalOut led2(LED2);
00016 DigitalOut led3(LED3);
00017 DigitalOut led4(LED4);
00018 
00019 DigitalOut doorlock(p29);
00020 
00021 // Create the interrupt receiver object on pin 26
00022 InterruptIn interrupt(p30);
00023 
00024 // Setup the i2c bus on pins 9 and 10
00025 I2C i2c(p9, p10);
00026 
00027 // Setup the Mpr121:
00028 // constructor(i2c object, i2c address of the mpr121)
00029 Mpr121 mpr121(&i2c, Mpr121::ADD_VSS);
00030 
00031 // pc serial communication for testing
00032 Serial pc(USBTX, USBRX);
00033 
00034 // ***** GLOBALS ***** //
00035 // Timer is to seed rand
00036 Timer t1;
00037 // code counter is the next position in inputCode array
00038 int codeCounter;
00039 // inputCode array is the sequence of numbers the user will enter
00040 int inputCode[CODE_LENGTH];
00041 bool code_enabled;
00042  
00043 // Key hit/release interrupt routine
00044 void fallInterrupt() {
00045     int key_code=0;
00046     int i=0;
00047     int value=mpr121.read(0x00);
00048     value +=mpr121.read(0x01)<<8;
00049     // LED demo mod
00050     i=0;
00051     // puts key number out to LEDs for demo
00052     for (i=0; i<12; i++) {
00053     if (((value>>i)&0x01)==1) key_code=i+1;
00054     }
00055     led4=key_code & 0x01;
00056     led3=(key_code>>1) & 0x01;
00057     led2=(key_code>>2) & 0x01;
00058     led1=(key_code>>3) & 0x01;
00059   
00060     // save the keypress to inputCode array
00061     if(codeCounter < CODE_LENGTH){
00062         // ignore odd numbers
00063         if(codeCounter % 2 != 0){
00064             inputCode[codeCounter] = 0;
00065         }
00066         // only save the even numbers (see lines 6-10)
00067         else{
00068             inputCode[codeCounter] = key_code - 1;
00069             pc.printf("codeCounter: %d  --  code:  %d\n\r", codeCounter, key_code - 1);
00070         }      
00071         codeCounter++;
00072     }
00073 }
00074 
00075 // generate randomized code
00076 void generate_random_code(int (&codeArray)[CODE_LENGTH]){
00077     int i = 0;
00078     // only care about the even numbers (see lines 6-10)
00079     pc.printf("NEW CODE: ");
00080     for(i = 0; i < CODE_LENGTH; i+=2){
00081         srand(t1.read_us());
00082         codeArray[i] = rand() % 12;
00083         pc.printf("%d,  ", codeArray[i]);
00084     }
00085     pc.printf("\n\r");
00086 }
00087 
00088 // check if the code entered is the correct code
00089 bool check_code_sequence(int (&codeArray)[CODE_LENGTH]){
00090     int i = 0;
00091     int j = 0;
00092     // only care about the even numbers (see lines 6-10)
00093     for(i = 0; i < CODE_LENGTH; i+=2){
00094         if(inputCode[i] == codeArray[i])
00095         j++; // count the number of right numbers
00096     }
00097     if(j == CODE_LENGTH/2)
00098         return(true);
00099     else
00100         return(false);
00101 }
00102  
00103 int main() {
00104     interrupt.fall(&fallInterrupt);
00105     interrupt.mode(PullUp);
00106     
00107     // authenticator is the randomly generated sequence of numbers by the machine
00108     // the user has to match this sequence to gain access, used for phase 2
00109     int authenticator[CODE_LENGTH];
00110     // passcode is the user's personal passcode, used for phase 1
00111     int passcode[CODE_LENGTH] = {0,0,1,0,2,0,3,0,4,0,5,0,6,0};
00112     codeCounter = 0;
00113     bool pass = false;
00114     
00115     // these 2 variables tell the machine when to generate a new random authentication code
00116     int new_code_timer = 0;
00117     int new_code_counter = 0;
00118     // this tells the state machine with phase of authentication we are in
00119     code_enabled = false;
00120     
00121     for(int i = 0; i < CODE_LENGTH; i++){
00122       authenticator[i] = 0;
00123       inputCode[i] = 0;
00124     }
00125     
00126     // go ahead and start the timer so that when a random code is generated,
00127     // the seed will always be random, unlike the predecessor version
00128     t1.start();
00129     
00130     // while loop constantly checks if the entered code sequence is right or wrong,
00131     // given that the correct amount of numbers were entered
00132     pc.printf("Please Enter Your Personal Security Code\n\r");
00133     while (1){
00134         switch(code_enabled){
00135             case false:
00136                 if(codeCounter >= CODE_LENGTH){
00137                     pass = check_code_sequence(passcode);
00138                     if(pass == true){
00139                         pc.printf("SENDING AUTHENTICATION CODE...\n\r");
00140                         generate_random_code(authenticator);
00141                         t1.stop(); // reset the time
00142                         t1.reset(); // so that it is an even
00143                         t1.start(); // 30 seconds before 1st new code is generated
00144                         codeCounter = 0;
00145                         code_enabled = true;
00146                     }
00147                     else{
00148                         pc.printf("WRONG passcode\n\r");
00149                         codeCounter = 0;
00150                     }
00151                 }
00152                 break;
00153             case true:
00154                 if(codeCounter >= CODE_LENGTH){
00155                     pass = check_code_sequence(authenticator);
00156                     if(pass == true){
00157                         pc.printf("ACCESS GRANTED\n\r");
00158                         doorlock = 1;
00159                         wait(5);
00160                         doorlock = 0;
00161                         pc.printf("Resetting....\n\r");
00162                         pc.printf("\n\n\rPlease Enter Your Personal Security Code\n\r");
00163                         codeCounter = 0;
00164                         code_enabled = false;
00165                     }
00166                     else{
00167                         pc.printf("ACCESS DENIED\n\r");
00168                         codeCounter = 0;
00169                     }
00170                 }
00171                 // this code generates a new authentication code every 30 seconds (30000 ms)
00172                 new_code_timer = (int)(t1.read_ms()/30000);
00173                 if(new_code_timer > new_code_counter){
00174                     new_code_counter++;
00175                     generate_random_code(authenticator);
00176                     codeCounter = 0;
00177                 }
00178                 break;
00179         }
00180         // reset the timer when the number gets too high, should last about 35 minutes
00181         //  We do this because int can only hold a number up to 2^32 - 1, preventing errors
00182         if(t1.read_us() > 2147400000){
00183             t1.stop();
00184             t1.reset();
00185             new_code_timer = 0;
00186             new_code_counter = 0;
00187             t1.start();
00188         }
00189     }
00190 }