Add button support

This commit is contained in:
Scott Bezek
2021-10-25 21:44:37 -07:00
parent 0b5785ecf3
commit a33c46b29b
4 changed files with 80 additions and 55 deletions

View File

@@ -41,4 +41,4 @@ build_flags =
-DTFT_BL=4
-DLOAD_GLCD=1
-DLOAD_GFXFF=1
-DSPI_FREQUENCY=50000000
-DSPI_FREQUENCY=40000000

View File

@@ -1,70 +1,92 @@
#include <AceButton.h>
#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_]);
}

View File

@@ -1,20 +1,26 @@
#pragma once
#include <AceButton.h>
#include <Arduino.h>
#include "motor_task.h"
#include "task.h"
class InterfaceTask : public Task<InterfaceTask> {
class InterfaceTask : public Task<InterfaceTask>, public ace_button::IEventHandler {
friend class Task<InterfaceTask>; // 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();
};

View File

@@ -1,4 +1,3 @@
#include <AceButton.h>
#include <Arduino.h>
#include <FastLED.h>
#include <SimpleFOC.h>
@@ -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);