2015-11-05 03:02:09 +00:00
|
|
|
#include "Joystick.h"
|
|
|
|
#include <Arduino.h>
|
2021-11-01 18:49:53 +00:00
|
|
|
#include <list>
|
|
|
|
|
|
|
|
using std::list;
|
2015-11-05 03:02:09 +00:00
|
|
|
|
2015-11-09 04:15:58 +00:00
|
|
|
bool operator ==(JoyReport a, JoyReport b){
|
2015-11-12 04:51:02 +00:00
|
|
|
for (uint8_t i=0; i < JOYSTICK_NUM_AXES; i++) {
|
|
|
|
if (a.axis[i] != b.axis[i]) return false;
|
|
|
|
}
|
|
|
|
for (uint8_t i=0; i < JOYSTICK_NUM_BYTES; i++) {
|
|
|
|
if (a.button[i] != b.button[i]) return false;
|
|
|
|
}
|
|
|
|
return true;
|
2015-11-09 04:15:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator !=(JoyReport a, JoyReport b){
|
|
|
|
return !(a == b);
|
|
|
|
}
|
|
|
|
|
2015-11-05 03:02:09 +00:00
|
|
|
Joystick::Joystick(bool debug) {
|
|
|
|
_debug = debug;
|
2021-11-01 18:49:53 +00:00
|
|
|
_last_button_index = 0;
|
2015-11-06 16:36:33 +00:00
|
|
|
_num_axes = 0;
|
2015-11-09 04:15:58 +00:00
|
|
|
_have_pulsed_button = false;
|
|
|
|
|
2015-11-05 03:02:09 +00:00
|
|
|
for (uint8_t i=0; i < JOYSTICK_NUM_AXES; i++) {
|
|
|
|
_joyReport.axis[i] = 0;
|
|
|
|
}
|
2015-11-06 16:36:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Joystick::Init() {
|
|
|
|
Serial.begin(115200);
|
|
|
|
delay(100);
|
2015-11-05 03:02:09 +00:00
|
|
|
}
|
|
|
|
|
2015-11-06 17:07:37 +00:00
|
|
|
void Joystick::AddButton(uint8_t pin, ButtonType type, bool pullup) {
|
2015-11-13 05:45:38 +00:00
|
|
|
uint8_t mode;
|
|
|
|
if (pullup) mode = INPUT_PULLUP;
|
|
|
|
else mode = INPUT;
|
2015-11-12 04:51:02 +00:00
|
|
|
|
|
|
|
Button button;
|
|
|
|
button.type = type;
|
2015-11-13 05:45:38 +00:00
|
|
|
button.bouncer.attach(pin, mode);
|
2021-11-01 18:49:53 +00:00
|
|
|
uint8_t index = _last_button_index + 1;
|
|
|
|
uint8_t increment = 1;
|
|
|
|
button.index0 = index;
|
2015-11-12 04:51:02 +00:00
|
|
|
|
2021-11-01 18:49:53 +00:00
|
|
|
if (type == BUTTON_PULSED_DOUBLE_ACTION_SPLIT) {
|
|
|
|
increment = 2;
|
|
|
|
button.index1 = index + 1;
|
|
|
|
}
|
2021-11-01 19:24:05 +00:00
|
|
|
|
2021-11-01 18:49:53 +00:00
|
|
|
if (_last_button_index + increment > JOYSTICK_NUM_BUTTONS) {
|
|
|
|
// todo: fail here
|
|
|
|
}
|
|
|
|
|
2021-11-01 19:38:01 +00:00
|
|
|
_buttons[_num_buttons] = button;
|
|
|
|
_num_buttons++;
|
2021-11-01 18:49:53 +00:00
|
|
|
last_button_index += increment;
|
2015-11-09 04:15:58 +00:00
|
|
|
|
2015-11-09 04:32:48 +00:00
|
|
|
if (type & _BUTTON_PULSED_TYPES) _have_pulsed_button = true;
|
2015-11-06 15:48:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Joystick::AddAxis(uint8_t pin) {
|
2015-11-06 16:36:33 +00:00
|
|
|
_axes[_num_axes] = pin;
|
|
|
|
_num_axes++;
|
2015-11-06 15:48:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Joystick::Update() {
|
2015-11-06 16:36:33 +00:00
|
|
|
JoyReport oldReport = _joyReport;
|
|
|
|
|
2021-11-01 19:38:01 +00:00
|
|
|
for (uint i = 0; i < _num_buttons; i++) {
|
|
|
|
_UpdateButton(i);
|
2015-11-06 16:36:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < _num_axes; i++) {
|
2015-11-11 05:10:45 +00:00
|
|
|
_UpdateAxis(i);
|
2015-11-06 16:36:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_joyReport != oldReport) {
|
2015-11-09 04:15:58 +00:00
|
|
|
Write();
|
2015-11-11 05:10:45 +00:00
|
|
|
if (_have_pulsed_button) {
|
|
|
|
_ReleasePulsedButtons();
|
|
|
|
Write();
|
|
|
|
}
|
2015-11-09 04:32:48 +00:00
|
|
|
}
|
2015-11-06 15:48:38 +00:00
|
|
|
}
|
|
|
|
|
2015-11-05 03:02:09 +00:00
|
|
|
void Joystick::SetAxis(uint8_t axis, int16_t value) {
|
|
|
|
if (axis >= JOYSTICK_NUM_AXES) return;
|
|
|
|
_joyReport.axis[axis] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Joystick::PressButton(uint8_t button) {
|
|
|
|
if (button >= JOYSTICK_NUM_BUTTONS) return;
|
|
|
|
uint8_t byte = button / 8;
|
|
|
|
uint8_t bit = button % 8;
|
|
|
|
_joyReport.button[byte] |= 1 << bit;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Joystick::ReleaseButton(uint8_t button) {
|
|
|
|
if (button >= JOYSTICK_NUM_BUTTONS) return;
|
|
|
|
uint8_t byte = button / 8;
|
|
|
|
uint8_t bit = button % 8;
|
2015-11-12 04:51:02 +00:00
|
|
|
_joyReport.button[byte] &= ~(1 << bit);
|
2015-11-05 03:02:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Joystick::ReleaseAllButtons() {
|
|
|
|
for (uint8_t i = 0; i < JOYSTICK_NUM_BYTES; i++) {
|
|
|
|
_joyReport.button[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-09 04:32:48 +00:00
|
|
|
void Joystick::_ReleasePulsedButtons() {
|
2021-11-01 19:38:01 +00:00
|
|
|
for (uint8_t i = 0; i < _num_buttons; i++ ) {
|
|
|
|
if (i->type & _BUTTON_PULSED_TYPES) ReleaseButton(_buttons[i].index0);
|
|
|
|
if (i->type & BUTTON_PULSED_DOUBLE_ACTION_SPLIT) ReleaseButton(_buttons[i].index1);
|
2015-11-09 04:32:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-05 03:02:09 +00:00
|
|
|
void Joystick::Write() {
|
2015-11-12 04:51:02 +00:00
|
|
|
if (!_debug) {
|
|
|
|
Serial.write((uint8_t *)&_joyReport, sizeof(JoyReport));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Serial.print("DEBUG: Writing data: ");
|
|
|
|
for (uint8_t i=0; i < JOYSTICK_NUM_AXES; i++) {
|
|
|
|
Serial.print(_joyReport.axis[i]);
|
|
|
|
Serial.print(" ");
|
|
|
|
}
|
|
|
|
for (uint8_t i=0; i < JOYSTICK_NUM_BYTES; i++) {
|
|
|
|
Serial.print(_joyReport.button[i]);
|
|
|
|
Serial.print(" ");
|
|
|
|
}
|
|
|
|
Serial.println();
|
|
|
|
}
|
2015-11-05 03:02:09 +00:00
|
|
|
delay(250);
|
|
|
|
}
|
2015-11-11 05:10:45 +00:00
|
|
|
|
2021-11-01 19:38:01 +00:00
|
|
|
void Joystick::_UpdateButton(uint8_t button_num) {
|
|
|
|
Button *button = &_button[button_num];
|
2015-11-13 06:21:49 +00:00
|
|
|
bool changed = button->bouncer.update();
|
|
|
|
|
|
|
|
switch (button->type) {
|
2021-11-01 18:49:53 +00:00
|
|
|
case BUTTON_PASSTHRU:
|
|
|
|
if (button->bouncer.rose()) PressButton(button->index0);
|
|
|
|
else if (button->bouncer.fell()) ReleaseButton(button->index0);
|
2015-11-11 05:10:45 +00:00
|
|
|
break;
|
|
|
|
case BUTTON_PULSED:
|
2021-11-01 18:49:53 +00:00
|
|
|
if (button->bouncer.rose()) PressButton(button->index0);
|
2015-11-11 05:10:45 +00:00
|
|
|
break;
|
|
|
|
case BUTTON_PULSED_DOUBLE_ACTION:
|
2021-11-01 18:49:53 +00:00
|
|
|
if (changed) PressButton(button->index0);
|
|
|
|
break;
|
|
|
|
case BUTTON_PULSED_DOUBLE_ACTION_SPLIT:
|
|
|
|
if (button->bouncer.rose()) PressButton(button->index0);
|
|
|
|
else if (button->bouncer.fell()) PressButton(button->index1);
|
2015-11-11 05:10:45 +00:00
|
|
|
break;
|
2021-11-01 19:24:05 +00:00
|
|
|
case BUTTON_LATCHED_MOMENTARY:
|
|
|
|
if (button->bouncer.rose()) {
|
|
|
|
if !(button->pressed) {
|
|
|
|
PressButton(button->index0);
|
|
|
|
button->pressed = true;
|
|
|
|
} else {
|
|
|
|
ReleaseButton(button->index0);
|
|
|
|
button->pressed = false;
|
|
|
|
}
|
|
|
|
break;
|
2015-11-11 05:10:45 +00:00
|
|
|
default:
|
|
|
|
if (_debug) {
|
|
|
|
Serial.print("DEBUG: Unhandled button type: ");
|
2015-11-13 06:21:49 +00:00
|
|
|
Serial.println(button->type);
|
2015-11-11 05:10:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Joystick::_UpdateAxis(uint8_t index) {
|
|
|
|
if (_debug) Serial.println("STUB: Joystick::_UpdateAxis");
|
|
|
|
}
|