Thorkild Pedersen / Mbed 2 deprecated morsemsg

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers morsemsg.cpp Source File

morsemsg.cpp

00001 #include "mbed.h"
00002 
00003 DigitalOut myled(LED1);
00004 
00005 /// Send the string message in morse code on 'myled
00006 void morse_string(char *msg);
00007 
00008 // Timing constants for morse code generation
00009 #define TDOT   0.2
00010 #define TDASH  (3*TDOT)
00011 #define TSPLIT (1*TDOT)
00012 #define TSPACE (3*TDASH)
00013 
00014 int main() {
00015     while (1) {
00016         morse_string("hello world");
00017         wait(2*TSPACE);
00018     }
00019 }
00020 
00021 /// Table with morse code bit-patterns.
00022 /// Each symbols dot/dash sequence is stored starting at bit 0 where
00023 /// a set bit encodes a dash and cleared bit a dot. The sequence is terminated by a set trailing bit-
00024 unsigned char alphabet[] = {
00025     0x06, // .-     a
00026     0x11, // -...     b
00027     0x15, // -.-.     c
00028     0x09, // -..     d
00029     0x02, // .     e
00030     0x14, // ..-.     f
00031     0x0b, // --.     g
00032     0x10, // ....     h
00033     0x08, // ..     i
00034     0x1e, // .---     j
00035     0x0d, // -.-     k
00036     0x12, // .-..     l
00037     0x07, // --     m
00038     0x05, // -.     n
00039     0x0F, // ---     o
00040     0x16, // .--.     p
00041     0x1b, // --.-     q
00042     0x0a, // .-.     r
00043     0x08, // ...     s
00044     0x03, // -     t
00045     0x0c, // ..-     u
00046     0x18, // ...-     v
00047     0x0e, // .--     w
00048     0x19, // -..-     x
00049     0x1d, // -.--     y
00050     0x13, // --..     z
00051     0x3f, // ----- 0
00052     0x3e, // .---- 1
00053     0x3c, // ..--- 2
00054     0x38, // ...-- 3
00055     0x30, // ....- 4
00056     0x20, // ..... 5
00057     0x21, // -.... 6
00058     0x23, // --... 7
00059     0x27, // ---.. 8
00060     0x2f, // ----. 9
00061     0x73, // --..-- ,
00062     0x6a, // .-.-.- .
00063     0x00
00064 };
00065 
00066 /// Convert a character to a morse-code bit pattern.
00067 unsigned char getCode(char symbol) {
00068     if      (symbol >= 'a' && symbol <= 'z')    return alphabet[symbol-'a'];
00069     else if (symbol >= '0' && symbol <= '9')    return alphabet[symbol-'0'+26];
00070     else if (symbol == '.')            return alphabet[37];
00071     else if (symbol == ',')            return alphabet[36];
00072     else return 0;
00073 }
00074 
00075 /// Pattern of current symbol being send.
00076 unsigned char morse_pattern = 0;
00077 
00078 /// Set the symbol to be morsed.
00079 /// Returns bit pattern of morse code or zero if symbol not found (e.g. for a space)
00080 int morse_setsymbol(char symbol) {
00081     return morse_pattern = getCode(symbol);
00082 }
00083 
00084 /// Generate next dot/dash to transmit
00085 /// Returns 0 for dot, 1 for dash and -1 for finished
00086 int morse_next() {
00087     int retval = (morse_pattern & 1) ? 1 : 0;
00088     morse_pattern >>= 1;
00089     if (! morse_pattern) retval = -1;
00090     return retval;
00091 }
00092 
00093 void morse_string(char *msg) {
00094     int x;
00095     for (; *msg; msg++) {
00096         if (!morse_setsymbol(*msg)) {
00097             wait(TSPACE);
00098             continue;
00099         }
00100         for (x=morse_next(); x != -1; x=morse_next()) {
00101             myled = 1;
00102             wait(x ? TDASH : TDOT);
00103             myled = 0;
00104             wait(TSPLIT);
00105         }
00106         wait(TDASH);
00107     }
00108 }