From 99da1e0e390f8824dad1cfdac17bb508229785d0 Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Fri, 23 Feb 2024 08:58:48 +0100 Subject: [PATCH] some morse stuff added --- src/CMakeLists.txt | 1 + src/morse.cpp | 47 ++++++++++++++++++++++++++++++++++++++++++++++ src/morse.h | 5 +++++ 3 files changed, 53 insertions(+) create mode 100644 src/morse.cpp create mode 100644 src/morse.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index abd760e..74e638f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -3,5 +3,6 @@ target_sources(raspi_keyer PRIVATE settings.cpp keyer.cpp sidetone.cpp + morse.cpp usb_descriptors.c ) diff --git a/src/morse.cpp b/src/morse.cpp new file mode 100644 index 0000000..819a26a --- /dev/null +++ b/src/morse.cpp @@ -0,0 +1,47 @@ +#include +#include +#include + +#include "morse.h" + +std::map morseCode = { + {'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', "-----"}, + {',', "--..--"}, {'.', ".-.-.-"}, {'?', "..--.."}, {'/', "-..-."}, {'-', "-....-"}, {':', "---..."}, + {'&', ".-..."}, {'\'', ".----."}, {'@', ".--.-."}, {')', "-.--.-"}, {'(', "-.--."}, {'\"', ".-..-."}, + {'=', "-...-"}, // '=' == + {'k', "-.--."}, // k == + {'s', "...-.-"}, // s == + {'+', ".-.-."}, // + == + {'a', "-.-.-"}, // a == +}; + +std::string refurbishMessage(const std::string &msg) +{ + std::string msgUpper; + msgUpper.resize(msg.length()); + + // Make the message all upper case + std::transform(msg.cbegin(), msg.cend(), msgUpper.begin(), [](unsigned char c) { return std::toupper(c); }); + + // Encode the special characters as we like it + msgUpper = std::regex_replace(msgUpper, std::regex(""), "="); + msgUpper = std::regex_replace(msgUpper, std::regex(""), "k"); + msgUpper = std::regex_replace(msgUpper, std::regex(""), "s"); + msgUpper = std::regex_replace(msgUpper, std::regex(""), "+"); + msgUpper = std::regex_replace(msgUpper, std::regex(""), "a"); + + // Remove all other unknown characters + msgUpper.erase(remove_if(msgUpper.begin(), msgUpper.end(), + [](const char &c) { return c != ' ' && morseCode.find(c) == morseCode.end(); }), + msgUpper.end()); + + // Remove spaces, if there are too many of them + msgUpper = std::regex_replace(msgUpper, std::regex("(\\s+)"), " "); + + return msgUpper; +} \ No newline at end of file diff --git a/src/morse.h b/src/morse.h new file mode 100644 index 0000000..9723aa6 --- /dev/null +++ b/src/morse.h @@ -0,0 +1,5 @@ +#pragma once + +#include + +std::string refurbishMessage(const std::string msg); \ No newline at end of file