get rid of std::string

This commit is contained in:
Martin Brodbeck 2024-02-29 09:25:53 +01:00
parent 1ba9653578
commit da54029403
4 changed files with 122 additions and 174 deletions

View File

@ -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;
}

View File

@ -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;
};

View File

@ -1,187 +1,65 @@
#include <map>
#include <string>
#include <cstring>
#include <cstdio>
#include "pico/stdio.h"
#include "morse.h"
std::map<char, std::string> 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 …
{'\"', ".-..-."}, // " => <RR>
{'$', "...-..-"}, // $ => <SX>
{'\'', ".----."}, // ' => <WG>
{'(', "-.--."}, // ( => <KN>
{')', "-.--.-"}, // ) => <KK>
{'+', ".-.-."}, // + => <AR>
{'-', "-....-"}, // - => <DU>
{'/', "-..-."}, // / => <DN>
{':', "-.--."}, // : => <KN>
{';', ".-.-"}, // ; => <AA>
{'<', ".-.-."}, // < => <AR> [sic!]
{'=', "-...-"}, // = => <BT>
{'>', "...-.-"}, // > == <SK>
{'@', ".--.-."}, // @ => <AC>
{'[', ".-..."}, // [ => <AS>
{'\\', "-..-."}, // \ => <DN>
{']', "-.--."}, // ] => <KN> [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("<BT>"), "=");
msg = std::regex_replace(msg, std::regex("<BK>"), "b");
msg = std::regex_replace(msg, std::regex("<KN>"), "k");
msg = std::regex_replace(msg, std::regex("<SK>"), "s");
msg = std::regex_replace(msg, std::regex("<AR>"), "+");
msg = std::regex_replace(msg, std::regex("<KA>"), "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;
}
*/
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];
}
}

View File

@ -1,3 +1,70 @@
#pragma once
bool charToMorse(const char ch, char * morseSymbols);
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[] = {
".-..-.", // " => <RR>
"...-..-", // $ => <SX>
".----.", // ' => <WG>
"-.--.", // ( => <KN>
"-.--.-", // ) => <KK>
".-.-.", // + => <AR>
"--..--", // ,
"-....-", // - => <DU>
".-.-.-", // .
"-..-.", // / => <DN>
"-----", // 0
".----", // 1
"..---", // 2
"...--", // 3
"....-", // 4
".....", // 5
"-....", // 6
"--...", // 7
"---..", // 8
"----.", // 9
"-.--.", // : => <KN>
".-.-", // ; => <AA>
".-.-.", // < => <AR> [sic!]
"-...-", // = => <BT>
"...-.-", // > == <SK>
"..--..", // ?
".--.-.", // @ => <AC>
".-", // 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
".-...", // [ => <AS>
"-..-.", // \ => <DN>
"-.--.", // ] => <KN> [sic!]
};
};