From a33c46b29ba819501be81251ba6dfa5fcccfe6ab Mon Sep 17 00:00:00 2001 From: Scott Bezek Date: Mon, 25 Oct 2021 21:44:37 -0700 Subject: [PATCH] Add button support --- firmware/platformio.ini | 2 +- firmware/src/interface_task.cpp | 122 +++++++++++++++++++------------- firmware/src/interface_task.h | 8 ++- firmware/src/main.cpp | 3 - 4 files changed, 80 insertions(+), 55 deletions(-) diff --git a/firmware/platformio.ini b/firmware/platformio.ini index 46e3108..8b29000 100644 --- a/firmware/platformio.ini +++ b/firmware/platformio.ini @@ -41,4 +41,4 @@ build_flags = -DTFT_BL=4 -DLOAD_GLCD=1 -DLOAD_GFXFF=1 - -DSPI_FREQUENCY=50000000 + -DSPI_FREQUENCY=40000000 diff --git a/firmware/src/interface_task.cpp b/firmware/src/interface_task.cpp index 53648f2..a734c99 100644 --- a/firmware/src/interface_task.cpp +++ b/firmware/src/interface_task.cpp @@ -1,70 +1,92 @@ +#include #include "interface_task.h" +using namespace ace_button; + #define COUNT_OF(A) (sizeof(A) / sizeof(A[0])) +static KnobConfig configs[] = { + { + .num_positions = 0, + .position = 0, + .position_width_radians = 10 * PI / 180, + .detent_strength_unit = 0, + }, + { + .num_positions = 11, + .position = 0, + .position_width_radians = 10 * PI / 180, + .detent_strength_unit = 0, + }, + { + .num_positions = 2, + .position = 0, + .position_width_radians = 60 * PI / 180, + .detent_strength_unit = 1, + }, + { + .num_positions = 256, + .position = 127, + .position_width_radians = 1 * PI / 180, + .detent_strength_unit = 0, + }, + { + .num_positions = 256, + .position = 127, + .position_width_radians = 1 * PI / 180, + .detent_strength_unit = 1, + }, + { + .num_positions = 32, + .position = 0, + .position_width_radians = 8.225806452 * PI / 180, + .detent_strength_unit = 1, + }, + { + .num_positions = 32, + .position = 0, + .position_width_radians = 8.225806452 * PI / 180, + .detent_strength_unit = 0.1, + }, +}; + InterfaceTask::InterfaceTask(const uint8_t task_core, MotorTask& motor_task) : Task{"Interface", 8192, 1, task_core}, motor_task_(motor_task) { } InterfaceTask::~InterfaceTask() {} void InterfaceTask::run() { - KnobConfig configs[] = { - { - .num_positions = 0, - .position = 0, - .position_width_radians = 10 * PI / 180, - .detent_strength_unit = 0, - }, - { - .num_positions = 11, - .position = 0, - .position_width_radians = 10 * PI / 180, - .detent_strength_unit = 0, - }, - { - .num_positions = 2, - .position = 0, - .position_width_radians = 60 * PI / 180, - .detent_strength_unit = 1, - }, - { - .num_positions = 256, - .position = 127, - .position_width_radians = 1 * PI / 180, - .detent_strength_unit = 0, - }, - { - .num_positions = 256, - .position = 127, - .position_width_radians = 1 * PI / 180, - .detent_strength_unit = 1, - }, - { - .num_positions = 32, - .position = 0, - .position_width_radians = 8.225806452 * PI / 180, - .detent_strength_unit = 1, - }, - { - .num_positions = 32, - .position = 0, - .position_width_radians = 8.225806452 * PI / 180, - .detent_strength_unit = 0.1, - }, - }; + AceButton button(36); + pinMode(36, INPUT); + button.getButtonConfig()->setIEventHandler(this); - int current_config = 0; - - motor_task_.setConfig(configs[current_config]); + motor_task_.setConfig(configs[0]); while (1) { + button.check(); if (Serial.available()) { int v = Serial.read(); if (v == ' ') { - current_config = (current_config + 1) % COUNT_OF(configs); - Serial.printf("Chaning config to %d\n", current_config); - motor_task_.setConfig(configs[current_config]); + nextConfig(); } } + // Serial.println(digitalRead(36)); delay(10); } } + +void InterfaceTask::handleEvent(AceButton* button, uint8_t event_type, uint8_t button_state) { + Serial.println("EVENT!"); + switch (event_type) { + case AceButton::kEventPressed: + nextConfig(); + break; + case AceButton::kEventReleased: + break; + } +} + +void InterfaceTask::nextConfig() { + current_config_ = (current_config_ + 1) % COUNT_OF(configs); + Serial.printf("Changing config to %d\n", current_config_); + motor_task_.setConfig(configs[current_config_]); +} diff --git a/firmware/src/interface_task.h b/firmware/src/interface_task.h index cc3e3aa..5173fe7 100644 --- a/firmware/src/interface_task.h +++ b/firmware/src/interface_task.h @@ -1,20 +1,26 @@ #pragma once +#include #include #include "motor_task.h" #include "task.h" -class InterfaceTask : public Task { +class InterfaceTask : public Task, public ace_button::IEventHandler { friend class Task; // Allow base Task to invoke protected run() public: InterfaceTask(const uint8_t task_core, MotorTask& motor_task); ~InterfaceTask(); + void handleEvent(ace_button::AceButton* button, uint8_t event_type, uint8_t button_state) override; + protected: void run(); private: MotorTask& motor_task_; + int current_config_ = 0; + + void nextConfig(); }; diff --git a/firmware/src/main.cpp b/firmware/src/main.cpp index 3f08602..1d4ecf6 100644 --- a/firmware/src/main.cpp +++ b/firmware/src/main.cpp @@ -1,4 +1,3 @@ -#include #include #include #include @@ -9,8 +8,6 @@ #include "motor_task.h" #include "tlv_sensor.h" -using namespace ace_button; - DisplayTask display_task = DisplayTask(1); MotorTask motor_task = MotorTask(0, display_task); InterfaceTask interface_task = InterfaceTask(1, motor_task);