1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
// _____________________ Header Files _____________________
#include <ee108.h>
#include <ee108_switches.h>
// _________________________ Types ________________________
struct MorseCode {
char decodedChar; // this represents the decoded character
const char *encodedStr; // the represents the morse encoding
};
typedef struct MorseCode MorseCode;
// _______________________ Constants _______________________
// lookup table to allow conversion from characters to morse codes
// and from morse codes to characters
const MorseCode MORSE_TABLE[] = {
{ 'A', ".-" },
{ 'B', "-..." },
{ 'C', "-.-." },
{ 'D', "-..", },
{ 'E', "." },
{ 'F', "..-." },
{ 'G', "--." },
{ 'H', "...." },
{ 'I', ".." },
{ 'J', ".---" },
{ 'K', "-.-" },
{ 'L', ".-.." },
{ 'M', "--" },
{ 'N', "-." },
{ 'O', "---" },
{ 'P', ".--." },
{ 'Q', "--.-" },
{ 'R', ".-." },
{ 'S', "..." },
{ 'T', "-" },
{ 'U', "..-" },
{ 'V', "...-" },
{ 'W', ".--" },
{ 'X', "-..-" },
{ 'Y', "-.--" },
{ 'Z', "--.." },
{ '0', ".----" },
{ '1', "..---" },
{ '2', "...--" },
{ '3', "....-" },
{ '4', "....." },
{ '5', "-...." },
{ '6', "--..." },
{ '7', "---.." },
{ '8', "----." },
{ '9', "-----" }
};
const int SUPERLOOP_DELAY_MS = 25;
const int MORSE_TABLE_SIZE = sizeof(MORSE_TABLE) / sizeof(MORSE_TABLE[0]);
const int BUFFER_SIZE = 11;
// ___________________ Function Declarration __________________
// ____________________________________________________________
void setup() {
// set up serial
Serial.begin(9600);
Serial.println("\n\n\t________A4_SerialMorseToChar________\n");
}
void loop() {
char buf[BUFFER_SIZE] = "";
Serial.println("\n\tType a pattern of . and - chars & hit send:");
int bufIndex = 0;
// read characters & break out of while loop if end of line,
// space character or max buffer size is detected.
while (bufIndex < BUFFER_SIZE) {
char symbol;
// wait until at least one character has been entered
while (Serial.available() == 0)
continue;
// OK, we now have at least 1 character
symbol = Serial.read(); // read 1 character
// a newline or space indicates the end of the word
// so we'll break out to process it
if ((symbol == '\n') || (symbol == '\r') || (symbol == isspace(symbol))) {
if (bufIndex > 0)
break;
}
// ignore any characters that are not alphabetic
if (symbol != '.' && symbol != '-')
continue; // go directly to next iteration of while loop
// at this point only valid characters should be left
buf[bufIndex] = symbol; // add the character to the buffer
bufIndex++; // increment index
}
// newline, space or max number of characters?
buf[bufIndex] = '\0'; // add nul terminator to buf
Serial.print("\tInput was: ");
Serial.println(buf);
for (bufIndex = 0; bufIndex < MORSE_TABLE_SIZE; bufIndex++) {
if (buf == MORSE_TABLE[bufIndex].encodedStr) {
Serial.print("Decoded character is: ");
Serial.println(MORSE_TABLE[bufIndex].decodedChar);
}
}
}
|