Update encoder to better handle press rotate and use button subclass.

This commit is contained in:
2025-05-23 17:12:31 -07:00
parent 9ed9e01966
commit efd569e4df

View File

@ -30,6 +30,7 @@ class EncoderDir {
RotateCallbackFunction on_press_rotate; RotateCallbackFunction on_press_rotate;
RotateCallbackFunction on_rotate; RotateCallbackFunction on_rotate;
int change; int change;
Direction dir;
public: public:
EncoderDir() : encoder_(ENCODER_PIN1, ENCODER_PIN2, RotaryEncoder::LatchMode::FOUR3), EncoderDir() : encoder_(ENCODER_PIN1, ENCODER_PIN2, RotaryEncoder::LatchMode::FOUR3),
@ -41,7 +42,7 @@ class EncoderDir {
reversed_ = reversed; reversed_ = reversed;
} }
void AttachPressHandler(CallbackFunction f) { void AttachPressHandler(CallbackFunction f) {
button_.AttachPressHandler(f); on_press = f;
} }
void AttachRotateHandler(RotateCallbackFunction f) { void AttachRotateHandler(RotateCallbackFunction f) {
@ -52,18 +53,6 @@ class EncoderDir {
on_press_rotate = f; on_press_rotate = f;
} }
void OnRotate() {
if (on_rotate != NULL) {
on_rotate(RotateDirection(), change);
}
}
void OnPressRotate() {
if (on_press_rotate != NULL) {
on_press_rotate(RotateDirection(), change);
}
}
// Parse EncoderButton increment direction. // Parse EncoderButton increment direction.
Direction RotateDirection() { Direction RotateDirection() {
int dir = (int)(encoder_.getDirection()); int dir = (int)(encoder_.getDirection());
@ -71,23 +60,24 @@ class EncoderDir {
} }
void Process() { void Process() {
// Get encoder position change amount.
int encoder_rotated = _rotate_change() != 0;
bool button_pressed = button_.On();
button_.Process(); button_.Process();
int change = _rotate_change();
// Handle encoder position change and button press. // Handle encoder position change and button press.
if (button_.On() && change != 0) { if (button_pressed && encoder_rotated) {
OnPressRotate(); rotated_while_held_ = true;
rotated_while_held = true; if (on_press_rotate != NULL) on_press_rotate(dir, change);
} else if (button_.Change() == Button::CHANGE_RELEASED && !rotated_while_held) { } else if (!button_pressed && encoder_rotated) {
button_.OnPress(); if (on_rotate != NULL) on_rotate(dir, change);
} else if (button_.Change() == Button::CHANGE_RELEASED && !rotated_while_held_) {
if (on_press != NULL) on_press();
} }
if (button_.Change() == Button::CHANGE_RELEASED) { // Reset rotate while held state.
rotated_while_held = false; if (button_.Change() == Button::CHANGE_RELEASED && rotated_while_held_) {
} rotated_while_held_ = false;
if (change != 0 && !button_.On()) {
OnRotate();
} }
} }
@ -98,10 +88,10 @@ class EncoderDir {
private: private:
int previous_pos_; int previous_pos_;
bool rotated_while_held_;
bool reversed_ = true; bool reversed_ = true;
RotaryEncoder encoder_; RotaryEncoder encoder_;
Button button_; Button button_;
bool rotated_while_held;
// Return the number of ticks change since last polled. // Return the number of ticks change since last polled.
int _rotate_change() { int _rotate_change() {
@ -116,6 +106,7 @@ class EncoderDir {
// Update state variables. // Update state variables.
change = position - previous_pos_; change = position - previous_pos_;
previous_pos_ = position; previous_pos_ = position;
dir = RotateDirection();
// Encoder rotate acceleration. // Encoder rotate acceleration.
if (ms < 16) { if (ms < 16) {