Implement logic for pulsed button presses. This commit has not been tested.
This commit is contained in:
parent
c2974d3d21
commit
c814b85d59
18
Joystick.cpp
18
Joystick.cpp
|
@ -39,9 +39,7 @@ void Joystick::AddButton(uint8_t pin, ButtonType type, bool pullup) {
|
||||||
_buttons[_num_buttons].last_state = digitalRead(pin);
|
_buttons[_num_buttons].last_state = digitalRead(pin);
|
||||||
_num_buttons++;
|
_num_buttons++;
|
||||||
|
|
||||||
if (type == BUTTON_PULSED || type == BUTTON_PULSED_DOUBLE_ACTION) {
|
if (type & _BUTTON_PULSED_TYPES) _have_pulsed_button = true;
|
||||||
_have_pulsed_button = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (_debug) {
|
if (_debug) {
|
||||||
Serial.print("Debug: added button of type ");
|
Serial.print("Debug: added button of type ");
|
||||||
|
@ -78,6 +76,11 @@ void Joystick::Update() {
|
||||||
if (_joyReport != oldReport) {
|
if (_joyReport != oldReport) {
|
||||||
Write();
|
Write();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (_have_pulsed_button) {
|
||||||
|
_ReleasePulsedButtons();
|
||||||
|
Write();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Joystick::SetAxis(uint8_t axis, int16_t value) {
|
void Joystick::SetAxis(uint8_t axis, int16_t value) {
|
||||||
|
@ -109,6 +112,15 @@ void Joystick::ReleaseAllButtons() {
|
||||||
if (_debug) Serial.println("DEBUG: All-button release recorded.");
|
if (_debug) Serial.println("DEBUG: All-button release recorded.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Joystick::_ReleasePulsedButtons() {
|
||||||
|
for (uint8_t i = 0; i < _num_buttons; i++) {
|
||||||
|
if (_buttons[i].type & _BUTTON_PULSED_TYPES)
|
||||||
|
ReleaseButton(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_debug) Serial.println("DEBUG: Pulse button release recorded.");
|
||||||
|
}
|
||||||
|
|
||||||
void Joystick::Write() {
|
void Joystick::Write() {
|
||||||
Serial.write((uint8_t *)&_joyReport, sizeof(JoyReport));
|
Serial.write((uint8_t *)&_joyReport, sizeof(JoyReport));
|
||||||
delay(250);
|
delay(250);
|
||||||
|
|
11
Joystick.h
11
Joystick.h
|
@ -15,9 +15,9 @@
|
||||||
#define JOYSTICK_NUM_BYTES (JOYSTICK_NUM_BUTTONS+7)/8
|
#define JOYSTICK_NUM_BYTES (JOYSTICK_NUM_BUTTONS+7)/8
|
||||||
|
|
||||||
enum ButtonType {
|
enum ButtonType {
|
||||||
BUTTON_MAINTAINED,
|
BUTTON_MAINTAINED = 0x1,
|
||||||
BUTTON_PULSED,
|
BUTTON_PULSED = 0x2,
|
||||||
BUTTON_PULSED_DOUBLE_ACTION
|
BUTTON_PULSED_DOUBLE_ACTION = 0x4
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct JoyReport {
|
typedef struct JoyReport {
|
||||||
|
@ -46,6 +46,8 @@ class Joystick {
|
||||||
void Write();
|
void Write();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void _ReleasePulsedButtons();
|
||||||
|
|
||||||
struct {
|
struct {
|
||||||
uint8_t pin;
|
uint8_t pin;
|
||||||
ButtonType type;
|
ButtonType type;
|
||||||
|
@ -61,4 +63,7 @@ class Joystick {
|
||||||
bool _debug;
|
bool _debug;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Internal use only.
|
||||||
|
#define _BUTTON_PULSED_TYPES (BUTTON_PULSED | BUTTON_PULSED_DOUBLE_ACTION)
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue
Block a user