Stage-1 Students SoCEM / Mbed OS MorseCodeSolution
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // Elec143 Morsecode labwork
00002 //
00003 // Suggested solution using functions
00004 // Chris Tipney September 2017
00005 //
00006 // Morse code
00007 // dot = 150 ms
00008 // dash = 450 ms
00009 // symbol space = 150 ms
00010 // letter space = 450 ms
00011 // word space = 900 ms
00012 //
00013 // Note
00014 // Every symbol is followed by at least 150 ms
00015 // only the space beyond that varies according
00016 // to context
00017 
00018 #include "mbed.h"
00019 
00020 // function prototypes
00021 void sierra(int r);     // output the pattern for the letter 'S' repeat according to value of r
00022 void oscar(int r);      // output the pattern for the letter 'O' repeat according to value of r
00023 void dot(int r);        // output a short flash followed by 150 ms space repeat according to value of r
00024 void dash(int r);       // output a long flash followed by 150 ms space repeat according to value of r
00025 void letterspace();     // output 300 ms space to make up the rest of the letter space 450 ms
00026 void wordspace();       // output 750 ms space to make up the rest of the word space 900 ms
00027 
00028 // led attached to port D7
00029 DigitalOut myled(D7);
00030 
00031 int main() 
00032 {
00033     while(1)                // loop forever
00034     {
00035         sierra(1);          // S
00036         oscar(1);           // O
00037         sierra(1);          // S
00038         wordspace();
00039     }
00040 }
00041 
00042 // function implementations
00043 
00044 void sierra(int rep)
00045 {
00046     for (int i = 0; i < rep; i++)
00047     {
00048         dot(3);
00049         letterspace();
00050     }
00051 }
00052 
00053 void oscar(int rep)
00054 {
00055         for (int i = 0; i < rep; i++)
00056     {
00057         dash(3);
00058         letterspace();
00059     }
00060 }
00061 
00062 
00063 void dot(int rep)
00064 {
00065     for (int i = 0; i < rep; i++)
00066     {
00067         myled = 1;
00068         wait(0.150);
00069         myled = 0;
00070         wait(0.150);
00071     }
00072 }
00073 
00074 void dash(int rep)
00075 {
00076     for (int i = 0; i < rep; i++)
00077     {
00078         myled = 1;
00079         wait(0.450);
00080         myled = 0;
00081         wait(0.150);
00082     }
00083 }
00084 
00085 void wordspace()
00086 {
00087     wait(.750);
00088 }
00089 
00090 void letterspace()
00091 {
00092     wait(.300);
00093 }