Skip to content

Instantly share code, notes, and snippets.

@AltimorTASDK
Created January 28, 2015 00:57
Show Gist options
  • Select an option

  • Save AltimorTASDK/806102f2bc632be9c09c to your computer and use it in GitHub Desktop.

Select an option

Save AltimorTASDK/806102f2bc632be9c09c to your computer and use it in GitHub Desktop.
#include <string.h>
#include <stdlib.h>
#define BEEPER_PIN 2
#define UNIT_MS 50
#define UNIT_US (1000L * UNIT_MS)
#define DOT 0
#define DASH 1
#define WAIT_3 2 // end of a letter
#define WAIT_7 3 // end of a word
const int morse_map[][6] = {
{ DOT, DASH, WAIT_3 }, // A
{ DASH, DOT, DOT, DOT, WAIT_3 }, // B
{ DASH, DOT, DASH, DOT, WAIT_3 }, // C
{ DASH, DOT, DOT, WAIT_3 }, // D
{ DOT, WAIT_3 }, // E
{ DOT, DOT, DASH, DOT, WAIT_3 }, // F
{ DASH, DASH, DOT, WAIT_3 }, // G
{ DOT, DOT, DOT, DOT, WAIT_3 }, // H
{ DOT, DOT, WAIT_3 }, // I
{ DOT, DASH, DASH, DASH, WAIT_3 }, // J
{ DASH, DOT, DASH, WAIT_3 }, // K
{ DOT, DASH, DOT, DOT, WAIT_3 }, // L
{ DASH, DASH, WAIT_3 }, // M
{ DASH, DOT, WAIT_3 }, // N
{ DASH, DASH, DASH, WAIT_3 }, // O
{ DOT, DASH, DASH, DOT, WAIT_3 }, // P
{ DASH, DASH, DOT, DASH, WAIT_3 }, // Q
{ DOT, DASH, DOT, WAIT_3 }, // R
{ DOT, DOT, DOT, WAIT_3 }, // S
{ DASH, WAIT_3 }, // T
{ DOT, DOT, DASH, WAIT_3 }, // U
{ DOT, DOT, DOT, DASH, WAIT_3 }, // V
{ DOT, DASH, DASH, WAIT_3 }, // W
{ DASH, DOT, DOT, DASH, WAIT_3 }, // X
{ DASH, DOT, DASH, DASH, WAIT_3 }, // Y
{ DASH, DASH, DOT, DOT, WAIT_3 } // Z
};
int *morse_sequence = NULL;
int morse_length = 0;
int sequence_idx = 0;
/**
* add_char_to_sequence - Add a character to the morse code sequence
* @c: Character to add
*
* Copy over the corresponding sequence from morse_map, if it's a space just
* append a WAIT_7
*/
static void add_char_to_sequence(const char c)
{
// 7 unit wait for spaces
if (c == ' ') {
morse_sequence = (int*)(
realloc(morse_sequence, morse_length + 1));
morse_sequence[morse_length++] = WAIT_7;
return;
}
// string must be all caps alphabetical
if (c < 'A' || c > 'Z')
return;
const int map_idx = c - 'A';
const int start_idx = morse_length;
// update the buffer size
for (int i = 0; morse_map[map_idx][i] != WAIT_3; i++)
morse_length++;
morse_length++; // need an extra space to copy the wait
morse_sequence = (int*)(realloc(morse_sequence, morse_length));
// copy the sequence over
const int copy_size = (morse_length - start_idx) * sizeof(int);
memcpy(&morse_sequence[start_idx], morse_map[map_idx], copy_size);
}
/**
* setup - Initialization function
*
* Generate a sequence of morse code
*/
void setup()
{
const char *string = "ACD ON TOP";
const int len = strlen(string);
for (int i = 0; i < len; i++)
add_char_to_sequence(string[i]);
// initialize the beeper pin
pinMode(BEEPER_PIN, OUTPUT);
}
/**
* beep - Beep at the specified interval
* @interval_us: Interval in microseconds
* @time_us: Time to beep in microseconds
*
* Loop and use delayMicroseconds to beep every interval_us. Add interval_us to
* a counter and break when it reaches time_us.
*/
static void beep(const long interval_us, const long time_us)
{
const long delay_us = interval_us / 2;
for (long i = 0; i < time_us; i += interval_us) {
digitalWrite(BEEPER_PIN, HIGH);
delayMicroseconds(delay_us);
digitalWrite(BEEPER_PIN, LOW);
delayMicroseconds(delay_us);
}
delay(UNIT_MS);
}
/**
* loop - Main loop
*
* Play the morse code sequence through the beeper
*/
void loop()
{
const int cmd = morse_sequence[sequence_idx];
if (cmd == DOT)
beep(500, UNIT_US);
else if (cmd == DASH)
beep(2000, UNIT_US * 3);
else if (cmd == WAIT_3)
delay(UNIT_MS * 3);
else if (cmd == WAIT_7)
delay(UNIT_MS * 7);
if (++sequence_idx == morse_length) {
// restart sequence
sequence_idx = 0;
delay(3000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment