[WIP] state machine revised

This commit is contained in:
Martin Brodbeck 2024-02-16 09:46:46 +01:00
parent 86cd97b721
commit 375c328dca
2 changed files with 79 additions and 59 deletions

View file

@ -50,40 +50,27 @@ void Keyer::run()
{
auto timestamp = get_absolute_time();
switch (state)
switch (m_state)
{
case State::Wait:
if (left_paddle_pressed() && right_paddle_pressed())
if (left_paddle_pressed())
{
m_keyNextIambicB = false;
if (m_previousState == State::Dit)
{
state = State::Dah;
}
else if (m_previousState == State::Dah)
{
state = State::Dit;
}
}
else if (left_paddle_pressed())
{
m_keyNextIambicB = false;
state = State::Dit;
m_state = State::Dit;
}
else if (right_paddle_pressed())
{
m_keyNextIambicB = false;
state = State::Dah;
m_state = State::Dah;
}
else
{
if (m_mode == Mode::IAMBIC_B && m_keyNextIambicB)
{
if (m_previousState == State::Dit)
state = State::Dah;
m_state = State::Dah;
else
state = State::Dit;
m_state = State::Dit;
m_keyNextIambicB = false;
}
@ -93,14 +80,19 @@ void Keyer::run()
if (!m_currentlyKeying)
{
m_currentlyKeying = true;
m_bothPaddlesPressed = (left_paddle_pressed() && right_paddle_pressed()) ? true : false;
m_keying_until = make_timeout_time_us(m_elementDuration);
gpio_put(LED_PIN, 1);
m_Sidetone.on(SIDETONE_FREQ);
}
else
{
if (right_paddle_pressed() && !m_keyNextIambicB)
// If both paddles are released during this dit -> note for Iambic B
if (m_bothPaddlesPressed && !right_paddle_pressed() && !left_paddle_pressed() && !m_keyNextIambicB)
{
m_keyNextIambicB = true;
m_bothPaddlesPressed = false;
}
if (absolute_time_diff_us(timestamp, m_keying_until) <= 0)
{
@ -108,7 +100,7 @@ void Keyer::run()
gpio_put(LED_PIN, 0);
m_Sidetone.off();
m_previousState = State::Dit;
state = State::InterCharSpace;
m_state = State::DitPause;
}
}
break;
@ -116,14 +108,19 @@ void Keyer::run()
if (!m_currentlyKeying)
{
m_currentlyKeying = true;
m_bothPaddlesPressed = (left_paddle_pressed() && right_paddle_pressed()) ? true : false;
m_keying_until = make_timeout_time_us(m_elementDuration * 3);
gpio_put(LED_PIN, 1);
m_Sidetone.on(SIDETONE_FREQ);
}
else
{
if (left_paddle_pressed() && !m_keyNextIambicB)
// If both paddles are released during this dah -> note for Iambic B
if (m_bothPaddlesPressed && !right_paddle_pressed() && !left_paddle_pressed() && !m_keyNextIambicB)
{
m_keyNextIambicB = true;
m_bothPaddlesPressed = false;
}
if (absolute_time_diff_us(timestamp, m_keying_until) <= 0)
{
@ -131,49 +128,67 @@ void Keyer::run()
gpio_put(LED_PIN, 0);
m_Sidetone.off();
m_previousState = State::Dah;
state = State::InterCharSpace;
m_state = State::DahPause;
}
}
break;
case State::InterCharSpace:
case State::DitPause:
if (!m_currentlyPausing)
{
m_pausing_until = make_timeout_time_us(m_elementDuration);
m_currentlyPausing = true;
}
// Handle the case when the other paddle is pressed during the inter character space
else if (absolute_time_diff_us(timestamp, m_pausing_until) > 0)
{
if (left_paddle_pressed() && m_previousState == State::Dah && m_nextState == State::Wait)
{
// printf("--- Pause --- Next state is Dit!\n");
m_nextState = State::Dit;
m_keyNextIambicB = right_paddle_pressed() ? true : false;
}
else if (right_paddle_pressed() && m_previousState == State::Dit && m_nextState == State::Wait)
{
// printf("--- Pause --- Next state is Dah!\n");
m_nextState = State::Dah;
m_keyNextIambicB = left_paddle_pressed() ? true : false;
}
else if (m_previousState == State::Wait)
{
m_nextState = State::Wait;
}
}
else
{
if (absolute_time_diff_us(timestamp, m_pausing_until) <= 0)
{
m_currentlyPausing = false;
state = m_nextState;
m_nextState = State::Wait;
if (right_paddle_pressed())
{
m_state = State::Dah;
m_keyNextIambicB = false;
}
else if (left_paddle_pressed())
{
m_state = State::Dit;
m_keyNextIambicB = false;
}
else
{
m_state = State::Wait;
}
}
}
break;
case State::DahPause:
if (!m_currentlyPausing)
{
m_pausing_until = make_timeout_time_us(m_elementDuration);
m_currentlyPausing = true;
}
else
{
if (absolute_time_diff_us(timestamp, m_pausing_until) <= 0)
{
m_currentlyPausing = false;
if (left_paddle_pressed())
{
m_state = State::Dit;
m_keyNextIambicB = false;
}
else if (right_paddle_pressed())
{
m_state = State::Dah;
m_keyNextIambicB = false;
}
else
{
m_state = State::Wait;
}
}
}
break;
case State::Abort:
gpio_put(LED_PIN, 0);
@ -181,9 +196,9 @@ void Keyer::run()
m_keyNextIambicB = false;
m_currentlyPausing = false;
m_currentlyKeying = false;
m_bothPaddlesPressed = false;
m_previousState = State::Abort;
m_nextState = State::Wait;
state = State::Wait;
m_state = State::Wait;
break;
default:
break;
@ -192,5 +207,5 @@ void Keyer::run()
void Keyer::stop()
{
state = State::Abort;
m_state = State::Abort;
}

View file

@ -24,21 +24,26 @@ private:
Wait,
Dit,
Dah,
InterCharSpace,
DitPause,
DahPause,
Abort,
};
State state{State::Wait};
State m_previousState{State::Wait};
State m_nextState{State::Wait};
uint8_t m_wpm{18};
Mode m_mode{Mode::IAMBIC_B};
absolute_time_t m_keying_until{0};
absolute_time_t m_pausing_until{0};
Sidetone m_Sidetone{BUZZER_PIN};
State m_state{State::Wait};
State m_previousState{State::Wait};
uint64_t m_elementDuration{0};
bool m_currentlyKeying{false};
bool m_currentlyPausing{false};
absolute_time_t m_keying_until{0};
absolute_time_t m_pausing_until{0};
Sidetone m_Sidetone{BUZZER_PIN};
bool m_keyNextIambicB{false};
bool m_bothPaddlesPressed{false};
};
#endif