2015-11-05 03:02:09 +00:00
|
|
|
#ifndef _JOYSTICK_H_
|
|
|
|
#define _JOYSTICK_H_
|
|
|
|
|
2021-11-08 01:37:30 +00:00
|
|
|
#include "Button.h"
|
2021-11-22 20:10:08 +00:00
|
|
|
#include "Matrix.h"
|
2015-11-05 03:02:09 +00:00
|
|
|
#include <Arduino.h>
|
2021-11-07 22:49:14 +00:00
|
|
|
#include <Mux.h>
|
2021-11-01 23:12:30 +00:00
|
|
|
|
|
|
|
using namespace admux;
|
2021-11-01 18:49:53 +00:00
|
|
|
|
2015-11-05 03:02:09 +00:00
|
|
|
// If you're using the arduino-big-joystick firmware, these numbers can't be
|
2015-11-06 15:48:38 +00:00
|
|
|
// 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
|
2015-11-06 15:48:38 +00:00
|
|
|
#endif
|
|
|
|
#ifndef JOYSTICK_NUM_BUTTONS
|
2015-11-05 03:02:09 +00:00
|
|
|
#define JOYSTICK_NUM_BUTTONS 40
|
2015-11-06 15:48:38 +00:00
|
|
|
#endif
|
2015-11-05 03:02:09 +00:00
|
|
|
#define JOYSTICK_NUM_BYTES (JOYSTICK_NUM_BUTTONS+7)/8
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2015-11-09 04:15:58 +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:
|
2021-12-22 22:08:26 +00:00
|
|
|
Joystick(uint8_t release_delay = 50, bool debug = false);
|
2015-11-05 03:02:09 +00:00
|
|
|
void Init();
|
2015-11-06 15:48:38 +00:00
|
|
|
void Update();
|
2021-11-01 23:12:30 +00:00
|
|
|
|
2021-11-24 05:57:41 +00:00
|
|
|
// Use these functions to add a button to the joystick.
|
|
|
|
// Button types are documented in the ButtonType enum. See Button.h for details.
|
|
|
|
// If 'pullup' is true, your button should connect the pin to ground. (also be sure that your board supports INPUT_PULLUP on that pin)
|
|
|
|
// If 'pullup' is false, your button should connect the pin to VCC.
|
|
|
|
|
|
|
|
// Add a button connected directly to a pin on the microcontroller.
|
2021-11-22 19:12:44 +00:00
|
|
|
void AddButton(uint8_t pin, ButtonType type, bool pullup=true);
|
2021-11-24 05:57:41 +00:00
|
|
|
|
|
|
|
// Add a button connected to a channel on a Multiplexer.
|
|
|
|
// FIXME: This doesn't currently work!
|
2021-11-22 19:12:44 +00:00
|
|
|
void AddMuxButton(uint8_t channel, Mux* mux, ButtonType type, bool pullup=true);
|
2021-11-24 05:57:41 +00:00
|
|
|
|
|
|
|
// Add a button connected to a scan matrix.
|
2021-11-22 20:10:08 +00:00
|
|
|
void AddMatrixButton(uint8_t row, uint8_t col, Matrix* matrix, ButtonType type, bool pullup=true);
|
2021-11-22 19:12:44 +00:00
|
|
|
|
2021-11-07 22:49:14 +00:00
|
|
|
// Add a rotary encoder. ENCODER button types allow you to treat an encoder as a momentary button or an axis (TODO)
|
2021-12-21 04:16:07 +00:00
|
|
|
void AddEncoder(uint8_t pin1, uint8_t pin2, int8_t tick_threshold, ButtonType type);
|
2021-11-01 23:12:30 +00:00
|
|
|
|
|
|
|
// Add an analog axis to the joystick. THIS METHOD IS NOT CURRENTLY TESTED OR SUPPORTED. It might work, but probably not.
|
|
|
|
void AddAxis(uint8_t pin);
|
|
|
|
|
2021-11-09 06:11:36 +00:00
|
|
|
|
|
|
|
// These members should not be used by end users; todo: remember how friend classes work
|
2015-11-05 03:02:09 +00:00
|
|
|
void PressButton(uint8_t button);
|
|
|
|
void ReleaseButton(uint8_t button);
|
2021-11-08 01:37:30 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
void SetAxis(uint8_t axis, int16_t value);
|
2015-11-05 03:02:09 +00:00
|
|
|
void ReleaseAllButtons();
|
|
|
|
void Write();
|
|
|
|
|
2015-11-11 05:10:45 +00:00
|
|
|
void _UpdateAxis(uint8_t index);
|
2021-11-02 23:17:25 +00:00
|
|
|
|
2021-11-22 19:12:44 +00:00
|
|
|
void _addButton(ButtonType type, Reader* reader);
|
|
|
|
|
2021-11-08 01:37:30 +00:00
|
|
|
Button* _buttons[JOYSTICK_NUM_BUTTONS];
|
2021-11-01 19:38:01 +00:00
|
|
|
uint8_t _num_buttons;
|
2021-11-01 22:09:11 +00:00
|
|
|
uint8_t _virtual_buttons; // a single user-defined button can have multiple virtual buttons.
|
2015-11-06 16:36:33 +00:00
|
|
|
|
|
|
|
uint8_t _axes[JOYSTICK_NUM_AXES];
|
|
|
|
uint8_t _num_axes;
|
|
|
|
|
2015-11-05 03:02:09 +00:00
|
|
|
JoyReport _joyReport;
|
2021-11-13 05:56:21 +00:00
|
|
|
|
|
|
|
bool _debug;
|
2021-12-22 22:07:57 +00:00
|
|
|
uint8_t release_delay;
|
2015-11-05 03:02:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|