From 98183b14ec5921a7dd672dcd90c3b5dd257af37b Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Wed, 6 Mar 2024 12:29:06 +0100 Subject: [PATCH] Toggle keying modes --- src/raspikeyer.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/raspikeyer.cpp b/src/raspikeyer.cpp index 95e1ab9..5fc3d6f 100644 --- a/src/raspikeyer.cpp +++ b/src/raspikeyer.cpp @@ -1,6 +1,8 @@ #include #include +#include + #include "bsp/board.h" #include "hardware/adc.h" #include "pico/binary_info/code.h" @@ -26,6 +28,7 @@ extern constexpr uint BUZZER_PIN = 18; constexpr uint DISPLAY_DIO_PIN = 20; constexpr uint DISPLAY_CLK_PIN = 21; extern constexpr uint ADC_PIN = 26; +constexpr uint BUTTON_KEYER_MODE = 2; queue_t keyerQueue; @@ -47,6 +50,11 @@ void setup() gpio_set_dir(CW_OUT_PIN, GPIO_OUT); gpio_put(CW_OUT_PIN, 0); + // Setup Button Keyer Mode + gpio_init(BUTTON_KEYER_MODE); + gpio_set_dir(BUTTON_KEYER_MODE, GPIO_IN); + gpio_pull_down(BUTTON_KEYER_MODE); + // Setup ADC adc_init(); gpio_init(ADC_PIN); @@ -131,7 +139,12 @@ int main() display.displayIambicMode(settings.mode); display.displaySpeed(currentWpm); + auto timestamp = get_absolute_time(); + auto ignoreButtonKeyerModeUntil = timestamp; + uint count = static_cast::type>(settings.mode); // TODO: Simplify with C++23 + while (true) { + timestamp = get_absolute_time(); currentWpm = calcWPM(potiRead(), settings.wpmPotiMin, settings.wpmPotiMax); // If WPM in settings is set to 0 -> take speed from poti @@ -143,6 +156,33 @@ int main() display.displaySpeed(currentWpm); } + // If Button for keying mode is pressed, toggle through the possible values (Iambic A, Iambic B, Straight) + if (absolute_time_diff_us(timestamp, ignoreButtonKeyerModeUntil) <= 0 && gpio_get(BUTTON_KEYER_MODE)) { + ++count; + count = count % 3; + + switch (count) { + case 0: + settings.mode = Mode::IambicA; + break; + case 1: + settings.mode = Mode::IambicB; + break; + case 2: + settings.mode = Mode::Straight; + break; + default: + break; + } + + KeyerQueueData keyerQueueData {KeyerQueueCommand::Config, currentWpm, settings.mode, 0}; + queue_add_blocking(&keyerQueue, &keyerQueueData); + + display.displayIambicMode(settings.mode); + + ignoreButtonKeyerModeUntil = make_timeout_time_us(500'000); + } + tud_task(); // Internal PICO purposes winKeyer.run(keyerQueue);