Add some logic to the encoder to only register a button press after a certain number of inputs.
This commit is contained in:
parent
4c5b51f7b8
commit
145e383bc5
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->type = ENCODER_PULSED_SPLIT;
|
||||||
this->vbutton2 = vbutton + 1;
|
this->vbutton2 = vbutton + 1;
|
||||||
this->encoder = new Encoder(pin1, pin2);
|
this->encoder = new Encoder(pin1, pin2);
|
||||||
this->last_value = encoder->read();
|
this->last_value = encoder->read();
|
||||||
this->release_time1 = 0;
|
this->release_time1 = 0;
|
||||||
this->release_time2 = 0;
|
this->release_time2 = 0;
|
||||||
|
this->ticks = 0;
|
||||||
|
this->tick_threshold = tick_threshold;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool EncoderButton::Update(Joystick* js) {
|
bool EncoderButton::Update(Joystick* js) {
|
||||||
|
@ -126,15 +128,24 @@ bool EncoderButton::Update(Joystick* js) {
|
||||||
bool changed = false;
|
bool changed = false;
|
||||||
long new_value = encoder->read();
|
long new_value = encoder->read();
|
||||||
if (new_value > last_value) {
|
if (new_value > last_value) {
|
||||||
|
ticks++;
|
||||||
|
} else if (new_value < last_value) {
|
||||||
|
ticks--;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ticks > tick_threshold) {
|
||||||
js->PressButton(vbutton);
|
js->PressButton(vbutton);
|
||||||
changed = true;
|
changed = true;
|
||||||
release_time1 = millis() + 250;
|
release_time1 = millis() + 250;
|
||||||
|
ticks = 0;
|
||||||
}
|
}
|
||||||
else if (new_value < last_value) {
|
else if (ticks < tick_threshold * -1) {
|
||||||
js->PressButton(vbutton2);
|
js->PressButton(vbutton2);
|
||||||
changed = true;
|
changed = true;
|
||||||
release_time2 = millis() + 250;
|
release_time2 = millis() + 250;
|
||||||
|
ticks = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
last_value = new_value;
|
last_value = new_value;
|
||||||
return changed;
|
return changed;
|
||||||
}
|
}
|
||||||
|
|
4
Button.h
4
Button.h
|
@ -63,7 +63,7 @@ class PulsedButton : public Button {
|
||||||
|
|
||||||
class EncoderButton : public Button {
|
class EncoderButton : public Button {
|
||||||
public:
|
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);
|
bool Update(Joystick* js);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
@ -72,6 +72,8 @@ class EncoderButton : public Button {
|
||||||
uint8_t vbutton2;
|
uint8_t vbutton2;
|
||||||
unsigned long release_time1;
|
unsigned long release_time1;
|
||||||
unsigned long release_time2;
|
unsigned long release_time2;
|
||||||
|
int8_t ticks;
|
||||||
|
int8_t tick_threshold;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -53,12 +53,12 @@ void Joystick::AddMatrixButton(uint8_t row, uint8_t col, Matrix* matrix, ButtonT
|
||||||
_addButton(type, new MatrixReader(row, col, matrix, pullup));
|
_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;
|
Button *button;
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case ENCODER_PULSED_SPLIT:
|
case ENCODER_PULSED_SPLIT:
|
||||||
// add an encoder button. _BuildButton() doesn't do everything we need, however...
|
// 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;
|
_buttons[_num_buttons] = button;
|
||||||
_num_buttons++;
|
_num_buttons++;
|
||||||
_virtual_buttons += 2;
|
_virtual_buttons += 2;
|
||||||
|
|
|
@ -49,7 +49,7 @@ class Joystick {
|
||||||
void AddMatrixButton(uint8_t row, uint8_t col, Matrix* matrix, ButtonType type, bool pullup=true);
|
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)
|
// 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.
|
// 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);
|
void AddAxis(uint8_t pin);
|
||||||
|
|
|
@ -48,7 +48,7 @@ void setup() {
|
||||||
js.Init();
|
js.Init();
|
||||||
|
|
||||||
Serial.println("Adding encoder.");
|
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
|
// 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...
|
// real state, and will be the most common, but you can also change how the button works in software like so...
|
||||||
|
|
Loading…
Reference in New Issue
Block a user