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 "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();

View file

@ -1,7 +1,7 @@
#pragma once
#include <queue>
#include <string>
//#include <string>
#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();

View file

@ -1,5 +1,6 @@
// #include <algorithm>
#include <map>
#include <string>
// #include <regex>
#include "morse.h"
@ -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 {};
@ -154,3 +155,32 @@ std::string charToMorse(char ch)
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
#include <string>
//#include <string>
std::string messageToMorse(std::string &msg);
std::string charToMorse(char ch);
//std::string messageToMorse(std::string &msg);
//std::string charToMorse(const char ch);
bool charToMorse(const char ch, char * morseSymbols);