diff --git a/src/keyer.cpp b/src/keyer.cpp index 23e859a..276b06b 100644 --- a/src/keyer.cpp +++ b/src/keyer.cpp @@ -1,10 +1,11 @@ +#include #include #include "pico/stdlib.h" #include "keyer.h" -#include "sidetone.h" #include "morse.h" +#include "sidetone.h" extern const uint LED_PIN; extern const uint LEFT_PADDLE_PIN; @@ -48,22 +49,32 @@ void Keyer::setSpeed(uint8_t wpm) m_elementDuration = calcElementDurationUs(wpm); } -void Keyer::sendMessage(std::string msg) +//void Keyer::sendMessage(std::string msg) +//{ +// std::string morse = messageToMorse(msg); +// +// for (char c : morse) { +// m_messageQueue.push(c); +// } +//} + +void Keyer::sendCharacter(const char ch) { - std::string morse = messageToMorse(msg); + char morseSymbols[32] {0}; - for (char c : morse) { - m_messageQueue.push(c); + if (!charToMorse(ch, morseSymbols)) { + return; } -} -void Keyer::sendCharacter(char ch) -{ - std::string morse = charToMorse(ch); - - for (char c : morse) { - m_messageQueue.push(c); + for (unsigned int i = 0; i < strlen(morseSymbols); i++) { + m_messageQueue.push(morseSymbols[i]); } + + // std::string morse = charToMorse(ch); + + // for (char c : morse) { + // m_messageQueue.push(c); + // } } void Keyer::run() @@ -174,7 +185,8 @@ void Keyer::run() break; case MessageState::InterWordSpace: if (!m_currentlyPausing) { - m_pausing_until = make_timeout_time_us(m_elementDuration * (7-3)); // 7-3, because we aleady have the InterCharSpace of 3 + m_pausing_until = make_timeout_time_us(m_elementDuration * + (7 - 3)); // 7-3, because we aleady have the InterCharSpace of 3 m_currentlyPausing = true; } if (left_paddle_pressed() || right_paddle_pressed()) { @@ -185,8 +197,7 @@ void Keyer::run() m_messageKeyingState = MessageState::Wait; } break; - case MessageState::Abort: - { + case MessageState::Abort: { gpio_put(LED_PIN, 0); gpio_put(CW_OUT_PIN, 0); m_buzzer.off(); diff --git a/src/keyer.h b/src/keyer.h index b6c378b..7e64816 100644 --- a/src/keyer.h +++ b/src/keyer.h @@ -1,7 +1,7 @@ #pragma once #include -#include +//#include #include "settings.h" #include "sidetone.h" @@ -14,8 +14,8 @@ class Keyer final void setMode(Mode mode) { m_mode = mode; } void setSpeed(uint8_t wpm); - void sendMessage(std::string msg); - void sendCharacter(char ch); + //void sendMessage(std::string msg); + void sendCharacter(const char ch); void run(); void stop(); diff --git a/src/morse.cpp b/src/morse.cpp index e5a46ef..5a529ee 100644 --- a/src/morse.cpp +++ b/src/morse.cpp @@ -1,6 +1,7 @@ -//#include +// #include #include -//#include +#include +// #include #include "morse.h" @@ -92,7 +93,7 @@ void refurbishMessage(std::string &msg) std::string messageToMorse(std::string &msg) { - //refurbishMessage(msg); + // refurbishMessage(msg); std::string morseString {}; @@ -131,7 +132,7 @@ std::string messageToMorse(std::string &msg) return morseString; } -std::string charToMorse(char ch) +std::string charToMorse(const char ch) { std::string morseString {}; @@ -153,4 +154,33 @@ std::string charToMorse(char ch) morseString.push_back('c'); return morseString; +} + +bool charToMorse(const char ch, char *morseSymbols) +{ + size_t index {0}; + + if (ch != ' ' && !morseMap.contains(ch)) { + return false; + } + + if (ch == ' ') { + morseSymbols[index++] = 'w'; + } else { + for (unsigned int i = 0; i < morseMap[ch].length(); i++) { + char m = morseMap[ch][i]; + + morseSymbols[index++] = m; + + if (i < morseMap[ch].length() - 1) { + morseSymbols[index++] = 'i'; + } + } + + morseSymbols[index++] = 'c'; + } + + morseSymbols[index] = '\0'; + + return true; } \ No newline at end of file diff --git a/src/morse.h b/src/morse.h index bdb10cd..d58fe88 100644 --- a/src/morse.h +++ b/src/morse.h @@ -1,6 +1,7 @@ #pragma once -#include +//#include -std::string messageToMorse(std::string &msg); -std::string charToMorse(char ch); \ No newline at end of file +//std::string messageToMorse(std::string &msg); +//std::string charToMorse(const char ch); +bool charToMorse(const char ch, char * morseSymbols); \ No newline at end of file