There's morse code all around us ...
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include <ctype.h>
#if defined(_BOARD_CEREBOT_MX4CK_)
const uint8_t pinLED = 64; // ON-BOARD LED1
#elif defined(_BOARD_UNO_)
const uint8_t pinLED = 13; // ON-BOARD LED1
#else
#error "!!!Target is unknown board!!!"
#endif
const unsigned long DELAY_CASE = 150UL;
const unsigned long DELAY_DOT = DELAY_CASE; // MILLISECONDS
const unsigned long DELAY_DOT = DELAY_INTRA;
const unsigned long DELAY_DASH = 3 * DELAY_CASE;
const unsigned long DELAY_CHAR = 3 * DELAY_CASE;
const unsigned long DELAY_WORD = 7 * DELAY_CASE;
const unsigned long DELAY_MESSAGE = 3 * 1000; // 3 SECONDS
const uint8_t LED_OFF = LOW; // SYNONYMN FOR READABILITY
const uint8_t LED_ON = HIGH; // SYNONYMN FOR READABILITY
#define SIZEOF_ARRAY(ARRAY) (sizeof(ARRAY) / sizeof(ARRAY[0]))
enum { EOS, DOT, DASH, MASK = 3 };
struct sequence_pair_t
{
const char character; // character
const uint16_t encoding; // sequence, zero terminated 'C' string
};
const sequence_pair_t morse_lookup[] =
{
// STRING OF 2-BIT VALUES ENCODE THE MORSE SEQUENCE, LSB -> MSB
//
// 00 END-OF-SEQUENCE
// 01 '.'
// 10 '-'
// 11 UNUSED
// EXAMPLES:
//
// { 'A', 0b0000000000001101 } // ".-" EOS EOS EOS EOS EOS EOS DASH DOT
//, { '0', 0b0000001010101010 } // "-----" EOS EOS EOS DASH DASH DASH DASH DASH
// --- Numeric
{ '0', 0b0000001010101010 } // "-----"
, { '1', 0b0000001010101001 } // ".----"
, { '2', 0b0000001010100101 } // "..---"
, { '3', 0b0000001010010101 } // "...--"
, { '4', 0b0000001001010101 } // "....-"
, { '5', 0b0000000101010101 } // "....."
, { '6', 0b0000000101010110 } // "-...."
, { '7', 0b0000000101011010 } // "--..."
, { '8', 0b0000000101101010 } // "---.."
, { '9', 0b0000000110101010 } // "----."
// --- Alphabetic
, { 'A', 0b0000000000001001 } // ".-"
, { 'B', 0b0000000001010110 } // "-..."
, { 'C', 0b0000000001100110 } // "-.-."
, { 'D', 0b0000000000010110 } // "-.."
, { 'E', 0b0000000000000001 } // "."
, { 'F', 0b0000000001100101 } // "..-."
, { 'G', 0b0000000000011010 } // "--."
, { 'H', 0b0000000001010101 } // "...."
, { 'I', 0b0000000000000101 } // ".."
, { 'J', 0b0000000010101001 } // ".---"
, { 'K', 0b0000000000100110 } // "-.-"
, { 'L', 0b0000000001011001 } // ".-.."
, { 'M', 0b0000000000001010 } // "--"
, { 'N', 0b0000000000000110 } // "-."
, { 'O', 0b0000000000101010 } // "---"
, { 'P', 0b0000000001101001 } // ".--."
, { 'Q', 0b0000000010011010 } // "--.-"
, { 'R', 0b0000000000011001 } // ".-."
, { 'S', 0b0000000000010101 } // "..."
, { 'T', 0b0000000000000010 } // "-"
, { 'U', 0b0000000000100101 } // "..-"
, { 'V', 0b0000000010010101 } // "...-"
, { 'W', 0b0000000000101001 } // ".--"
, { 'X', 0b0000000010010110 } // "-..-"
, { 'W', 0b0000000010100110 } // "-.--"
, { 'Z', 0b0000000001011010 } // "--.."
// --- Punctuation
, { ',', 0b0000101001011010 } // "--..--"
, { '.', 0b0000100110011001 } // ".-.-.-"
, { ':', 0b0000010101101010 } // "---..."
, { '?', 0b0000010110100101 } // "..--.."
, { '-', 0b0000100101010110 } // "-....-"
, { '/', 0b0000000110010110 } // "-..-."
, { '(', 0b0000100110100110 } // "-.--.-"
, { ')', 0b0000100110100110 } // "-.--.-"
// , { '\'', ".----." } // the windows IDE has problem with escaped character literals!
// , { '\"', ".-..-." } // the windows IDE has problem with escaped character literals!
};
void blinkDot()
{
digitalWrite(pinLED, LED_ON);
delay(DELAY_DOT);
digitalWrite(pinLED, LED_OFF);
}
void blinkDash()
{
digitalWrite(pinLED, LED_ON);
delay(DELAY_DASH);
digitalWrite(pinLED, LED_OFF);
}
void blinkCharacterSequence(uint16_t encoding)
{
do
{
switch ( encoding & MASK )
{
case DOT: blinkDot(); break;
case DASH: blinkDash(); break;
}
if ( encoding >>= 2 )
{
delay(DELAY_INTRA);
}
} while ( EOS != (encoding & MASK) );
}
void blinkCharactor(char ch)
{
const int cEntries = SIZEOF_ARRAY(morse_lookup);
for ( int i = cEntries; i--; )
{
if ( ch == morse_lookup[i].character )
{
blinkCharacterSequence(morse_lookup[i].encoding);
return;
}
}
}
void loop()
{
// Initialize message using "string" syntax which has invisible
// terminating 0
static const char szMessage[] = { "Hello AlphaTango" };
for ( int i = 0; szMessage[i]; )
{
blinkCharactor(toupper(szMessage[i++]));
delay(((' ' != szMessage[i]) ? DELAY_CHAR : DELAY_WORD));
}
delay(DELAY_MESSAGE);
}
void setup()
{
pinMode(pinLED, OUTPUT);
digitalWrite(pinLED, LED_OFF);
delay(DELAY_MESSAGE);
}