From 1a0ecea44a07835d7f39e352e414f8171414b979 Mon Sep 17 00:00:00 2001 From: Anna Wiggins Date: Wed, 4 Nov 2015 22:02:09 -0500 Subject: [PATCH] Initial commit. --- Joystick.cpp | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++ Joystick.h | 39 +++++++++++++++++++++++++++++++++++++ Readme.md | 7 +++++++ 3 files changed, 100 insertions(+) create mode 100644 Joystick.cpp create mode 100644 Joystick.h create mode 100644 Readme.md diff --git a/Joystick.cpp b/Joystick.cpp new file mode 100644 index 0000000..ed5fa27 --- /dev/null +++ b/Joystick.cpp @@ -0,0 +1,54 @@ +#include "Joystick.h" +#include + +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); +} diff --git a/Joystick.h b/Joystick.h new file mode 100644 index 0000000..e6b0dd1 --- /dev/null +++ b/Joystick.h @@ -0,0 +1,39 @@ +#ifndef _JOYSTICK_H_ +#define _JOYSTICK_H_ + +#include + +// 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 diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..714e8da --- /dev/null +++ b/Readme.md @@ -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 for more info. +