diff --git a/src/keyer.cpp b/src/keyer.cpp index 2a334cc..a804134 100644 --- a/src/keyer.cpp +++ b/src/keyer.cpp @@ -44,9 +44,21 @@ void Keyer::run() switch (state) { case State::Wait: - if (left_paddle_pressed()) { + if (left_paddle_pressed() && right_paddle_pressed()) + { + if (m_lastSymbolWas == Symbol::Dit) { + state = State::Dah; + } else if (m_lastSymbolWas == Symbol::Dah) { + state = State::Dit; + } + break; + } + if (left_paddle_pressed()) + { state = State::Dit; - } else if (right_paddle_pressed()) { + } + else if (right_paddle_pressed()) + { state = State::Dah; } break; @@ -56,6 +68,7 @@ void Keyer::run() m_currentlyKeying = true; m_keying_until = make_timeout_time_us(m_elementDuration); gpio_put(LED_PIN, 1); + m_lastSymbolWas = Symbol::Dit; } else { @@ -69,11 +82,15 @@ void Keyer::run() } break; case State::Dah: - if(!m_currentlyKeying) { + if (!m_currentlyKeying) + { m_currentlyKeying = true; m_keying_until = make_timeout_time_us(m_elementDuration * 3); gpio_put(LED_PIN, 1); - } else { + m_lastSymbolWas = Symbol::Dah; + } + else + { if (absolute_time_diff_us(timestamp, m_keying_until) <= 0) { m_currentlyKeying = false; @@ -81,7 +98,6 @@ void Keyer::run() m_previousState = State::Dah; state = State::InterCharSpace; } - } break; case State::InterCharSpace: @@ -91,7 +107,8 @@ void Keyer::run() m_previousState = State::InterCharSpace; } - if(absolute_time_diff_us(timestamp, m_pausing_until) <= 0) { + if (absolute_time_diff_us(timestamp, m_pausing_until) <= 0) + { state = State::Wait; } diff --git a/src/keyer.h b/src/keyer.h index b0d67cf..1dbce9f 100644 --- a/src/keyer.h +++ b/src/keyer.h @@ -17,15 +17,20 @@ public: void run(); private: - Keyer() {}; + enum class Symbol + { + Dit, + Dah + }; + Keyer(){}; State state{State::Wait}; State m_previousState{State::Wait}; uint8_t m_wpm{18}; uint64_t m_elementDuration{0}; - bool m_currentlyKeying {false}; + bool m_currentlyKeying{false}; absolute_time_t m_keying_until{0}; absolute_time_t m_pausing_until{0}; + Symbol m_lastSymbolWas{Symbol::Dit}; }; - #endif \ No newline at end of file diff --git a/src/pico_keyer.cpp b/src/pico_keyer.cpp index 38db9f7..c11cb22 100644 --- a/src/pico_keyer.cpp +++ b/src/pico_keyer.cpp @@ -45,7 +45,7 @@ int main() // printf("Element duration (u_sec): %" PRIu64 "\n", element_duration_us(settings.wpm)); printf("\n"); - Keyer keyer(settings.wpm); + Keyer keyer(15); while (true) { diff --git a/src/settings.h b/src/settings.h index 2b36485..b49d273 100644 --- a/src/settings.h +++ b/src/settings.h @@ -16,7 +16,7 @@ struct Settings { uint16_t magic_number{MAGIC_NUMBER}; // Bytes: 2 Mode mode{Mode::IAMBIC_B}; // Bytes: 1 - uint8_t wpm{20}; // Bytes: 1 + uint8_t wpm{18}; // Bytes: 1 uint8_t dummy[FLASH_PAGE_SIZE - 4]{0}; // Sum : 4 };