Initial commit.

This commit is contained in:
Anna Rose 2015-11-04 22:02:09 -05:00
commit 1a0ecea44a
3 changed files with 100 additions and 0 deletions

54
Joystick.cpp Normal file
View File

@ -0,0 +1,54 @@
#include "Joystick.h"
#include <Arduino.h>
Joystick::Joystick(bool debug) {
_debug = debug;
}
void Joystick::Init() {
Serial.begin(115200);
delay(100);
for (uint8_t i=0; i < JOYSTICK_NUM_AXES; i++) {
_joyReport.axis[i] = 0;
}
for (uint8_t i=0; i < JOYSTICK_NUM_BYTES; i++) {
_joyReport.button[i] = 0;
}
if (_debug) Serial.println("DEBUG: Joystick library initialized.");
}
void Joystick::SetAxis(uint8_t axis, int16_t value) {
if (axis >= JOYSTICK_NUM_AXES) return;
_joyReport.axis[axis] = value;
if (_debug) Serial.println("DEBUG: Axis change recorded.");
}
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;
if (_debug) Serial.println("DEBUG: Button press recorded.");
}
void Joystick::ReleaseButton(uint8_t button) {
if (button >= JOYSTICK_NUM_BUTTONS) return;
uint8_t byte = button / 8;
uint8_t bit = button % 8;
_joyReport.button[byte] |= 0 << bit;
if (_debug) Serial.println("DEBUG: Button release recorded.");
}
void Joystick::ReleaseAllButtons() {
for (uint8_t i = 0; i < JOYSTICK_NUM_BYTES; i++) {
_joyReport.button[i] = 0;
}
if (_debug) Serial.println("DEBUG: All-button release recorded.");
}
void Joystick::Write() {
Serial.write((uint8_t *)&_joyReport, sizeof(JoyReport));
delay(250);
}

39
Joystick.h Normal file
View File

@ -0,0 +1,39 @@
#ifndef _JOYSTICK_H_
#define _JOYSTICK_H_
#include <Arduino.h>
// 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.
// TODO: Pass these in to the constructor, with the same caveats. Shouldn't need
// to edit the header file to make something work in a library. :(
#define JOYSTICK_NUM_AXES 8
#define JOYSTICK_NUM_BUTTONS 40
#define JOYSTICK_NUM_BYTES (JOYSTICK_NUM_BUTTONS+7)/8
typedef struct JoyReport {
int16_t axis[JOYSTICK_NUM_AXES];
uint8_t button[JOYSTICK_NUM_BYTES];
} ;
class Joystick {
public:
Joystick(bool debug=false);
void Init();
void SetAxis(uint8_t axis, int16_t value);
void PressButton(uint8_t button);
void ReleaseButton(uint8_t button);
void ReleaseAllButtons();
// This actually sends the commands. You *must* call this for the
// joystick to do anything!
void Write();
private:
JoyReport _joyReport;
bool _debug;
};
#endif

7
Readme.md Normal file
View File

@ -0,0 +1,7 @@
This is a simple library that builds and sends USB HID Joystick reports.
This requires that your Arduino's USB communication chip be programmed
with the arduino-big-joystick firmware (or similar).
See <https://github.com/harlequin-tech/arduino-usb> for more info.