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