Pt. 4 | Encryption encrypt program
Dependencies: mbed 4DGL-uLCD-SE SDFileSystem PinDetect
main.cpp@0:72aeef60e1fc, 2019-01-03 (annotated)
- Committer:
- sralph3
- Date:
- Thu Jan 03 22:40:26 2019 +0000
- Revision:
- 0:72aeef60e1fc
Pt. 4 | Encryption encrypt program
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sralph3 | 0:72aeef60e1fc | 1 | #include "mbed.h" |
sralph3 | 0:72aeef60e1fc | 2 | #include "PinDetect.h" |
sralph3 | 0:72aeef60e1fc | 3 | #include "uLCD_4DGL.h" |
sralph3 | 0:72aeef60e1fc | 4 | #include "SDFileSystem.h" |
sralph3 | 0:72aeef60e1fc | 5 | #include "Letter.h" |
sralph3 | 0:72aeef60e1fc | 6 | uLCD_4DGL uLCD(p28, p27, p29); // serial tx, serial rx, reset pin; |
sralph3 | 0:72aeef60e1fc | 7 | SDFileSystem sd(p5, p6, p7, p8, "sd"); |
sralph3 | 0:72aeef60e1fc | 8 | |
sralph3 | 0:72aeef60e1fc | 9 | //declare objects for pins used with pushbuttons |
sralph3 | 0:72aeef60e1fc | 10 | PinDetect pb1(p18); |
sralph3 | 0:72aeef60e1fc | 11 | PinDetect pb2(p17); |
sralph3 | 0:72aeef60e1fc | 12 | PinDetect pb3(p16); |
sralph3 | 0:72aeef60e1fc | 13 | |
sralph3 | 0:72aeef60e1fc | 14 | //Populate arrayCipher from cipher on SD |
sralph3 | 0:72aeef60e1fc | 15 | char arrayCipher [1001]; |
sralph3 | 0:72aeef60e1fc | 16 | |
sralph3 | 0:72aeef60e1fc | 17 | enum InputType {FWD,BACK,ENC,NON}; |
sralph3 | 0:72aeef60e1fc | 18 | enum StateType {Q0, Q1, Q2, Q3}; |
sralph3 | 0:72aeef60e1fc | 19 | Letter currLetter; |
sralph3 | 0:72aeef60e1fc | 20 | int position=0; |
sralph3 | 0:72aeef60e1fc | 21 | FILE *fp2; |
sralph3 | 0:72aeef60e1fc | 22 | |
sralph3 | 0:72aeef60e1fc | 23 | InputType input = NON; |
sralph3 | 0:72aeef60e1fc | 24 | StateType state = Q0; |
sralph3 | 0:72aeef60e1fc | 25 | |
sralph3 | 0:72aeef60e1fc | 26 | //Encrypt |
sralph3 | 0:72aeef60e1fc | 27 | char decrypt (char inp, char arrayCipher[], int startPt) |
sralph3 | 0:72aeef60e1fc | 28 | { |
sralph3 | 0:72aeef60e1fc | 29 | int sum = (inp-65)-(arrayCipher[startPt]-65) + 26; |
sralph3 | 0:72aeef60e1fc | 30 | sum = sum % 26; |
sralph3 | 0:72aeef60e1fc | 31 | char sumC = sum; |
sralph3 | 0:72aeef60e1fc | 32 | return sumC+65; |
sralph3 | 0:72aeef60e1fc | 33 | } |
sralph3 | 0:72aeef60e1fc | 34 | |
sralph3 | 0:72aeef60e1fc | 35 | // Callback routine is interrupt activated by a debounced pb3 hit |
sralph3 | 0:72aeef60e1fc | 36 | void pb3_hit_callback (void) |
sralph3 | 0:72aeef60e1fc | 37 | { |
sralph3 | 0:72aeef60e1fc | 38 | if (state==Q0){ |
sralph3 | 0:72aeef60e1fc | 39 | uLCD.cls(); |
sralph3 | 0:72aeef60e1fc | 40 | uLCD.printf("DECRYPTING"); |
sralph3 | 0:72aeef60e1fc | 41 | wait(1.0); |
sralph3 | 0:72aeef60e1fc | 42 | char inC = currLetter.getChar(); |
sralph3 | 0:72aeef60e1fc | 43 | char outC=decrypt(inC, arrayCipher, position); |
sralph3 | 0:72aeef60e1fc | 44 | uLCD.cls(); |
sralph3 | 0:72aeef60e1fc | 45 | uLCD.text_width(5); |
sralph3 | 0:72aeef60e1fc | 46 | uLCD.text_height(5); |
sralph3 | 0:72aeef60e1fc | 47 | uLCD.printf("%c", outC); |
sralph3 | 0:72aeef60e1fc | 48 | position++; |
sralph3 | 0:72aeef60e1fc | 49 | |
sralph3 | 0:72aeef60e1fc | 50 | //update position in txt doc |
sralph3 | 0:72aeef60e1fc | 51 | fp2 = fopen("/sd/mydir/positionCipherReceiver.txt", "w"); |
sralph3 | 0:72aeef60e1fc | 52 | if(fp2 == NULL) { |
sralph3 | 0:72aeef60e1fc | 53 | uLCD.printf("Error Open \n"); |
sralph3 | 0:72aeef60e1fc | 54 | } |
sralph3 | 0:72aeef60e1fc | 55 | else{ |
sralph3 | 0:72aeef60e1fc | 56 | fprintf(fp2, "%i",position); |
sralph3 | 0:72aeef60e1fc | 57 | fclose(fp2); |
sralph3 | 0:72aeef60e1fc | 58 | } |
sralph3 | 0:72aeef60e1fc | 59 | |
sralph3 | 0:72aeef60e1fc | 60 | wait(1.0); |
sralph3 | 0:72aeef60e1fc | 61 | input = ENC; |
sralph3 | 0:72aeef60e1fc | 62 | } |
sralph3 | 0:72aeef60e1fc | 63 | else{ |
sralph3 | 0:72aeef60e1fc | 64 | input=ENC; |
sralph3 | 0:72aeef60e1fc | 65 | } |
sralph3 | 0:72aeef60e1fc | 66 | |
sralph3 | 0:72aeef60e1fc | 67 | } |
sralph3 | 0:72aeef60e1fc | 68 | // Callback routine is interrupt activated by a debounced pb1 hit |
sralph3 | 0:72aeef60e1fc | 69 | void pb1_hit_callback (void) |
sralph3 | 0:72aeef60e1fc | 70 | { |
sralph3 | 0:72aeef60e1fc | 71 | // ADD CODE HERE THAT YOU WHAT TO RUN WHEN INTERUPT IS GENERATED |
sralph3 | 0:72aeef60e1fc | 72 | currLetter.inc(); |
sralph3 | 0:72aeef60e1fc | 73 | } |
sralph3 | 0:72aeef60e1fc | 74 | |
sralph3 | 0:72aeef60e1fc | 75 | // Callback routine is interrupt activated by a debounced pb2 hit |
sralph3 | 0:72aeef60e1fc | 76 | void pb2_hit_callback (void) |
sralph3 | 0:72aeef60e1fc | 77 | { |
sralph3 | 0:72aeef60e1fc | 78 | currLetter.dec(); |
sralph3 | 0:72aeef60e1fc | 79 | } |
sralph3 | 0:72aeef60e1fc | 80 | |
sralph3 | 0:72aeef60e1fc | 81 | |
sralph3 | 0:72aeef60e1fc | 82 | int main() { |
sralph3 | 0:72aeef60e1fc | 83 | |
sralph3 | 0:72aeef60e1fc | 84 | FILE *fp = fopen("/sd/mydir/OTP.txt", "r"); |
sralph3 | 0:72aeef60e1fc | 85 | if(fp == NULL){ |
sralph3 | 0:72aeef60e1fc | 86 | uLCD.printf("Open Error!!!\n"); |
sralph3 | 0:72aeef60e1fc | 87 | } |
sralph3 | 0:72aeef60e1fc | 88 | else{ |
sralph3 | 0:72aeef60e1fc | 89 | fscanf(fp, "%s", arrayCipher); |
sralph3 | 0:72aeef60e1fc | 90 | fclose(fp); |
sralph3 | 0:72aeef60e1fc | 91 | } |
sralph3 | 0:72aeef60e1fc | 92 | |
sralph3 | 0:72aeef60e1fc | 93 | FILE *fp2 = fopen("/sd/mydir/positionCipherReceiver.txt", "r"); |
sralph3 | 0:72aeef60e1fc | 94 | if(fp2 == NULL) { |
sralph3 | 0:72aeef60e1fc | 95 | uLCD.printf("Open Error!!!\n"); |
sralph3 | 0:72aeef60e1fc | 96 | } |
sralph3 | 0:72aeef60e1fc | 97 | else{ |
sralph3 | 0:72aeef60e1fc | 98 | fscanf(fp2, "%i",&position); |
sralph3 | 0:72aeef60e1fc | 99 | fclose(fp2); |
sralph3 | 0:72aeef60e1fc | 100 | } |
sralph3 | 0:72aeef60e1fc | 101 | |
sralph3 | 0:72aeef60e1fc | 102 | // int sizeIn = sizeof(inChars)/sizeof(*inChars); |
sralph3 | 0:72aeef60e1fc | 103 | |
sralph3 | 0:72aeef60e1fc | 104 | |
sralph3 | 0:72aeef60e1fc | 105 | pb1.mode(PullUp); |
sralph3 | 0:72aeef60e1fc | 106 | pb2.mode(PullUp); |
sralph3 | 0:72aeef60e1fc | 107 | pb3.mode(PullUp); |
sralph3 | 0:72aeef60e1fc | 108 | |
sralph3 | 0:72aeef60e1fc | 109 | // Delay for initial pullup to take effect |
sralph3 | 0:72aeef60e1fc | 110 | wait(.01); |
sralph3 | 0:72aeef60e1fc | 111 | |
sralph3 | 0:72aeef60e1fc | 112 | // Setup Interrupt callback functions for a pb hit |
sralph3 | 0:72aeef60e1fc | 113 | pb1.attach_deasserted(&pb1_hit_callback); |
sralph3 | 0:72aeef60e1fc | 114 | pb2.attach_deasserted(&pb2_hit_callback); |
sralph3 | 0:72aeef60e1fc | 115 | pb3.attach_deasserted(&pb3_hit_callback); |
sralph3 | 0:72aeef60e1fc | 116 | |
sralph3 | 0:72aeef60e1fc | 117 | // Start sampling pb inputs using interrupts |
sralph3 | 0:72aeef60e1fc | 118 | pb1.setSampleFrequency(); //default is 20KHz sampling |
sralph3 | 0:72aeef60e1fc | 119 | pb2.setSampleFrequency(); |
sralph3 | 0:72aeef60e1fc | 120 | pb3.setSampleFrequency(); |
sralph3 | 0:72aeef60e1fc | 121 | // pushbuttons now setup and running |
sralph3 | 0:72aeef60e1fc | 122 | |
sralph3 | 0:72aeef60e1fc | 123 | while(1) { |
sralph3 | 0:72aeef60e1fc | 124 | switch(state){ |
sralph3 | 0:72aeef60e1fc | 125 | case(Q0): |
sralph3 | 0:72aeef60e1fc | 126 | //Produce output for this state |
sralph3 | 0:72aeef60e1fc | 127 | uLCD.locate(0,0); |
sralph3 | 0:72aeef60e1fc | 128 | uLCD.text_width(5); |
sralph3 | 0:72aeef60e1fc | 129 | uLCD.text_height(5); |
sralph3 | 0:72aeef60e1fc | 130 | uLCD.printf("%c",currLetter.getChar()); |
sralph3 | 0:72aeef60e1fc | 131 | wait(.5); |
sralph3 | 0:72aeef60e1fc | 132 | //calculate next state |
sralph3 | 0:72aeef60e1fc | 133 | if (input == ENC){ |
sralph3 | 0:72aeef60e1fc | 134 | state = Q1; |
sralph3 | 0:72aeef60e1fc | 135 | input=NON; |
sralph3 | 0:72aeef60e1fc | 136 | uLCD.cls(); |
sralph3 | 0:72aeef60e1fc | 137 | } |
sralph3 | 0:72aeef60e1fc | 138 | else{ //input should be stay |
sralph3 | 0:72aeef60e1fc | 139 | state = Q0; |
sralph3 | 0:72aeef60e1fc | 140 | } |
sralph3 | 0:72aeef60e1fc | 141 | break; |
sralph3 | 0:72aeef60e1fc | 142 | |
sralph3 | 0:72aeef60e1fc | 143 | case (Q1): |
sralph3 | 0:72aeef60e1fc | 144 | //Produce output for this state |
sralph3 | 0:72aeef60e1fc | 145 | |
sralph3 | 0:72aeef60e1fc | 146 | uLCD.locate(0,0); |
sralph3 | 0:72aeef60e1fc | 147 | uLCD.text_width(1); |
sralph3 | 0:72aeef60e1fc | 148 | uLCD.text_height(1); |
sralph3 | 0:72aeef60e1fc | 149 | uLCD.printf("Press Again To Decrypt More"); |
sralph3 | 0:72aeef60e1fc | 150 | //calculate next state |
sralph3 | 0:72aeef60e1fc | 151 | if (input == ENC){ |
sralph3 | 0:72aeef60e1fc | 152 | state = Q0; |
sralph3 | 0:72aeef60e1fc | 153 | input = NON; |
sralph3 | 0:72aeef60e1fc | 154 | uLCD.cls(); |
sralph3 | 0:72aeef60e1fc | 155 | } |
sralph3 | 0:72aeef60e1fc | 156 | else //input should be stay |
sralph3 | 0:72aeef60e1fc | 157 | state = Q1; |
sralph3 | 0:72aeef60e1fc | 158 | break; |
sralph3 | 0:72aeef60e1fc | 159 | |
sralph3 | 0:72aeef60e1fc | 160 | } |
sralph3 | 0:72aeef60e1fc | 161 | //end switch |
sralph3 | 0:72aeef60e1fc | 162 | wait (0.1); |
sralph3 | 0:72aeef60e1fc | 163 | } |
sralph3 | 0:72aeef60e1fc | 164 | } |