refactor: put encoder input logic into dedicated file

This commit is contained in:
Fritz Heiden 2025-05-24 15:23:58 +02:00
parent 49127d52b2
commit bbfc082095
3 changed files with 93 additions and 61 deletions

79
src/input/encoder.cpp Normal file
View File

@ -0,0 +1,79 @@
#include <Arduino.h>
#include <Versatile_RotaryEncoder.h>
#include <ui/display.hpp>
#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");
}
}

8
src/input/encoder.hpp Normal file
View File

@ -0,0 +1,8 @@
#ifndef DRUMZ_INPUT_ENCODER_HPP
#define DRUMZ_INPUT_ENCODER_HPP
namespace Encoder {
void init();
}
#endif //DRUMZ_INPUT_ENCODER_HPP

View File

@ -1,33 +1,25 @@
#include <Arduino.h>
#include <Versatile_RotaryEncoder.h>
#include <ui/display.hpp>
#include <input/encoder.hpp>
#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");
}