Stephen Ralph / Mbed 2 deprecated CryptoPenEncrypt

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 encrypt (char inp, char arrayCipher[], int startPt)
00028 {
00029     int sum = (arrayCipher[startPt]-65) + (inp-65);
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("ENCRYPTING");
00041         wait(1.0);
00042         char inC = currLetter.getChar();
00043         char outC=encrypt(inC, arrayCipher, position);
00044         uLCD.cls();
00045         
00046         uLCD.locate(0,0);
00047         uLCD.text_width(5);
00048         uLCD.text_height(5);
00049         
00050         
00051         uLCD.printf("%c", outC);
00052         position++;
00053         
00054         //update position in txt doc
00055         fp2 = fopen("/sd/mydir/positionCipherSender.txt", "w");
00056         if(fp2 == NULL) {
00057             uLCD.printf("Error Open \n");
00058              }
00059         else{
00060             fprintf(fp2, "%i",position);
00061             fclose(fp2);
00062             }
00063         
00064         wait(1.0);
00065         uLCD.cls();
00066         input = ENC;
00067         }
00068     else{
00069         input=ENC;
00070         }
00071     
00072 }
00073     // Callback routine is interrupt activated by a debounced pb1 hit
00074 void pb1_hit_callback (void)
00075     {
00076         // ADD CODE HERE THAT YOU WHAT TO RUN WHEN INTERUPT IS GENERATED
00077         currLetter.inc();
00078 }
00079         
00080 // Callback routine is interrupt activated by a debounced pb2 hit
00081 void pb2_hit_callback (void)
00082  {
00083          currLetter.dec();
00084 }
00085 
00086 
00087 int main() {
00088     
00089     FILE *fp = fopen("/sd/mydir/OTP.txt", "r");
00090     if(fp == NULL){
00091         uLCD.printf("Open Error!!!\n");
00092     }
00093     else{
00094         fscanf(fp, "%s", arrayCipher);
00095         fclose(fp);
00096     }
00097     
00098     FILE *fp2 = fopen("/sd/mydir/positionCipherSender.txt", "r");
00099     if(fp2 == NULL) {
00100         uLCD.printf("Open Error!!!\n");
00101          }
00102     else{
00103         fscanf(fp2, "%i",&position);
00104         fclose(fp2);
00105         }
00106     
00107   //  int sizeIn = sizeof(inChars)/sizeof(*inChars);
00108 
00109 
00110     pb1.mode(PullUp);
00111     pb2.mode(PullUp);
00112     pb3.mode(PullUp);
00113     
00114     // Delay for initial pullup to take effect
00115     wait(.01);
00116     
00117     // Setup Interrupt callback functions for a pb hit
00118     pb1.attach_deasserted(&pb1_hit_callback);
00119     pb2.attach_deasserted(&pb2_hit_callback);
00120     pb3.attach_deasserted(&pb3_hit_callback);
00121 
00122     // Start sampling pb inputs using interrupts
00123     pb1.setSampleFrequency(); //default is 20KHz sampling
00124     pb2.setSampleFrequency();
00125     pb3.setSampleFrequency();
00126     // pushbuttons now setup and running
00127 
00128 while(1) {
00129     switch(state){
00130         case(Q0):
00131             //Produce output for this state
00132             uLCD.locate(0,0);
00133             uLCD.text_width(5);
00134             uLCD.text_height(5);
00135             uLCD.printf("%c",currLetter.getChar());
00136             wait(.5);
00137             //calculate next state
00138             if (input == ENC){
00139                 state = Q1;
00140                 input=NON;
00141              }
00142             else{ //input should be stay
00143                 state = Q0;
00144             }
00145             break;
00146         
00147         case (Q1):
00148              //Produce output for this state
00149             
00150             uLCD.locate(0,0);
00151             uLCD.text_width(1);
00152             uLCD.text_height(1);
00153             uLCD.printf("Press Again To Encrypt More");
00154               //calculate next state
00155               if (input == ENC){
00156                  state = Q0;
00157                  input = NON;
00158                  uLCD.cls();
00159                  }
00160             else //input should be stay
00161                 state = Q1;
00162             break;
00163             
00164         }
00165         //end switch
00166         wait (0.1);
00167     }
00168 }