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.
Fork of Morse by
Revision 2:39d67345f7b3, committed 2018-02-03
- Comitter:
- marcoperciavalle
- Date:
- Sat Feb 03 11:45:23 2018 +0000
- Parent:
- 1:84ef66bf435d
- Commit message:
- 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.
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/FIFO.lib Sat Feb 03 11:45:23 2018 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/marcoperciavalle/code/FIFO/#1260dbaaa9c4
--- 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
--- a/MorseDecoder.cpp Thu Sep 18 17:26:24 2014 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,135 +0,0 @@ -/* - 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. -*/ - -#include "Morse.h" -#include "mbed.h" - -struct _morse_tree { - char value; - const struct _morse_tree* dot; - const struct _morse_tree* dash; -}; - -// Depth 5 of the tree -static const struct _morse_tree _morse_tree_1 = {'1', 0, 0}; -static const struct _morse_tree _morse_tree_2 = {'2', 0, 0}; -static const struct _morse_tree _morse_tree_3 = {'3', 0, 0}; -static const struct _morse_tree _morse_tree_4 = {'4', 0, 0}; -static const struct _morse_tree _morse_tree_5 = {'5', 0, 0}; -static const struct _morse_tree _morse_tree_6 = {'6', 0, 0}; -static const struct _morse_tree _morse_tree_7 = {'7', 0, 0}; -static const struct _morse_tree _morse_tree_8 = {'8', 0, 0}; -static const struct _morse_tree _morse_tree_9 = {'9', 0, 0}; -static const struct _morse_tree _morse_tree_0 = {'0', 0, 0}; -static const struct _morse_tree _morse_tree_AR= {'@', 0, 0}; // @ added as DOT,DOT,DOT,DASH,DOT -static const struct _morse_tree _morse_tree_IN= {'?', 0, 0}; // ? added as DOT,DOT,DASH,DOT,DOT - -// Depth 4 of the tree -static const struct _morse_tree _morse_tree_H = {'H', &_morse_tree_5, &_morse_tree_4}; -static const struct _morse_tree _morse_tree_V = {'V',&_morse_tree_AR ,&_morse_tree_3}; -static const struct _morse_tree _morse_tree_F = {'F',&_morse_tree_IN, 0}; -static const struct _morse_tree _morse_tree_MN= {'-', 0, &_morse_tree_2}; // - added as DOT,DOT,DASH,DASH -static const struct _morse_tree _morse_tree_L = {'L', 0, 0}; -static const struct _morse_tree _morse_tree_SL= {'/', 0, 0}; // / added as DOT,DASH,DOT,DASH -static const struct _morse_tree _morse_tree_P = {'P', 0, 0}; -static const struct _morse_tree _morse_tree_J = {'J', 0, &_morse_tree_1}; -static const struct _morse_tree _morse_tree_B = {'B', &_morse_tree_6, 0}; -static const struct _morse_tree _morse_tree_X = {'X', 0, 0}; -static const struct _morse_tree _morse_tree_C = {'C', 0, 0}; -static const struct _morse_tree _morse_tree_Y = {'Y', 0, 0}; -static const struct _morse_tree _morse_tree_Z = {'Z', &_morse_tree_7, 0}; -static const struct _morse_tree _morse_tree_Q = {'Q', 0, 0}; -static const struct _morse_tree _morse_tree_UN= {'_', &_morse_tree_8, 0}; // _ added as DASH,DASH,DASH,DOT -static const struct _morse_tree _morse_tree_DO= {'.', &_morse_tree_9, &_morse_tree_0}; // . added as DASH,DASH,DASH,DASH - -// Depth 3 of the tree -static const struct _morse_tree _morse_tree_S = {'S', &_morse_tree_H, &_morse_tree_V}; -static const struct _morse_tree _morse_tree_U = {'U', &_morse_tree_F, &_morse_tree_MN}; -static const struct _morse_tree _morse_tree_R = {'R', &_morse_tree_L, &_morse_tree_SL}; -static const struct _morse_tree _morse_tree_W = {'W', &_morse_tree_P, &_morse_tree_J}; -static const struct _morse_tree _morse_tree_D = {'D', &_morse_tree_B, &_morse_tree_X}; -static const struct _morse_tree _morse_tree_K = {'K', &_morse_tree_C, &_morse_tree_Y}; -static const struct _morse_tree _morse_tree_G = {'G', &_morse_tree_Z, &_morse_tree_Q}; -static const struct _morse_tree _morse_tree_O = {'O', &_morse_tree_UN,&_morse_tree_DO}; - -// Depth 2 of the tree -static const struct _morse_tree _morse_tree_I = {'I', &_morse_tree_S, &_morse_tree_U}; -static const struct _morse_tree _morse_tree_A = {'A', &_morse_tree_R, &_morse_tree_W}; -static const struct _morse_tree _morse_tree_N = {'N', &_morse_tree_D, &_morse_tree_K}; -static const struct _morse_tree _morse_tree_M = {'M', &_morse_tree_G, &_morse_tree_O}; - -// Depth 1 of the tree -static const struct _morse_tree _morse_tree_E = {'E', &_morse_tree_I, &_morse_tree_A}; -static const struct _morse_tree _morse_tree_T = {'T', &_morse_tree_N, &_morse_tree_M}; - -static const struct _morse_tree _morse_tree_root = {'#', &_morse_tree_E, &_morse_tree_T}; - -unsigned int morse_getWordSize(Morse_data* data) { - unsigned int i; - unsigned int blank_counter=0; - unsigned int size=0; - - for (i=0; i<data->length; i++) { - if (data->data[i]==false) blank_counter++; - else { // else true - // checks all the blanks so far, and then reset the counter - if (blank_counter==3) size++; // if blanks to separate two chars - else if (blank_counter==7) size+=2; // if blanks for a space : acknowledge the space and the char before the space - blank_counter=0; - } - } - size++; // acknowledge the last char - if (blank_counter>=7) size++; - return size; -} - -void morse_decode(Morse_data* data, char* word) { - unsigned int i; - unsigned int char_i=0; - unsigned int false_counter=0; - unsigned int true_counter=0; - const struct _morse_tree* tree = &_morse_tree_root; // CHANGE TO THE BINRARY TREE IMPLEMENTATION AND PUT THE ROOT INSTEAD - - for (i=0; i<data->length; i++) { - if (data->data[i]==false) { - // if false, increase the false_counter, manage the number of true and false before, and reset the true_counter - false_counter++; - - if (true_counter==1) tree = tree->dot; - else if (true_counter==3) tree = tree->dash; - - if (false_counter==3) { - word[char_i++] = tree->value; - tree = &_morse_tree_root; - } - else if (false_counter==7) word[char_i++] = ' '; - - true_counter=0; - } - else { // else true - true_counter++; - false_counter=0; - } - } - - if (true_counter==1) word[char_i++] = tree->dot->value; - else if (true_counter==3) word[char_i++] = tree->dash->value; - - word[char_i] = '\0'; // finally, end the char array -}
--- a/MorseEncoder.cpp Thu Sep 18 17:26:24 2014 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,217 +0,0 @@ -/* - 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. -*/ - -#include "Morse.h" - -// Definitions of encoder/decoder data and Morse letter definitions -#define DOT false -#define DASH true - -#define MORSE_MN {DOT, DOT, DASH, DASH, 0 } -#define MORSE_DO {DASH, DASH, DASH, DASH, 0 } -#define MORSE_SL {DOT, DASH, DOT, DASH, 0 } -#define MORSE_0 {DASH, DASH, DASH, DASH, DASH} -#define MORSE_1 {DOT, DASH, DASH, DASH, DASH} -#define MORSE_2 {DOT, DOT, DASH, DASH, DASH} -#define MORSE_3 {DOT, DOT, DOT, DASH, DASH} -#define MORSE_4 {DOT, DOT, DOT, DOT, DASH} -#define MORSE_5 {DOT, DOT, DOT, DOT, DOT } -#define MORSE_6 {DASH, DOT, DOT, DOT, DOT } -#define MORSE_7 {DASH, DASH, DOT, DOT, DOT } -#define MORSE_8 {DASH, DASH, DASH, DOT, DOT } -#define MORSE_9 {DASH, DASH, DASH, DASH, DOT } -#define MORSE_IN {DOT, DOT, DASH, DOT, DOT } -#define MORSE_AR {DOT, DOT, DOT, DASH, DOT } -#define MORSE_A {DOT, DASH, 0, 0, 0 } -#define MORSE_B {DASH, DOT, DOT, DOT, 0 } -#define MORSE_C {DASH, DOT, DASH, DOT, 0 } -#define MORSE_D {DASH, DOT, DOT, 0, 0 } -#define MORSE_E {DOT, 0, 0, 0, 0 } -#define MORSE_F {DOT, DOT, DASH, DOT, 0 } -#define MORSE_G {DASH, DASH, DOT, 0, 0 } -#define MORSE_H {DOT, DOT, DOT, DOT, 0 } -#define MORSE_I {DOT, DOT, 0, 0, 0 } -#define MORSE_J {DOT, DASH, DASH, DASH, 0 } -#define MORSE_K {DASH, DOT, DASH, 0, 0 } -#define MORSE_L {DOT, DASH, DOT, DOT, 0 } -#define MORSE_M {DASH, DASH, 0, 0, 0 } -#define MORSE_N {DASH, DOT, 0, 0, 0 } -#define MORSE_O {DASH, DASH, DASH, 0, 0 } -#define MORSE_P {DOT, DASH, DASH, DOT, 0 } -#define MORSE_Q {DASH, DASH, DOT, DASH, 0 } -#define MORSE_R {DOT, DASH, DOT, 0, 0 } -#define MORSE_S {DOT, DOT, DOT, 0, 0 } -#define MORSE_T {DASH, 0, 0, 0, 0 } -#define MORSE_U {DOT, DOT, DASH, 0, 0 } -#define MORSE_V {DOT, DOT, DOT, DASH, 0 } -#define MORSE_W {DOT, DASH, DASH, 0 , 0 } -#define MORSE_X {DASH, DOT, DOT, DASH, 0 } -#define MORSE_Y {DASH, DOT, DASH, DASH, 0 } -#define MORSE_Z {DASH, DASH, DOT, DOT, 0 } -#define MORSE_UN {DASH, DASH, DASH, DOT, 0 } - -#define MORSE_MN_SIZE 11 -#define MORSE_DO_SIZE 15 -#define MORSE_SL_SIZE 11 -#define MORSE_0_SIZE 19 -#define MORSE_1_SIZE 17 -#define MORSE_2_SIZE 15 -#define MORSE_3_SIZE 13 -#define MORSE_4_SIZE 11 -#define MORSE_5_SIZE 9 -#define MORSE_6_SIZE 11 -#define MORSE_7_SIZE 13 -#define MORSE_8_SIZE 15 -#define MORSE_9_SIZE 17 -#define MORSE_IN_SIZE 11 -#define MORSE_AR_SIZE 11 -#define MORSE_A_SIZE 5 -#define MORSE_B_SIZE 9 -#define MORSE_C_SIZE 11 -#define MORSE_D_SIZE 7 -#define MORSE_E_SIZE 1 -#define MORSE_F_SIZE 9 -#define MORSE_G_SIZE 9 -#define MORSE_H_SIZE 7 -#define MORSE_I_SIZE 3 -#define MORSE_J_SIZE 13 -#define MORSE_K_SIZE 9 -#define MORSE_L_SIZE 9 -#define MORSE_M_SIZE 7 -#define MORSE_N_SIZE 5 -#define MORSE_O_SIZE 11 -#define MORSE_P_SIZE 11 -#define MORSE_Q_SIZE 13 -#define MORSE_R_SIZE 7 -#define MORSE_S_SIZE 5 -#define MORSE_T_SIZE 3 -#define MORSE_U_SIZE 7 -#define MORSE_V_SIZE 9 -#define MORSE_W_SIZE 9 -#define MORSE_X_SIZE 11 -#define MORSE_Y_SIZE 13 -#define MORSE_Z_SIZE 11 -#define MORSE_UN_SIZE 13 - -// Array containing the morse letter definitions and the size of each letter (size is in bits) -// Example: A = DOT-DASH. -// DOT has size 1 and DASH 3. Plus the blank between the DOT and the DASH -// So A has size 1+3+1 = 5 -static const unsigned int _morse_values_size[42] = {MORSE_MN_SIZE, MORSE_DO_SIZE, MORSE_SL_SIZE, MORSE_0_SIZE, MORSE_1_SIZE, MORSE_2_SIZE, - MORSE_3_SIZE, MORSE_4_SIZE, MORSE_5_SIZE, MORSE_6_SIZE, MORSE_7_SIZE, MORSE_8_SIZE, - MORSE_9_SIZE, MORSE_IN_SIZE, MORSE_AR_SIZE, MORSE_A_SIZE, MORSE_B_SIZE, MORSE_C_SIZE, - MORSE_D_SIZE, MORSE_E_SIZE, MORSE_F_SIZE, MORSE_G_SIZE, MORSE_H_SIZE, MORSE_I_SIZE, - MORSE_J_SIZE, MORSE_K_SIZE, MORSE_L_SIZE, MORSE_M_SIZE, MORSE_N_SIZE, MORSE_O_SIZE, - MORSE_P_SIZE, MORSE_Q_SIZE, MORSE_R_SIZE, MORSE_S_SIZE, MORSE_T_SIZE, MORSE_U_SIZE, - MORSE_V_SIZE, MORSE_W_SIZE, MORSE_X_SIZE, MORSE_Y_SIZE, MORSE_Z_SIZE, MORSE_UN_SIZE}; - -static const bool _morse_values[42][5] = {MORSE_MN, MORSE_DO, MORSE_SL, MORSE_0, MORSE_1, MORSE_2, - MORSE_3, MORSE_4, MORSE_5, MORSE_6, MORSE_7, MORSE_8, - MORSE_9, MORSE_IN, MORSE_AR, MORSE_A, MORSE_B, MORSE_C, - MORSE_D, MORSE_E, MORSE_F, MORSE_G, MORSE_H, MORSE_I, - MORSE_J, MORSE_K, MORSE_L, MORSE_M, MORSE_N, MORSE_O, - MORSE_P, MORSE_Q, MORSE_R, MORSE_S, MORSE_T, MORSE_U, - MORSE_V, MORSE_W, MORSE_X, MORSE_Y, MORSE_Z, MORSE_UN}; - -// Returns the index of the letter in the above arrays. returns -1 if not found -static int _morse_find(char c) { - int i=-1; - if (c>='-' && c<='9') i=c-'-'; - else if (c>='?' && c<='Z') i=c-'?'+13; - else if (c>='a' && c<='z') i=c-'a'+15; - else if (c=='_') i=41; - return i; -} - -// Add a single char (index array) to the result array at the specified position -static unsigned int _morse_addChar(unsigned int index_value, bool* result, unsigned int place) { - unsigned char i,j; - i=0; - for (j=0; j<_morse_values_size[index_value]; j++) { - if (_morse_values[index_value][i]==DOT) { // if DOT - result[place+j] = true; - } else { // else DASH - result[place+j] = true; - result[place+j+1]=true; - result[place+j+2]=true; - j+=2; - } - j++; // add a blank - i++; // next element - } - - return j-1; -} - -unsigned int morse_getBoolSize(char* word) { - unsigned int char_i; - int morse_i; - unsigned int size = 0; - - for (char_i=0; word[char_i]!='\0'; char_i++) { - if (word[char_i]!=' ') { // if not space - morse_i = _morse_find(word[char_i]); - if (morse_i==-1) continue; // if not found - - size += _morse_values_size[morse_i]; - size += 3; // plus the 3 blanks between chars - } - else { // else space - if (char_i==0 || word[char_i-1]==' ') size+=7; - else size+=4; // if not first letter, already 3 from the last char - } - } - if (word[char_i-1]!=' ') size -= 3; // remove last space between elements (but not if space) - - return size; -} - -void morse_encode(char* word, Morse_data* data) { - unsigned int char_i; - int morse_i; - unsigned int res_counter; - - res_counter = 0; - for (char_i=0; word[char_i]!='\0'; char_i++) { - if (word[char_i]!=' ') { - morse_i = _morse_find(word[char_i]); - if (morse_i==-1) continue; // if not found - - res_counter += _morse_addChar(morse_i, data->data, res_counter); // if not space - res_counter += 3; // add space between elements - } - else { // else space - if (char_i==0 || word[char_i-1]==' ') res_counter+=7; // if not char last time - else res_counter += 4; - } - } - if (word[char_i-1]!=' ') res_counter -= 3; // remove last space between elements (but not if space) - data->length = res_counter; -} - -Morse_data* morse_create(unsigned int length) { - Morse_data* data = new Morse_data; - data->length = length; - data->data = new bool[length](); - return data; -} -void morse_destroy(Morse_data* data) { - delete[] data->data; - delete data; -}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Speaker.h Sat Feb 03 11:45:23 2018 +0000 @@ -0,0 +1,20 @@ +#include "mbed.h" +// new class to play a note on Speaker based on PwmOut class +class Speaker +{ +public: + Speaker(PinName pin) : _pin(pin) { + // _pin(pin) means pass pin to the Speaker Constructor + } + + // class method to play a note based on PwmOut class + void PlayNote(float frequency, float duration, float volume) { + _pin.period(1.0/frequency); // higher number = more base + _pin = volume/2.0; // higher number = more weird treble + wait(duration); + _pin = 0.0; + } + +private: + PwmOut _pin; +};
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Sat Feb 03 11:45:23 2018 +0000 @@ -0,0 +1,50 @@ +#include "mbed.h" +#include "Morse.h" +#include "Speaker.h" +#include "FIFO.h" +// Global Variables +float SpeakerFreq = 2000; +float SpeakerVol = 1; +float DotLength = 0.1; // Duration of a dot. +float Speed = 1.2/DotLength; // Words per minute (1 word = 50 dots). + +int cnt_todo=0; +char* mdata; + +FIFO <char> message(1000); +Speaker buzzer(PB_6); + +char character_in, character_out; + +Serial pc(USBTX, USBRX); + +Thread thread_user_intf; + +void pc_intf() +{ + while(true) { + while(!pc.readable()) {}; + cnt_todo++; + character_in = pc.getc(); + message.put(character_in); + } +} + +int main() +{ + thread_user_intf.start(pc_intf); + while(true) { + if(cnt_todo>0) { + character_out = message.get(); + mdata = EncodeMorse(character_out); + for (int i=0; i<strlen(mdata); i++) { + if(mdata[i]!='0') + buzzer.PlayNote(SpeakerFreq,((mdata[i]-'0')/Speed),SpeakerVol); + else + Thread::wait(1000/Speed); + } + Thread::wait(3000/Speed); + cnt_todo--; + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-os.lib Sat Feb 03 11:45:23 2018 +0000 @@ -0,0 +1,1 @@ +https://github.com/ARMmbed/mbed-os/#caeaa49d68c67ee00275cece10cd88e0ed0f6ed3