Compare commits
2 Commits
398668205d
...
bcfd67b50d
Author | SHA1 | Date | |
---|---|---|---|
bcfd67b50d | |||
3fd595c7c2 |
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
*.elf
|
||||||
|
*.hex
|
38
Joystick.cpp
38
Joystick.cpp
|
@ -17,7 +17,7 @@ bool operator !=(JoyReport a, JoyReport b){
|
||||||
|
|
||||||
Joystick::Joystick(bool debug) {
|
Joystick::Joystick(bool debug) {
|
||||||
_debug = debug;
|
_debug = debug;
|
||||||
_last_button_index = 0;
|
_virtual_buttons = 0;
|
||||||
_num_axes = 0;
|
_num_axes = 0;
|
||||||
_have_pulsed_button = false;
|
_have_pulsed_button = false;
|
||||||
|
|
||||||
|
@ -38,23 +38,23 @@ void Joystick::AddButton(uint8_t pin, ButtonType type, bool pullup) {
|
||||||
|
|
||||||
Button button;
|
Button button;
|
||||||
button.type = type;
|
button.type = type;
|
||||||
|
button.inverted = pullup;
|
||||||
button.bouncer.attach(pin, mode);
|
button.bouncer.attach(pin, mode);
|
||||||
uint8_t index = _last_button_index + 1;
|
|
||||||
uint8_t increment = 1;
|
uint8_t increment = 1;
|
||||||
button.index0 = index;
|
button.vbutton0 = _virtual_buttons;
|
||||||
|
|
||||||
if (type == BUTTON_PULSED_DOUBLE_ACTION_SPLIT) {
|
if (type == BUTTON_PULSED_DOUBLE_ACTION_SPLIT) {
|
||||||
increment = 2;
|
increment = 2;
|
||||||
button.index1 = index + 1;
|
button.vbutton1 = _virtual_buttons + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_last_button_index + increment > JOYSTICK_NUM_BUTTONS) {
|
if (_virtual_buttons + increment > JOYSTICK_NUM_BUTTONS) {
|
||||||
// todo: fail here
|
// todo: fail here
|
||||||
}
|
}
|
||||||
|
|
||||||
_buttons[_num_buttons] = button;
|
_buttons[_num_buttons] = button;
|
||||||
_num_buttons++;
|
_num_buttons++;
|
||||||
_last_button_index += increment;
|
_virtual_buttons += increment;
|
||||||
|
|
||||||
if (type & _BUTTON_PULSED_TYPES) _have_pulsed_button = true;
|
if (type & _BUTTON_PULSED_TYPES) _have_pulsed_button = true;
|
||||||
}
|
}
|
||||||
|
@ -112,8 +112,8 @@ void Joystick::ReleaseAllButtons() {
|
||||||
void Joystick::_ReleasePulsedButtons() {
|
void Joystick::_ReleasePulsedButtons() {
|
||||||
for (uint8_t i = 0; i < _num_buttons; i++ ) {
|
for (uint8_t i = 0; i < _num_buttons; i++ ) {
|
||||||
Button button = _buttons[i];
|
Button button = _buttons[i];
|
||||||
if (button.type & _BUTTON_PULSED_TYPES) ReleaseButton(button.index0);
|
if (button.type & _BUTTON_PULSED_TYPES) ReleaseButton(button.vbutton0);
|
||||||
if (button.type & BUTTON_PULSED_DOUBLE_ACTION_SPLIT) ReleaseButton(button.index1);
|
if (button.type & BUTTON_PULSED_DOUBLE_ACTION_SPLIT) ReleaseButton(button.vbutton1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -139,29 +139,33 @@ void Joystick::Write() {
|
||||||
void Joystick::_UpdateButton(uint8_t button_num) {
|
void Joystick::_UpdateButton(uint8_t button_num) {
|
||||||
Button *button = &_buttons[button_num];
|
Button *button = &_buttons[button_num];
|
||||||
bool changed = button->bouncer.update();
|
bool changed = button->bouncer.update();
|
||||||
|
if (!changed) return;
|
||||||
|
|
||||||
|
bool on = button->bouncer.rose();
|
||||||
|
if (button->inverted) on = button->bouncer.fell();
|
||||||
|
|
||||||
switch (button->type) {
|
switch (button->type) {
|
||||||
case BUTTON_PASSTHRU:
|
case BUTTON_PASSTHRU:
|
||||||
if (button->bouncer.rose()) PressButton(button->index0);
|
if (on) PressButton(button->vbutton0);
|
||||||
else if (button->bouncer.fell()) ReleaseButton(button->index0);
|
else ReleaseButton(button->vbutton0);
|
||||||
break;
|
break;
|
||||||
case BUTTON_PULSED:
|
case BUTTON_PULSED:
|
||||||
if (button->bouncer.rose()) PressButton(button->index0);
|
if (on) PressButton(button->vbutton0);
|
||||||
break;
|
break;
|
||||||
case BUTTON_PULSED_DOUBLE_ACTION:
|
case BUTTON_PULSED_DOUBLE_ACTION:
|
||||||
if (changed) PressButton(button->index0);
|
PressButton(button->vbutton0);
|
||||||
break;
|
break;
|
||||||
case BUTTON_PULSED_DOUBLE_ACTION_SPLIT:
|
case BUTTON_PULSED_DOUBLE_ACTION_SPLIT:
|
||||||
if (button->bouncer.rose()) PressButton(button->index0);
|
if (on) PressButton(button->vbutton0);
|
||||||
else if (button->bouncer.fell()) PressButton(button->index1);
|
else PressButton(button->vbutton1);
|
||||||
break;
|
break;
|
||||||
case BUTTON_LATCHED_MOMENTARY:
|
case BUTTON_LATCHED_MOMENTARY:
|
||||||
if (button->bouncer.rose()) {
|
if (on) {
|
||||||
if (!button->pressed) {
|
if (!button->pressed) {
|
||||||
PressButton(button->index0);
|
PressButton(button->vbutton0);
|
||||||
button->pressed = true;
|
button->pressed = true;
|
||||||
} else {
|
} else {
|
||||||
ReleaseButton(button->index0);
|
ReleaseButton(button->vbutton0);
|
||||||
button->pressed = false;
|
button->pressed = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,8 +31,8 @@ struct JoyReport {
|
||||||
struct Button {
|
struct Button {
|
||||||
ButtonType type;
|
ButtonType type;
|
||||||
Bounce bouncer;
|
Bounce bouncer;
|
||||||
uint8_t index0;
|
uint8_t vbutton0;
|
||||||
uint8_t index1; // only used by BUTTON_PULSED_DOUBLE_ACTION_SPLIT
|
uint8_t vbutton1; // only used by BUTTON_PULSED_DOUBLE_ACTION_SPLIT
|
||||||
bool pressed = false; // only used by BUTTON_LATCHED_MOMENTARY
|
bool pressed = false; // only used by BUTTON_LATCHED_MOMENTARY
|
||||||
bool inverted = false; // if true, send button press on release and vice versa.
|
bool inverted = false; // if true, send button press on release and vice versa.
|
||||||
};
|
};
|
||||||
|
@ -64,7 +64,7 @@ class Joystick {
|
||||||
|
|
||||||
Button _buttons[JOYSTICK_NUM_BUTTONS];
|
Button _buttons[JOYSTICK_NUM_BUTTONS];
|
||||||
uint8_t _num_buttons;
|
uint8_t _num_buttons;
|
||||||
uint8_t _last_button_index; // a single physical button can have multiple logical buttons. _last_button_index tracks the number of logical / virtual buttons we have defined.
|
uint8_t _virtual_buttons; // a single user-defined button can have multiple virtual buttons.
|
||||||
bool _have_pulsed_button;
|
bool _have_pulsed_button;
|
||||||
|
|
||||||
uint8_t _axes[JOYSTICK_NUM_AXES];
|
uint8_t _axes[JOYSTICK_NUM_AXES];
|
||||||
|
|
8
examples/type_test/Makefile
Normal file
8
examples/type_test/Makefile
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
TARGET_BOARD=arduino:avr:uno
|
||||||
|
COM_PORT=/dev/ttyACM0
|
||||||
|
|
||||||
|
build:
|
||||||
|
arduino-cli compile -b ${TARGET_BOARD}
|
||||||
|
|
||||||
|
upload:
|
||||||
|
arduino-cli upload -b ${TARGET_BOARD} -p ${COM_PORT}
|
Loading…
Reference in New Issue
Block a user