Encoding messsages, continuously written on serial terminal, in Morse code. Resulting code is played by a buzzer. Please, use local echo on terminal for more impressive experience. To verify coded messages, there are some app for smartphone.

Dependencies:   FIFO

Fork of Morse by Romain Berrada

Revision:
2:39d67345f7b3
Parent:
1:84ef66bf435d
diff -r 84ef66bf435d -r 39d67345f7b3 Morse.h
--- a/Morse.h	Thu Sep 18 17:26:24 2014 +0000
+++ b/Morse.h	Sat Feb 03 11:45:23 2018 +0000
@@ -1,98 +1,76 @@
-/* 
-    Copyright (c) 2014 Romain Berrada
-    
-    Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
-    and associated documentation files (the "Software"), to deal in the Software without restriction, 
-    including without limitation the rights to use, copy, modify, merge, publish, distribute, 
-    sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
-    furnished to do so, subject to the following conditions:
-
-    The above copyright notice and this permission notice shall be included in all copies or 
-    substantial portions of the Software.
-
-    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 
-    BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
-    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
-    DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
-    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+/*
+INTERNATIONAL MORSE CODE - recommended characters and spacing definitions from:
+http://www.itu.int/rec/R-REC-M.1677-1-200910-I/
+ 
+SPACING IMPLEMENTATION:
+dot = "dit" = [.] = 1
+dash = "dah" = [-] = 3
+Spacing within character = 0 (This is pre-inserted for characters in the switch statement below.)
+Spacing between letters = 000 (3 zeros; resolved in main.cpp)
+Spacing between words = 0000000 (7 zeros; defined with keyboard [Space] button)
 */
-
-#ifndef INCLUDE_MORSE_H
-#define INCLUDE_MORSE_H
-
-/** A  Morse Encoding/Decoding Library \n
-    Transforms char arrays into bool arrays and vice-versa
-    
-    Morse code taken from http://en.wikipedia.org/wiki/Morse_code \n
-    Added some more characters : \n
-        minus : DOT DOT DASH DASH \n
-        _ : DASH DASH DASH DOT \n
-        . : DASH DASH DASH DASH \n
-        / : DOT DASH DOT DASH \n
-        @ : DOT DOT DOT DASH DOT \n
-        ? : DOT DOT DASH DOT DOT \n
-    
-    Here is an quick hello-world that show how to use this library \n
-    @code
-    #include "mbed.h
-    #include "Morse.h"
-    
-    Serial pc(USBTX, USBRX);
-    
-    int main() {
-        int i;
-        Morse_data* data;
-        char message[] = "Hello World";
+ 
+static char* EncodeMorse(char character)
+{
+    int ascii = character;  // char to int
+ 
+    switch(ascii)
+    {
+        case 8:     return "101010101010101";   // [Backspace] (8 dots)
+        case 32:    return "0000000";           // [Space]
+        case 34:    return "10301010301";       // ["] (quotation mark)
+        case 39:    return "10303030301";       // ['] (apostrophe)
+        case 40:    return "301030301";         // [(]
+        case 41:    return "30103030103";       // [)]
+        case 43:    return "103010301";         // [+]
+        case 44:    return "30301010303";       // [,] (comma)
+        case 45:    return "30101010103";       // [-]
+        case 46:    return "10301030103";       // [.] (period)
+        case 47:    return "301010301";         // [/]
+        case 48:    return "303030303";         // 0
+        case 49:    return "103030303";         // 1
+        case 50:    return "101030303";         // 2
+        case 51:    return "101010303";         // 3
+        case 52:    return "101010103";         // 4
+        case 53:    return "101010101";         // 5
+        case 54:    return "301010101";         // 6
+        case 55:    return "303010101";         // 7
+        case 56:    return "303030101";         // 8
+        case 57:    return "303030301";         // 9
+        case 58:    return "30303010101";       // [:] (colon)
+        case 61:    return "301010103";         // [=]
+        case 63:    return "10103030101";       // [?]
+        case 64:    return "10303010301";       // [@]
+        case 65: case 97:   return "103";       // A,a
+        case 66: case 98:   return "3010101";   // B,b
+        case 67: case 99:   return "3010301";   // C,c
+        case 68: case 100:  return "30101";     // D,d
+        case 69: case 101:  return "1";         // E,e
+        case 70: case 102:  return "1010301";   // F,f
+        case 71: case 103:  return "30301";     // G,g
+        case 72: case 104:  return "1010101";   // H,h
+        case 73: case 105:  return "101";       // I,i
+        case 74: case 106:  return "1030303";   // J,j
+        case 75: case 107:  return "30103";     // K,k
+        case 76: case 108:  return "1030101";   // L,l
+        case 77: case 109:  return "303";       // M,m
+        case 78: case 110:  return "301";       // N,n
+        case 79: case 111:  return "30303";     // O,o
+        case 80: case 112:  return "1030301";   // P,p
+        case 81: case 113:  return "3030103";   // Q,q
+        case 82: case 114:  return "10301";     // R,r
+        case 83: case 115:  return "10101";     // S,s
+        case 84: case 116:  return "3";         // T,t
+        case 85: case 117:  return "10103";     // U,u
+        case 86: case 118:  return "1010103";   // V,v
+        case 87: case 119:  return "10303";     // W,w
+        case 88: case 120:  return "3010103";   // X,x (also use for multiplication sign)
+        case 89: case 121:  return "3010303";   // Y,y
+        case 90: case 122:  return "3030101";   // Z,z
         
-        data = morse_create(morse_getBoolSize(message));
-        morse_encode(message, data);
-        for (i=0; i<data->length; i++) pc.printf("%d", data->data[i]);
+        case 0:     return "-1";    // null (for debugging)
+        case 10:    return "-1";    // newline (for debugging)
         
-        morse_decode(data, message);
-        pc.printf("\nMessage was : %s\n", message);
-        
-        while(1);
+        default:    return "";
     }
-    @endcode
-*/
-
-/** Morse_data Structure
-*/
-typedef struct {
-    int length; /**< Length of the data field */
-    bool* data; /**< data field containing 'length' booleans */
-} Morse_data;
-
-/** Give the size of the Morse_data that would be encoded from this word.
-    @param word the word.
-    @return the size of the encoded version of the word.
-*/
-unsigned int morse_getBoolSize(char* word);
-/** Give the size of the char array that would be decoded from this Morse_data.
-    @param data the data.
-    @return the size of the decoded version of the data.
-*/
-unsigned int morse_getWordSize(Morse_data* data);
-
-/** Encode the char array and put the result into the Morse_data structure.
-    @param word the word to encode.
-    @param data the Morse_data structure to store the result. This structure must have a sufficient size.
-*/
-void morse_encode(char* word, Morse_data* data);
-/** Decode the Morse_data structure and put the result into the char array.
-    @param data the data to decode.
-    @param word the char array to store the result. The array must have a sufficient size. (Also think of the '\0')
-*/
-void morse_decode(Morse_data* data, char* word);
-
-/** Allocate a Morse_data structure with the given size.
-    @param length the size of the structure to create.
-    @return the data pointer.
-*/
-Morse_data* morse_create(unsigned int length);
-/** Free a Morse_data structure.
-    @param data the data to free.
-*/
-void morse_destroy(Morse_data* data);
-
-#endif
\ No newline at end of file
+}
\ No newline at end of file