Stephen Ralph / Mbed 2 deprecated CryptoPenCreateKey

Dependencies:   mbed 4DGL-uLCD-SE SDFileSystem PinDetect

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mainPart1.cpp Source File

mainPart1.cpp

00001 #include "mbed.h"
00002 #include "Speaker.h"
00003 #include "PinDetect.h"
00004 #include <ctime>
00005 #include "uLCD_4DGL.h"
00006 #include "SDFileSystem.h"
00007 #include "TMP36.h"
00008 uLCD_4DGL uLCD(p28, p27, p29); // serial tx, serial rx, reset pin;
00009 SDFileSystem sd(p5, p6, p7, p8, "sd");
00010 
00011 //declare objects for pins used with pushbuttons
00012 PinDetect pb1(p18);
00013 PinDetect pb2(p17);
00014 PinDetect pb3(p16);
00015 
00016 //declare a speaker object
00017 Speaker mySpeaker(p21);
00018 
00019 //declare enums
00020 enum InputType {YES,NO};
00021 enum StateType {Q0, Q1, Q2, Q3};
00022 
00023 //declare rest of variables
00024 InputType input = NO;
00025 StateType state = Q0;
00026 char cipherText [1001];
00027 bool isWritten= false;
00028 
00029 // Callback routine is interrupt activated by a debounced pb3 hit
00030 void pb3_hit_callback (void)
00031 {
00032     // ADD CODE HERE THAT YOU WHAT TO RUN WHEN INTERUPT IS GENERATED
00033     input = NO;
00034 }
00035     // Callback routine is interrupt activated by a debounced pb1 hit
00036 void pb1_hit_callback (void)
00037     {
00038         mkdir("/sd/mydir", 0777);
00039                 FILE *fp = fopen("/sd/mydir/OTP.txt", "w");
00040                 if(fp == NULL) {
00041                     uLCD.printf("Error Open \n");
00042                  }
00043          
00044                 uLCD.cls();
00045                  fprintf(fp, "%s", cipherText);
00046                 fclose(fp);
00047                 uLCD.printf("Done;");
00048         input = YES;
00049 }
00050         
00051 // Callback routine is interrupt activated by a debounced pb2 hit
00052 void pb2_hit_callback (void)
00053  {
00054          input = NO;
00055 }
00056 
00057     
00058 int main() {
00059     
00060     //instantiatenew class to set p15 to analog input
00061     //to read and convert TMP36 sensor's voltage output
00062     TMP36 myTMP36(p15);
00063     float tempC1, tempC2, tDiff;
00064     srand (time(NULL));
00065     int ary [1000];
00066     
00067     for(int i=0; i< 1000; i++){
00068         int rando=0;
00069         int fin=0;
00070         rando = rand();
00071         
00072         tempC1 = myTMP36.read();
00073         //wait(.1);
00074         tempC2 = myTMP36.read();
00075         tDiff = (tempC1-tempC2)*1000;
00076         int TRN = static_cast<int>(tDiff);
00077         fin = TRN + rando;
00078         fin = fin% 26;
00079         ary[i]=fin;
00080         }
00081         
00082     for (int i = 0; i < 1000; i++){
00083          int asciiVal = ary[i]+65;
00084         char asciiChar = asciiVal;
00085         cipherText[i]=asciiChar;
00086     }
00087     char z;
00088     z=' ';
00089     cipherText [1000]= z;
00090     
00091     
00092     //Make both positioncipher files and initialize them to 1;
00093     mkdir("/sd/mydir", 0777);
00094     FILE *fp = fopen("/sd/mydir/positionCipherSender.txt", "w");
00095     if(fp == NULL) {
00096         uLCD.printf("Error Open \n");
00097     }
00098     fprintf(fp, "%i", 0);
00099     fclose(fp);
00100     mkdir("/sd/mydir", 0777);
00101     fp = fopen("/sd/mydir/positionCipherReceiver.txt", "w");
00102     if(fp == NULL) {
00103         uLCD.printf("Error Open \n");
00104     }
00105     fprintf(fp, "%i", 0);
00106     fclose(fp);
00107     
00108     //Initialize pins
00109     pb1.mode(PullUp);
00110     pb2.mode(PullUp);
00111     pb3.mode(PullUp);
00112     
00113     // Delay for initial pullup to take effect
00114     wait(.01);
00115     
00116     // Setup Interrupt callback functions for a pb hit
00117 pb1.attach_deasserted(&pb1_hit_callback);
00118 pb2.attach_deasserted(&pb2_hit_callback);
00119 pb3.attach_deasserted(&pb3_hit_callback);
00120 
00121 // Start sampling pb inputs using interrupts
00122 pb1.setSampleFrequency(); //default is 20KHz sampling
00123 pb2.setSampleFrequency();
00124 pb3.setSampleFrequency();
00125 // pushbuttons now setup and running
00126 
00127 while(1) {
00128     switch(state){
00129         case(Q0):
00130             //Produce output for this state
00131             if(!isWritten){
00132                 uLCD.cls();
00133                 uLCD.printf("Save to SD?");
00134                 isWritten=true;
00135             }
00136             //calculate next state
00137             if (input == YES){
00138                 input = NO;
00139                 state = Q1;
00140                 isWritten=false;
00141                 uLCD.cls();
00142                 }
00143             else {
00144                 state = Q0;
00145                 }
00146             break;
00147         
00148         case (Q1):
00149              //Produce output for this state
00150              if(!isWritten){
00151                 uLCD.cls();
00152                 uLCD.printf("Save to another SD?");
00153                 isWritten=true;
00154             }
00155                 
00156               //calculate next state
00157               if (input == YES){
00158                  input = NO;
00159                  state = Q1;
00160                  isWritten=false;
00161                  }
00162             else //input should be stay
00163                 state = Q1;
00164             break;
00165             
00166         }
00167         //end switch
00168         //wait (0.1);
00169     }
00170 }