From 4b0b6bbcea5bf3938b597528bf773500d16f0708 Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Fri, 23 Feb 2024 23:26:59 +0100 Subject: [PATCH] beware of memory constraints!? --- .vscode/c_cpp_properties.json | 2 +- .vscode/settings.json | 79 ++++++++++++++++++++++++++++++++++- src/keyer.cpp | 11 +++-- src/keyer.h | 4 +- src/morse.cpp | 61 +++++++++++++++------------ src/morse.h | 2 +- src/raspi_keyer.cpp | 7 +++- 7 files changed, 130 insertions(+), 36 deletions(-) diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index ad7fa91..e385a5e 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -9,7 +9,7 @@ "defines": [], "compilerPath": "/usr/bin/arm-none-eabi-gcc", "cStandard": "gnu17", - "cppStandard": "gnu++20", + "cppStandard": "gnu++14", "intelliSenseMode": "linux-gcc-arm", "configurationProvider" : "ms-vscode.cmake-tools" } diff --git a/.vscode/settings.json b/.vscode/settings.json index 9f7c531..25178c7 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -15,6 +15,83 @@ }, }, "files.associations": { - "board.h": "c" + "board.h": "c", + "any": "cpp", + "array": "cpp", + "atomic": "cpp", + "bit": "cpp", + "*.tcc": "cpp", + "bitset": "cpp", + "cctype": "cpp", + "charconv": "cpp", + "chrono": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "codecvt": "cpp", + "compare": "cpp", + "complex": "cpp", + "concepts": "cpp", + "condition_variable": "cpp", + "csignal": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cstring": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "deque": "cpp", + "forward_list": "cpp", + "list": "cpp", + "map": "cpp", + "set": "cpp", + "string": "cpp", + "unordered_map": "cpp", + "unordered_set": "cpp", + "vector": "cpp", + "exception": "cpp", + "algorithm": "cpp", + "functional": "cpp", + "iterator": "cpp", + "memory": "cpp", + "memory_resource": "cpp", + "numeric": "cpp", + "optional": "cpp", + "random": "cpp", + "ratio": "cpp", + "regex": "cpp", + "string_view": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp", + "format": "cpp", + "fstream": "cpp", + "future": "cpp", + "initializer_list": "cpp", + "iomanip": "cpp", + "iosfwd": "cpp", + "iostream": "cpp", + "istream": "cpp", + "limits": "cpp", + "mutex": "cpp", + "new": "cpp", + "numbers": "cpp", + "ostream": "cpp", + "ranges": "cpp", + "semaphore": "cpp", + "span": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "stop_token": "cpp", + "streambuf": "cpp", + "thread": "cpp", + "cinttypes": "cpp", + "typeindex": "cpp", + "typeinfo": "cpp", + "valarray": "cpp", + "variant": "cpp" }, } diff --git a/src/keyer.cpp b/src/keyer.cpp index 3c90c8f..453098b 100644 --- a/src/keyer.cpp +++ b/src/keyer.cpp @@ -4,6 +4,7 @@ #include "keyer.h" #include "sidetone.h" +#include "morse.h" extern const uint LED_PIN; extern const uint LEFT_PADDLE_PIN; @@ -47,9 +48,11 @@ void Keyer::setSpeed(uint8_t wpm) m_elementDuration = calcElementDurationUs(wpm); } -void Keyer::sendMessage(const std::string &msg) +void Keyer::sendMessage(std::string &msg) { - for (auto c : msg) { + std::string morse = messageToMorse(msg); + printf("Morse (%i): %s\n", morse.length(), morse.c_str()); + for (unsigned char c : morse) { m_messageQueue.push(c); } } @@ -77,12 +80,14 @@ void Keyer::run() m_messageChar = m_messageQueue.front(); m_messageQueue.pop(); + printf("Char is: %c\n", m_messageChar); + switch (m_messageChar) { case '.': m_messageKeyingState = MessageState::Dit; break; case '-': - m_messageKeyingState = MessageState::Dit; + m_messageKeyingState = MessageState::Dah; break; case 'i': m_messageKeyingState = MessageState::IntraCharSpace; diff --git a/src/keyer.h b/src/keyer.h index 333a94a..05cef60 100644 --- a/src/keyer.h +++ b/src/keyer.h @@ -14,7 +14,7 @@ class Keyer final void setMode(Mode mode) { m_mode = mode; } void setSpeed(uint8_t wpm); - void sendMessage(const std::string &msg); + void sendMessage(std::string &msg); void run(); void stop(); @@ -53,7 +53,7 @@ class Keyer final std::queue m_messageQueue; MessageState m_messageKeyingState {MessageState::Wait}; - char m_messageChar; + char m_messageChar{}; uint64_t m_elementDuration {0}; bool m_currentlyKeying {false}; diff --git a/src/morse.cpp b/src/morse.cpp index 57aafb0..42f79bb 100644 --- a/src/morse.cpp +++ b/src/morse.cpp @@ -20,41 +20,49 @@ std::map morseCode = { {'a', "-.-.-"}, // a == }; -std::string refurbishMessage(const std::string &msg) +void refurbishMessage(std::string &msg) { - std::string msgRefurb; - msgRefurb.resize(msg.length()); + printf("The message is: %s\n", msg.c_str()); + //std::string msgRefurb; + //msgRefurb.resize(msg.size()); // Make the message all upper case - std::transform(msg.cbegin(), msg.cend(), msgRefurb.begin(), [](unsigned char c) { return std::toupper(c); }); + //std::transform(msg.begin(), msg.end(), msgRefurb.begin(), [](unsigned char c) { return std::toupper(c); }); + std::transform(msg.begin(), msg.end(), msg.begin(), ::toupper); + // Encode the special characters as we like it - msgRefurb = std::regex_replace(msgRefurb, std::regex(""), "="); - msgRefurb = std::regex_replace(msgRefurb, std::regex(""), "k"); - msgRefurb = std::regex_replace(msgRefurb, std::regex(""), "s"); - msgRefurb = std::regex_replace(msgRefurb, std::regex(""), "+"); - msgRefurb = std::regex_replace(msgRefurb, std::regex(""), "a"); + //msg = std::regex_replace(msg, std::regex(""), "="); + //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"); + + //printf("Msg Origgg: %s\n", msg.c_str()); // Remove all other unknown characters - msgRefurb.erase(remove_if(msgRefurb.begin(), msgRefurb.end(), - [](const char &c) { return c != ' ' && morseCode.find(c) == morseCode.end(); }), - msgRefurb.end()); + //msg.erase(remove_if(msg.begin(), msg.end(), + // [](const char &c) { return c != ' ' && morseCode.find(c) == morseCode.end(); }), + // msg.end()); // Remove spaces, if there are too many of them - msgRefurb = std::regex_replace(msgRefurb, std::regex("(\\s+)"), " "); + //msg = std::regex_replace(msg, std::regex("(\\s+)"), " "); - return msgRefurb; + //printf("Msg Origgg2: %s\n", msg.c_str()); + //return msgRefurb; } -std::string messageToMorse(const std::string &msg) +std::string messageToMorse(std::string &msg) { - std::string refMsg = refurbishMessage(msg); - std::string morseString; + refurbishMessage(msg); + + std::string morseString = ""; - for (unsigned int i = 0; i < refMsg.length(); i++) { - auto c = refMsg[i]; + printf("Ref Mesg (%i): %s\n", msg.length(), msg.c_str()); + + for (unsigned int i = 0; i < msg.length(); i++) { + char c = msg[i]; if (c == ' ') { - // morseString.append(" "); morseString += 'w'; continue; } @@ -62,30 +70,31 @@ std::string messageToMorse(const std::string &msg) // Ignore and continue with next char, if not found auto search = morseCode.find(c); if (search == morseCode.end()) { + printf("Nanu (i=%i)? %c\n", i, c); continue; } for (unsigned int j = 0; j < morseCode[c].length(); j++) { - auto m = morseCode[c][j]; - if (j == 0 && i > 0 && refMsg[i - 1] != ' ') { - // morseString.append(" "); + char m = morseCode[c][j]; printf("M = %c\n", m); + if (j == 0 && i > 0 && msg[i - 1] != ' ') { morseString += 'c'; } morseString += m; if (j < morseCode[c].length() - 1) { - // morseString.append(" "); morseString += 'i'; } } } // Append word space if last char was not a blank - if (refMsg.back() != ' ') { - // morseString.append(" "); + if (msg.back() != ' ') { morseString += 'w'; } + printf("Sodele: %s\n", morseString.c_str()); + printf("Ref Mesg 2 (%i): %s\n", msg.length(), msg.c_str()); + return morseString; } \ No newline at end of file diff --git a/src/morse.h b/src/morse.h index a7f0ecd..4594f6b 100644 --- a/src/morse.h +++ b/src/morse.h @@ -2,4 +2,4 @@ #include -std::string messageToMorse(const std::string &msg); \ No newline at end of file +std::string messageToMorse(std::string &msg); \ No newline at end of file diff --git a/src/raspi_keyer.cpp b/src/raspi_keyer.cpp index 2ee91e4..d9edd22 100644 --- a/src/raspi_keyer.cpp +++ b/src/raspi_keyer.cpp @@ -94,6 +94,7 @@ void core1_main() break; case KeyerQueueCommand::SendMessage: keyer.sendMessage(data.message); + data.cmd = KeyerQueueCommand::Run; break; case KeyerQueueCommand::Wait: break; @@ -203,10 +204,12 @@ int main() lastWpm = currentWpm; } - busy_wait_ms(10000); + busy_wait_ms(5000); if (!used) { - KeyerQueueData keyerQueueData {KeyerQueueCommand::SendMessage, 0, settings.mode, "cq cq de dg2smb dg2smb pse k"}; + //KeyerQueueData keyerQueueData {KeyerQueueCommand::SendMessage, 0, settings.mode, "cq cq de dg2smb dg2smb pse k"}; + KeyerQueueData keyerQueueData {KeyerQueueCommand::SendMessage, 0, settings.mode, "cq cqde dg2smbk"}; queue_add_blocking(&keyerQueue, &keyerQueueData); + used = true; } }