Add some logic to the encoder to only register a button press after a certain number of inputs.

This commit is contained in:
Anna Rose 2021-12-21 04:16:07 +00:00
parent 4c5b51f7b8
commit 145e383bc5
5 changed files with 20 additions and 7 deletions

View File

@ -103,13 +103,15 @@ bool PulsedButton::Update(Joystick* js) {
}
EncoderButton::EncoderButton(uint8_t pin1, uint8_t pin2, uint8_t vbutton) : Button(vbutton, NULL) {
EncoderButton::EncoderButton(uint8_t pin1, uint8_t pin2, uint8_t vbutton, int8_t tick_threshold) : Button(vbutton, NULL) {
this->type = ENCODER_PULSED_SPLIT;
this->vbutton2 = vbutton + 1;
this->encoder = new Encoder(pin1, pin2);
this->last_value = encoder->read();
this->release_time1 = 0;
this->release_time2 = 0;
this->ticks = 0;
this->tick_threshold = tick_threshold;
}
bool EncoderButton::Update(Joystick* js) {
@ -126,15 +128,24 @@ bool EncoderButton::Update(Joystick* js) {
bool changed = false;
long new_value = encoder->read();
if (new_value > last_value) {
ticks++;
} else if (new_value < last_value) {
ticks--;
}
if (ticks > tick_threshold) {
js->PressButton(vbutton);
changed = true;
release_time1 = millis() + 250;
ticks = 0;
}
else if (new_value < last_value) {
else if (ticks < tick_threshold * -1) {
js->PressButton(vbutton2);
changed = true;
release_time2 = millis() + 250;
ticks = 0;
}
last_value = new_value;
return changed;
}

View File

@ -63,7 +63,7 @@ class PulsedButton : public Button {
class EncoderButton : public Button {
public:
EncoderButton(uint8_t pin1, uint8_t pin2, uint8_t vbutton);
EncoderButton(uint8_t pin1, uint8_t pin2, uint8_t vbutton, int8_t tick_threshold);
bool Update(Joystick* js);
protected:
@ -72,6 +72,8 @@ class EncoderButton : public Button {
uint8_t vbutton2;
unsigned long release_time1;
unsigned long release_time2;
int8_t ticks;
int8_t tick_threshold;
};
#endif

View File

@ -53,12 +53,12 @@ void Joystick::AddMatrixButton(uint8_t row, uint8_t col, Matrix* matrix, ButtonT
_addButton(type, new MatrixReader(row, col, matrix, pullup));
}
void Joystick::AddEncoder(uint8_t pin1, uint8_t pin2, ButtonType type) {
void Joystick::AddEncoder(uint8_t pin1, uint8_t pin2, int8_t tick_threshold, ButtonType type) {
Button *button;
switch (type) {
case ENCODER_PULSED_SPLIT:
// add an encoder button. _BuildButton() doesn't do everything we need, however...
button = new EncoderButton(pin1, pin2, _virtual_buttons);
button = new EncoderButton(pin1, pin2, _virtual_buttons, tick_threshold);
_buttons[_num_buttons] = button;
_num_buttons++;
_virtual_buttons += 2;

View File

@ -49,7 +49,7 @@ class Joystick {
void AddMatrixButton(uint8_t row, uint8_t col, Matrix* matrix, ButtonType type, bool pullup=true);
// Add a rotary encoder. ENCODER button types allow you to treat an encoder as a momentary button or an axis (TODO)
void AddEncoder(uint8_t pin1, uint8_t pin2, ButtonType type);
void AddEncoder(uint8_t pin1, uint8_t pin2, int8_t tick_threshold, ButtonType type);
// Add an analog axis to the joystick. THIS METHOD IS NOT CURRENTLY TESTED OR SUPPORTED. It might work, but probably not.
void AddAxis(uint8_t pin);

View File

@ -48,7 +48,7 @@ void setup() {
js.Init();
Serial.println("Adding encoder.");
js.AddEncoder(ENCODER, ENCODER_PULSED_SPLIT);
js.AddEncoder(ENCODER, 3, ENCODER_PULSED_SPLIT);
// Different types of button programming are available. BUTTON_PASSTHRU simply passes on the button's
// real state, and will be the most common, but you can also change how the button works in software like so...