2024-02-29 09:25:53 +01:00
|
|
|
#include <cstdio>
|
2024-02-29 09:27:59 +01:00
|
|
|
#include <cstring>
|
2024-02-23 08:58:48 +01:00
|
|
|
|
2024-02-29 09:25:53 +01:00
|
|
|
#include "pico/stdio.h"
|
2024-02-23 08:58:48 +01:00
|
|
|
|
2024-02-29 09:25:53 +01:00
|
|
|
#include "morse.h"
|
2024-02-23 08:58:48 +01:00
|
|
|
|
2024-02-29 09:25:53 +01:00
|
|
|
MorseCode::MorseCode() { buildMap(); }
|
2024-02-28 14:39:55 +01:00
|
|
|
|
2024-02-29 09:27:59 +01:00
|
|
|
bool MorseCode::charToMorse(const unsigned char ch, char *morseSymbols)
|
|
|
|
{
|
|
|
|
size_t index {0};
|
2024-02-28 14:39:55 +01:00
|
|
|
|
2024-02-29 09:27:59 +01:00
|
|
|
if (ch != ' ' && morseMap[ch] == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
2024-02-28 14:39:55 +01:00
|
|
|
|
2024-02-29 09:27:59 +01:00
|
|
|
if (ch == ' ') {
|
|
|
|
morseSymbols[index++] = 'w';
|
|
|
|
} else {
|
|
|
|
for (unsigned int i = 0; i < strlen(morseMap[ch]); i++) {
|
|
|
|
char m = morseMap[ch][i];
|
2024-02-28 14:39:55 +01:00
|
|
|
|
2024-02-29 09:27:59 +01:00
|
|
|
morseSymbols[index++] = m;
|
2024-02-28 14:39:55 +01:00
|
|
|
|
2024-02-29 09:27:59 +01:00
|
|
|
if (i < strlen(morseMap[ch]) - 1) {
|
|
|
|
morseSymbols[index++] = 'i';
|
|
|
|
}
|
|
|
|
}
|
2024-02-28 14:39:55 +01:00
|
|
|
|
2024-02-29 09:27:59 +01:00
|
|
|
morseSymbols[index++] = 'c';
|
|
|
|
}
|
2024-02-23 23:26:59 +01:00
|
|
|
|
2024-02-29 09:27:59 +01:00
|
|
|
morseSymbols[index] = '\0';
|
2024-02-23 08:58:48 +01:00
|
|
|
|
2024-02-29 09:27:59 +01:00
|
|
|
return true;
|
2024-02-23 14:29:36 +01:00
|
|
|
}
|
|
|
|
|
2024-02-29 09:27:59 +01:00
|
|
|
void MorseCode::sendCharacter(const char ch)
|
|
|
|
{
|
|
|
|
char morseSymbols[32] {0};
|
2024-02-23 14:29:36 +01:00
|
|
|
|
2024-02-29 09:27:59 +01:00
|
|
|
if (!charToMorse(ch, morseSymbols)) {
|
|
|
|
return;
|
|
|
|
}
|
2024-02-23 14:29:36 +01:00
|
|
|
|
2024-02-29 09:27:59 +01:00
|
|
|
for (unsigned int i = 0; i < strlen(morseSymbols); i++) {
|
|
|
|
printf("%c", morseSymbols[i]);
|
|
|
|
}
|
2024-02-26 15:19:11 +01:00
|
|
|
}
|
|
|
|
|
2024-02-29 09:27:59 +01:00
|
|
|
void MorseCode::buildMap()
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < 256; i++) {
|
|
|
|
if (i == '\"')
|
|
|
|
morseMap[i] = morseTable[0];
|
|
|
|
else if (i == '$')
|
|
|
|
morseMap[i] = morseTable[1];
|
|
|
|
else if (i == '\'')
|
|
|
|
morseMap[i] = morseTable[2];
|
|
|
|
else if (i == '(')
|
|
|
|
morseMap[i] = morseTable[3];
|
|
|
|
else if (i == ')')
|
|
|
|
morseMap[i] = morseTable[4];
|
|
|
|
else if (i >= '+' && i <= ']')
|
|
|
|
morseMap[i] = morseTable[i - 0x26];
|
|
|
|
}
|
2024-02-29 09:25:53 +01:00
|
|
|
}
|