diff --git a/src/keyer.cpp b/src/keyer.cpp index b4389dc..95477de 100644 --- a/src/keyer.cpp +++ b/src/keyer.cpp @@ -8,7 +8,7 @@ extern const uint LEFT_PADDLE_PIN; bool left_paddle_pressed() { - if (gpio_get(LEFT_PADDLE_PIN)) + if (!gpio_get(LEFT_PADDLE_PIN)) { return true; } @@ -25,7 +25,20 @@ void Keyer::run() break; case State::LeftPaddlePressed: printf("."); - state = State::Wait; + + if (left_paddle_pressed()) + state = State::InterCharSpace; + else + state = State::Wait; + break; + + case State::InterCharSpace: + printf("(.)"); + + if (left_paddle_pressed()) + state = State::LeftPaddlePressed; + else + state = State::Wait; break; default: diff --git a/src/keyer.h b/src/keyer.h index 98b7c7f..43b7c2c 100644 --- a/src/keyer.h +++ b/src/keyer.h @@ -8,6 +8,7 @@ public: { Wait, LeftPaddlePressed, + InterCharSpace, }; @@ -15,6 +16,13 @@ public: private: State state{State::Wait}; + uint8_t numElements{0}; }; +inline uint64_t element_duration_us(uint8_t wpm) +{ + uint64_t duration = static_cast(1.2 / static_cast(wpm) * 1000 * 1000); + return duration; +} + #endif \ No newline at end of file diff --git a/src/pico_keyer.cpp b/src/pico_keyer.cpp index d29cf40..3f0a4cb 100644 --- a/src/pico_keyer.cpp +++ b/src/pico_keyer.cpp @@ -1,4 +1,5 @@ #include +#include #include "pico/stdlib.h" @@ -23,10 +24,10 @@ void setup() // Setup pins for left and right paddles gpio_init(LEFT_PADDLE_PIN); gpio_set_dir(LEFT_PADDLE_PIN, GPIO_IN); - gpio_pull_down(LEFT_PADDLE_PIN); + gpio_pull_up(LEFT_PADDLE_PIN); gpio_init(RIGHT_PADDLE_PIN); gpio_set_dir(RIGHT_PADDLE_PIN, GPIO_IN); - gpio_pull_down(RIGHT_PADDLE_PIN); + gpio_pull_up(RIGHT_PADDLE_PIN); } int main() @@ -39,14 +40,15 @@ int main() Settings settings{read_settings()}; - printf("Iambic mode (loaded): %d\n", static_cast(settings.mode)); + printf("\nIambic mode (loaded): %d\n", static_cast(settings.mode)); printf("WPM (loaded): %d\n", settings.wpm); + printf("Element duration (u_sec): %" PRIu64 "\n", element_duration_us(settings.wpm)); Keyer keyer; while (true) { - sleep_ms(1.2 * settings.wpm); + sleep_us(element_duration_us(settings.wpm)); keyer.run(); }