refactor: use event callbacks for encoder inputs
This commit is contained in:
parent
fc4aa72d5f
commit
3b1ab0ee5e
@ -1,5 +1,9 @@
|
||||
#include <Arduino.h>
|
||||
|
||||
#include <input/encoder.hpp>
|
||||
#include <ui/display.hpp>
|
||||
#include <audio/tone_generator.hpp>
|
||||
|
||||
#ifndef ANALAOG_OUTPUT_PIN
|
||||
#define ANALOG_OUTPUT_PIN 3
|
||||
#endif
|
||||
@ -7,6 +11,7 @@
|
||||
namespace ToneGenerator
|
||||
{
|
||||
void handleToneGeneration(void *pvParameters);
|
||||
void handleEncoderRotate(int rotation);
|
||||
|
||||
bool tone_on = false;
|
||||
int frequency = 220; // A4
|
||||
@ -14,6 +19,9 @@ namespace ToneGenerator
|
||||
void init()
|
||||
{
|
||||
pinMode(ANALOG_OUTPUT_PIN, OUTPUT);
|
||||
|
||||
Encoder::onRotate(handleEncoderRotate);
|
||||
|
||||
xTaskCreate(handleToneGeneration, "handleToneGeneration", 2048, NULL, 1, NULL);
|
||||
}
|
||||
|
||||
@ -31,6 +39,23 @@ namespace ToneGenerator
|
||||
delay(10);
|
||||
}
|
||||
}
|
||||
|
||||
void handleEncoderRotate(int rotation) {
|
||||
int frequency = getFrequency();
|
||||
if (rotation > 0)
|
||||
{
|
||||
frequency += 10;
|
||||
}
|
||||
else if (rotation < 0)
|
||||
{
|
||||
frequency -= 10;
|
||||
}
|
||||
setFrequency(frequency);
|
||||
Serial.print("Frequency: ");
|
||||
Serial.println(frequency);
|
||||
|
||||
Display::setLabelText("Frequency: " + String(frequency) + "Hz");
|
||||
}
|
||||
|
||||
void setToneOn(bool on)
|
||||
{
|
||||
|
||||
@ -1,8 +1,10 @@
|
||||
#include <vector>
|
||||
#include <Arduino.h>
|
||||
#include <Versatile_RotaryEncoder.h>
|
||||
|
||||
#include <ui/display.hpp>
|
||||
#include <audio/tone_generator.hpp>
|
||||
#include <input/encoder.hpp>
|
||||
|
||||
#ifndef ENCODER_A_PIN
|
||||
#define ENCODER_A_PIN 12
|
||||
@ -18,12 +20,17 @@
|
||||
|
||||
namespace Encoder
|
||||
{
|
||||
using std::vector;
|
||||
|
||||
void pollEncoder(void *pvParameters);
|
||||
void handlePress();
|
||||
void handleRelease();
|
||||
void handleRotate(int8_t rotation);
|
||||
|
||||
Versatile_RotaryEncoder *encoder;
|
||||
vector<OnButtonReleaseCallback> onButtonReleaseCallbacks;
|
||||
vector<OnButtonPressCallback> onButtonPressCallbacks;
|
||||
vector<OnRotateCallback> onRotateCallbacks;
|
||||
|
||||
void init()
|
||||
{
|
||||
@ -51,31 +58,37 @@ namespace Encoder
|
||||
void handlePress()
|
||||
{
|
||||
Serial.println("Encoder pressed");
|
||||
Display::setLabelText("Button pressed.");
|
||||
for (OnButtonPressCallback callback : onButtonPressCallbacks) {
|
||||
callback();
|
||||
}
|
||||
}
|
||||
|
||||
void handleRelease()
|
||||
{
|
||||
Serial.println("Encoder released");
|
||||
for (OnButtonReleaseCallback callback : onButtonReleaseCallbacks) {
|
||||
callback();
|
||||
}
|
||||
}
|
||||
|
||||
void handleRotate(int8_t rotation)
|
||||
{
|
||||
Serial.print("Encoder rotated by ");
|
||||
Serial.println(rotation);
|
||||
int frequency = ToneGenerator::getFrequency();
|
||||
if (rotation > 0)
|
||||
{
|
||||
frequency += 10;
|
||||
for (OnRotateCallback callback : onRotateCallbacks) {
|
||||
callback(rotation);
|
||||
}
|
||||
else if (rotation < 0)
|
||||
{
|
||||
frequency -= 10;
|
||||
}
|
||||
ToneGenerator::setFrequency(frequency);
|
||||
Serial.print("Frequency: ");
|
||||
Serial.println(frequency);
|
||||
|
||||
Display::setLabelText("Frequency: " + String(frequency) + "Hz");
|
||||
}
|
||||
|
||||
void onButtonRelease(OnButtonReleaseCallback callback) {
|
||||
onButtonReleaseCallbacks.push_back(callback);
|
||||
}
|
||||
|
||||
void onButtonPress(OnButtonPressCallback callback) {
|
||||
onButtonPressCallbacks.push_back(callback);
|
||||
}
|
||||
|
||||
void onRotate(OnRotateCallback callback) {
|
||||
onRotateCallbacks.push_back(callback);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,8 +1,15 @@
|
||||
#ifndef DRUMZ_INPUT_ENCODER_HPP
|
||||
#define DRUMZ_INPUT_ENCODER_HPP
|
||||
|
||||
namespace Encoder {
|
||||
void init();
|
||||
namespace Encoder
|
||||
{
|
||||
using OnButtonReleaseCallback = void (*)(void);
|
||||
using OnButtonPressCallback = void (*)(void);
|
||||
using OnRotateCallback = void (*)(int);
|
||||
void init();
|
||||
void onButtonRelease(OnButtonReleaseCallback callback);
|
||||
void onButtonPress(OnButtonPressCallback callback);
|
||||
void onRotate(OnRotateCallback callback);
|
||||
}
|
||||
|
||||
#endif //DRUMZ_INPUT_ENCODER_HPP
|
||||
#endif // DRUMZ_INPUT_ENCODER_HPP
|
||||
@ -25,6 +25,7 @@ void setup()
|
||||
PadReader::init();
|
||||
Encoder::init();
|
||||
Display::init();
|
||||
ToneGenerator::init();
|
||||
|
||||
Serial.println("setup complete");
|
||||
Display::setLabelText("Boot complete.");
|
||||
|
||||
@ -3,6 +3,8 @@
|
||||
#include <TFT_eSPI.h>
|
||||
#include <Arduino.h>
|
||||
|
||||
#include <input/encoder.hpp>
|
||||
|
||||
#ifndef SCREEN_WIDTH
|
||||
#define SCREEN_WIDTH 320
|
||||
#endif
|
||||
@ -19,6 +21,7 @@ namespace Display
|
||||
uint32_t getTime();
|
||||
void handleLvglLogs(lv_log_level_t level, const char *buf);
|
||||
void handleUpdateValues(lv_timer_t *timer);
|
||||
void handleEnterButton();
|
||||
|
||||
String label_text;
|
||||
lv_obj_t *label;
|
||||
@ -39,6 +42,8 @@ namespace Display
|
||||
|
||||
label = lv_label_create(lv_screen_active());
|
||||
lv_obj_align(label, LV_ALIGN_CENTER, 0, 0);
|
||||
|
||||
Encoder::onButtonRelease(handleEnterButton);
|
||||
}
|
||||
|
||||
void update()
|
||||
@ -68,4 +73,9 @@ namespace Display
|
||||
lv_label_set_text(label, label_text.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
void handleEnterButton()
|
||||
{
|
||||
setLabelText("Button pressed.");
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user