Stephen Ralph / Mbed 2 deprecated CryptoPenDecrypt

Dependencies:   mbed 4DGL-uLCD-SE SDFileSystem PinDetect

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "PinDetect.h"
00003 #include "uLCD_4DGL.h"
00004 #include "SDFileSystem.h"
00005 #include "Letter.h"
00006 uLCD_4DGL uLCD(p28, p27, p29); // serial tx, serial rx, reset pin;
00007 SDFileSystem sd(p5, p6, p7, p8, "sd");
00008 
00009 //declare objects for pins used with pushbuttons
00010 PinDetect pb1(p18);
00011 PinDetect pb2(p17);
00012 PinDetect pb3(p16);
00013 
00014 //Populate arrayCipher from cipher on SD
00015 char arrayCipher [1001];
00016 
00017 enum InputType {FWD,BACK,ENC,NON};
00018 enum StateType {Q0, Q1, Q2, Q3};
00019 Letter currLetter;
00020 int position=0;
00021 FILE *fp2;
00022 
00023 InputType input = NON;
00024 StateType state = Q0;
00025 
00026 //Encrypt
00027 char decrypt (char inp, char arrayCipher[], int startPt)
00028 {
00029     int sum = (inp-65)-(arrayCipher[startPt]-65) + 26;
00030     sum = sum % 26;
00031     char sumC = sum;
00032     return sumC+65;
00033 }
00034 
00035 // Callback routine is interrupt activated by a debounced pb3 hit
00036 void pb3_hit_callback (void)
00037 {
00038     if (state==Q0){
00039         uLCD.cls();
00040         uLCD.printf("DECRYPTING");
00041         wait(1.0);
00042         char inC = currLetter.getChar();
00043         char outC=decrypt(inC, arrayCipher, position);
00044         uLCD.cls();
00045         uLCD.text_width(5);
00046         uLCD.text_height(5);
00047         uLCD.printf("%c", outC);
00048         position++;
00049         
00050         //update position in txt doc
00051         fp2 = fopen("/sd/mydir/positionCipherReceiver.txt", "w");
00052         if(fp2 == NULL) {
00053             uLCD.printf("Error Open \n");
00054              }
00055         else{
00056             fprintf(fp2, "%i",position);
00057             fclose(fp2);
00058             }
00059         
00060         wait(1.0);
00061         input = ENC;
00062         }
00063     else{
00064         input=ENC;
00065         }
00066     
00067 }
00068     // Callback routine is interrupt activated by a debounced pb1 hit
00069 void pb1_hit_callback (void)
00070     {
00071         // ADD CODE HERE THAT YOU WHAT TO RUN WHEN INTERUPT IS GENERATED
00072         currLetter.inc();
00073 }
00074         
00075 // Callback routine is interrupt activated by a debounced pb2 hit
00076 void pb2_hit_callback (void)
00077  {
00078          currLetter.dec();
00079 }
00080 
00081 
00082 int main() {
00083     
00084     FILE *fp = fopen("/sd/mydir/OTP.txt", "r");
00085     if(fp == NULL){
00086         uLCD.printf("Open Error!!!\n");
00087     }
00088     else{
00089         fscanf(fp, "%s", arrayCipher);
00090         fclose(fp);
00091     }
00092     
00093     FILE *fp2 = fopen("/sd/mydir/positionCipherReceiver.txt", "r");
00094     if(fp2 == NULL) {
00095         uLCD.printf("Open Error!!!\n");
00096          }
00097     else{
00098         fscanf(fp2, "%i",&position);
00099         fclose(fp2);
00100         }
00101     
00102   //  int sizeIn = sizeof(inChars)/sizeof(*inChars);
00103 
00104 
00105     pb1.mode(PullUp);
00106     pb2.mode(PullUp);
00107     pb3.mode(PullUp);
00108     
00109     // Delay for initial pullup to take effect
00110     wait(.01);
00111     
00112     // Setup Interrupt callback functions for a pb hit
00113     pb1.attach_deasserted(&pb1_hit_callback);
00114     pb2.attach_deasserted(&pb2_hit_callback);
00115     pb3.attach_deasserted(&pb3_hit_callback);
00116 
00117     // Start sampling pb inputs using interrupts
00118     pb1.setSampleFrequency(); //default is 20KHz sampling
00119     pb2.setSampleFrequency();
00120     pb3.setSampleFrequency();
00121     // pushbuttons now setup and running
00122 
00123 while(1) {
00124     switch(state){
00125         case(Q0):
00126             //Produce output for this state
00127             uLCD.locate(0,0);
00128             uLCD.text_width(5);
00129             uLCD.text_height(5);
00130             uLCD.printf("%c",currLetter.getChar());
00131             wait(.5);
00132             //calculate next state
00133             if (input == ENC){
00134                 state = Q1;
00135                 input=NON;
00136                 uLCD.cls();
00137              }
00138             else{ //input should be stay
00139                 state = Q0;
00140             }
00141             break;
00142         
00143         case (Q1):
00144              //Produce output for this state
00145             
00146             uLCD.locate(0,0);
00147             uLCD.text_width(1);
00148             uLCD.text_height(1);
00149             uLCD.printf("Press Again To Decrypt More");
00150               //calculate next state
00151               if (input == ENC){
00152                  state = Q0;
00153                  input = NON;
00154                  uLCD.cls();
00155                  }
00156             else //input should be stay
00157                 state = Q1;
00158             break;
00159             
00160         }
00161         //end switch
00162         wait (0.1);
00163     }
00164 }