arduino-joystick/Joystick.h

74 lines
1.8 KiB
C
Raw Normal View History

2015-11-05 03:02:09 +00:00
#ifndef _JOYSTICK_H_
#define _JOYSTICK_H_
#include <Arduino.h>
#include <Bounce2.h>
2015-11-05 03:02:09 +00:00
// If you're using the arduino-big-joystick firmware, these numbers can't be
// changed. If you're writing your own Report Descriptor, tune these to match by
// defining them *before* you include Joystick.h.
#ifndef JOYSTICK_NUM_AXES
2015-11-05 03:02:09 +00:00
#define JOYSTICK_NUM_AXES 8
#endif
#ifndef JOYSTICK_NUM_BUTTONS
2015-11-05 03:02:09 +00:00
#define JOYSTICK_NUM_BUTTONS 40
#endif
2015-11-05 03:02:09 +00:00
#define JOYSTICK_NUM_BYTES (JOYSTICK_NUM_BUTTONS+7)/8
enum ButtonType {
BUTTON_LATCHED = 0x1,
BUTTON_PULSED = 0x2,
BUTTON_PULSED_DOUBLE_ACTION = 0x4
};
2015-11-11 05:10:45 +00:00
struct JoyReport {
2015-11-05 03:02:09 +00:00
int16_t axis[JOYSTICK_NUM_AXES];
uint8_t button[JOYSTICK_NUM_BYTES];
2015-11-11 05:10:45 +00:00
};
struct Button {
ButtonType type;
Bounce bouncher;
2015-11-11 05:10:45 +00:00
};
2015-11-05 03:02:09 +00:00
bool operator ==(JoyReport a, JoyReport b);
bool operator !=(JoyReport a, JoyReport b);
2015-11-05 03:02:09 +00:00
class Joystick {
public:
Joystick(bool debug=false);
void Init();
void Update();
2015-11-05 03:02:09 +00:00
void AddButton(uint8_t pin, ButtonType type, bool pullup=false);
void AddAxis(uint8_t pin); // Axes don't actually work yet!
// These functions are deprecated and may become private in a future
// version. Prefer the above API instead.
2015-11-05 03:02:09 +00:00
void SetAxis(uint8_t axis, int16_t value);
void PressButton(uint8_t button);
void ReleaseButton(uint8_t button);
void ReleaseAllButtons();
void Write();
private:
void _ReleasePulsedButtons();
void _UpdateButton(uint8_t index);
2015-11-11 05:10:45 +00:00
void _UpdateAxis(uint8_t index);
2015-11-11 05:10:45 +00:00
Button _buttons[JOYSTICK_NUM_BUTTONS];
uint8_t _num_buttons;
bool _have_pulsed_button;
uint8_t _axes[JOYSTICK_NUM_AXES];
uint8_t _num_axes;
2015-11-05 03:02:09 +00:00
JoyReport _joyReport;
bool _debug;
};
// Internal use only.
#define _BUTTON_PULSED_TYPES (BUTTON_PULSED | BUTTON_PULSED_DOUBLE_ACTION)
2015-11-05 03:02:09 +00:00
#endif