/*
MORSE LED flasher v01

Danijel Pticar 2015

*/


//char mmesage[]="SOS";
char mmesage[] = "DANIJEL PTICAR";

//                            A    B    C    D    E
unsigned char morse_table[]={0x42,0x84,0xA4,0x83,0x01,
//                            F    G    H     I   J
                             0x24,0xC3,0x04,0x02,0x74,
//                            K    L    M    N    O
                             0xA3,0x44,0xC2,0x82,0xE3,
//                            P    Q    R    S    T
                             0x64,0xD4,0x43,0x03,0x81,
//                            U    V    W    X    Y    Z
                             0x23,0x14,0x63,0x94,0xB4,0xC4
                             };

#define morse_unit 300
#define ledPIN 3


void setup() {
  // put your setup code here, to run once:
  pinMode(ledPIN, OUTPUT);
}


void morse_send_DASH()
{
    digitalWrite(ledPIN, HIGH);
    delay(morse_unit*3);
    digitalWrite(ledPIN, LOW);
    delay(morse_unit);
}

void morse_send_DOT()
{
    digitalWrite(ledPIN, HIGH);
    delay(morse_unit);
    digitalWrite(ledPIN, LOW);
    delay(morse_unit);
}

void morse_print_letter(char letter)
{
    int letter_index;
    unsigned char morse_length, morse_table_code;

    letter_index = letter - 'A';
    morse_length = morse_table[letter_index] & 0x07; // extract length
    morse_table_code = morse_table[letter_index];

    for(int i=0;i<morse_length;i++) // max 4 (5 if numbers are in table)
    {
        if(morse_table_code & 0x80) morse_send_DASH(); // 1 is dash, 0 is dot
          else morse_send_DOT();
        morse_table_code<<=1;    // shift out used dot/dash

    }
    
    delay(morse_unit*2);  // 2+one from dash/dot

}

void loop()
{
    int m_len=strlen(mmesage);

    for(int i=0;i<m_len;i++)
    {
       if(mmesage[i]==' ') delay(4*morse_unit); // we have +3 unit from letter
        else  morse_print_letter(mmesage[i]);
    }
    delay(4*morse_unit);
}