
Pt. 2 | Encryption encrypt program
Dependencies: mbed 4DGL-uLCD-SE SDFileSystem PinDetect
Revision 0:35bd4a705079, committed 2019-01-03
- Comitter:
- sralph3
- Date:
- Thu Jan 03 22:36:11 2019 +0000
- Commit message:
- Pt. 2 | Encryption decrypt program
Changed in this revision
diff -r 000000000000 -r 35bd4a705079 4DGL-uLCD-SE.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/4DGL-uLCD-SE.lib Thu Jan 03 22:36:11 2019 +0000 @@ -0,0 +1,1 @@ +https://mbed.org/users/4180_1/code/4DGL-uLCD-SE/#e39a44de229a
diff -r 000000000000 -r 35bd4a705079 Letter.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Letter.cpp Thu Jan 03 22:36:11 2019 +0000 @@ -0,0 +1,29 @@ +#include "Letter.h" +using namespace std; + +Letter::Letter() +{ + current= 'A'; +} + +void Letter::inc() +{ + if (current=='Z'){ + current = 'A'; + } + else{ + current++; + } +} +void Letter::dec() +{ + if (current=='A'){ + current = 'Z'; + } + else { + current--; + } + } +char Letter::getChar(){ + return current; + }
diff -r 000000000000 -r 35bd4a705079 Letter.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Letter.h Thu Jan 03 22:36:11 2019 +0000 @@ -0,0 +1,12 @@ +#ifndef LETTER_H +#define LETTER_H +class Letter{ + public: + Letter(); + void inc(); + void dec(); + char getChar(); + private: + char current; +}; +#endif \ No newline at end of file
diff -r 000000000000 -r 35bd4a705079 PinDetect.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PinDetect.lib Thu Jan 03 22:36:11 2019 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/AjK/code/PinDetect/#cb3afc45028b
diff -r 000000000000 -r 35bd4a705079 SDFileSystem.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SDFileSystem.lib Thu Jan 03 22:36:11 2019 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/SDFileSystem/#c8f66dc765d4
diff -r 000000000000 -r 35bd4a705079 Shiftbrite.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Shiftbrite.h Thu Jan 03 22:36:11 2019 +0000 @@ -0,0 +1,26 @@ +#include "mbed.h" + +//Setup a new class for a Shiftbrite RGB LED module +class Shiftbrite +{ +public: + Shiftbrite(PinName pin_e, PinName pin_l, PinName pin_do, PinName pin_di, PinName pin_clk); + void write(int red, int green, int blue); + +private: +//class sets up the pins + DigitalOut _pin_e; + DigitalOut _pin_l; + SPI _spi; +}; + +Shiftbrite::Shiftbrite(PinName pin_e, PinName pin_l, PinName pin_do, PinName pin_di, PinName pin_clk) + : _pin_e(pin_e), _pin_l(pin_l), _spi(pin_do, pin_di, pin_clk) +{ + // ADD CODE HERE +} + +void Shiftbrite::write(int red, int green, int blue) +{ + // ADD CODE HERE +}
diff -r 000000000000 -r 35bd4a705079 Speaker.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Speaker.h Thu Jan 03 22:36:11 2019 +0000 @@ -0,0 +1,19 @@ +#include "mbed.h" +// new class to play a note on Speaker based on PwmOut class +class Speaker +{ +public: + Speaker(PinName pin) : _pin(pin) { +// _pin(pin) means pass pin to the Speaker Constructor + } +// class method to play a note based on PwmOut class + void PlayNote(float frequency, float duration, float volume) { + _pin.period(1.0/frequency); + _pin = volume/2.0; + wait(duration); + _pin = 0.0; + } + +private: + PwmOut _pin; +}; \ No newline at end of file
diff -r 000000000000 -r 35bd4a705079 TMP36.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TMP36.h Thu Jan 03 22:36:11 2019 +0000 @@ -0,0 +1,31 @@ +#include "mbed.h" + +//Setup a new class for TMP36 sensor +class TMP36 +{ +public: + TMP36(PinName pin); + TMP36(); + operator float (); + float read(); +private: +//class sets up the AnalogIn pin + AnalogIn _pin; +}; + +TMP36::TMP36(PinName pin) : _pin(pin) +{ +// _pin(pin) means pass pin to the AnalogIn constructor +} + +float TMP36::read() +{ +//convert sensor reading to temperature in degrees C + return ((_pin.read()*3.3)-0.500)*100.0; +} +//overload of float conversion (avoids needing to type .read() in equations) +TMP36::operator float () +{ +//convert sensor reading to temperature in degrees C + return ((_pin.read()*3.3)-0.500)*100.0; +} \ No newline at end of file
diff -r 000000000000 -r 35bd4a705079 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Thu Jan 03 22:36:11 2019 +0000 @@ -0,0 +1,168 @@ +#include "mbed.h" +#include "PinDetect.h" +#include "uLCD_4DGL.h" +#include "SDFileSystem.h" +#include "Letter.h" +uLCD_4DGL uLCD(p28, p27, p29); // serial tx, serial rx, reset pin; +SDFileSystem sd(p5, p6, p7, p8, "sd"); + +//declare objects for pins used with pushbuttons +PinDetect pb1(p18); +PinDetect pb2(p17); +PinDetect pb3(p16); + +//Populate arrayCipher from cipher on SD +char arrayCipher [1001]; + +enum InputType {FWD,BACK,ENC,NON}; +enum StateType {Q0, Q1, Q2, Q3}; +Letter currLetter; +int position=0; +FILE *fp2; + +InputType input = NON; +StateType state = Q0; + +//Encrypt +char encrypt (char inp, char arrayCipher[], int startPt) +{ + int sum = (arrayCipher[startPt]-65) + (inp-65); + sum = sum % 26; + char sumC = sum; + return sumC+65; +} + +// Callback routine is interrupt activated by a debounced pb3 hit +void pb3_hit_callback (void) +{ + if (state==Q0){ + uLCD.cls(); + uLCD.printf("ENCRYPTING"); + wait(1.0); + char inC = currLetter.getChar(); + char outC=encrypt(inC, arrayCipher, position); + uLCD.cls(); + + uLCD.locate(0,0); + uLCD.text_width(5); + uLCD.text_height(5); + + + uLCD.printf("%c", outC); + position++; + + //update position in txt doc + fp2 = fopen("/sd/mydir/positionCipherSender.txt", "w"); + if(fp2 == NULL) { + uLCD.printf("Error Open \n"); + } + else{ + fprintf(fp2, "%i",position); + fclose(fp2); + } + + wait(1.0); + uLCD.cls(); + input = ENC; + } + else{ + input=ENC; + } + +} + // Callback routine is interrupt activated by a debounced pb1 hit +void pb1_hit_callback (void) + { + // ADD CODE HERE THAT YOU WHAT TO RUN WHEN INTERUPT IS GENERATED + currLetter.inc(); +} + +// Callback routine is interrupt activated by a debounced pb2 hit +void pb2_hit_callback (void) + { + currLetter.dec(); +} + + +int main() { + + FILE *fp = fopen("/sd/mydir/OTP.txt", "r"); + if(fp == NULL){ + uLCD.printf("Open Error!!!\n"); + } + else{ + fscanf(fp, "%s", arrayCipher); + fclose(fp); + } + + FILE *fp2 = fopen("/sd/mydir/positionCipherSender.txt", "r"); + if(fp2 == NULL) { + uLCD.printf("Open Error!!!\n"); + } + else{ + fscanf(fp2, "%i",&position); + fclose(fp2); + } + + // int sizeIn = sizeof(inChars)/sizeof(*inChars); + + + pb1.mode(PullUp); + pb2.mode(PullUp); + pb3.mode(PullUp); + + // Delay for initial pullup to take effect + wait(.01); + + // Setup Interrupt callback functions for a pb hit + pb1.attach_deasserted(&pb1_hit_callback); + pb2.attach_deasserted(&pb2_hit_callback); + pb3.attach_deasserted(&pb3_hit_callback); + + // Start sampling pb inputs using interrupts + pb1.setSampleFrequency(); //default is 20KHz sampling + pb2.setSampleFrequency(); + pb3.setSampleFrequency(); + // pushbuttons now setup and running + +while(1) { + switch(state){ + case(Q0): + //Produce output for this state + uLCD.locate(0,0); + uLCD.text_width(5); + uLCD.text_height(5); + uLCD.printf("%c",currLetter.getChar()); + wait(.5); + //calculate next state + if (input == ENC){ + state = Q1; + input=NON; + } + else{ //input should be stay + state = Q0; + } + break; + + case (Q1): + //Produce output for this state + + uLCD.locate(0,0); + uLCD.text_width(1); + uLCD.text_height(1); + uLCD.printf("Press Again To Encrypt More"); + //calculate next state + if (input == ENC){ + state = Q0; + input = NON; + uLCD.cls(); + } + else //input should be stay + state = Q1; + break; + + } + //end switch + wait (0.1); + } +} \ No newline at end of file
diff -r 000000000000 -r 35bd4a705079 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Thu Jan 03 22:36:11 2019 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/824293ae5e43 \ No newline at end of file