get rid of some string stuff

This commit is contained in:
Martin Brodbeck 2024-02-28 14:29:00 +01:00
parent 751e4dfef7
commit 7614544e55
4 changed files with 67 additions and 25 deletions

View file

@ -1,10 +1,11 @@
#include <cstring>
#include <stdio.h> #include <stdio.h>
#include "pico/stdlib.h" #include "pico/stdlib.h"
#include "keyer.h" #include "keyer.h"
#include "sidetone.h"
#include "morse.h" #include "morse.h"
#include "sidetone.h"
extern const uint LED_PIN; extern const uint LED_PIN;
extern const uint LEFT_PADDLE_PIN; extern const uint LEFT_PADDLE_PIN;
@ -48,22 +49,32 @@ void Keyer::setSpeed(uint8_t wpm)
m_elementDuration = calcElementDurationUs(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) { if (!charToMorse(ch, morseSymbols)) {
m_messageQueue.push(c); return;
} }
}
void Keyer::sendCharacter(char ch) 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);
} }
// std::string morse = charToMorse(ch);
// for (char c : morse) {
// m_messageQueue.push(c);
// }
} }
void Keyer::run() void Keyer::run()
@ -174,7 +185,8 @@ void Keyer::run()
break; break;
case MessageState::InterWordSpace: case MessageState::InterWordSpace:
if (!m_currentlyPausing) { 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; m_currentlyPausing = true;
} }
if (left_paddle_pressed() || right_paddle_pressed()) { if (left_paddle_pressed() || right_paddle_pressed()) {
@ -185,8 +197,7 @@ void Keyer::run()
m_messageKeyingState = MessageState::Wait; m_messageKeyingState = MessageState::Wait;
} }
break; break;
case MessageState::Abort: case MessageState::Abort: {
{
gpio_put(LED_PIN, 0); gpio_put(LED_PIN, 0);
gpio_put(CW_OUT_PIN, 0); gpio_put(CW_OUT_PIN, 0);
m_buzzer.off(); m_buzzer.off();

View file

@ -1,7 +1,7 @@
#pragma once #pragma once
#include <queue> #include <queue>
#include <string> //#include <string>
#include "settings.h" #include "settings.h"
#include "sidetone.h" #include "sidetone.h"
@ -14,8 +14,8 @@ class Keyer final
void setMode(Mode mode) { m_mode = mode; } void setMode(Mode mode) { m_mode = mode; }
void setSpeed(uint8_t wpm); void setSpeed(uint8_t wpm);
void sendMessage(std::string msg); //void sendMessage(std::string msg);
void sendCharacter(char ch); void sendCharacter(const char ch);
void run(); void run();
void stop(); void stop();

View file

@ -1,6 +1,7 @@
//#include <algorithm> // #include <algorithm>
#include <map> #include <map>
//#include <regex> #include <string>
// #include <regex>
#include "morse.h" #include "morse.h"
@ -92,7 +93,7 @@ void refurbishMessage(std::string &msg)
std::string messageToMorse(std::string &msg) std::string messageToMorse(std::string &msg)
{ {
//refurbishMessage(msg); // refurbishMessage(msg);
std::string morseString {}; std::string morseString {};
@ -131,7 +132,7 @@ std::string messageToMorse(std::string &msg)
return morseString; return morseString;
} }
std::string charToMorse(char ch) std::string charToMorse(const char ch)
{ {
std::string morseString {}; std::string morseString {};
@ -153,4 +154,33 @@ std::string charToMorse(char ch)
morseString.push_back('c'); morseString.push_back('c');
return morseString; 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;
} }

View file

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <string> //#include <string>
std::string messageToMorse(std::string &msg); //std::string messageToMorse(std::string &msg);
std::string charToMorse(char ch); //std::string charToMorse(const char ch);
bool charToMorse(const char ch, char * morseSymbols);