diff --git a/src/keyer.cpp b/src/keyer.cpp index 8fd26f5..553ed5c 100644 --- a/src/keyer.cpp +++ b/src/keyer.cpp @@ -53,7 +53,7 @@ void Keyer::sendCharacter(const char ch) { char morseSymbols[32] {0}; - if (!charToMorse(ch, morseSymbols)) { + if (!m_morseCode.charToMorse(ch, morseSymbols)) { return; } diff --git a/src/keyer.h b/src/keyer.h index 6b13e34..99ce35f 100644 --- a/src/keyer.h +++ b/src/keyer.h @@ -4,6 +4,7 @@ #include "settings.h" #include "sidetone.h" +#include "morse.h" class Keyer final { @@ -58,4 +59,6 @@ class Keyer final bool m_currentlyKeying {false}; bool m_currentlyPausing {false}; bool m_keyNextIambicB {false}; + + MorseCode m_morseCode; }; \ No newline at end of file diff --git a/src/morse.cpp b/src/morse.cpp index fc66525..c549851 100644 --- a/src/morse.cpp +++ b/src/morse.cpp @@ -1,187 +1,65 @@ -#include -#include +#include +#include + +#include "pico/stdio.h" #include "morse.h" -std::map morseMap = { - {'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', "--.."}, - {'1', ".----"}, - {'2', "..---"}, - {'3', "...--"}, - {'4', "....-"}, - {'5', "....."}, - {'6', "-...."}, - {'7', "--..."}, - {'8', "---.."}, - {'9', "----."}, - {'0', "-----"}, - {',', "--..--"}, - {'.', ".-.-.-"}, - {'?', "..--.."}, - {':', "---..."}, - {'&', ".-..."}, - // Some WinKeyer compatible mappings … - {'\"', ".-..-."}, // " => - {'$', "...-..-"}, // $ => - {'\'', ".----."}, // ' => - {'(', "-.--."}, // ( => - {')', "-.--.-"}, // ) => - {'+', ".-.-."}, // + => - {'-', "-....-"}, // - => - {'/', "-..-."}, // / => - {':', "-.--."}, // : => - {';', ".-.-"}, // ; => - {'<', ".-.-."}, // < => [sic!] - {'=', "-...-"}, // = => - {'>', "...-.-"}, // > == - {'@', ".--.-."}, // @ => - {'[', ".-..."}, // [ => - {'\\', "-..-."}, // \ => - {']', "-.--."}, // ] => [sic!] -}; +MorseCode::MorseCode() { buildMap(); } -bool charToMorse(const char ch, char *morseSymbols) -{ - size_t index {0}; +bool MorseCode::charToMorse(const unsigned char ch, char *morseSymbols) { + size_t index{0}; - if (ch != ' ' && !morseMap.contains(ch)) { - return false; + if (ch != ' ' && morseMap[ch] == nullptr) { + return false; + } + + if (ch == ' ') { + morseSymbols[index++] = 'w'; + } else { + for (unsigned int i = 0; i < strlen(morseMap[ch]); i++) { + char m = morseMap[ch][i]; + + morseSymbols[index++] = m; + + if (i < strlen(morseMap[ch]) - 1) { + morseSymbols[index++] = 'i'; + } } - if (ch == ' ') { - morseSymbols[index++] = 'w'; - } else { - for (size_t i = 0; i < morseMap[ch].length(); i++) { - char m = morseMap[ch][i]; + morseSymbols[index++] = 'c'; + } - morseSymbols[index++] = m; + morseSymbols[index] = '\0'; - if (i < morseMap[ch].length() - 1) { - morseSymbols[index++] = 'i'; - } - } - - morseSymbols[index++] = 'c'; - } - - morseSymbols[index] = '\0'; - - return true; + return true; } -/* -void refurbishMessage(std::string &msg) -{ - // Make the message all upper case - std::transform(msg.begin(), msg.end(), msg.begin(), ::toupper); +void MorseCode::sendCharacter(const char ch) { + char morseSymbols[32]{0}; - // Encode the special characters as we like it - msg = std::regex_replace(msg, std::regex(""), "="); - msg = std::regex_replace(msg, std::regex(""), "b"); - msg = std::regex_replace(msg, std::regex(""), "k"); - msg = std::regex_replace(msg, std::regex(""), "s"); - msg = std::regex_replace(msg, std::regex(""), "+"); - msg = std::regex_replace(msg, std::regex(""), "a"); + if (!charToMorse(ch, morseSymbols)) { + return; + } - // Remove all other unknown characters - msg.erase(remove_if(msg.begin(), msg.end(), - [](const char &c) { return c != ' ' && morseMap.find(c) == morseMap.end(); }), - msg.end()); - - // Remove spaces, if there are too many of them - msg = std::regex_replace(msg, std::regex("(\\s+)"), " "); + for (unsigned int i = 0; i < strlen(morseSymbols); i++) { + printf("%c", morseSymbols[i]); + } } -*/ -/* -std::string messageToMorse(std::string &msg) -{ - // refurbishMessage(msg); - std::string morseString {}; - - for (unsigned int i = 0; i < msg.length(); i++) { - char c = msg[i]; - if (c == ' ') { - morseString.push_back('w'); - continue; - } - - // Ignore and continue with next char, if not found - auto search = morseMap.find(c); - if (search == morseMap.end()) { - continue; - } - - for (unsigned int j = 0; j < morseMap[c].length(); j++) { - char m = morseMap[c][j]; - if (j == 0 && i > 0 && msg[i - 1] != ' ') { - morseString.push_back('c'); - } - - morseString += m; - - if (j < morseMap[c].length() - 1) { - morseString.push_back('i'); - } - } - } - - // Append word space if last char was not a blank - if (msg.back() != ' ') { - morseString.push_back('w'); - } - - return morseString; -} -*/ - -/* -std::string charToMorse(const char ch) -{ - std::string morseString {}; - - if (ch == ' ') { - morseString.push_back('w'); - return morseString; - } - - for (unsigned int j = 0; j < morseMap[ch].length(); j++) { - char m = morseMap[ch][j]; - - morseString += m; - - if (j < morseMap[ch].length() - 1) { - morseString.push_back('i'); - } - } - - morseString.push_back('c'); - - return morseString; -} -*/ \ No newline at end of file +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]; + } +} \ No newline at end of file diff --git a/src/morse.h b/src/morse.h index 1ed7496..441a147 100644 --- a/src/morse.h +++ b/src/morse.h @@ -1,3 +1,70 @@ #pragma once -bool charToMorse(const char ch, char * morseSymbols); \ No newline at end of file +class MorseCode final { +public: + MorseCode(); + void sendCharacter(const char ch); + bool charToMorse(const unsigned char ch, char *morseSymbols); + +private: + void buildMap(); + const char *morseMap[256] {nullptr}; + static constexpr const char *morseTable[] = { + ".-..-.", // " => + "...-..-", // $ => + ".----.", // ' => + "-.--.", // ( => + "-.--.-", // ) => + ".-.-.", // + => + "--..--", // , + "-....-", // - => + ".-.-.-", // . + "-..-.", // / => + "-----", // 0 + ".----", // 1 + "..---", // 2 + "...--", // 3 + "....-", // 4 + ".....", // 5 + "-....", // 6 + "--...", // 7 + "---..", // 8 + "----.", // 9 + "-.--.", // : => + ".-.-", // ; => + ".-.-.", // < => [sic!] + "-...-", // = => + "...-.-", // > == + "..--..", // ? + ".--.-.", // @ => + ".-", // 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 + ".-...", // [ => + "-..-.", // \ => + "-.--.", // ] => [sic!] + }; +}; \ No newline at end of file