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