Add some logic to the encoder to only register a button press after a certain number of inputs.
This commit is contained in:
15
Button.cpp
15
Button.cpp
@ -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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user