From bbfc0820951f6144308553a47d80a6c927ae782b Mon Sep 17 00:00:00 2001 From: Fritz Heiden Date: Sat, 24 May 2025 15:23:58 +0200 Subject: [PATCH] refactor: put encoder input logic into dedicated file --- src/input/encoder.cpp | 79 +++++++++++++++++++++++++++++++++++++++++++ src/input/encoder.hpp | 8 +++++ src/main.cpp | 67 ++++-------------------------------- 3 files changed, 93 insertions(+), 61 deletions(-) create mode 100644 src/input/encoder.cpp create mode 100644 src/input/encoder.hpp diff --git a/src/input/encoder.cpp b/src/input/encoder.cpp new file mode 100644 index 0000000..31a25bf --- /dev/null +++ b/src/input/encoder.cpp @@ -0,0 +1,79 @@ +#include +#include + +#include + +#ifndef ENCODER_A_PIN +#define ENCODER_A_PIN 12 +#endif + +#ifndef ENCODER_B_PIN +#define ENCODER_B_PIN 13 +#endif + +#ifndef ENTER_BUTTON_PIN +#define ENTER_BUTTON_PIN 14 +#endif + +namespace Encoder +{ + void pollEncoder(void *pvParameters); + void handlePress(); + void handleRelease(); + void handleRotate(int8_t rotation); + + Versatile_RotaryEncoder *encoder; + int frequency = 220; // A4 + + void init() + { + pinMode(ENTER_BUTTON_PIN, INPUT); + pinMode(ENCODER_A_PIN, INPUT); + pinMode(ENCODER_B_PIN, INPUT); + + encoder = new Versatile_RotaryEncoder(ENCODER_A_PIN, ENCODER_B_PIN, ENTER_BUTTON_PIN); + encoder->setHandlePress(handlePress); + encoder->setHandlePressRelease(handleRelease); + encoder->setHandleRotate(handleRotate); + + xTaskCreate(pollEncoder, "pollEncoder", 2048, NULL, 1, NULL); + } + + void pollEncoder(void *pvParameters) + { + while (true) + { + encoder->ReadEncoder(); + delay(1); + } + } + + void handlePress() + { + Serial.println("Encoder pressed"); + Display::set_label_text("Button pressed."); + } + + void handleRelease() + { + Serial.println("Encoder released"); + } + + void handleRotate(int8_t rotation) + { + Serial.print("Encoder rotated by "); + Serial.println(rotation); + if (rotation > 0) + { + frequency += 10; + } + else if (rotation < 0) + { + frequency -= 10; + } + Serial.print("Frequency: "); + Serial.println(frequency); + + Display::set_label_text("Frequency: " + String(frequency) + "Hz"); + } +} diff --git a/src/input/encoder.hpp b/src/input/encoder.hpp new file mode 100644 index 0000000..c352bcd --- /dev/null +++ b/src/input/encoder.hpp @@ -0,0 +1,8 @@ +#ifndef DRUMZ_INPUT_ENCODER_HPP +#define DRUMZ_INPUT_ENCODER_HPP + +namespace Encoder { + void init(); +} + +#endif //DRUMZ_INPUT_ENCODER_HPP \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index d32c1e0..dba9b54 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,33 +1,25 @@ #include -#include #include +#include #define SCREEN_WIDTH 320 #define SCREEN_HEIGHT 240 +#define ENCODER_BUTTON_PIN = 14; +#define ENCODER_A_PIN = 12; +#define ENCODER_B_PIN = 13; + const int ONBOARD_LED_PIN = 2; const int ANALOG_INPUT_PIN = 34; const int ANALOG_OUTPUT_PIN = 25; -const int ENTER_BUTTON_PIN = 14; -const int ENCODER_A_PIN = 12; -const int ENCODER_B_PIN = 13; - void handleToneGeneration(void *pvParameters); -void handleInputControls(void *pvParameters); void handle_pad_inputs(void *pvParameters); bool toneOn = false; int frequency = 220; // A4 - -Versatile_RotaryEncoder *encoder; -void handlePress(); -void handleRelease(); -void handleRotate(int8_t rotation); - - void setup() { Serial.begin(115200); @@ -35,19 +27,11 @@ void setup() pinMode(ANALOG_INPUT_PIN, INPUT); pinMode(ANALOG_OUTPUT_PIN, OUTPUT); - pinMode(ENTER_BUTTON_PIN, INPUT); - pinMode(ENCODER_A_PIN, INPUT); - pinMode(ENCODER_B_PIN, INPUT); - - encoder = new Versatile_RotaryEncoder(ENCODER_A_PIN, ENCODER_B_PIN, ENTER_BUTTON_PIN); - encoder->setHandlePress(handlePress); - encoder->setHandlePressRelease(handleRelease); - encoder->setHandleRotate(handleRotate); xTaskCreate(handleToneGeneration, "handleToneGeneration", 2048, NULL, 1, NULL); - xTaskCreate(handleInputControls, "handleInputContols", 2048, NULL, 1, NULL); xTaskCreate(handle_pad_inputs, "handle_pad_inputs", 2048, NULL, 1, NULL); + Encoder::init(); Display::init(); Serial.println("setup complete"); @@ -96,42 +80,3 @@ void handleToneGeneration(void *pvParameters) } } -void handleInputControls(void *pvParameters) -{ - while (true) - { - encoder->ReadEncoder(); - delay(1); - } -} - -void handlePress() -{ - Serial.println("Encoder pressed"); - digitalWrite(ONBOARD_LED_PIN, HIGH); - Display::set_label_text("Button pressed."); -} - -void handleRelease() -{ - Serial.println("Encoder released"); - digitalWrite(ONBOARD_LED_PIN, LOW); -} - -void handleRotate(int8_t rotation) -{ - Serial.print("Encoder rotated by "); - Serial.println(rotation); - if (rotation > 0) - { - frequency += 10; - } - else if (rotation < 0) - { - frequency -= 10; - } - Serial.print("Frequency: "); - Serial.println(frequency); - - Display::set_label_text("Frequency: " + String(frequency) + "Hz"); -}