Add rotary encoder support, simplify AddMuxButton interface.
This commit is contained in:
40
Joystick.cpp
40
Joystick.cpp
@ -43,7 +43,28 @@ void Joystick::AddButton(uint8_t pin, ButtonType type, bool pullup) {
|
||||
_BuildButton(pin, type, pullup);
|
||||
}
|
||||
|
||||
void Joystick::AddMuxButton(uint8_t pin, uint8_t mux_id, uint8_t mux_channel, ButtonType type, bool pullup) {
|
||||
void Joystick::AddEncoder(uint8_t pin0, uint8_t pin1, ButtonType type, bool pullup) {
|
||||
switch (type) {
|
||||
case ENCODER_PULSED_SPLIT:
|
||||
// add an encoder button. _BuildButton() doesn't do everything we need, however...
|
||||
Button *button = _BuildButton(pin0, type, pullup);
|
||||
|
||||
// ... so we set up the encoder stuff here. button->pin and button->bouncer will be
|
||||
// ignored
|
||||
button->encoder_button = true;
|
||||
button->encoder = new Encoder(pin0, pin1);
|
||||
long last_enc = button->encoder->read();
|
||||
break;
|
||||
default:
|
||||
if (_debug) {
|
||||
Serial.print("DEBUG: Unhandled encoder type: ");
|
||||
Serial.println(type);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Joystick::AddMuxButton(uint8_t mux_id, uint8_t mux_channel, ButtonType type, bool pullup) {
|
||||
uint8_t pin = _mux[mux_id]->signalPin().pin;
|
||||
Button *button = _BuildButton(pin, type, pullup);
|
||||
button->mux = true;
|
||||
button->mux_id = mux_id;
|
||||
@ -62,7 +83,7 @@ Button* Joystick::_BuildButton(uint8_t pin, ButtonType type, bool pullup) {
|
||||
uint8_t increment = 1;
|
||||
button.vbutton0 = _virtual_buttons;
|
||||
|
||||
if (type == BUTTON_PULSED_DOUBLE_ACTION_SPLIT) {
|
||||
if (type & (BUTTON_PULSED_DOUBLE_ACTION_SPLIT | ENCODER_PULSED_SPLIT)) {
|
||||
increment = 2;
|
||||
button.vbutton1 = _virtual_buttons + 1;
|
||||
}
|
||||
@ -99,7 +120,11 @@ void Joystick::Update() {
|
||||
JoyReport oldReport = _joyReport;
|
||||
|
||||
for (uint8_t i = 0; i < _num_buttons; i++) {
|
||||
_UpdateButton(i);
|
||||
if (_buttons[i].type == ENCODER_PULSED_SPLIT) { // todo: make this check for any encoder type
|
||||
_UpdateEncoder(i);
|
||||
} else {
|
||||
_UpdateButton(i);
|
||||
}
|
||||
}
|
||||
|
||||
for (uint8_t i = 0; i < _num_axes; i++) {
|
||||
@ -167,6 +192,7 @@ void Joystick::Write() {
|
||||
delay(250);
|
||||
}
|
||||
|
||||
// todo: bite the bullet and use inheritance here, this is getting out of hand
|
||||
void Joystick::_UpdateButton(uint8_t button_num) {
|
||||
Button *button = &_buttons[button_num];
|
||||
if (button->mux) {
|
||||
@ -212,6 +238,14 @@ void Joystick::_UpdateButton(uint8_t button_num) {
|
||||
}
|
||||
}
|
||||
|
||||
void Joystick::_UpdateEncoder(uint8_t index) {
|
||||
Button *button = &_buttons[index];
|
||||
long new_value = button->encoder->read();
|
||||
if (new_value > button->last_enc) PressButton(button->vbutton0);
|
||||
else if (new_value < button->last_enc) PressButton(button->vbutton1);
|
||||
button->last_enc = new_value;
|
||||
}
|
||||
|
||||
void Joystick::_UpdateAxis(uint8_t index) {
|
||||
if (_debug) Serial.println("STUB: Joystick::_UpdateAxis");
|
||||
}
|
||||
|
Reference in New Issue
Block a user