refactor encoder. no need for Dir enum.
This commit is contained in:
@ -16,28 +16,21 @@
|
||||
#include "button.h"
|
||||
#include "peripherials.h"
|
||||
|
||||
enum Direction {
|
||||
DIRECTION_UNCHANGED,
|
||||
DIRECTION_INCREMENT,
|
||||
DIRECTION_DECREMENT,
|
||||
};
|
||||
|
||||
class EncoderDir {
|
||||
class Encoder {
|
||||
protected:
|
||||
typedef void (*CallbackFunction)(void);
|
||||
typedef void (*RotateCallbackFunction)(Direction dir, int val);
|
||||
typedef void (*RotateCallbackFunction)(int val);
|
||||
CallbackFunction on_press;
|
||||
RotateCallbackFunction on_press_rotate;
|
||||
RotateCallbackFunction on_rotate;
|
||||
int change;
|
||||
Direction dir;
|
||||
|
||||
public:
|
||||
EncoderDir() : encoder_(ENCODER_PIN1, ENCODER_PIN2, RotaryEncoder::LatchMode::FOUR3),
|
||||
Encoder() : encoder_(ENCODER_PIN1, ENCODER_PIN2, RotaryEncoder::LatchMode::FOUR3),
|
||||
button_(ENCODER_SW_PIN) {
|
||||
_instance = this;
|
||||
}
|
||||
~EncoderDir() {}
|
||||
~Encoder() {}
|
||||
|
||||
// Set to true if the encoder read direction should be reversed.
|
||||
void SetReverseDirection(bool reversed) {
|
||||
@ -55,12 +48,6 @@ class EncoderDir {
|
||||
on_press_rotate = f;
|
||||
}
|
||||
|
||||
// Parse EncoderButton increment direction.
|
||||
Direction RotateDirection() {
|
||||
int dir = (int)(encoder_.getDirection());
|
||||
return rotate_(dir, reversed_);
|
||||
}
|
||||
|
||||
void Process() {
|
||||
// Get encoder position change amount.
|
||||
int encoder_rotated = _rotate_change() != 0;
|
||||
@ -70,9 +57,9 @@ class EncoderDir {
|
||||
// Handle encoder position change and button press.
|
||||
if (button_pressed && encoder_rotated) {
|
||||
rotated_while_held_ = true;
|
||||
if (on_press_rotate != NULL) on_press_rotate(dir, change);
|
||||
if (on_press_rotate != NULL) on_press_rotate(change);
|
||||
} else if (!button_pressed && encoder_rotated) {
|
||||
if (on_rotate != NULL) on_rotate(dir, change);
|
||||
if (on_rotate != NULL) on_rotate(change);
|
||||
} else if (button_.Change() == Button::CHANGE_RELEASED && !rotated_while_held_) {
|
||||
if (on_press != NULL) on_press();
|
||||
}
|
||||
@ -91,7 +78,7 @@ class EncoderDir {
|
||||
}
|
||||
|
||||
private:
|
||||
static EncoderDir* _instance;
|
||||
static Encoder* _instance;
|
||||
|
||||
int previous_pos_;
|
||||
bool rotated_while_held_;
|
||||
@ -112,7 +99,6 @@ class EncoderDir {
|
||||
// Update state variables.
|
||||
change = position - previous_pos_;
|
||||
previous_pos_ = position;
|
||||
dir = RotateDirection();
|
||||
|
||||
// Encoder rotate acceleration.
|
||||
if (ms < 16) {
|
||||
@ -126,17 +112,6 @@ class EncoderDir {
|
||||
}
|
||||
return change;
|
||||
}
|
||||
|
||||
inline Direction rotate_(int dir, bool reversed) {
|
||||
switch (dir) {
|
||||
case 1:
|
||||
return (reversed) ? DIRECTION_DECREMENT : DIRECTION_INCREMENT;
|
||||
case -1:
|
||||
return (reversed) ? DIRECTION_INCREMENT : DIRECTION_DECREMENT;
|
||||
default:
|
||||
return DIRECTION_UNCHANGED;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
@ -11,8 +11,8 @@ struct AppState {
|
||||
bool encoder_reversed = false;
|
||||
bool refresh_screen = true;
|
||||
bool editing_param = false;
|
||||
int selected_param = 0;
|
||||
int selected_sub_param = 0;
|
||||
byte selected_param = 0;
|
||||
byte selected_sub_param = 0;
|
||||
byte selected_channel = 0; // 0=tempo, 1-6=output channel
|
||||
byte selected_shuffle = 0;
|
||||
Clock::Source selected_source = Clock::SOURCE_INTERNAL;
|
||||
|
||||
@ -13,7 +13,7 @@
|
||||
|
||||
// Initialize the static pointer for the EncoderDir class to null. We want to
|
||||
// have a static pointer to decouple the ISR from the global gravity object.
|
||||
EncoderDir* EncoderDir::_instance = nullptr;
|
||||
Encoder* Encoder::_instance = nullptr;
|
||||
|
||||
void Gravity::Init() {
|
||||
initClock();
|
||||
@ -74,11 +74,11 @@ void Gravity::Process() {
|
||||
|
||||
// Pin Change Interrupt on Port D (D4).
|
||||
ISR(PCINT2_vect) {
|
||||
EncoderDir::isr();
|
||||
Encoder::isr();
|
||||
};
|
||||
// Pin Change Interrupt on Port C (D17/A3).
|
||||
ISR(PCINT1_vect) {
|
||||
EncoderDir::isr();
|
||||
Encoder::isr();
|
||||
};
|
||||
|
||||
// Global instance
|
||||
|
||||
@ -8,7 +8,7 @@
|
||||
#include "button.h"
|
||||
#include "clock.h"
|
||||
#include "digital_output.h"
|
||||
#include "encoder_dir.h"
|
||||
#include "encoder.h"
|
||||
#include "peripherials.h"
|
||||
|
||||
// Hardware abstraction wrapper for the Gravity module.
|
||||
@ -32,7 +32,7 @@ class Gravity {
|
||||
U8G2_SSD1306_128X64_NONAME_1_HW_I2C display; // OLED display object.
|
||||
Clock clock; // Clock source wrapper.
|
||||
DigitalOutput outputs[OUTPUT_COUNT]; // An array containing each Output object.
|
||||
EncoderDir encoder; // Rotary encoder with button instance
|
||||
Encoder encoder; // Rotary encoder with button instance
|
||||
Button shift_button;
|
||||
Button play_button;
|
||||
AnalogInput cv1;
|
||||
|
||||
Reference in New Issue
Block a user