Generate Morse code using console text input and output to LED and speaker.

Dependencies:   4DGL-uLCD-SE PinDetect mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /**********************
00002 Jae Kyung Han
00003 ECE 4180 Lab 4
00004 Morse Code Generator
00005 **********************/
00006 
00007 #include "mbed.h"
00008 #include "uLCD_4DGL.h"
00009 #include "Speaker.h"
00010 #include "PinDetect.h"
00011 #include "EncodeMorse.cpp"
00012 #include <string>
00013 
00014 // USB Serial
00015 Serial pc(USBTX, USBRX);
00016 
00017 // uLCD
00018 uLCD_4DGL uLCD(p28,p27,p29); // serial tx, serial rx, reset pin.
00019 
00020 // Speaker PWM
00021 Speaker mySpeaker(p26); // Parameter must be a PwmOut pin.
00022 
00023 // Pushbuttons
00024 PinDetect pb1(p19);
00025 PinDetect pb2(p20);
00026 PinDetect pb3(p21);
00027 PinDetect pb4(p22);
00028 PinDetect pb5(p23);
00029 PinDetect pb6(p24);
00030 
00031 // mbed LEDs
00032 DigitalOut led1(LED1);
00033 DigitalOut led2(LED2);
00034 DigitalOut led3(LED3);
00035 DigitalOut led4(LED4);
00036 
00037 // Global Variables
00038 static const char space[] = "000";  // Internationally defined spacing length between Morse code letters is 3. Do not change.
00039 float SpeakerFreq = 1000;
00040 float SpeakerVol = 0.01;
00041 float DotLength = 0.1;          // Duration of a dot.
00042 float Speed = 1.2/DotLength;    // Words per minute (1 word = 50 dots).
00043 
00044 void pb1_hit_callback(void)
00045 {
00046     if(SpeakerFreq < 1960)
00047     SpeakerFreq = SpeakerFreq + 50;
00048     
00049     uLCD.color(DGREY);
00050     uLCD.locate(0,2); uLCD.printf("Pitch: ");
00051     uLCD.color(WHITE);
00052     uLCD.locate(8,2); uLCD.printf("%4.0f Hz",SpeakerFreq);
00053 }
00054 void pb2_hit_callback(void)
00055 {
00056     if(SpeakerFreq > 100)
00057         SpeakerFreq = SpeakerFreq - 50;
00058     else
00059         SpeakerFreq = 100;
00060     
00061     uLCD.color(DGREY);
00062     uLCD.locate(0,2); uLCD.printf("Pitch: ");
00063     uLCD.color(WHITE);
00064     uLCD.locate(8,2); uLCD.printf("%4.0f Hz",SpeakerFreq);
00065 }
00066 void pb3_hit_callback(void)
00067 {
00068     if(Speed < 100)
00069         Speed = Speed + 1;
00070     
00071     uLCD.color(DGREY);
00072     uLCD.locate(0,4); uLCD.printf("Speed: ");
00073     uLCD.color(WHITE);
00074     uLCD.locate(10,4); uLCD.printf("%2.0f wpm",Speed);
00075 }
00076 void pb4_hit_callback(void)
00077 {
00078     if(Speed > 1)
00079         Speed = Speed - 1;
00080     else
00081         Speed = 1;
00082     
00083     uLCD.color(DGREY);
00084     uLCD.locate(0,4); uLCD.printf("Speed: ");
00085     uLCD.color(WHITE);
00086     uLCD.locate(10,4); uLCD.printf("%2.0f wpm",Speed);
00087 }
00088 void pb5_hit_callback(void)
00089 {
00090     if(SpeakerVol < 0.045)
00091         SpeakerVol = SpeakerVol + 0.005;
00092     
00093     uLCD.color(DGREY);
00094     uLCD.locate(0,6); uLCD.printf("Volume: ");
00095     uLCD.color(WHITE);
00096     uLCD.locate(10,6); uLCD.printf("%2.0f",SpeakerVol*1000);
00097 }
00098 void pb6_hit_callback(void)
00099 {
00100     if(SpeakerVol > 0.005)
00101         SpeakerVol = SpeakerVol - 0.005;
00102     else
00103         SpeakerVol = 0;
00104     
00105     uLCD.color(DGREY);
00106     uLCD.locate(0,6); uLCD.printf("Volume: ");
00107     uLCD.color(WHITE);
00108     uLCD.locate(10,6); uLCD.printf("%2.0f",SpeakerVol*1000);
00109 }
00110 
00111 void UpdateLCD()
00112 {
00113     uLCD.cls();
00114     uLCD.color(DGREY);
00115     uLCD.locate(4,0); uLCD.printf("Morse Code");
00116     uLCD.locate(0,2); uLCD.printf("Pitch: ");
00117     uLCD.locate(0,4); uLCD.printf("Speed: ");
00118     uLCD.locate(0,6); uLCD.printf("Volume: ");
00119     uLCD.locate(0,8); uLCD.printf("Input: ");
00120     uLCD.color(WHITE);
00121     uLCD.locate(8,2); uLCD.printf("%4.0f Hz",SpeakerFreq);
00122     uLCD.locate(10,4); uLCD.printf("%2.0f wpm",Speed);
00123     uLCD.locate(10,6); uLCD.printf("%2.0f",SpeakerVol*1000);
00124 }
00125 
00126 int main() {
00127 // INITIAL SETTINGS AND DISPLAY
00128     // Set mode so that no external PullUp resistor is needed:
00129     pb1.mode(PullUp);
00130     pb2.mode(PullUp);
00131     pb3.mode(PullUp);
00132     pb4.mode(PullUp);
00133     pb5.mode(PullUp);
00134     pb6.mode(PullUp);
00135     wait(0.01); // Wait for mode to take effect.
00136     
00137     // Set up interrupt callback functions for a pb hit:
00138     pb1.attach_deasserted(&pb1_hit_callback);
00139     pb2.attach_deasserted(&pb2_hit_callback);
00140     pb3.attach_deasserted(&pb3_hit_callback);
00141     pb4.attach_deasserted(&pb4_hit_callback);
00142     pb5.attach_deasserted(&pb5_hit_callback);
00143     pb6.attach_deasserted(&pb6_hit_callback);
00144     
00145     // Sample pushbutton inputs using interrupts:
00146     pb1.setSampleFrequency();
00147     pb2.setSampleFrequency();
00148     pb3.setSampleFrequency();
00149     pb4.setSampleFrequency();
00150     pb5.setSampleFrequency();
00151     pb6.setSampleFrequency();
00152     
00153     // Set up LCD:
00154     uLCD.baudrate(3000000); // Set LCD to max baudrate.
00155     UpdateLCD();
00156     
00157     while(1) {
00158 // INPUT TEXT USING SERIAL USB PORT
00159         pc.printf("\n========== MORSE CODE GENERATOR ==========\n");
00160         pc.printf("Type a sentence below (end with [.] or [?]):\n");
00161         
00162         char myChar = NULL;             // Initialize to save each character from port.
00163         char myString[100] = {NULL};    // Initialize to store concatenated characters. Set to 100 character limit for a sentence.
00164     
00165         while( !(myChar=='.' || myChar=='?') )
00166         {
00167             myChar = pc.getc(); // Read character from port.
00168             pc.putc( myChar );  // Echo character to port.
00169             sprintf(myString,"%s%c",myString,myChar); // Concatenate myString and myChar into myString.
00170         }
00171         
00172 // DISPLAY TO LCD
00173         UpdateLCD();
00174         uLCD.color(GREEN);
00175         uLCD.locate(8,8); uLCD.printf("%s",myString);   // Display final input.
00176         
00177 // TRANSLATE TEXT TO MORSE CODE
00178         pc.printf("\n\nTranslation:\n");
00179         int i = 0;                  // Initialize iterator.
00180         static char* X;             // Initialize output variable for Morse function.
00181         char myCode[1000] = {NULL}; // Initialize to store Morse code. This can get pretty big - set around 10 times the string length (Optimize this).
00182         char X2[50] = {NULL};       // Initialize to save X with or without spacing (leaving as X causes type-casting conflict).
00183         int SpaceDetect = 0;        // True if character is a space.
00184         
00185         while(myString[i] != '\0')
00186         {
00187             myChar = myString[i];               // Get each character.
00188             X = EncodeMorse(myChar);            // Translate it into Morse code.
00189     
00190             // Account for the spacing between letters in Morse code.
00191             if(i > 0)   // Is not the first letter in the code...
00192             {
00193                 if(myChar != 32)    // Is not [Space]...
00194                 {
00195                     if(SpaceDetect == 0)    // Is not a letter after [Space].
00196                     {
00197                         sprintf(X2,"%s%s",space,X); // Add spacing before letter.
00198                     }
00199                     else    // Is a letter after [Space].
00200                     {
00201                         sprintf(X2,"%s%s","",X);    // Don't add spacing before letter.
00202                         SpaceDetect = 0;            // Reset "space detected" flag
00203                     }
00204                 }
00205                 else    // Is [Space].
00206                 {
00207                     sprintf(X2,"%s%s","",X);    // Don't add spacing.
00208                     SpaceDetect = 1;            // Enable to avoid adding spacing to the following letter.
00209                 }
00210             }
00211             else    // Is the first letter in the code.
00212             {
00213                 sprintf(X2,"%s%s","",X);        // Don't add spacing before the very first letter.
00214             }
00215             sprintf(myCode,"%s%s",myCode,X2);   // Concatenate into complete code.
00216             i++;                                // Get next character index.
00217             // DEBUGGING
00218             pc.printf("%c: %s -> %s\n",myChar,X,X2);
00219         }
00220         pc.printf("myCode = %s\n",myCode);      // Display code on PC. 
00221     
00222 // PLAY MORSE CODE TO SPEAKER AND LED
00223         pc.printf("\nMorse code playback:\n");
00224         int j = 0;                  // Initialize iterator.
00225         int myInt;                  // Initialize for character to integer conversion.
00226         myChar = myCode[j];         // Initialize first character.
00227         while( myChar != NULL)
00228         {
00229             myInt = myChar - '0';    // Character to integer for numericals.
00230             if( myInt )
00231             {
00232                 if(myInt==1) pc.printf(".");    // Display 1s as dots.
00233                 if(myInt==3) pc.printf("-");    // Display 3s as dashes.
00234                 led1 = 1;
00235                 mySpeaker.PlayNote(SpeakerFreq,(myInt/Speed),SpeakerVol);
00236             }
00237             else
00238             {
00239                 pc.printf(" ");     // Display 0s as spaces.
00240                 led1 = 0;
00241                 wait(1/Speed);
00242             }
00243             myChar = myCode[++j];   // Increment to next character.
00244         }
00245         // DEBUGGING
00246         pc.printf("\nmyString character length: %d\n",i);
00247         pc.printf("myCode character length: %d\n",j);
00248         led1 = 0;   // Turn LED off at the end because otherwise it stays on.
00249         pc.printf("[END]\n");
00250     } //infinite while loop
00251 }//main