Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed 4DGL-uLCD-SE SDFileSystem PinDetect
Revision 0:72aeef60e1fc, committed 2019-01-03
- Comitter:
- sralph3
- Date:
- Thu Jan 03 22:40:26 2019 +0000
- Commit message:
- Pt. 4 | Encryption encrypt program
Changed in this revision
diff -r 000000000000 -r 72aeef60e1fc 4DGL-uLCD-SE.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/4DGL-uLCD-SE.lib Thu Jan 03 22:40:26 2019 +0000 @@ -0,0 +1,1 @@ +https://mbed.org/users/4180_1/code/4DGL-uLCD-SE/#e39a44de229a
diff -r 000000000000 -r 72aeef60e1fc Letter.cpp
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Letter.cpp Thu Jan 03 22:40:26 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 72aeef60e1fc Letter.h
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Letter.h Thu Jan 03 22:40:26 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 72aeef60e1fc PinDetect.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PinDetect.lib Thu Jan 03 22:40:26 2019 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/AjK/code/PinDetect/#cb3afc45028b
diff -r 000000000000 -r 72aeef60e1fc SDFileSystem.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SDFileSystem.lib Thu Jan 03 22:40:26 2019 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/SDFileSystem/#c8f66dc765d4
diff -r 000000000000 -r 72aeef60e1fc Shiftbrite.h
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Shiftbrite.h Thu Jan 03 22:40:26 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 72aeef60e1fc Speaker.h
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Speaker.h Thu Jan 03 22:40:26 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 72aeef60e1fc TMP36.h
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/TMP36.h Thu Jan 03 22:40:26 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 72aeef60e1fc main.cpp
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Thu Jan 03 22:40:26 2019 +0000
@@ -0,0 +1,164 @@
+#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 decrypt (char inp, char arrayCipher[], int startPt)
+{
+ int sum = (inp-65)-(arrayCipher[startPt]-65) + 26;
+ 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("DECRYPTING");
+ wait(1.0);
+ char inC = currLetter.getChar();
+ char outC=decrypt(inC, arrayCipher, position);
+ uLCD.cls();
+ uLCD.text_width(5);
+ uLCD.text_height(5);
+ uLCD.printf("%c", outC);
+ position++;
+
+ //update position in txt doc
+ fp2 = fopen("/sd/mydir/positionCipherReceiver.txt", "w");
+ if(fp2 == NULL) {
+ uLCD.printf("Error Open \n");
+ }
+ else{
+ fprintf(fp2, "%i",position);
+ fclose(fp2);
+ }
+
+ wait(1.0);
+ 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/positionCipherReceiver.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;
+ uLCD.cls();
+ }
+ 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 Decrypt 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 72aeef60e1fc mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Thu Jan 03 22:40:26 2019 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/824293ae5e43 \ No newline at end of file