Compare commits
2 Commits
49127d52b2
...
fffd9c6b5d
| Author | SHA1 | Date | |
|---|---|---|---|
| fffd9c6b5d | |||
| bbfc082095 |
79
src/input/encoder.cpp
Normal file
79
src/input/encoder.cpp
Normal 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::setLabelText("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::setLabelText("Frequency: " + String(frequency) + "Hz");
|
||||||
|
}
|
||||||
|
}
|
||||||
8
src/input/encoder.hpp
Normal file
8
src/input/encoder.hpp
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
#ifndef DRUMZ_INPUT_ENCODER_HPP
|
||||||
|
#define DRUMZ_INPUT_ENCODER_HPP
|
||||||
|
|
||||||
|
namespace Encoder {
|
||||||
|
void init();
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif //DRUMZ_INPUT_ENCODER_HPP
|
||||||
69
src/main.cpp
69
src/main.cpp
@ -1,33 +1,25 @@
|
|||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include <Versatile_RotaryEncoder.h>
|
|
||||||
|
|
||||||
#include <ui/display.hpp>
|
#include <ui/display.hpp>
|
||||||
|
#include <input/encoder.hpp>
|
||||||
|
|
||||||
#define SCREEN_WIDTH 320
|
#define SCREEN_WIDTH 320
|
||||||
#define SCREEN_HEIGHT 240
|
#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 ONBOARD_LED_PIN = 2;
|
||||||
const int ANALOG_INPUT_PIN = 34;
|
const int ANALOG_INPUT_PIN = 34;
|
||||||
const int ANALOG_OUTPUT_PIN = 25;
|
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 handleToneGeneration(void *pvParameters);
|
||||||
void handleInputControls(void *pvParameters);
|
|
||||||
void handle_pad_inputs(void *pvParameters);
|
void handle_pad_inputs(void *pvParameters);
|
||||||
|
|
||||||
bool toneOn = false;
|
bool toneOn = false;
|
||||||
int frequency = 220; // A4
|
int frequency = 220; // A4
|
||||||
|
|
||||||
|
|
||||||
Versatile_RotaryEncoder *encoder;
|
|
||||||
void handlePress();
|
|
||||||
void handleRelease();
|
|
||||||
void handleRotate(int8_t rotation);
|
|
||||||
|
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
@ -35,23 +27,15 @@ void setup()
|
|||||||
pinMode(ANALOG_INPUT_PIN, INPUT);
|
pinMode(ANALOG_INPUT_PIN, INPUT);
|
||||||
pinMode(ANALOG_OUTPUT_PIN, OUTPUT);
|
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(handleToneGeneration, "handleToneGeneration", 2048, NULL, 1, NULL);
|
||||||
xTaskCreate(handleInputControls, "handleInputContols", 2048, NULL, 1, NULL);
|
|
||||||
xTaskCreate(handle_pad_inputs, "handle_pad_inputs", 2048, NULL, 1, NULL);
|
xTaskCreate(handle_pad_inputs, "handle_pad_inputs", 2048, NULL, 1, NULL);
|
||||||
|
|
||||||
|
Encoder::init();
|
||||||
Display::init();
|
Display::init();
|
||||||
|
|
||||||
Serial.println("setup complete");
|
Serial.println("setup complete");
|
||||||
Display::set_label_text("Boot complete.");
|
Display::setLabelText("Boot complete.");
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
@ -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");
|
|
||||||
}
|
|
||||||
|
|||||||
@ -12,25 +12,25 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define DRAW_BUF_SIZE (SCREEN_WIDTH * SCREEN_HEIGHT / 10 * (LV_COLOR_DEPTH / 8))
|
#define DRAW_BUF_SIZE (SCREEN_WIDTH * SCREEN_HEIGHT / 10 * (LV_COLOR_DEPTH / 8))
|
||||||
uint32_t draw_buf[DRAW_BUF_SIZE / 4];
|
|
||||||
|
|
||||||
uint32_t get_time();
|
|
||||||
void handle_lvgl_logs(lv_log_level_t level, const char *buf);
|
|
||||||
void handle_update_values(lv_timer_t *timer);
|
|
||||||
|
|
||||||
String label_text;
|
|
||||||
lv_obj_t *label;
|
|
||||||
lv_timer_t *update_values_timer;
|
|
||||||
|
|
||||||
namespace Display
|
namespace Display
|
||||||
{
|
{
|
||||||
|
uint32_t draw_buf[DRAW_BUF_SIZE / 4];
|
||||||
|
|
||||||
|
uint32_t getTime();
|
||||||
|
void handleLvglLogs(lv_log_level_t level, const char *buf);
|
||||||
|
void handleUpdateValues(lv_timer_t *timer);
|
||||||
|
|
||||||
|
String label_text;
|
||||||
|
lv_obj_t *label;
|
||||||
|
lv_timer_t *update_values_timer;
|
||||||
|
|
||||||
void init()
|
void init()
|
||||||
{
|
{
|
||||||
lv_init();
|
lv_init();
|
||||||
lv_log_register_print_cb(handle_lvgl_logs);
|
lv_log_register_print_cb(handleLvglLogs);
|
||||||
lv_tick_set_cb(get_time);
|
lv_tick_set_cb(getTime);
|
||||||
|
|
||||||
update_values_timer = lv_timer_create(handle_update_values, 5, NULL);
|
update_values_timer = lv_timer_create(handleUpdateValues, 5, NULL);
|
||||||
lv_timer_ready(update_values_timer);
|
lv_timer_ready(update_values_timer);
|
||||||
|
|
||||||
lv_display_t *display;
|
lv_display_t *display;
|
||||||
@ -46,26 +46,26 @@ namespace Display
|
|||||||
lv_timer_handler();
|
lv_timer_handler();
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_label_text(String text)
|
void setLabelText(String text)
|
||||||
{
|
{
|
||||||
label_text = text;
|
label_text = text;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void handle_lvgl_logs(lv_log_level_t level, const char *buf)
|
void handleLvglLogs(lv_log_level_t level, const char *buf)
|
||||||
{
|
|
||||||
Serial.printf("LVGL: %s\r\n", buf);
|
|
||||||
}
|
|
||||||
|
|
||||||
uint32_t get_time()
|
|
||||||
{
|
|
||||||
return esp_timer_get_time() / 1000;
|
|
||||||
}
|
|
||||||
|
|
||||||
void handle_update_values(lv_timer_t *timer)
|
|
||||||
{
|
|
||||||
if (label != NULL && lv_label_get_text(label) != label_text.c_str())
|
|
||||||
{
|
{
|
||||||
lv_label_set_text(label, label_text.c_str());
|
Serial.printf("LVGL: %s\r\n", buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t getTime()
|
||||||
|
{
|
||||||
|
return esp_timer_get_time() / 1000;
|
||||||
|
}
|
||||||
|
|
||||||
|
void handleUpdateValues(lv_timer_t *timer)
|
||||||
|
{
|
||||||
|
if (label != NULL && lv_label_get_text(label) != label_text.c_str())
|
||||||
|
{
|
||||||
|
lv_label_set_text(label, label_text.c_str());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -4,7 +4,7 @@
|
|||||||
namespace Display {
|
namespace Display {
|
||||||
void init();
|
void init();
|
||||||
void update();
|
void update();
|
||||||
void set_label_text(String text);
|
void setLabelText(String text);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // DRUMZ_UI_DISPLAY_HPP
|
#endif // DRUMZ_UI_DISPLAY_HPP
|
||||||
Loading…
Reference in New Issue
Block a user