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);
}

MorseEncoder.cpp

Committer:
rominos2
Date:
2014-09-16
Revision:
0:4648894e0d80
Child:
1:84ef66bf435d

File content as of revision 0:4648894e0d80:

#include "MorseEncoder.h"
#include "Morse.h"

const unsigned int MorseEncoder::_values_size[36] = {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_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};

const bool MorseEncoder::_values[36][5] =     {MORSE_0, MORSE_1, MORSE_2, MORSE_3, MORSE_4, MORSE_5, MORSE_6, MORSE_7, MORSE_8, MORSE_9, 
                                               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};
                                                   
unsigned int MorseEncoder::find(char c) {
    unsigned int i=37;
    if (c>='0' && c<='9') i=c-'0';
    else if (c>='A' && c<='Z') i=c-'A'+10;
    else if (c>='a' && c<='z') i=c-'a'+10;
    return i;                                             
}

unsigned int MorseEncoder::addChar(unsigned int index_value, bool* result, unsigned int place) {
    unsigned char i,j;
    i=0;
    for (j=0; j<_values_size[index_value]; j++) {
        if (_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;
}
                                              
MorseEncoder_data* MorseEncoder::encode(char* word) {
    int char_i;
    unsigned int size;
    unsigned int morse_i;
    unsigned int res_counter;
    MorseEncoder_data* data = new MorseEncoder_data;
    bool* res;
    
    // first loop to know the size
    size = 0;
    for (char_i=0; word[char_i]!='\0'; char_i++) {
        if (word[char_i]!=' ') { // if not space
            morse_i = find(word[char_i]);
            if (morse_i==37) continue; // if not found
            
            size += _values_size[morse_i];
            size += 3;
        }
        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)
    
    res = new bool[size]();
    
    // second loop to fill the result
    res_counter = 0;
    for (char_i=0; word[char_i]!='\0'; char_i++) {
        if (word[char_i]!=' ') {
            morse_i = find(word[char_i]);
            if (morse_i==37) continue; // if not found
            
            res_counter += addChar(morse_i, res, 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+=3; // if not char last time
            res_counter += 4;
        }
        
    }
    
    data->length = size;
    data->data = res;
    return data;
}

void MorseEncoder::destroy(MorseEncoder_data* data) {
    delete[] data->data;
    delete data;
}