first attempt implementing iambic b

This commit is contained in:
Martin Brodbeck 2024-02-13 09:00:39 +01:00
parent 8a50645cfe
commit 7e26ea2c06
3 changed files with 33 additions and 8 deletions

View file

@ -9,7 +9,6 @@ extern const uint LED_PIN;
extern const uint LEFT_PADDLE_PIN; extern const uint LEFT_PADDLE_PIN;
extern const uint RIGHT_PADDLE_PIN; extern const uint RIGHT_PADDLE_PIN;
const uint SIDETONE_FREQ = 622; const uint SIDETONE_FREQ = 622;
bool left_paddle_pressed() bool left_paddle_pressed()
@ -36,7 +35,7 @@ uint64_t calcElementDurationUs(uint8_t wpm)
return duration; return duration;
} }
Keyer::Keyer(uint8_t wpm) : m_wpm(wpm) Keyer::Keyer(uint8_t wpm, Mode mode) : m_wpm(wpm), m_mode(mode)
{ {
m_elementDuration = calcElementDurationUs(m_wpm); m_elementDuration = calcElementDurationUs(m_wpm);
} }
@ -50,9 +49,12 @@ void Keyer::run()
case State::Wait: case State::Wait:
if (left_paddle_pressed() && right_paddle_pressed()) if (left_paddle_pressed() && right_paddle_pressed())
{ {
if (m_lastSymbolWas == Symbol::Dit) { if (m_lastSymbolWas == Symbol::Dit)
{
state = State::Dah; state = State::Dah;
} else if (m_lastSymbolWas == Symbol::Dah) { }
else if (m_lastSymbolWas == Symbol::Dah)
{
state = State::Dit; state = State::Dit;
} }
break; break;
@ -77,6 +79,9 @@ void Keyer::run()
} }
else else
{ {
if (right_paddle_pressed() && !m_keyNextIambicB)
m_keyNextIambicB = true;
if (absolute_time_diff_us(timestamp, m_keying_until) <= 0) if (absolute_time_diff_us(timestamp, m_keying_until) <= 0)
{ {
m_currentlyKeying = false; m_currentlyKeying = false;
@ -98,6 +103,9 @@ void Keyer::run()
} }
else else
{ {
if (left_paddle_pressed() && !m_keyNextIambicB)
m_keyNextIambicB = true;
if (absolute_time_diff_us(timestamp, m_keying_until) <= 0) if (absolute_time_diff_us(timestamp, m_keying_until) <= 0)
{ {
m_currentlyKeying = false; m_currentlyKeying = false;
@ -109,14 +117,27 @@ void Keyer::run()
} }
break; break;
case State::InterCharSpace: case State::InterCharSpace:
if (m_previousState != State::InterCharSpace) if (!m_currentlyPausing)
{ {
m_pausing_until = make_timeout_time_us(m_elementDuration); m_pausing_until = make_timeout_time_us(m_elementDuration);
m_previousState = State::InterCharSpace; m_currentlyPausing = true;
} }
if (absolute_time_diff_us(timestamp, m_pausing_until) <= 0) if (absolute_time_diff_us(timestamp, m_pausing_until) <= 0)
{ {
m_currentlyPausing = false;
if (m_mode == Mode::IAMBIC_B && m_keyNextIambicB)
{
if (m_previousState == State::Dit)
state = State::Dah;
else
state = State::Dit;
m_keyNextIambicB = false;
break;
}
state = State::Wait; state = State::Wait;
} }

View file

@ -2,6 +2,7 @@
#define KEYER_H #define KEYER_H
#include "sidetone.h" #include "sidetone.h"
#include "settings.h"
extern const uint BUZZER_PIN; extern const uint BUZZER_PIN;
@ -16,7 +17,7 @@ public:
InterCharSpace, InterCharSpace,
// TODO: perhaps new state: Keying // TODO: perhaps new state: Keying
}; };
Keyer(uint8_t wpm); Keyer(uint8_t wpm, Mode mode);
void run(); void run();
@ -30,12 +31,15 @@ private:
State state{State::Wait}; State state{State::Wait};
State m_previousState{State::Wait}; State m_previousState{State::Wait};
uint8_t m_wpm{18}; uint8_t m_wpm{18};
Mode m_mode{Mode::IAMBIC_B};
uint64_t m_elementDuration{0}; uint64_t m_elementDuration{0};
bool m_currentlyKeying{false}; bool m_currentlyKeying{false};
bool m_currentlyPausing{false};
absolute_time_t m_keying_until{0}; absolute_time_t m_keying_until{0};
absolute_time_t m_pausing_until{0}; absolute_time_t m_pausing_until{0};
Symbol m_lastSymbolWas{Symbol::Dit}; Symbol m_lastSymbolWas{Symbol::Dit};
Sidetone m_Sidetone{BUZZER_PIN}; Sidetone m_Sidetone{BUZZER_PIN};
bool m_keyNextIambicB{false};
}; };
#endif #endif

View file

@ -47,7 +47,7 @@ int main()
// printf("Element duration (u_sec): %" PRIu64 "\n", element_duration_us(settings.wpm)); // printf("Element duration (u_sec): %" PRIu64 "\n", element_duration_us(settings.wpm));
printf("\n"); printf("\n");
Keyer keyer(25); Keyer keyer(settings.wpm, settings.mode);
while (true) while (true)
{ {