some morse stuff added

This commit is contained in:
Martin Brodbeck 2024-02-23 08:58:48 +01:00
parent 8ae604ed4e
commit 99da1e0e39
3 changed files with 53 additions and 0 deletions

View file

@ -3,5 +3,6 @@ target_sources(raspi_keyer PRIVATE
settings.cpp settings.cpp
keyer.cpp keyer.cpp
sidetone.cpp sidetone.cpp
morse.cpp
usb_descriptors.c usb_descriptors.c
) )

47
src/morse.cpp Normal file
View file

@ -0,0 +1,47 @@
#include <algorithm>
#include <map>
#include <regex>
#include "morse.h"
std::map<char, std::string> 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', "-----"},
{',', "--..--"}, {'.', ".-.-.-"}, {'?', "..--.."}, {'/', "-..-."}, {'-', "-....-"}, {':', "---..."},
{'&', ".-..."}, {'\'', ".----."}, {'@', ".--.-."}, {')', "-.--.-"}, {'(', "-.--."}, {'\"', ".-..-."},
{'=', "-...-"}, // '=' == <BT>
{'k', "-.--."}, // k == <KN>
{'s', "...-.-"}, // s == <SK>
{'+', ".-.-."}, // + == <AR>
{'a', "-.-.-"}, // a == <KA>
};
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("<BT>"), "=");
msgUpper = std::regex_replace(msgUpper, std::regex("<KN>"), "k");
msgUpper = std::regex_replace(msgUpper, std::regex("<SK>"), "s");
msgUpper = std::regex_replace(msgUpper, std::regex("<AR>"), "+");
msgUpper = std::regex_replace(msgUpper, std::regex("<KA>"), "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;
}

5
src/morse.h Normal file
View file

@ -0,0 +1,5 @@
#pragma once
#include <string>
std::string refurbishMessage(const std::string msg);