[WIP] state machine revised
This commit is contained in:
parent
86cd97b721
commit
375c328dca
2 changed files with 79 additions and 59 deletions
123
src/keyer.cpp
123
src/keyer.cpp
|
@ -50,40 +50,27 @@ void Keyer::run()
|
||||||
{
|
{
|
||||||
auto timestamp = get_absolute_time();
|
auto timestamp = get_absolute_time();
|
||||||
|
|
||||||
switch (state)
|
switch (m_state)
|
||||||
{
|
{
|
||||||
case State::Wait:
|
case State::Wait:
|
||||||
if (left_paddle_pressed() && right_paddle_pressed())
|
if (left_paddle_pressed())
|
||||||
{
|
{
|
||||||
m_keyNextIambicB = false;
|
m_keyNextIambicB = false;
|
||||||
|
m_state = State::Dit;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
else if (right_paddle_pressed())
|
else if (right_paddle_pressed())
|
||||||
{
|
{
|
||||||
m_keyNextIambicB = false;
|
m_keyNextIambicB = false;
|
||||||
state = State::Dah;
|
m_state = State::Dah;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (m_mode == Mode::IAMBIC_B && m_keyNextIambicB)
|
if (m_mode == Mode::IAMBIC_B && m_keyNextIambicB)
|
||||||
{
|
{
|
||||||
if (m_previousState == State::Dit)
|
if (m_previousState == State::Dit)
|
||||||
state = State::Dah;
|
m_state = State::Dah;
|
||||||
else
|
else
|
||||||
state = State::Dit;
|
m_state = State::Dit;
|
||||||
|
|
||||||
m_keyNextIambicB = false;
|
m_keyNextIambicB = false;
|
||||||
}
|
}
|
||||||
|
@ -93,14 +80,19 @@ void Keyer::run()
|
||||||
if (!m_currentlyKeying)
|
if (!m_currentlyKeying)
|
||||||
{
|
{
|
||||||
m_currentlyKeying = true;
|
m_currentlyKeying = true;
|
||||||
|
m_bothPaddlesPressed = (left_paddle_pressed() && right_paddle_pressed()) ? true : false;
|
||||||
m_keying_until = make_timeout_time_us(m_elementDuration);
|
m_keying_until = make_timeout_time_us(m_elementDuration);
|
||||||
gpio_put(LED_PIN, 1);
|
gpio_put(LED_PIN, 1);
|
||||||
m_Sidetone.on(SIDETONE_FREQ);
|
m_Sidetone.on(SIDETONE_FREQ);
|
||||||
}
|
}
|
||||||
else
|
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_keyNextIambicB = true;
|
||||||
|
m_bothPaddlesPressed = false;
|
||||||
|
}
|
||||||
|
|
||||||
if (absolute_time_diff_us(timestamp, m_keying_until) <= 0)
|
if (absolute_time_diff_us(timestamp, m_keying_until) <= 0)
|
||||||
{
|
{
|
||||||
|
@ -108,7 +100,7 @@ void Keyer::run()
|
||||||
gpio_put(LED_PIN, 0);
|
gpio_put(LED_PIN, 0);
|
||||||
m_Sidetone.off();
|
m_Sidetone.off();
|
||||||
m_previousState = State::Dit;
|
m_previousState = State::Dit;
|
||||||
state = State::InterCharSpace;
|
m_state = State::DitPause;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
@ -116,14 +108,19 @@ void Keyer::run()
|
||||||
if (!m_currentlyKeying)
|
if (!m_currentlyKeying)
|
||||||
{
|
{
|
||||||
m_currentlyKeying = true;
|
m_currentlyKeying = true;
|
||||||
|
m_bothPaddlesPressed = (left_paddle_pressed() && right_paddle_pressed()) ? true : false;
|
||||||
m_keying_until = make_timeout_time_us(m_elementDuration * 3);
|
m_keying_until = make_timeout_time_us(m_elementDuration * 3);
|
||||||
gpio_put(LED_PIN, 1);
|
gpio_put(LED_PIN, 1);
|
||||||
m_Sidetone.on(SIDETONE_FREQ);
|
m_Sidetone.on(SIDETONE_FREQ);
|
||||||
}
|
}
|
||||||
else
|
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_keyNextIambicB = true;
|
||||||
|
m_bothPaddlesPressed = false;
|
||||||
|
}
|
||||||
|
|
||||||
if (absolute_time_diff_us(timestamp, m_keying_until) <= 0)
|
if (absolute_time_diff_us(timestamp, m_keying_until) <= 0)
|
||||||
{
|
{
|
||||||
|
@ -131,49 +128,67 @@ void Keyer::run()
|
||||||
gpio_put(LED_PIN, 0);
|
gpio_put(LED_PIN, 0);
|
||||||
m_Sidetone.off();
|
m_Sidetone.off();
|
||||||
m_previousState = State::Dah;
|
m_previousState = State::Dah;
|
||||||
state = State::InterCharSpace;
|
m_state = State::DahPause;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case State::InterCharSpace:
|
case State::DitPause:
|
||||||
if (!m_currentlyPausing)
|
if (!m_currentlyPausing)
|
||||||
{
|
{
|
||||||
m_pausing_until = make_timeout_time_us(m_elementDuration);
|
m_pausing_until = make_timeout_time_us(m_elementDuration);
|
||||||
m_currentlyPausing = true;
|
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
|
else
|
||||||
{
|
{
|
||||||
|
|
||||||
if (absolute_time_diff_us(timestamp, m_pausing_until) <= 0)
|
if (absolute_time_diff_us(timestamp, m_pausing_until) <= 0)
|
||||||
{
|
{
|
||||||
m_currentlyPausing = false;
|
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;
|
break;
|
||||||
case State::Abort:
|
case State::Abort:
|
||||||
gpio_put(LED_PIN, 0);
|
gpio_put(LED_PIN, 0);
|
||||||
|
@ -181,9 +196,9 @@ void Keyer::run()
|
||||||
m_keyNextIambicB = false;
|
m_keyNextIambicB = false;
|
||||||
m_currentlyPausing = false;
|
m_currentlyPausing = false;
|
||||||
m_currentlyKeying = false;
|
m_currentlyKeying = false;
|
||||||
|
m_bothPaddlesPressed = false;
|
||||||
m_previousState = State::Abort;
|
m_previousState = State::Abort;
|
||||||
m_nextState = State::Wait;
|
m_state = State::Wait;
|
||||||
state = State::Wait;
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
|
@ -192,5 +207,5 @@ void Keyer::run()
|
||||||
|
|
||||||
void Keyer::stop()
|
void Keyer::stop()
|
||||||
{
|
{
|
||||||
state = State::Abort;
|
m_state = State::Abort;
|
||||||
}
|
}
|
19
src/keyer.h
19
src/keyer.h
|
@ -24,21 +24,26 @@ private:
|
||||||
Wait,
|
Wait,
|
||||||
Dit,
|
Dit,
|
||||||
Dah,
|
Dah,
|
||||||
InterCharSpace,
|
DitPause,
|
||||||
|
DahPause,
|
||||||
Abort,
|
Abort,
|
||||||
};
|
};
|
||||||
State state{State::Wait};
|
|
||||||
State m_previousState{State::Wait};
|
|
||||||
State m_nextState{State::Wait};
|
|
||||||
uint8_t m_wpm{18};
|
uint8_t m_wpm{18};
|
||||||
Mode m_mode{Mode::IAMBIC_B};
|
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};
|
uint64_t m_elementDuration{0};
|
||||||
bool m_currentlyKeying{false};
|
bool m_currentlyKeying{false};
|
||||||
bool m_currentlyPausing{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_keyNextIambicB{false};
|
||||||
|
bool m_bothPaddlesPressed{false};
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
Loading…
Reference in a new issue