Morse Encoder/Decoder Library. Transforms char array to binary array and vice-versa.
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
Added some more characters :
- : DOT DOT DASH DASH
_ : DASH DASH DASH DOT
. : DASH DASH DASH DASH
/ : DOT DASH DOT DASH
@ : DOT DOT DOT DASH DOT
? : DOT DOT DASH DOT DOT
Here is an quick hello-world that show how to use this library
#include "mbed.h #include "Morse.h" Serial pc(USBTX, USBRX); int main() { int i; Morse_data* data; char message[] = "Hello World"; data = morse_create(morse_getBoolSize(message)); morse_encode(message, data); for (i=0; i<data->length; i++) pc.printf("%d", data->data[i]); morse_decode(data, message); pc.printf("\nMessage was : %s\n", message); while(1); }
MorseDecoder.cpp@1:84ef66bf435d, 2014-09-18 (annotated)
- Committer:
- rominos2
- Date:
- Thu Sep 18 17:26:24 2014 +0000
- Revision:
- 1:84ef66bf435d
Morse Encoder/Decoder Library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rominos2 | 1:84ef66bf435d | 1 | /* |
rominos2 | 1:84ef66bf435d | 2 | Copyright (c) 2014 Romain Berrada |
rominos2 | 1:84ef66bf435d | 3 | |
rominos2 | 1:84ef66bf435d | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
rominos2 | 1:84ef66bf435d | 5 | and associated documentation files (the "Software"), to deal in the Software without restriction, |
rominos2 | 1:84ef66bf435d | 6 | including without limitation the rights to use, copy, modify, merge, publish, distribute, |
rominos2 | 1:84ef66bf435d | 7 | sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
rominos2 | 1:84ef66bf435d | 8 | furnished to do so, subject to the following conditions: |
rominos2 | 1:84ef66bf435d | 9 | |
rominos2 | 1:84ef66bf435d | 10 | The above copyright notice and this permission notice shall be included in all copies or |
rominos2 | 1:84ef66bf435d | 11 | substantial portions of the Software. |
rominos2 | 1:84ef66bf435d | 12 | |
rominos2 | 1:84ef66bf435d | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
rominos2 | 1:84ef66bf435d | 14 | BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
rominos2 | 1:84ef66bf435d | 15 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
rominos2 | 1:84ef66bf435d | 16 | DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
rominos2 | 1:84ef66bf435d | 17 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
rominos2 | 1:84ef66bf435d | 18 | */ |
rominos2 | 1:84ef66bf435d | 19 | |
rominos2 | 1:84ef66bf435d | 20 | #include "Morse.h" |
rominos2 | 1:84ef66bf435d | 21 | #include "mbed.h" |
rominos2 | 1:84ef66bf435d | 22 | |
rominos2 | 1:84ef66bf435d | 23 | struct _morse_tree { |
rominos2 | 1:84ef66bf435d | 24 | char value; |
rominos2 | 1:84ef66bf435d | 25 | const struct _morse_tree* dot; |
rominos2 | 1:84ef66bf435d | 26 | const struct _morse_tree* dash; |
rominos2 | 1:84ef66bf435d | 27 | }; |
rominos2 | 1:84ef66bf435d | 28 | |
rominos2 | 1:84ef66bf435d | 29 | // Depth 5 of the tree |
rominos2 | 1:84ef66bf435d | 30 | static const struct _morse_tree _morse_tree_1 = {'1', 0, 0}; |
rominos2 | 1:84ef66bf435d | 31 | static const struct _morse_tree _morse_tree_2 = {'2', 0, 0}; |
rominos2 | 1:84ef66bf435d | 32 | static const struct _morse_tree _morse_tree_3 = {'3', 0, 0}; |
rominos2 | 1:84ef66bf435d | 33 | static const struct _morse_tree _morse_tree_4 = {'4', 0, 0}; |
rominos2 | 1:84ef66bf435d | 34 | static const struct _morse_tree _morse_tree_5 = {'5', 0, 0}; |
rominos2 | 1:84ef66bf435d | 35 | static const struct _morse_tree _morse_tree_6 = {'6', 0, 0}; |
rominos2 | 1:84ef66bf435d | 36 | static const struct _morse_tree _morse_tree_7 = {'7', 0, 0}; |
rominos2 | 1:84ef66bf435d | 37 | static const struct _morse_tree _morse_tree_8 = {'8', 0, 0}; |
rominos2 | 1:84ef66bf435d | 38 | static const struct _morse_tree _morse_tree_9 = {'9', 0, 0}; |
rominos2 | 1:84ef66bf435d | 39 | static const struct _morse_tree _morse_tree_0 = {'0', 0, 0}; |
rominos2 | 1:84ef66bf435d | 40 | static const struct _morse_tree _morse_tree_AR= {'@', 0, 0}; // @ added as DOT,DOT,DOT,DASH,DOT |
rominos2 | 1:84ef66bf435d | 41 | static const struct _morse_tree _morse_tree_IN= {'?', 0, 0}; // ? added as DOT,DOT,DASH,DOT,DOT |
rominos2 | 1:84ef66bf435d | 42 | |
rominos2 | 1:84ef66bf435d | 43 | // Depth 4 of the tree |
rominos2 | 1:84ef66bf435d | 44 | static const struct _morse_tree _morse_tree_H = {'H', &_morse_tree_5, &_morse_tree_4}; |
rominos2 | 1:84ef66bf435d | 45 | static const struct _morse_tree _morse_tree_V = {'V',&_morse_tree_AR ,&_morse_tree_3}; |
rominos2 | 1:84ef66bf435d | 46 | static const struct _morse_tree _morse_tree_F = {'F',&_morse_tree_IN, 0}; |
rominos2 | 1:84ef66bf435d | 47 | static const struct _morse_tree _morse_tree_MN= {'-', 0, &_morse_tree_2}; // - added as DOT,DOT,DASH,DASH |
rominos2 | 1:84ef66bf435d | 48 | static const struct _morse_tree _morse_tree_L = {'L', 0, 0}; |
rominos2 | 1:84ef66bf435d | 49 | static const struct _morse_tree _morse_tree_SL= {'/', 0, 0}; // / added as DOT,DASH,DOT,DASH |
rominos2 | 1:84ef66bf435d | 50 | static const struct _morse_tree _morse_tree_P = {'P', 0, 0}; |
rominos2 | 1:84ef66bf435d | 51 | static const struct _morse_tree _morse_tree_J = {'J', 0, &_morse_tree_1}; |
rominos2 | 1:84ef66bf435d | 52 | static const struct _morse_tree _morse_tree_B = {'B', &_morse_tree_6, 0}; |
rominos2 | 1:84ef66bf435d | 53 | static const struct _morse_tree _morse_tree_X = {'X', 0, 0}; |
rominos2 | 1:84ef66bf435d | 54 | static const struct _morse_tree _morse_tree_C = {'C', 0, 0}; |
rominos2 | 1:84ef66bf435d | 55 | static const struct _morse_tree _morse_tree_Y = {'Y', 0, 0}; |
rominos2 | 1:84ef66bf435d | 56 | static const struct _morse_tree _morse_tree_Z = {'Z', &_morse_tree_7, 0}; |
rominos2 | 1:84ef66bf435d | 57 | static const struct _morse_tree _morse_tree_Q = {'Q', 0, 0}; |
rominos2 | 1:84ef66bf435d | 58 | static const struct _morse_tree _morse_tree_UN= {'_', &_morse_tree_8, 0}; // _ added as DASH,DASH,DASH,DOT |
rominos2 | 1:84ef66bf435d | 59 | static const struct _morse_tree _morse_tree_DO= {'.', &_morse_tree_9, &_morse_tree_0}; // . added as DASH,DASH,DASH,DASH |
rominos2 | 1:84ef66bf435d | 60 | |
rominos2 | 1:84ef66bf435d | 61 | // Depth 3 of the tree |
rominos2 | 1:84ef66bf435d | 62 | static const struct _morse_tree _morse_tree_S = {'S', &_morse_tree_H, &_morse_tree_V}; |
rominos2 | 1:84ef66bf435d | 63 | static const struct _morse_tree _morse_tree_U = {'U', &_morse_tree_F, &_morse_tree_MN}; |
rominos2 | 1:84ef66bf435d | 64 | static const struct _morse_tree _morse_tree_R = {'R', &_morse_tree_L, &_morse_tree_SL}; |
rominos2 | 1:84ef66bf435d | 65 | static const struct _morse_tree _morse_tree_W = {'W', &_morse_tree_P, &_morse_tree_J}; |
rominos2 | 1:84ef66bf435d | 66 | static const struct _morse_tree _morse_tree_D = {'D', &_morse_tree_B, &_morse_tree_X}; |
rominos2 | 1:84ef66bf435d | 67 | static const struct _morse_tree _morse_tree_K = {'K', &_morse_tree_C, &_morse_tree_Y}; |
rominos2 | 1:84ef66bf435d | 68 | static const struct _morse_tree _morse_tree_G = {'G', &_morse_tree_Z, &_morse_tree_Q}; |
rominos2 | 1:84ef66bf435d | 69 | static const struct _morse_tree _morse_tree_O = {'O', &_morse_tree_UN,&_morse_tree_DO}; |
rominos2 | 1:84ef66bf435d | 70 | |
rominos2 | 1:84ef66bf435d | 71 | // Depth 2 of the tree |
rominos2 | 1:84ef66bf435d | 72 | static const struct _morse_tree _morse_tree_I = {'I', &_morse_tree_S, &_morse_tree_U}; |
rominos2 | 1:84ef66bf435d | 73 | static const struct _morse_tree _morse_tree_A = {'A', &_morse_tree_R, &_morse_tree_W}; |
rominos2 | 1:84ef66bf435d | 74 | static const struct _morse_tree _morse_tree_N = {'N', &_morse_tree_D, &_morse_tree_K}; |
rominos2 | 1:84ef66bf435d | 75 | static const struct _morse_tree _morse_tree_M = {'M', &_morse_tree_G, &_morse_tree_O}; |
rominos2 | 1:84ef66bf435d | 76 | |
rominos2 | 1:84ef66bf435d | 77 | // Depth 1 of the tree |
rominos2 | 1:84ef66bf435d | 78 | static const struct _morse_tree _morse_tree_E = {'E', &_morse_tree_I, &_morse_tree_A}; |
rominos2 | 1:84ef66bf435d | 79 | static const struct _morse_tree _morse_tree_T = {'T', &_morse_tree_N, &_morse_tree_M}; |
rominos2 | 1:84ef66bf435d | 80 | |
rominos2 | 1:84ef66bf435d | 81 | static const struct _morse_tree _morse_tree_root = {'#', &_morse_tree_E, &_morse_tree_T}; |
rominos2 | 1:84ef66bf435d | 82 | |
rominos2 | 1:84ef66bf435d | 83 | unsigned int morse_getWordSize(Morse_data* data) { |
rominos2 | 1:84ef66bf435d | 84 | unsigned int i; |
rominos2 | 1:84ef66bf435d | 85 | unsigned int blank_counter=0; |
rominos2 | 1:84ef66bf435d | 86 | unsigned int size=0; |
rominos2 | 1:84ef66bf435d | 87 | |
rominos2 | 1:84ef66bf435d | 88 | for (i=0; i<data->length; i++) { |
rominos2 | 1:84ef66bf435d | 89 | if (data->data[i]==false) blank_counter++; |
rominos2 | 1:84ef66bf435d | 90 | else { // else true |
rominos2 | 1:84ef66bf435d | 91 | // checks all the blanks so far, and then reset the counter |
rominos2 | 1:84ef66bf435d | 92 | if (blank_counter==3) size++; // if blanks to separate two chars |
rominos2 | 1:84ef66bf435d | 93 | else if (blank_counter==7) size+=2; // if blanks for a space : acknowledge the space and the char before the space |
rominos2 | 1:84ef66bf435d | 94 | blank_counter=0; |
rominos2 | 1:84ef66bf435d | 95 | } |
rominos2 | 1:84ef66bf435d | 96 | } |
rominos2 | 1:84ef66bf435d | 97 | size++; // acknowledge the last char |
rominos2 | 1:84ef66bf435d | 98 | if (blank_counter>=7) size++; |
rominos2 | 1:84ef66bf435d | 99 | return size; |
rominos2 | 1:84ef66bf435d | 100 | } |
rominos2 | 1:84ef66bf435d | 101 | |
rominos2 | 1:84ef66bf435d | 102 | void morse_decode(Morse_data* data, char* word) { |
rominos2 | 1:84ef66bf435d | 103 | unsigned int i; |
rominos2 | 1:84ef66bf435d | 104 | unsigned int char_i=0; |
rominos2 | 1:84ef66bf435d | 105 | unsigned int false_counter=0; |
rominos2 | 1:84ef66bf435d | 106 | unsigned int true_counter=0; |
rominos2 | 1:84ef66bf435d | 107 | const struct _morse_tree* tree = &_morse_tree_root; // CHANGE TO THE BINRARY TREE IMPLEMENTATION AND PUT THE ROOT INSTEAD |
rominos2 | 1:84ef66bf435d | 108 | |
rominos2 | 1:84ef66bf435d | 109 | for (i=0; i<data->length; i++) { |
rominos2 | 1:84ef66bf435d | 110 | if (data->data[i]==false) { |
rominos2 | 1:84ef66bf435d | 111 | // if false, increase the false_counter, manage the number of true and false before, and reset the true_counter |
rominos2 | 1:84ef66bf435d | 112 | false_counter++; |
rominos2 | 1:84ef66bf435d | 113 | |
rominos2 | 1:84ef66bf435d | 114 | if (true_counter==1) tree = tree->dot; |
rominos2 | 1:84ef66bf435d | 115 | else if (true_counter==3) tree = tree->dash; |
rominos2 | 1:84ef66bf435d | 116 | |
rominos2 | 1:84ef66bf435d | 117 | if (false_counter==3) { |
rominos2 | 1:84ef66bf435d | 118 | word[char_i++] = tree->value; |
rominos2 | 1:84ef66bf435d | 119 | tree = &_morse_tree_root; |
rominos2 | 1:84ef66bf435d | 120 | } |
rominos2 | 1:84ef66bf435d | 121 | else if (false_counter==7) word[char_i++] = ' '; |
rominos2 | 1:84ef66bf435d | 122 | |
rominos2 | 1:84ef66bf435d | 123 | true_counter=0; |
rominos2 | 1:84ef66bf435d | 124 | } |
rominos2 | 1:84ef66bf435d | 125 | else { // else true |
rominos2 | 1:84ef66bf435d | 126 | true_counter++; |
rominos2 | 1:84ef66bf435d | 127 | false_counter=0; |
rominos2 | 1:84ef66bf435d | 128 | } |
rominos2 | 1:84ef66bf435d | 129 | } |
rominos2 | 1:84ef66bf435d | 130 | |
rominos2 | 1:84ef66bf435d | 131 | if (true_counter==1) word[char_i++] = tree->dot->value; |
rominos2 | 1:84ef66bf435d | 132 | else if (true_counter==3) word[char_i++] = tree->dash->value; |
rominos2 | 1:84ef66bf435d | 133 | |
rominos2 | 1:84ef66bf435d | 134 | word[char_i] = '\0'; // finally, end the char array |
rominos2 | 1:84ef66bf435d | 135 | } |