mirror of https://github.com/OpenIPC/wiki.git
Add files via upload
parent
7232a5f062
commit
8a01d89bb7
|
@ -0,0 +1,28 @@
|
|||
## How to contribute to the Arduino Joystick Library
|
||||
|
||||
### Report a bug
|
||||
|
||||
* **Check to see if the bug has already reported** by searching on GitHub under [Issues](https://github.com/MHeironimus/ArduinoJoystickLibrary/issues).
|
||||
|
||||
* If you're unable to find an open issue addressing the problem, [open a new one](https://github.com/MHeironimus/ArduinoJoystickLibrary/issues/new). Be sure to include a **title and clear description**, as much relevant information as possible (e.g. host OS, Arduino IDE version, etc.), any important hardware information (e.g. name of Arduino board being used, specific pin wirings if they are relavant, etc.), and a **sample sketch** file demonstrating the issue.
|
||||
|
||||
### Fix a bug
|
||||
|
||||
* Open a new GitHub pull request with the fix.
|
||||
|
||||
* Ensure the PR description clearly describes the problem and solution. Include the relevant issue number if applicable.
|
||||
|
||||
### Whitespace, format code, or cosmetic updates
|
||||
|
||||
* Changes that are cosmetic in nature and do not add anything substantial to the stability, functionality, or testability will generally not be accepted.
|
||||
|
||||
### Adding a new feature
|
||||
|
||||
* Feel free to [create a new issue](https://github.com/MHeironimus/ArduinoJoystickLibrary/issues/new) to document the feature you are planning to add. This will allow others to see and comment on what you would plan to add to the library.
|
||||
|
||||
### Questions about the library
|
||||
|
||||
* Be sure to check the [FAQ](https://github.com/MHeironimus/ArduinoJoystickLibrary/wiki/FAQ) in the [Wiki](https://github.com/MHeironimus/ArduinoJoystickLibrary/wiki) to see if your question has already been answered.
|
||||
* Another good place to look for general Arduino questions is the [Arduino]() website.
|
||||
|
||||
The Arduino Joystick Library is a volunteer effort done in volunteer's spare time. Please be patient as it may take some time for bugs to be fixed, features to be added, and questions to be answered.
|
|
@ -0,0 +1,165 @@
|
|||
GNU LESSER GENERAL PUBLIC LICENSE
|
||||
Version 3, 29 June 2007
|
||||
|
||||
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
|
||||
This version of the GNU Lesser General Public License incorporates
|
||||
the terms and conditions of version 3 of the GNU General Public
|
||||
License, supplemented by the additional permissions listed below.
|
||||
|
||||
0. Additional Definitions.
|
||||
|
||||
As used herein, "this License" refers to version 3 of the GNU Lesser
|
||||
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
||||
General Public License.
|
||||
|
||||
"The Library" refers to a covered work governed by this License,
|
||||
other than an Application or a Combined Work as defined below.
|
||||
|
||||
An "Application" is any work that makes use of an interface provided
|
||||
by the Library, but which is not otherwise based on the Library.
|
||||
Defining a subclass of a class defined by the Library is deemed a mode
|
||||
of using an interface provided by the Library.
|
||||
|
||||
A "Combined Work" is a work produced by combining or linking an
|
||||
Application with the Library. The particular version of the Library
|
||||
with which the Combined Work was made is also called the "Linked
|
||||
Version".
|
||||
|
||||
The "Minimal Corresponding Source" for a Combined Work means the
|
||||
Corresponding Source for the Combined Work, excluding any source code
|
||||
for portions of the Combined Work that, considered in isolation, are
|
||||
based on the Application, and not on the Linked Version.
|
||||
|
||||
The "Corresponding Application Code" for a Combined Work means the
|
||||
object code and/or source code for the Application, including any data
|
||||
and utility programs needed for reproducing the Combined Work from the
|
||||
Application, but excluding the System Libraries of the Combined Work.
|
||||
|
||||
1. Exception to Section 3 of the GNU GPL.
|
||||
|
||||
You may convey a covered work under sections 3 and 4 of this License
|
||||
without being bound by section 3 of the GNU GPL.
|
||||
|
||||
2. Conveying Modified Versions.
|
||||
|
||||
If you modify a copy of the Library, and, in your modifications, a
|
||||
facility refers to a function or data to be supplied by an Application
|
||||
that uses the facility (other than as an argument passed when the
|
||||
facility is invoked), then you may convey a copy of the modified
|
||||
version:
|
||||
|
||||
a) under this License, provided that you make a good faith effort to
|
||||
ensure that, in the event an Application does not supply the
|
||||
function or data, the facility still operates, and performs
|
||||
whatever part of its purpose remains meaningful, or
|
||||
|
||||
b) under the GNU GPL, with none of the additional permissions of
|
||||
this License applicable to that copy.
|
||||
|
||||
3. Object Code Incorporating Material from Library Header Files.
|
||||
|
||||
The object code form of an Application may incorporate material from
|
||||
a header file that is part of the Library. You may convey such object
|
||||
code under terms of your choice, provided that, if the incorporated
|
||||
material is not limited to numerical parameters, data structure
|
||||
layouts and accessors, or small macros, inline functions and templates
|
||||
(ten or fewer lines in length), you do both of the following:
|
||||
|
||||
a) Give prominent notice with each copy of the object code that the
|
||||
Library is used in it and that the Library and its use are
|
||||
covered by this License.
|
||||
|
||||
b) Accompany the object code with a copy of the GNU GPL and this license
|
||||
document.
|
||||
|
||||
4. Combined Works.
|
||||
|
||||
You may convey a Combined Work under terms of your choice that,
|
||||
taken together, effectively do not restrict modification of the
|
||||
portions of the Library contained in the Combined Work and reverse
|
||||
engineering for debugging such modifications, if you also do each of
|
||||
the following:
|
||||
|
||||
a) Give prominent notice with each copy of the Combined Work that
|
||||
the Library is used in it and that the Library and its use are
|
||||
covered by this License.
|
||||
|
||||
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
||||
document.
|
||||
|
||||
c) For a Combined Work that displays copyright notices during
|
||||
execution, include the copyright notice for the Library among
|
||||
these notices, as well as a reference directing the user to the
|
||||
copies of the GNU GPL and this license document.
|
||||
|
||||
d) Do one of the following:
|
||||
|
||||
0) Convey the Minimal Corresponding Source under the terms of this
|
||||
License, and the Corresponding Application Code in a form
|
||||
suitable for, and under terms that permit, the user to
|
||||
recombine or relink the Application with a modified version of
|
||||
the Linked Version to produce a modified Combined Work, in the
|
||||
manner specified by section 6 of the GNU GPL for conveying
|
||||
Corresponding Source.
|
||||
|
||||
1) Use a suitable shared library mechanism for linking with the
|
||||
Library. A suitable mechanism is one that (a) uses at run time
|
||||
a copy of the Library already present on the user's computer
|
||||
system, and (b) will operate properly with a modified version
|
||||
of the Library that is interface-compatible with the Linked
|
||||
Version.
|
||||
|
||||
e) Provide Installation Information, but only if you would otherwise
|
||||
be required to provide such information under section 6 of the
|
||||
GNU GPL, and only to the extent that such information is
|
||||
necessary to install and execute a modified version of the
|
||||
Combined Work produced by recombining or relinking the
|
||||
Application with a modified version of the Linked Version. (If
|
||||
you use option 4d0, the Installation Information must accompany
|
||||
the Minimal Corresponding Source and Corresponding Application
|
||||
Code. If you use option 4d1, you must provide the Installation
|
||||
Information in the manner specified by section 6 of the GNU GPL
|
||||
for conveying Corresponding Source.)
|
||||
|
||||
5. Combined Libraries.
|
||||
|
||||
You may place library facilities that are a work based on the
|
||||
Library side by side in a single library together with other library
|
||||
facilities that are not Applications and are not covered by this
|
||||
License, and convey such a combined library under terms of your
|
||||
choice, if you do both of the following:
|
||||
|
||||
a) Accompany the combined library with a copy of the same work based
|
||||
on the Library, uncombined with any other library facilities,
|
||||
conveyed under the terms of this License.
|
||||
|
||||
b) Give prominent notice with the combined library that part of it
|
||||
is a work based on the Library, and explaining where to find the
|
||||
accompanying uncombined form of the same work.
|
||||
|
||||
6. Revised Versions of the GNU Lesser General Public License.
|
||||
|
||||
The Free Software Foundation may publish revised and/or new versions
|
||||
of the GNU Lesser General Public License from time to time. Such new
|
||||
versions will be similar in spirit to the present version, but may
|
||||
differ in detail to address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the
|
||||
Library as you received it specifies that a certain numbered version
|
||||
of the GNU Lesser General Public License "or any later version"
|
||||
applies to it, you have the option of following the terms and
|
||||
conditions either of that published version or of any later version
|
||||
published by the Free Software Foundation. If the Library as you
|
||||
received it does not specify a version number of the GNU Lesser
|
||||
General Public License, you may choose any version of the GNU Lesser
|
||||
General Public License ever published by the Free Software Foundation.
|
||||
|
||||
If the Library as you received it specifies that a proxy can decide
|
||||
whether future versions of the GNU Lesser General Public License shall
|
||||
apply, that proxy's public statement of acceptance of any version is
|
||||
permanent authorization for you to choose that version for the
|
||||
Library.
|
|
@ -0,0 +1,234 @@
|
|||
# Arduino Joystick Library
|
||||
|
||||
#### Version 2.1.1
|
||||
|
||||
This library can be used with Arduino IDE 1.6.6 or above (see [Wiki - Testing Details](https://github.com/MHeironimus/ArduinoJoystickLibrary/wiki/Testing-Details) for more information) to add one or more joysticks (or gamepads) to the list of HID devices an [Arduino Leonardo](https://www.arduino.cc/en/Main/ArduinoBoardLeonardo) or [Arduino Micro](https://www.arduino.cc/en/Main/ArduinoBoardMicro) (or any Arduino clone that is based on the ATmega32u4) can support. This library will also work with the [Arduino Due](https://www.arduino.cc/en/Main/ArduinoBoardDue), thanks to [@Palakis](https://github.com/Palakis). A complete list of supported boards can be found in the [Wiki - Supported Boards](https://github.com/MHeironimus/ArduinoJoystickLibrary/wiki/Supported-Boards). This will not work with Arduino IDE 1.6.5 (or below) or with non-32u4 based Arduino devices (e.g. Arduino UNO, Arduino MEGA, etc.).
|
||||
|
||||
## Features
|
||||
|
||||
The joystick or gamepad can have the following features:
|
||||
- Buttons (default: 32)
|
||||
- Up to 2 Hat Switches
|
||||
- X, Y, and/or Z Axis (up to 16-bit precision)
|
||||
- X, Y, and/or Z Axis Rotation (up to 16-bit precision)
|
||||
- Rudder (up to 16-bit precision)
|
||||
- Throttle (up to 16-bit precision)
|
||||
- Accelerator (up to 16-bit precision)
|
||||
- Brake (up to 16-bit precision)
|
||||
- Steering (up to 16-bit precision)
|
||||
|
||||
## Installation Instructions
|
||||
|
||||
The following instructions can be used to install the latest version of the library in the Arduino IDE (thanks to [@per1234](https://github.com/per1234) for this update):
|
||||
|
||||
1. Download https://github.com/MHeironimus/ArduinoJoystickLibrary/archive/master.zip
|
||||
2. In the Arduino IDE, select `Sketch` > `Include Library` > `Add .ZIP Library...`. Browse to where the downloaded ZIP file is located and click `Open`. The Joystick library's examples will now appear under `File` > `Examples` > `Joystick`.
|
||||
|
||||
## Examples
|
||||
|
||||
### Simple example
|
||||
|
||||
```C++
|
||||
#include <Joystick.h>
|
||||
|
||||
// Create the Joystick
|
||||
Joystick_ Joystick;
|
||||
|
||||
// Constant that maps the physical pin to the joystick button.
|
||||
const int pinToButtonMap = 9;
|
||||
|
||||
void setup() {
|
||||
// Initialize Button Pins
|
||||
pinMode(pinToButtonMap, INPUT_PULLUP);
|
||||
|
||||
// Initialize Joystick Library
|
||||
Joystick.begin();
|
||||
}
|
||||
|
||||
// Last state of the button
|
||||
int lastButtonState = 0;
|
||||
|
||||
void loop() {
|
||||
|
||||
// Read pin values
|
||||
int currentButtonState = !digitalRead(pinToButtonMap);
|
||||
if (currentButtonState != lastButtonState)
|
||||
{
|
||||
Joystick.setButton(0, currentButtonState);
|
||||
lastButtonState = currentButtonState;
|
||||
}
|
||||
|
||||
delay(50);
|
||||
}
|
||||
```
|
||||
|
||||
### Included Examples
|
||||
|
||||
The following example Arduino sketch files are included in this library:
|
||||
|
||||
#### Simple Samples
|
||||
|
||||
- `JoystickButton` - Creates a Joystick and maps pin 9 to button 0 of the joystick, pin 10 to button 1, pin 11 to button 2, and pin 12 to button 3.
|
||||
- `JoystickKeyboard` - Creates a Joystick and a Keyboard. Maps pin 9 to Joystick Button 0, pin 10 to Joystick Button 1, pin 11 to Keyboard key 1, and pin 12 to Keyboard key 2.
|
||||
- `GamepadExample` - Creates a simple Gamepad with an Up, Down, Left, Right, and Fire button.
|
||||
- `FunduinoJoystickShield` - Creates a simple Gamepad using a Funduino Joystick Shield (https://protosupplies.com/product/funduino-joystick-shield-v1-a/).
|
||||
- `ArcadeStickExample` - Simple arcade stick example that demonstrates how to read twelve Arduino Pro Micro digital pins and map them to the library (thanks to [@nebhead](https://github.com/nebhead) for this example). NOTE: This sketch is for the Arduino Pro Micro only.
|
||||
|
||||
#### Used for Testing
|
||||
|
||||
- `JoystickTest` - Simple test of the Joystick library. It exercises many of the Joystick library’s functions when pin A0 is grounded.
|
||||
- `MultipleJoystickTest` - Creates 4 Joysticks using the library (each with a slightly different configuration) and exercises the first 16 buttons (if present), the X axis, and the Y axis of each joystick when pin A0 is grounded.
|
||||
- `FlightControllerTest` - Creates a Flight Controller and tests 32 buttons, the X and Y axis, the Throttle, and the Rudder when pin A0 is grounded.
|
||||
- `HatSwitchTest` - Creates a joystick with two hat switches. Grounding pins 4 - 11 cause the hat switches to change position.
|
||||
- `DrivingControllerTest` - Creates a Driving Controller and tests 4 buttons, the Steering, Brake, and Accelerator when pin A0 is grounded.
|
||||
|
||||
## Joystick Library API
|
||||
|
||||
The following API is available if the Joystick library in included in a sketch file.
|
||||
|
||||
### Joystick\_(...)
|
||||
|
||||
Constructor used to initialize and setup the Joystick. The following optional parameters are available:
|
||||
|
||||
- `uint8_t hidReportId` - Default: `0x03` - Indicates the joystick's HID report ID. This value must be unique if you are creating multiple instances of Joystick. Do not use `0x01` or `0x02` as they are used by the built-in Arduino Keyboard and Mouse libraries.
|
||||
- `uint8_t joystickType` - Default: `JOYSTICK_TYPE_JOYSTICK` or `0x04` - Indicates the HID input device type. Supported values:
|
||||
- `JOYSTICK_TYPE_JOYSTICK` or `0x04` - Joystick
|
||||
- `JOYSTICK_TYPE_GAMEPAD` or `0x05` - Gamepad
|
||||
- `JOYSTICK_TYPE_MULTI_AXIS` or `0x08` - Multi-axis Controller
|
||||
- `uint8_t buttonCount` - Default: `32` - Indicates how many buttons will be available on the joystick.
|
||||
- `uint8_t hatSwitchCount` - Default: `2` - Indicates how many hat switches will be available on the joystick. Range: `0` - `2`
|
||||
- `bool includeXAxis` - Default: `true` - Indicates if the X Axis is available on the joystick.
|
||||
- `bool includeYAxis` - Default: `true` - Indicates if the Y Axis is available on the joystick.
|
||||
- `bool includeZAxis` - Default: `true` - Indicates if the Z Axis (in some situations this is the right X Axis) is available on the joystick.
|
||||
- `bool includeRxAxis` - Default: `true` - Indicates if the X Axis Rotation (in some situations this is the right Y Axis) is available on the joystick.
|
||||
- `bool includeRyAxis` - Default: `true` - Indicates if the Y Axis Rotation is available on the joystick.
|
||||
- `bool includeRzAxis` - Default: `true` - Indicates if the Z Axis Rotation is available on the joystick.
|
||||
- `bool includeRudder` - Default: `true` - Indicates if the Rudder is available on the joystick.
|
||||
- `bool includeThrottle` - Default: `true` - Indicates if the Throttle is available on the joystick.
|
||||
- `bool includeAccelerator` - Default: `true` - Indicates if the Accelerator is available on the joystick.
|
||||
- `bool includeBrake` - Default: `true` - Indicates if the Brake is available on the joystick.
|
||||
- `bool includeSteering` - Default: `true` - Indicates if the Steering is available on the joystick.
|
||||
|
||||
The following constants define the default values for the constructor parameters listed above:
|
||||
|
||||
- `JOYSTICK_DEFAULT_REPORT_ID` is set to `0x03`
|
||||
- `JOYSTICK_DEFAULT_BUTTON_COUNT` is set to `32`
|
||||
- `JOYSTICK_DEFAULT_HATSWITCH_COUNT` is set to `2`
|
||||
|
||||
### Joystick.begin(bool initAutoSendState)
|
||||
|
||||
Starts emulating a game controller connected to a computer. By default, all methods update the game controller state immediately. If `initAutoSendState` is set to `false`, the `Joystick.sendState` method must be called to update the game controller state.
|
||||
|
||||
### Joystick.end()
|
||||
|
||||
Stops the game controller emulation to a connected computer (Note: just like the Arduino `Keyboard.h` and `Mouse.h` libraries, the `end()` function does not actually do anything).
|
||||
|
||||
### Joystick.setXAxisRange(int32_t minimum, int32_t maximum)
|
||||
|
||||
Sets the range of values that will be used for the X axis. Default: `0` to `1023`
|
||||
|
||||
### Joystick.setXAxis(int32_t value)
|
||||
|
||||
Sets the X axis value. See `setXAxisRange` for the range.
|
||||
|
||||
### Joystick.setYAxisRange(int32_t minimum, int32_t maximum)
|
||||
|
||||
Sets the range of values that will be used for the Y axis. Default: `0` to `1023`
|
||||
|
||||
### Joystick.setYAxis(int32_t value)
|
||||
|
||||
Sets the Y axis value. See `setYAxisRange` for the range.
|
||||
|
||||
### Joystick.setZAxisRange(int32_t minimum, int32_t maximum)
|
||||
|
||||
Sets the range of values that will be used for the Z axis. Default: `0` to `1023`
|
||||
|
||||
### Joystick.setZAxis(int32_t value)
|
||||
|
||||
Sets the Z axis value. See `setZAxisRange` for the range.
|
||||
|
||||
### Joystick.setRxAxisRange(int32_t minimum, int32_t maximum)
|
||||
|
||||
Sets the range of values that will be used for the X axis rotation. Default: `0` to `1023`
|
||||
|
||||
### Joystick.setRxAxis(int32_t value)
|
||||
|
||||
Sets the X axis rotation value. See `setRxAxisRange` for the range.
|
||||
|
||||
### Joystick.setRyAxisRange(int32_t minimum, int32_t maximum)
|
||||
|
||||
Sets the range of values that will be used for the Y axis rotation. Default: `0` to `1023`
|
||||
|
||||
### Joystick.setRyAxis(int32_t value)
|
||||
|
||||
Sets the Y axis rotation value. See `setRyAxisRange` for the range.
|
||||
|
||||
### Joystick.setRzAxisRange(int32_t minimum, int32_t maximum)
|
||||
|
||||
Sets the range of values that will be used for the Z axis rotation. Default: `0` to `1023`
|
||||
|
||||
### Joystick.setRzAxis(int32_t value)
|
||||
|
||||
Sets the Z axis rotation value. See `setRzAxisRange` for the range.
|
||||
|
||||
### Joystick.setRudderRange(int32_t minimum, int32_t maximum)
|
||||
|
||||
Sets the range of values that will be used for the Rudder. Default: `0` to `1023`
|
||||
|
||||
### Joystick.setRudder(int32_t value)
|
||||
|
||||
Sets the Rudder value. See `setRudderRange` for the range.
|
||||
|
||||
### Joystick.setThrottleRange(int32_t minimum, int32_t maximum)
|
||||
|
||||
Sets the range of values that will be used for the Throttle. Default: `0` to `1023`
|
||||
|
||||
### Joystick.setThrottle(int32_t value)
|
||||
|
||||
Sets the Throttle value. See `setThrottleRange` for the range.
|
||||
|
||||
### Joystick.setAcceleratorRange(int32_t minimum, int32_t maximum)
|
||||
|
||||
Sets the range of values that will be used for the Accelerator. Default: `0` to `1023`
|
||||
|
||||
### Joystick.setAccelerator(int32_t value)
|
||||
|
||||
Sets the Accelerator value. See `setAcceleratorRange` for the range.
|
||||
|
||||
### Joystick.setBrakeRange(int32_t minimum, int32_t maximum)
|
||||
|
||||
Sets the range of values that will be used for the Brake. Default: `0` to `1023`
|
||||
|
||||
### Joystick.setBrake(int32_t value)
|
||||
|
||||
Sets the Brake value. See `setBrakeRange` for the range.
|
||||
|
||||
### Joystick.setSteeringRange(int32_t minimum, int32_t maximum)
|
||||
|
||||
Sets the range of values that will be used for the Steering. Default: `0` to `1023`
|
||||
|
||||
### Joystick.setSteering(int32_t value)
|
||||
|
||||
Sets the Steering value. See `setSteeringRange` for the range.
|
||||
|
||||
### Joystick.setButton(uint8_t button, uint8_t value)
|
||||
|
||||
Sets the state (`0` or `1`) of the specified button (range: `0` - (`buttonCount - 1`)). The button is the 0-based button number (i.e. button #1 is `0`, button #2 is `1`, etc.). The value is `1` if the button is pressed and `0` if the button is released.
|
||||
|
||||
### Joystick.pressButton(uint8_t button)
|
||||
|
||||
Press the indicated button (range: `0` - (`buttonCount - 1`)). The button is the 0-based button number (i.e. button #1 is `0`, button #2 is `1`, etc.).
|
||||
|
||||
### Joystick.releaseButton(uint8_t button)
|
||||
|
||||
Release the indicated button (range: `0` - (`buttonCount - 1`)). The button is the 0-based button number (i.e. button #1 is `0`, button #2 is `1`, etc.).
|
||||
|
||||
### Joystick.setHatSwitch(int8_t hatSwitch, int16_t value)
|
||||
|
||||
Sets the value of the specified hat switch. The hatSwitch is 0-based (i.e. hat switch #1 is `0` and hat switch #2 is `1`). The value is from 0° to 360°, but in 45° increments. Any value less than 45° will be rounded down (i.e. 44° is rounded down to 0°, 89° is rounded down to 45°, etc.). Set the value to `JOYSTICK_HATSWITCH_RELEASE` or `-1` to release the hat switch.
|
||||
|
||||
### Joystick.sendState()
|
||||
|
||||
Sends the updated joystick state to the host computer. Only needs to be called if `AutoSendState` is `false` (see `Joystick.begin` for more details).
|
||||
|
||||
See the [Wiki](https://github.com/MHeironimus/ArduinoJoystickLibrary/wiki) for more details on things like FAQ, supported boards, testing, etc.
|
|
@ -0,0 +1,173 @@
|
|||
// Simple arcade stick example that demonstrates how to read twelve
|
||||
// Arduino Pro Micro digital pins and map them to the
|
||||
// Arduino Joystick library.
|
||||
//
|
||||
|
||||
// The digital pins 2 - 20 are grounded when they are pressed.
|
||||
// Pin 10, A10, Red = UP
|
||||
// Pin 15, D15, Yellow = RIGHT
|
||||
// Pin 16, D16, Orange = DOWN
|
||||
// Pin 14, D14, Green = LEFT
|
||||
|
||||
// Pin 9, A9 = Button 1
|
||||
// Pin 8, A8 = Button 2
|
||||
// Pin 7, D7 = Button 3
|
||||
// Pin 3, D3 = Button 4
|
||||
// Pin 2, D2 = Button 5
|
||||
// Pin 4, A6 = Button 6
|
||||
|
||||
// Pin 20, A2 = Select Button 1
|
||||
// Pin 19, A1 = Start Button 2
|
||||
|
||||
// Pin 5, D5 = Other Button
|
||||
// Pin 6, A7 = Other Button
|
||||
// Pin 18, A0 = Other Button
|
||||
// Pin 21, A3 = Other Button
|
||||
|
||||
// NOTE: This sketch file is for use with Arduino Pro Micro only.
|
||||
//
|
||||
// Original gamepad example by Matthew Heironimus
|
||||
// 2016-11-24
|
||||
// Adapted for arcade machine setup by Ben Parmeter
|
||||
// 2019-05-20
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
#include <Joystick.h>
|
||||
|
||||
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_GAMEPAD,
|
||||
8, 0, // Button Count, Hat Switch Count
|
||||
true, true, false, // X and Y, but no Z Axis
|
||||
false, false, false, // No Rx, Ry, or Rz
|
||||
false, false, // No rudder or throttle
|
||||
false, false, false); // No accelerator, brake, or steering
|
||||
|
||||
void setup() {
|
||||
// Initialize Button Pins
|
||||
pinMode(2, INPUT_PULLUP);
|
||||
pinMode(3, INPUT_PULLUP);
|
||||
pinMode(4, INPUT_PULLUP);
|
||||
pinMode(5, INPUT_PULLUP);
|
||||
pinMode(6, INPUT_PULLUP);
|
||||
pinMode(7, INPUT_PULLUP);
|
||||
pinMode(8, INPUT_PULLUP);
|
||||
pinMode(9, INPUT_PULLUP);
|
||||
pinMode(10, INPUT_PULLUP);
|
||||
pinMode(14, INPUT_PULLUP);
|
||||
pinMode(15, INPUT_PULLUP);
|
||||
pinMode(16, INPUT_PULLUP);
|
||||
pinMode(18, INPUT_PULLUP);
|
||||
pinMode(19, INPUT_PULLUP);
|
||||
pinMode(20, INPUT_PULLUP);
|
||||
pinMode(21, INPUT_PULLUP);
|
||||
|
||||
// Initialize Joystick Library
|
||||
Joystick.begin();
|
||||
Joystick.setXAxisRange(-1, 1);
|
||||
Joystick.setYAxisRange(-1, 1);
|
||||
}
|
||||
|
||||
// Last state of the buttons
|
||||
int lastButtonState[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
|
||||
int buttonMap[16] = {10,15,16,14,9,8,7,3,2,4,20,19,5,6,18,21};
|
||||
|
||||
// ButtonMap = 0, Pin 10 = UP
|
||||
// ButtonMap = 1, Pin 15 = RIGHT
|
||||
// ButtonMap = 2, Pin 16 = DOWN
|
||||
// ButtonMap = 3, Pin 14 = LEFT
|
||||
|
||||
// ButtonMap = 4, Pin 9 = Button 1
|
||||
// ButtonMap = 5, Pin 8 = Button 2
|
||||
// ButtonMap = 6, Pin 7 = Button 3
|
||||
// ButtonMap = 7, Pin 3 = Button 4
|
||||
// ButtonMap = 8, Pin 2 = Button 5
|
||||
// ButtonMap = 9, Pin 4 = Button 6
|
||||
|
||||
// ButtonMap = 10, Pin 20 = Select Button 1
|
||||
// ButtonMap = 11, Pin 19 = Start Button 2
|
||||
|
||||
// ButtonMap = 12, Pin 5 = Other Button
|
||||
// ButtonMap = 13, Pin 6 = Other Button
|
||||
// ButtonMap = 14, Pin 18 = Other Button
|
||||
// ButtonMap = 15, Pin 21 = Other Button
|
||||
|
||||
|
||||
void loop() {
|
||||
|
||||
// Read pin values
|
||||
for (int index = 0; index < 16; index++)
|
||||
{
|
||||
int currentButtonState = !digitalRead(buttonMap[index]);
|
||||
if (currentButtonState != lastButtonState[index])
|
||||
{
|
||||
switch (index) {
|
||||
case 0: // UP
|
||||
if (currentButtonState == 1) {
|
||||
Joystick.setYAxis(-1);
|
||||
} else {
|
||||
Joystick.setYAxis(0);
|
||||
}
|
||||
break;
|
||||
case 1: // RIGHT
|
||||
if (currentButtonState == 1) {
|
||||
Joystick.setXAxis(1);
|
||||
} else {
|
||||
Joystick.setXAxis(0);
|
||||
}
|
||||
break;
|
||||
case 2: // DOWN
|
||||
if (currentButtonState == 1) {
|
||||
Joystick.setYAxis(1);
|
||||
} else {
|
||||
Joystick.setYAxis(0);
|
||||
}
|
||||
break;
|
||||
case 3: // LEFT
|
||||
if (currentButtonState == 1) {
|
||||
Joystick.setXAxis(-1);
|
||||
} else {
|
||||
Joystick.setXAxis(0);
|
||||
}
|
||||
break;
|
||||
case 4: // Black Button 1
|
||||
Joystick.setButton(0, currentButtonState);
|
||||
break;
|
||||
case 5: // Black Button 2
|
||||
Joystick.setButton(1, currentButtonState);
|
||||
break;
|
||||
case 6: // Black Button 3
|
||||
Joystick.setButton(2, currentButtonState);
|
||||
break;
|
||||
case 7: // Black Button 4
|
||||
Joystick.setButton(3, currentButtonState);
|
||||
break;
|
||||
case 8: // Black Button 5
|
||||
Joystick.setButton(4, currentButtonState);
|
||||
break;
|
||||
case 9: // Black Button 6
|
||||
Joystick.setButton(5, currentButtonState);
|
||||
break;
|
||||
case 10: // Select Button
|
||||
Joystick.setButton(6, currentButtonState);
|
||||
break;
|
||||
case 11: // Start Button
|
||||
Joystick.setButton(7, currentButtonState);
|
||||
break;
|
||||
case 12: // Other Button 1
|
||||
Joystick.setButton(8, currentButtonState);
|
||||
break;
|
||||
case 13: // Other Button 2
|
||||
Joystick.setButton(9, currentButtonState);
|
||||
break;
|
||||
case 14: // Other Button 3
|
||||
Joystick.setButton(10, currentButtonState);
|
||||
break;
|
||||
case 15: // Other Button 4
|
||||
Joystick.setButton(11, currentButtonState);
|
||||
break;
|
||||
}
|
||||
lastButtonState[index] = currentButtonState;
|
||||
}
|
||||
}
|
||||
|
||||
delay(10);
|
||||
}
|
|
@ -0,0 +1,153 @@
|
|||
// Program used to test the driving simulator functions on
|
||||
// the USB Joystick object on the Arduino Leonardo or
|
||||
// Arduino Micro.
|
||||
//
|
||||
// Matthew Heironimus
|
||||
// 2016-05-29 Original version.
|
||||
//------------------------------------------------------------
|
||||
|
||||
#include "Joystick.h"
|
||||
|
||||
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,
|
||||
JOYSTICK_TYPE_MULTI_AXIS, 4, 0,
|
||||
false, false, false, false, false, false,
|
||||
false, false, true, true, true);
|
||||
|
||||
// Set to true to test "Auto Send" mode or false to test "Manual Send" mode.
|
||||
//const bool testAutoSendMode = true;
|
||||
const bool testAutoSendMode = false;
|
||||
|
||||
const unsigned long gcCycleDelta = 1000;
|
||||
const unsigned long gcButtonDelta = 500;
|
||||
const unsigned long gcAnalogDelta = 25;
|
||||
unsigned long gNextTime = 0;
|
||||
unsigned int gCurrentStep = 0;
|
||||
|
||||
void testSingleButtonPush(unsigned int button)
|
||||
{
|
||||
if (button > 0)
|
||||
{
|
||||
Joystick.releaseButton(button - 1);
|
||||
}
|
||||
if (button < 4)
|
||||
{
|
||||
Joystick.pressButton(button);
|
||||
}
|
||||
}
|
||||
|
||||
void testMultiButtonPush(unsigned int currentStep)
|
||||
{
|
||||
for (int button = 0; button < 4; button++)
|
||||
{
|
||||
if ((currentStep == 0) || (currentStep == 2))
|
||||
{
|
||||
if ((button % 2) == 0)
|
||||
{
|
||||
Joystick.pressButton(button);
|
||||
} else if (currentStep != 2)
|
||||
{
|
||||
Joystick.releaseButton(button);
|
||||
}
|
||||
} // if ((currentStep == 0) || (currentStep == 2))
|
||||
if ((currentStep == 1) || (currentStep == 2))
|
||||
{
|
||||
if ((button % 2) != 0)
|
||||
{
|
||||
Joystick.pressButton(button);
|
||||
} else if (currentStep != 2)
|
||||
{
|
||||
Joystick.releaseButton(button);
|
||||
}
|
||||
} // if ((currentStep == 1) || (currentStep == 2))
|
||||
if (currentStep == 3)
|
||||
{
|
||||
Joystick.releaseButton(button);
|
||||
} // if (currentStep == 3)
|
||||
} // for (int button = 0; button < 32; button++)
|
||||
}
|
||||
|
||||
void testAcceleratorBrake(int value)
|
||||
{
|
||||
Joystick.setAccelerator(value);
|
||||
Joystick.setBrake(260 - value);
|
||||
}
|
||||
|
||||
void testSteering(int value)
|
||||
{
|
||||
if (value < 300) {
|
||||
Joystick.setSteering(value);
|
||||
} else {
|
||||
Joystick.setSteering(600 - value);
|
||||
}
|
||||
}
|
||||
|
||||
void setup() {
|
||||
|
||||
Joystick.setAcceleratorRange(0, 260);
|
||||
Joystick.setBrakeRange(0, 260);
|
||||
Joystick.setSteeringRange(0, 300);
|
||||
|
||||
if (testAutoSendMode)
|
||||
{
|
||||
Joystick.begin();
|
||||
}
|
||||
else
|
||||
{
|
||||
Joystick.begin(false);
|
||||
}
|
||||
|
||||
pinMode(A0, INPUT_PULLUP);
|
||||
pinMode(LED_BUILTIN, OUTPUT);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
// System Disabled
|
||||
if (digitalRead(A0) != 0)
|
||||
{
|
||||
// Turn indicator light off.
|
||||
digitalWrite(LED_BUILTIN, 0);
|
||||
return;
|
||||
}
|
||||
|
||||
// Turn indicator light on.
|
||||
digitalWrite(LED_BUILTIN, 1);
|
||||
|
||||
if (millis() >= gNextTime)
|
||||
{
|
||||
|
||||
if (gCurrentStep < 4)
|
||||
{
|
||||
gNextTime = millis() + gcButtonDelta;
|
||||
testSingleButtonPush(gCurrentStep);
|
||||
}
|
||||
else if (gCurrentStep < 9)
|
||||
{
|
||||
gNextTime = millis() + gcButtonDelta;
|
||||
testMultiButtonPush(gCurrentStep - 5);
|
||||
}
|
||||
else if (gCurrentStep < (9 + 260))
|
||||
{
|
||||
gNextTime = millis() + gcAnalogDelta;
|
||||
testAcceleratorBrake(gCurrentStep - 9);
|
||||
}
|
||||
else if (gCurrentStep < (9 + 260 + 600))
|
||||
{
|
||||
gNextTime = millis() + gcAnalogDelta;
|
||||
testSteering(gCurrentStep - (9 + 260));
|
||||
}
|
||||
|
||||
if (testAutoSendMode == false)
|
||||
{
|
||||
Joystick.sendState();
|
||||
}
|
||||
|
||||
gCurrentStep++;
|
||||
if (gCurrentStep >= (9 + 260 + 600))
|
||||
{
|
||||
gNextTime = millis() + gcCycleDelta;
|
||||
gCurrentStep = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,181 @@
|
|||
// Program used to test the USB Joystick library when used as
|
||||
// a Flight Controller on the Arduino Leonardo or Arduino
|
||||
// Micro.
|
||||
//
|
||||
// Matthew Heironimus
|
||||
// 2016-05-29 - Original Version
|
||||
//------------------------------------------------------------
|
||||
|
||||
#include "Joystick.h"
|
||||
|
||||
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,
|
||||
JOYSTICK_TYPE_MULTI_AXIS, 32, 0,
|
||||
true, true, false, false, false, false,
|
||||
true, true, false, false, false);
|
||||
|
||||
// Set to true to test "Auto Send" mode or false to test "Manual Send" mode.
|
||||
//const bool testAutoSendMode = true;
|
||||
const bool testAutoSendMode = false;
|
||||
|
||||
const unsigned long gcCycleDelta = 1000;
|
||||
const unsigned long gcAnalogDelta = 25;
|
||||
const unsigned long gcButtonDelta = 500;
|
||||
unsigned long gNextTime = 0;
|
||||
unsigned int gCurrentStep = 0;
|
||||
|
||||
void testSingleButtonPush(unsigned int button)
|
||||
{
|
||||
if (button > 0)
|
||||
{
|
||||
Joystick.releaseButton(button - 1);
|
||||
}
|
||||
if (button < 32)
|
||||
{
|
||||
Joystick.pressButton(button);
|
||||
}
|
||||
}
|
||||
|
||||
void testMultiButtonPush(unsigned int currentStep)
|
||||
{
|
||||
for (int button = 0; button < 32; button++)
|
||||
{
|
||||
if ((currentStep == 0) || (currentStep == 2))
|
||||
{
|
||||
if ((button % 2) == 0)
|
||||
{
|
||||
Joystick.pressButton(button);
|
||||
} else if (currentStep != 2)
|
||||
{
|
||||
Joystick.releaseButton(button);
|
||||
}
|
||||
} // if ((currentStep == 0) || (currentStep == 2))
|
||||
if ((currentStep == 1) || (currentStep == 2))
|
||||
{
|
||||
if ((button % 2) != 0)
|
||||
{
|
||||
Joystick.pressButton(button);
|
||||
} else if (currentStep != 2)
|
||||
{
|
||||
Joystick.releaseButton(button);
|
||||
}
|
||||
} // if ((currentStep == 1) || (currentStep == 2))
|
||||
if (currentStep == 3)
|
||||
{
|
||||
Joystick.releaseButton(button);
|
||||
} // if (currentStep == 3)
|
||||
} // for (int button = 0; button < 32; button++)
|
||||
}
|
||||
|
||||
void testXYAxis(unsigned int currentStep)
|
||||
{
|
||||
int xAxis;
|
||||
int yAxis;
|
||||
|
||||
if (currentStep < 256)
|
||||
{
|
||||
xAxis = currentStep - 127;
|
||||
yAxis = -127;
|
||||
Joystick.setXAxis(xAxis);
|
||||
Joystick.setYAxis(yAxis);
|
||||
}
|
||||
else if (currentStep < 512)
|
||||
{
|
||||
yAxis = currentStep - 256 - 127;
|
||||
Joystick.setYAxis(yAxis);
|
||||
}
|
||||
else if (currentStep < 768)
|
||||
{
|
||||
xAxis = 128 - (currentStep - 512);
|
||||
Joystick.setXAxis(xAxis);
|
||||
}
|
||||
else if (currentStep < 1024)
|
||||
{
|
||||
yAxis = 128 - (currentStep - 768);
|
||||
Joystick.setYAxis(yAxis);
|
||||
}
|
||||
else if (currentStep < 1024 + 128)
|
||||
{
|
||||
xAxis = currentStep - 1024 - 127;
|
||||
Joystick.setXAxis(xAxis);
|
||||
Joystick.setYAxis(xAxis);
|
||||
}
|
||||
}
|
||||
|
||||
void testThrottleRudder(unsigned int value)
|
||||
{
|
||||
Joystick.setThrottle(value);
|
||||
Joystick.setRudder(255 - value);
|
||||
}
|
||||
|
||||
void setup() {
|
||||
|
||||
Joystick.setXAxisRange(-127, 127);
|
||||
Joystick.setYAxisRange(-127, 127);
|
||||
Joystick.setZAxisRange(-127, 127);
|
||||
Joystick.setThrottleRange(0, 255);
|
||||
Joystick.setRudderRange(0, 255);
|
||||
|
||||
if (testAutoSendMode)
|
||||
{
|
||||
Joystick.begin();
|
||||
}
|
||||
else
|
||||
{
|
||||
Joystick.begin(false);
|
||||
}
|
||||
|
||||
pinMode(A0, INPUT_PULLUP);
|
||||
pinMode(LED_BUILTIN, OUTPUT);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
// System Disabled
|
||||
if (digitalRead(A0) != 0)
|
||||
{
|
||||
// Turn indicator light off.
|
||||
digitalWrite(LED_BUILTIN, 0);
|
||||
return;
|
||||
}
|
||||
|
||||
// Turn indicator light on.
|
||||
digitalWrite(LED_BUILTIN, 1);
|
||||
|
||||
if (millis() >= gNextTime)
|
||||
{
|
||||
|
||||
if (gCurrentStep < 33)
|
||||
{
|
||||
gNextTime = millis() + gcButtonDelta;
|
||||
testSingleButtonPush(gCurrentStep);
|
||||
}
|
||||
else if (gCurrentStep < 37)
|
||||
{
|
||||
gNextTime = millis() + gcButtonDelta;
|
||||
testMultiButtonPush(gCurrentStep - 33);
|
||||
}
|
||||
else if (gCurrentStep < (37 + 256))
|
||||
{
|
||||
gNextTime = millis() + gcAnalogDelta;
|
||||
testThrottleRudder(gCurrentStep - 37);
|
||||
}
|
||||
else if (gCurrentStep < (37 + 256 + 1024 + 128))
|
||||
{
|
||||
gNextTime = millis() + gcAnalogDelta;
|
||||
testXYAxis(gCurrentStep - (37 + 256));
|
||||
}
|
||||
|
||||
if (testAutoSendMode == false)
|
||||
{
|
||||
Joystick.sendState();
|
||||
}
|
||||
|
||||
gCurrentStep++;
|
||||
if (gCurrentStep >= (37 + 256 + 1024 + 128))
|
||||
{
|
||||
gNextTime = millis() + gcCycleDelta;
|
||||
gCurrentStep = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,85 @@
|
|||
#include <Joystick.h>
|
||||
|
||||
const uint8_t buttonCount = 7;
|
||||
Joystick_ controller(JOYSTICK_DEFAULT_REPORT_ID, JOYSTICK_TYPE_GAMEPAD, buttonCount,
|
||||
0, true, true, false,
|
||||
false, false, false,
|
||||
false, false, false,
|
||||
false, false);
|
||||
|
||||
int const BTN_A_PIN = 2;
|
||||
int const BTN_B_PIN = 3;
|
||||
int const BTN_C_PIN = 4;
|
||||
int const BTN_D_PIN = 5;
|
||||
int const BTN_E_PIN = 6;
|
||||
int const BTN_F_PIN = 7;
|
||||
int const BTN_K_PIN = 8;
|
||||
int const AXIS_X_PIN = A0;
|
||||
int const AXIS_Y_PIN = A1;
|
||||
|
||||
int const buttonPins[buttonCount] = {
|
||||
BTN_A_PIN,
|
||||
BTN_B_PIN,
|
||||
BTN_C_PIN,
|
||||
BTN_D_PIN,
|
||||
BTN_E_PIN,
|
||||
BTN_F_PIN,
|
||||
BTN_K_PIN
|
||||
};
|
||||
int lastButtonValue[buttonCount];
|
||||
int lastXAxisValue = -1;
|
||||
int lastYAxisValue = -1;
|
||||
|
||||
void setup()
|
||||
{
|
||||
controller.setYAxisRange(0, 1023);
|
||||
controller.setYAxisRange(1023, 0);
|
||||
controller.begin(false);
|
||||
|
||||
for (int i = 0; i < buttonCount; i++)
|
||||
{
|
||||
pinMode(buttonPins[i], INPUT_PULLUP);
|
||||
lastButtonValue[i] = -1;
|
||||
}
|
||||
|
||||
pinMode(LED_BUILTIN, OUTPUT);
|
||||
digitalWrite(LED_BUILTIN, LOW);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
bool sendUpdate = false;
|
||||
for (int i = 0; i < buttonCount; i++)
|
||||
{
|
||||
const int buttonValue = digitalRead(buttonPins[i]);
|
||||
|
||||
if (buttonValue != lastButtonValue[i])
|
||||
{
|
||||
controller.setButton(i, !buttonValue);
|
||||
lastButtonValue[i] = buttonValue;
|
||||
sendUpdate = true;
|
||||
}
|
||||
}
|
||||
|
||||
const int currentXAxisValue = analogRead(AXIS_X_PIN);
|
||||
if (currentXAxisValue != lastXAxisValue)
|
||||
{
|
||||
controller.setXAxis(currentXAxisValue);
|
||||
lastXAxisValue = currentXAxisValue;
|
||||
sendUpdate = true;
|
||||
}
|
||||
|
||||
const int currentYAxisValue = analogRead(AXIS_Y_PIN);
|
||||
if (currentYAxisValue != lastYAxisValue)
|
||||
{
|
||||
controller.setYAxis(currentYAxisValue);
|
||||
lastYAxisValue = currentYAxisValue;
|
||||
sendUpdate = true;
|
||||
}
|
||||
|
||||
if (sendUpdate)
|
||||
{
|
||||
controller.sendState();
|
||||
}
|
||||
delay(50);
|
||||
}
|
|
@ -0,0 +1,91 @@
|
|||
// Simple gamepad example that demonstraits how to read five Arduino
|
||||
// digital pins and map them to the Arduino Joystick library.
|
||||
//
|
||||
// The digital pins 2 - 6 are grounded when they are pressed.
|
||||
// Pin 2 = UP
|
||||
// Pin 3 = RIGHT
|
||||
// Pin 4 = DOWN
|
||||
// Pin 5 = LEFT
|
||||
// Pin 6 = FIRE
|
||||
//
|
||||
// NOTE: This sketch file is for use with Arduino Leonardo and
|
||||
// Arduino Micro only.
|
||||
//
|
||||
// by Matthew Heironimus
|
||||
// 2016-11-24
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
#include <Joystick.h>
|
||||
|
||||
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_GAMEPAD,
|
||||
1, 0, // Button Count, Hat Switch Count
|
||||
true, true, false, // X and Y, but no Z Axis
|
||||
false, false, false, // No Rx, Ry, or Rz
|
||||
false, false, // No rudder or throttle
|
||||
false, false, false); // No accelerator, brake, or steering
|
||||
|
||||
void setup() {
|
||||
// Initialize Button Pins
|
||||
pinMode(2, INPUT_PULLUP);
|
||||
pinMode(3, INPUT_PULLUP);
|
||||
pinMode(4, INPUT_PULLUP);
|
||||
pinMode(5, INPUT_PULLUP);
|
||||
pinMode(6, INPUT_PULLUP);
|
||||
|
||||
// Initialize Joystick Library
|
||||
Joystick.begin();
|
||||
Joystick.setXAxisRange(-1, 1);
|
||||
Joystick.setYAxisRange(-1, 1);
|
||||
}
|
||||
|
||||
// Last state of the buttons
|
||||
int lastButtonState[5] = {0,0,0,0,0};
|
||||
|
||||
void loop() {
|
||||
|
||||
// Read pin values
|
||||
for (int index = 0; index < 5; index++)
|
||||
{
|
||||
int currentButtonState = !digitalRead(index + 2);
|
||||
if (currentButtonState != lastButtonState[index])
|
||||
{
|
||||
switch (index) {
|
||||
case 0: // UP
|
||||
if (currentButtonState == 1) {
|
||||
Joystick.setYAxis(-1);
|
||||
} else {
|
||||
Joystick.setYAxis(0);
|
||||
}
|
||||
break;
|
||||
case 1: // RIGHT
|
||||
if (currentButtonState == 1) {
|
||||
Joystick.setXAxis(1);
|
||||
} else {
|
||||
Joystick.setXAxis(0);
|
||||
}
|
||||
break;
|
||||
case 2: // DOWN
|
||||
if (currentButtonState == 1) {
|
||||
Joystick.setYAxis(1);
|
||||
} else {
|
||||
Joystick.setYAxis(0);
|
||||
}
|
||||
break;
|
||||
case 3: // LEFT
|
||||
if (currentButtonState == 1) {
|
||||
Joystick.setXAxis(-1);
|
||||
} else {
|
||||
Joystick.setXAxis(0);
|
||||
}
|
||||
break;
|
||||
case 4: // FIRE
|
||||
Joystick.setButton(0, currentButtonState);
|
||||
break;
|
||||
}
|
||||
lastButtonState[index] = currentButtonState;
|
||||
}
|
||||
}
|
||||
|
||||
delay(10);
|
||||
}
|
||||
|
|
@ -0,0 +1,95 @@
|
|||
// Simple example application that shows how to read four Arduino
|
||||
// digital pins and map them to the USB Joystick library's hat switch.
|
||||
//
|
||||
// The digital pins 4, 5, 6, 7, 8, 9, 10, and 11 are grounded when
|
||||
// they are pressed.
|
||||
//
|
||||
// Pin Mappings:
|
||||
// 4 - Hat Switch #0 UP
|
||||
// 5 - Hat Switch #0 RIGHT
|
||||
// 6 - Hat Switch #0 DOWN
|
||||
// 7 - Hat Switch #0 LEFT
|
||||
// 8 - Hat Switch #1 UP
|
||||
// 9 - Hat Switch #1 RIGHT
|
||||
// 10 - Hat Switch #1 DOWN
|
||||
// 11 - Hat Switch #1 LEFT
|
||||
//
|
||||
// NOTE: This sketch file is for use with Arduino Leonardo and
|
||||
// Arduino Micro only.
|
||||
//
|
||||
// by Matthew Heironimus
|
||||
// 2016-05-30
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
#include <Joystick.h>
|
||||
|
||||
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,
|
||||
JOYSTICK_TYPE_GAMEPAD, 0,
|
||||
JOYSTICK_DEFAULT_HATSWITCH_COUNT,
|
||||
false, false, false, false, false, false,
|
||||
false, false, false, false, false);
|
||||
|
||||
void setup() {
|
||||
|
||||
// Initialize Button Pins
|
||||
for (int index = 4; index < 12; index++)
|
||||
{
|
||||
pinMode(index, INPUT_PULLUP);
|
||||
}
|
||||
|
||||
// Initialize Joystick Library
|
||||
Joystick.begin();
|
||||
}
|
||||
|
||||
// Last state of the pins
|
||||
int lastButtonState[2][4] = {{0,0,0,0}, {0,0,0,0}};
|
||||
|
||||
void loop() {
|
||||
|
||||
bool valueChanged[2] = {false, false};
|
||||
int currentPin = 4;
|
||||
|
||||
// Read pin values
|
||||
for (int hatSwitch = 0; hatSwitch < 2; hatSwitch++)
|
||||
{
|
||||
for (int index = 0; index < 4; index++)
|
||||
{
|
||||
int currentButtonState = !digitalRead(currentPin++);
|
||||
if (currentButtonState != lastButtonState[hatSwitch][index])
|
||||
{
|
||||
valueChanged[hatSwitch] = true;
|
||||
lastButtonState[hatSwitch][index] = currentButtonState;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (int hatSwitch = 0; hatSwitch < 2; hatSwitch++)
|
||||
{
|
||||
if (valueChanged[hatSwitch]) {
|
||||
|
||||
if ((lastButtonState[hatSwitch][0] == 0)
|
||||
&& (lastButtonState[hatSwitch][1] == 0)
|
||||
&& (lastButtonState[hatSwitch][2] == 0)
|
||||
&& (lastButtonState[hatSwitch][3] == 0)) {
|
||||
Joystick.setHatSwitch(hatSwitch, -1);
|
||||
}
|
||||
if (lastButtonState[hatSwitch][0] == 1) {
|
||||
Joystick.setHatSwitch(hatSwitch, 0);
|
||||
}
|
||||
if (lastButtonState[hatSwitch][1] == 1) {
|
||||
Joystick.setHatSwitch(hatSwitch, 90);
|
||||
}
|
||||
if (lastButtonState[hatSwitch][2] == 1) {
|
||||
Joystick.setHatSwitch(hatSwitch, 180);
|
||||
}
|
||||
if (lastButtonState[hatSwitch][3] == 1) {
|
||||
Joystick.setHatSwitch(hatSwitch, 270);
|
||||
}
|
||||
|
||||
} // if the value changed
|
||||
|
||||
} // for each hat switch
|
||||
|
||||
delay(50);
|
||||
}
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
// Simple example application that shows how to read four Arduino
|
||||
// digital pins and map them to the USB Joystick library.
|
||||
//
|
||||
// Ground digital pins 9, 10, 11, and 12 to press the joystick
|
||||
// buttons 0, 1, 2, and 3.
|
||||
//
|
||||
// NOTE: This sketch file is for use with Arduino Leonardo and
|
||||
// Arduino Micro only.
|
||||
//
|
||||
// by Matthew Heironimus
|
||||
// 2015-11-20
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
#include <Joystick.h>
|
||||
|
||||
Joystick_ Joystick;
|
||||
|
||||
void setup() {
|
||||
// Initialize Button Pins
|
||||
pinMode(9, INPUT_PULLUP);
|
||||
pinMode(10, INPUT_PULLUP);
|
||||
pinMode(11, INPUT_PULLUP);
|
||||
pinMode(12, INPUT_PULLUP);
|
||||
|
||||
// Initialize Joystick Library
|
||||
Joystick.begin();
|
||||
}
|
||||
|
||||
// Constant that maps the phyical pin to the joystick button.
|
||||
const int pinToButtonMap = 9;
|
||||
|
||||
// Last state of the button
|
||||
int lastButtonState[4] = {0,0,0,0};
|
||||
|
||||
void loop() {
|
||||
|
||||
// Read pin values
|
||||
for (int index = 0; index < 4; index++)
|
||||
{
|
||||
int currentButtonState = !digitalRead(index + pinToButtonMap);
|
||||
if (currentButtonState != lastButtonState[index])
|
||||
{
|
||||
Joystick.setButton(index, currentButtonState);
|
||||
lastButtonState[index] = currentButtonState;
|
||||
}
|
||||
}
|
||||
|
||||
delay(50);
|
||||
}
|
||||
|
|
@ -0,0 +1,63 @@
|
|||
// Simple example application that shows how to read four Arduino
|
||||
// digital pins and map them to buttons on a joystick or keys on a
|
||||
// keyboard uisng the Arduino Joystick and Keyboard libraries.
|
||||
//
|
||||
// The digital pins 9, 10, 11, and 12 are grounded when they are pressed.
|
||||
//
|
||||
// NOTE: This sketch file is for use with Arduino Leonardo and
|
||||
// Arduino Micro only.
|
||||
//
|
||||
// Pin 9 = Joystick Button 0
|
||||
// Pin 10 = Joystick Button 1
|
||||
// Pin 11 = 1 key on the Keyboard
|
||||
// Pin 12 = 2 key on the Keyboard
|
||||
//
|
||||
// by Matthew Heironimus
|
||||
// 2016-05-13
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
#include <Keyboard.h>
|
||||
#include <Joystick.h>
|
||||
|
||||
Joystick_ Joystick;
|
||||
|
||||
void setup() {
|
||||
// Initialize Button Pins
|
||||
pinMode(9, INPUT_PULLUP);
|
||||
pinMode(10, INPUT_PULLUP);
|
||||
pinMode(11, INPUT_PULLUP);
|
||||
pinMode(12, INPUT_PULLUP);
|
||||
|
||||
// Initialize Joystick Library
|
||||
Joystick.begin();
|
||||
}
|
||||
|
||||
// Constant that maps the phyical pin to the joystick button.
|
||||
const int pinToButtonMap = 9;
|
||||
|
||||
// Last state of the button
|
||||
int lastButtonState[4] = {0,0,0,0};
|
||||
|
||||
void loop() {
|
||||
|
||||
// Read pin values
|
||||
for (int index = 0; index < 4; index++)
|
||||
{
|
||||
int currentButtonState = !digitalRead(index + pinToButtonMap);
|
||||
if (currentButtonState != lastButtonState[index])
|
||||
{
|
||||
if (index < 2) {
|
||||
Joystick.setButton(index, currentButtonState);
|
||||
lastButtonState[index] = currentButtonState;
|
||||
} else {
|
||||
if (currentButtonState) {
|
||||
Keyboard.write(47 + index);
|
||||
delay(500);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
delay(100);
|
||||
}
|
||||
|
|
@ -0,0 +1,265 @@
|
|||
// Program used to test the USB Joystick object on the
|
||||
// Arduino Leonardo or Arduino Micro.
|
||||
//
|
||||
// Matthew Heironimus
|
||||
// 2015-03-28 - Original Version
|
||||
// 2015-11-18 - Updated to use the new Joystick library
|
||||
// written for Arduino IDE Version 1.6.6 and
|
||||
// above.
|
||||
// 2016-05-13 Updated to use new dynamic Joystick library
|
||||
// that can be customized.
|
||||
// 2022-03-29 Updated to work with version 2.1.0 of the
|
||||
// Joystick library.
|
||||
//------------------------------------------------------------
|
||||
|
||||
#include "Joystick.h"
|
||||
|
||||
// Create Joystick
|
||||
Joystick_ Joystick;
|
||||
|
||||
// Set to true to test "Auto Send" mode or false to test "Manual Send" mode.
|
||||
//const bool testAutoSendMode = true;
|
||||
const bool testAutoSendMode = false;
|
||||
|
||||
const unsigned long gcCycleDelta = 1000;
|
||||
const unsigned long gcAnalogDelta = 25;
|
||||
const unsigned long gcButtonDelta = 500;
|
||||
unsigned long gNextTime = 0;
|
||||
unsigned int gCurrentStep = 0;
|
||||
|
||||
void testSingleButtonPush(unsigned int button)
|
||||
{
|
||||
if (button > 0)
|
||||
{
|
||||
Joystick.releaseButton(button - 1);
|
||||
}
|
||||
if (button < 32)
|
||||
{
|
||||
Joystick.pressButton(button);
|
||||
}
|
||||
}
|
||||
|
||||
void testMultiButtonPush(unsigned int currentStep)
|
||||
{
|
||||
for (int button = 0; button < 32; button++)
|
||||
{
|
||||
if ((currentStep == 0) || (currentStep == 2))
|
||||
{
|
||||
if ((button % 2) == 0)
|
||||
{
|
||||
Joystick.pressButton(button);
|
||||
} else if (currentStep != 2)
|
||||
{
|
||||
Joystick.releaseButton(button);
|
||||
}
|
||||
} // if ((currentStep == 0) || (currentStep == 2))
|
||||
if ((currentStep == 1) || (currentStep == 2))
|
||||
{
|
||||
if ((button % 2) != 0)
|
||||
{
|
||||
Joystick.pressButton(button);
|
||||
} else if (currentStep != 2)
|
||||
{
|
||||
Joystick.releaseButton(button);
|
||||
}
|
||||
} // if ((currentStep == 1) || (currentStep == 2))
|
||||
if (currentStep == 3)
|
||||
{
|
||||
Joystick.releaseButton(button);
|
||||
} // if (currentStep == 3)
|
||||
} // for (int button = 0; button < 32; button++)
|
||||
}
|
||||
|
||||
void testXYAxis(unsigned int currentStep)
|
||||
{
|
||||
int xAxis;
|
||||
int yAxis;
|
||||
|
||||
if (currentStep < 256)
|
||||
{
|
||||
xAxis = currentStep - 127;
|
||||
yAxis = -127;
|
||||
Joystick.setXAxis(xAxis);
|
||||
Joystick.setYAxis(yAxis);
|
||||
}
|
||||
else if (currentStep < 512)
|
||||
{
|
||||
yAxis = currentStep - 256 - 127;
|
||||
Joystick.setYAxis(yAxis);
|
||||
}
|
||||
else if (currentStep < 768)
|
||||
{
|
||||
xAxis = 128 - (currentStep - 512);
|
||||
Joystick.setXAxis(xAxis);
|
||||
}
|
||||
else if (currentStep < 1024)
|
||||
{
|
||||
yAxis = 128 - (currentStep - 768);
|
||||
Joystick.setYAxis(yAxis);
|
||||
}
|
||||
else if (currentStep < 1024 + 128)
|
||||
{
|
||||
xAxis = currentStep - 1024 - 127;
|
||||
Joystick.setXAxis(xAxis);
|
||||
Joystick.setYAxis(xAxis);
|
||||
}
|
||||
}
|
||||
|
||||
void testZAxis(unsigned int currentStep)
|
||||
{
|
||||
int z;
|
||||
|
||||
if (currentStep < 128)
|
||||
{
|
||||
z = -currentStep;
|
||||
}
|
||||
else if (currentStep < 256 + 128)
|
||||
{
|
||||
z = currentStep - 128 - 127;
|
||||
}
|
||||
else if (currentStep < 256 + 128 + 127)
|
||||
{
|
||||
z = 127 - (currentStep - 383);
|
||||
}
|
||||
|
||||
Joystick.setZAxis(z);
|
||||
}
|
||||
|
||||
void testHatSwitch(unsigned int currentStep)
|
||||
{
|
||||
if (currentStep < 8)
|
||||
{
|
||||
Joystick.setHatSwitch(0, currentStep * 45);
|
||||
}
|
||||
else if (currentStep == 8)
|
||||
{
|
||||
Joystick.setHatSwitch(0, -1);
|
||||
}
|
||||
else if (currentStep < 17)
|
||||
{
|
||||
Joystick.setHatSwitch(1, (currentStep - 9) * 45);
|
||||
}
|
||||
else if (currentStep == 17)
|
||||
{
|
||||
Joystick.setHatSwitch(1, -1);
|
||||
}
|
||||
else if (currentStep == 18)
|
||||
{
|
||||
Joystick.setHatSwitch(0, 0);
|
||||
Joystick.setHatSwitch(1, 0);
|
||||
}
|
||||
else if (currentStep < 27)
|
||||
{
|
||||
Joystick.setHatSwitch(0, (currentStep - 18) * 45);
|
||||
Joystick.setHatSwitch(1, (8 - (currentStep - 18)) * 45);
|
||||
}
|
||||
else if (currentStep == 27)
|
||||
{
|
||||
Joystick.setHatSwitch(0, -1);
|
||||
Joystick.setHatSwitch(1, -1);
|
||||
}
|
||||
}
|
||||
|
||||
void testThrottleRudder(unsigned int value)
|
||||
{
|
||||
Joystick.setThrottle(value);
|
||||
Joystick.setRudder(value);
|
||||
}
|
||||
|
||||
void testXYZAxisRotation(unsigned int degree)
|
||||
{
|
||||
Joystick.setRxAxis(degree);
|
||||
Joystick.setRyAxis(degree);
|
||||
Joystick.setRzAxis(degree * 2);
|
||||
}
|
||||
|
||||
void setup() {
|
||||
|
||||
// Set Range Values
|
||||
Joystick.setXAxisRange(-127, 127);
|
||||
Joystick.setYAxisRange(-127, 127);
|
||||
Joystick.setZAxisRange(-127, 127);
|
||||
Joystick.setRxAxisRange(0, 360);
|
||||
Joystick.setRyAxisRange(360, 0);
|
||||
Joystick.setRzAxisRange(0, 720);
|
||||
Joystick.setThrottleRange(0, 255);
|
||||
Joystick.setRudderRange(255, 0);
|
||||
|
||||
if (testAutoSendMode)
|
||||
{
|
||||
Joystick.begin();
|
||||
}
|
||||
else
|
||||
{
|
||||
Joystick.begin(false);
|
||||
}
|
||||
|
||||
pinMode(A0, INPUT_PULLUP);
|
||||
pinMode(LED_BUILTIN, OUTPUT);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
// System Disabled
|
||||
if (digitalRead(A0) != 0)
|
||||
{
|
||||
// Turn indicator light off.
|
||||
digitalWrite(LED_BUILTIN, 0);
|
||||
return;
|
||||
}
|
||||
|
||||
// Turn indicator light on.
|
||||
digitalWrite(LED_BUILTIN, 1);
|
||||
|
||||
if (millis() >= gNextTime)
|
||||
{
|
||||
|
||||
if (gCurrentStep < 33)
|
||||
{
|
||||
gNextTime = millis() + gcButtonDelta;
|
||||
testSingleButtonPush(gCurrentStep);
|
||||
}
|
||||
else if (gCurrentStep < 37)
|
||||
{
|
||||
gNextTime = millis() + gcButtonDelta;
|
||||
testMultiButtonPush(gCurrentStep - 33);
|
||||
}
|
||||
else if (gCurrentStep < (37 + 256))
|
||||
{
|
||||
gNextTime = millis() + gcAnalogDelta;
|
||||
testThrottleRudder(gCurrentStep - 37);
|
||||
}
|
||||
else if (gCurrentStep < (37 + 256 + 1024 + 128))
|
||||
{
|
||||
gNextTime = millis() + gcAnalogDelta;
|
||||
testXYAxis(gCurrentStep - (37 + 256));
|
||||
}
|
||||
else if (gCurrentStep < (37 + 256 + 1024 + 128 + 510))
|
||||
{
|
||||
gNextTime = millis() + gcAnalogDelta;
|
||||
testZAxis(gCurrentStep - (37 + 256 + 1024 + 128));
|
||||
}
|
||||
else if (gCurrentStep < (37 + 256 + 1024 + 128 + 510 + 28))
|
||||
{
|
||||
gNextTime = millis() + gcButtonDelta;
|
||||
testHatSwitch(gCurrentStep - (37 + 256 + 1024 + 128 + 510));
|
||||
}
|
||||
else if (gCurrentStep < (37 + 256 + 1024 + 128 + 510 + 28 + 360))
|
||||
{
|
||||
gNextTime = millis() + gcAnalogDelta;
|
||||
testXYZAxisRotation(gCurrentStep - (37 + 256 + 1024 + 128 + 510 + 28));
|
||||
}
|
||||
|
||||
if (testAutoSendMode == false)
|
||||
{
|
||||
Joystick.sendState();
|
||||
}
|
||||
|
||||
gCurrentStep++;
|
||||
if (gCurrentStep == (37 + 256 + 1024 + 128 + 510 + 28 + 360))
|
||||
{
|
||||
gNextTime = millis() + gcCycleDelta;
|
||||
gCurrentStep = 0;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,180 @@
|
|||
// Program used to test using the Arduino Joystick Library
|
||||
// to create multiple Joystick objects on a single Arduino
|
||||
// Leonardo or Arduino Micro.
|
||||
//
|
||||
// Each joystick has a unique configuration.
|
||||
//
|
||||
// Matthew Heironimus
|
||||
// 2016-05-13 - Original Version
|
||||
// 2022-03-29 - Updated to work with Joystick Library v2.1.0
|
||||
//------------------------------------------------------------
|
||||
#include <Joystick.h>
|
||||
|
||||
#define JOYSTICK_COUNT 4
|
||||
|
||||
Joystick_ Joystick[JOYSTICK_COUNT] = {
|
||||
Joystick_(0x03, JOYSTICK_TYPE_JOYSTICK, 4, 2, true, true, false, false, false, false, false, false, false, false, false),
|
||||
Joystick_(0x04, JOYSTICK_TYPE_JOYSTICK, 8, 1, true, true, true, true, false, false, false, false, false, false, false),
|
||||
Joystick_(0x05, JOYSTICK_TYPE_JOYSTICK, 16, 0, false, true, false, true, false, false, true, true, false, false, false),
|
||||
Joystick_(0x06, JOYSTICK_TYPE_JOYSTICK, 32, 1, true, true, false, true, true, false, false, false, false, false, false)
|
||||
};
|
||||
|
||||
// Set to true to test "Auto Send" mode or false to test "Manual Send" mode.
|
||||
//const bool testAutoSendMode = true;
|
||||
const bool testAutoSendMode = false;
|
||||
|
||||
const unsigned long gcCycleDelta = 1000;
|
||||
const unsigned long gcAnalogDelta = 25;
|
||||
const unsigned long gcButtonDelta = 500;
|
||||
unsigned long gNextTime = 0;
|
||||
unsigned int gCurrentStep = 0;
|
||||
int gJoystickId = 0;
|
||||
|
||||
void testSingleButtonPush(int joystickId, unsigned int button)
|
||||
{
|
||||
if (button > 0)
|
||||
{
|
||||
Joystick[joystickId].releaseButton(button - 1);
|
||||
}
|
||||
if (button < 16)
|
||||
{
|
||||
Joystick[joystickId].pressButton(button);
|
||||
}
|
||||
}
|
||||
|
||||
void testMultiButtonPush(int joystickId, unsigned int currentStep)
|
||||
{
|
||||
for (int button = 0; button < 16; button++)
|
||||
{
|
||||
if ((currentStep == 0) || (currentStep == 2))
|
||||
{
|
||||
if ((button % 2) == 0)
|
||||
{
|
||||
Joystick[joystickId].pressButton(button);
|
||||
} else if (currentStep != 2)
|
||||
{
|
||||
Joystick[joystickId].releaseButton(button);
|
||||
}
|
||||
} // if ((currentStep == 0) || (currentStep == 2))
|
||||
if ((currentStep == 1) || (currentStep == 2))
|
||||
{
|
||||
if ((button % 2) != 0)
|
||||
{
|
||||
Joystick[joystickId].pressButton(button);
|
||||
} else if (currentStep != 2)
|
||||
{
|
||||
Joystick[joystickId].releaseButton(button);
|
||||
}
|
||||
} // if ((currentStep == 1) || (currentStep == 2))
|
||||
if (currentStep == 3)
|
||||
{
|
||||
Joystick[joystickId].releaseButton(button);
|
||||
} // if (currentStep == 3)
|
||||
} // for (int button = 0; button < 32; button++)
|
||||
}
|
||||
|
||||
void testXYAxis(int joystickId, unsigned int currentStep)
|
||||
{
|
||||
int x;
|
||||
int y;
|
||||
|
||||
if (currentStep < 255)
|
||||
{
|
||||
x = currentStep - 127;
|
||||
y = -127;
|
||||
}
|
||||
else if (currentStep < 510)
|
||||
{
|
||||
x = 127;
|
||||
y = currentStep - 255 - 127;
|
||||
}
|
||||
else if (currentStep < 765)
|
||||
{
|
||||
x = 127 - (currentStep - 510);
|
||||
y = 127;
|
||||
}
|
||||
else if (currentStep < 1020)
|
||||
{
|
||||
x = -127;
|
||||
y = 127 - (currentStep - 765);
|
||||
}
|
||||
else if (currentStep <= 1020 + 127)
|
||||
{
|
||||
x = currentStep - 1020 - 127;
|
||||
y = currentStep - 1020 - 127;
|
||||
}
|
||||
|
||||
Joystick[joystickId].setXAxis(x);
|
||||
Joystick[joystickId].setYAxis(y);
|
||||
}
|
||||
|
||||
void setup() {
|
||||
|
||||
for (int index = 0; index < JOYSTICK_COUNT; index++)
|
||||
{
|
||||
Joystick[index].setXAxisRange(-127, 127);
|
||||
Joystick[index].setYAxisRange(-127, 127);
|
||||
|
||||
if (testAutoSendMode)
|
||||
{
|
||||
Joystick[index].begin();
|
||||
}
|
||||
else
|
||||
{
|
||||
Joystick[index].begin(false);
|
||||
}
|
||||
}
|
||||
|
||||
pinMode(A0, INPUT_PULLUP);
|
||||
pinMode(LED_BUILTIN, OUTPUT);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
// System Disabled
|
||||
if (digitalRead(A0) != 0)
|
||||
{
|
||||
digitalWrite(LED_BUILTIN, 0);
|
||||
return;
|
||||
}
|
||||
|
||||
// Turn indicator light on.
|
||||
digitalWrite(LED_BUILTIN, 1);
|
||||
|
||||
if (millis() >= gNextTime)
|
||||
{
|
||||
|
||||
if (gCurrentStep < 17)
|
||||
{
|
||||
gNextTime = millis() + gcButtonDelta;
|
||||
testSingleButtonPush(gJoystickId, gCurrentStep);
|
||||
}
|
||||
else if (gCurrentStep < (17 + 4))
|
||||
{
|
||||
gNextTime = millis() + gcButtonDelta;
|
||||
testMultiButtonPush(gJoystickId, gCurrentStep - 17);
|
||||
}
|
||||
else if (gCurrentStep < (17 + 4 + 1024 + 128))
|
||||
{
|
||||
gNextTime = millis() + gcAnalogDelta;
|
||||
testXYAxis(gJoystickId, gCurrentStep - (17 + 4));
|
||||
}
|
||||
|
||||
if (testAutoSendMode == false)
|
||||
{
|
||||
Joystick[gJoystickId].sendState();
|
||||
}
|
||||
|
||||
gCurrentStep++;
|
||||
if (gCurrentStep == (17 + 4 + 1024 + 128))
|
||||
{
|
||||
gNextTime = millis() + gcCycleDelta;
|
||||
gCurrentStep = 0;
|
||||
|
||||
if (++gJoystickId >= JOYSTICK_COUNT)
|
||||
{
|
||||
gJoystickId = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
name=Joystick
|
||||
version=2.1.1
|
||||
author=Matthew Heironimus
|
||||
maintainer=Matthew Heironimus <heironimus@live.com>
|
||||
sentence=Allows an Arduino board with USB capabilities (e.g. Leonardo, Arduino Micro, Arudino Due, etc.) to appear as a Joystick or Gamepad.
|
||||
paragraph=This library is built on the PluggableUSB library. It can be used with or without other HID-based libraries (Mouse, Keyboard, etc.).
|
||||
category=Device Control
|
||||
url=https://github.com/MHeironimus/ArduinoJoystickLibrary
|
||||
architectures=avr,sam
|
|
@ -0,0 +1,169 @@
|
|||
/*
|
||||
Modified by Matthew Heironimus to support HID Report Descriptors to be in
|
||||
standard RAM in addition to program memory (PROGMEM).
|
||||
|
||||
Copyright (c) 2015, Arduino LLC
|
||||
Original code (pre-library): Copyright (c) 2011, Peter Barrett
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for
|
||||
any purpose with or without fee is hereby granted, provided that the
|
||||
above copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
|
||||
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
|
||||
BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
|
||||
OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
|
||||
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
|
||||
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "DynamicHID.h"
|
||||
|
||||
#if defined(USBCON)
|
||||
|
||||
#ifdef _VARIANT_ARDUINO_DUE_X_
|
||||
#define USB_SendControl USBD_SendControl
|
||||
#define USB_Send USBD_Send
|
||||
#endif
|
||||
|
||||
DynamicHID_& DynamicHID()
|
||||
{
|
||||
static DynamicHID_ obj;
|
||||
return obj;
|
||||
}
|
||||
|
||||
int DynamicHID_::getInterface(uint8_t* interfaceCount)
|
||||
{
|
||||
*interfaceCount += 1; // uses 1
|
||||
DYNAMIC_HIDDescriptor hidInterface = {
|
||||
D_INTERFACE(pluggedInterface, 1, USB_DEVICE_CLASS_HUMAN_INTERFACE, DYNAMIC_HID_SUBCLASS_NONE, DYNAMIC_HID_PROTOCOL_NONE),
|
||||
D_HIDREPORT(descriptorSize),
|
||||
D_ENDPOINT(USB_ENDPOINT_IN(pluggedEndpoint), USB_ENDPOINT_TYPE_INTERRUPT, USB_EP_SIZE, 0x01)
|
||||
};
|
||||
return USB_SendControl(0, &hidInterface, sizeof(hidInterface));
|
||||
}
|
||||
|
||||
int DynamicHID_::getDescriptor(USBSetup& setup)
|
||||
{
|
||||
// Check if this is a HID Class Descriptor request
|
||||
if (setup.bmRequestType != REQUEST_DEVICETOHOST_STANDARD_INTERFACE) { return 0; }
|
||||
if (setup.wValueH != DYNAMIC_HID_REPORT_DESCRIPTOR_TYPE) { return 0; }
|
||||
|
||||
// In a HID Class Descriptor wIndex cointains the interface number
|
||||
if (setup.wIndex != pluggedInterface) { return 0; }
|
||||
|
||||
int total = 0;
|
||||
DynamicHIDSubDescriptor* node;
|
||||
for (node = rootNode; node; node = node->next) {
|
||||
int res = USB_SendControl((node->inProgMem ? TRANSFER_PGM : 0), node->data, node->length);
|
||||
if (res == -1)
|
||||
return -1;
|
||||
total += res;
|
||||
}
|
||||
|
||||
// Reset the protocol on reenumeration. Normally the host should not assume the state of the protocol
|
||||
// due to the USB specs, but Windows and Linux just assumes its in report mode.
|
||||
protocol = DYNAMIC_HID_REPORT_PROTOCOL;
|
||||
|
||||
return total;
|
||||
}
|
||||
|
||||
uint8_t DynamicHID_::getShortName(char *name)
|
||||
{
|
||||
name[0] = 'H';
|
||||
name[1] = 'I';
|
||||
name[2] = 'D';
|
||||
name[3] = 'A' + (descriptorSize & 0x0F);
|
||||
name[4] = 'A' + ((descriptorSize >> 4) & 0x0F);
|
||||
return 5;
|
||||
}
|
||||
|
||||
void DynamicHID_::AppendDescriptor(DynamicHIDSubDescriptor *node)
|
||||
{
|
||||
if (!rootNode) {
|
||||
rootNode = node;
|
||||
} else {
|
||||
DynamicHIDSubDescriptor *current = rootNode;
|
||||
while (current->next) {
|
||||
current = current->next;
|
||||
}
|
||||
current->next = node;
|
||||
}
|
||||
descriptorSize += node->length;
|
||||
}
|
||||
|
||||
int DynamicHID_::SendReport(uint8_t id, const void* data, int len)
|
||||
{
|
||||
uint8_t p[len + 1];
|
||||
p[0] = id;
|
||||
memcpy(&p[1], data, len);
|
||||
return USB_Send(pluggedEndpoint | TRANSFER_RELEASE, p, len + 1);
|
||||
}
|
||||
|
||||
bool DynamicHID_::setup(USBSetup& setup)
|
||||
{
|
||||
if (pluggedInterface != setup.wIndex) {
|
||||
return false;
|
||||
}
|
||||
|
||||
uint8_t request = setup.bRequest;
|
||||
uint8_t requestType = setup.bmRequestType;
|
||||
|
||||
if (requestType == REQUEST_DEVICETOHOST_CLASS_INTERFACE)
|
||||
{
|
||||
if (request == DYNAMIC_HID_GET_REPORT) {
|
||||
// TODO: DYNAMIC_HID_GetReport();
|
||||
return true;
|
||||
}
|
||||
if (request == DYNAMIC_HID_GET_PROTOCOL) {
|
||||
// TODO: Send8(protocol);
|
||||
return true;
|
||||
}
|
||||
if (request == DYNAMIC_HID_GET_IDLE) {
|
||||
// TODO: Send8(idle);
|
||||
}
|
||||
}
|
||||
|
||||
if (requestType == REQUEST_HOSTTODEVICE_CLASS_INTERFACE)
|
||||
{
|
||||
if (request == DYNAMIC_HID_SET_PROTOCOL) {
|
||||
// The USB Host tells us if we are in boot or report mode.
|
||||
// This only works with a real boot compatible device.
|
||||
protocol = setup.wValueL;
|
||||
return true;
|
||||
}
|
||||
if (request == DYNAMIC_HID_SET_IDLE) {
|
||||
idle = setup.wValueL;
|
||||
return true;
|
||||
}
|
||||
if (request == DYNAMIC_HID_SET_REPORT)
|
||||
{
|
||||
//uint8_t reportID = setup.wValueL;
|
||||
//uint16_t length = setup.wLength;
|
||||
//uint8_t data[length];
|
||||
// Make sure to not read more data than USB_EP_SIZE.
|
||||
// You can read multiple times through a loop.
|
||||
// The first byte (may!) contain the reportID on a multreport.
|
||||
//USB_RecvControl(data, length);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
DynamicHID_::DynamicHID_(void) : PluggableUSBModule(1, 1, epType),
|
||||
rootNode(NULL), descriptorSize(0),
|
||||
protocol(DYNAMIC_HID_REPORT_PROTOCOL), idle(1)
|
||||
{
|
||||
epType[0] = EP_TYPE_INTERRUPT_IN;
|
||||
PluggableUSB().plug(this);
|
||||
}
|
||||
|
||||
int DynamicHID_::begin(void)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif /* if defined(USBCON) */
|
|
@ -0,0 +1,144 @@
|
|||
/*
|
||||
Modified by Matthew Heironimus to support HID Report Descriptors to be in
|
||||
standard RAM in addition to program memory (PROGMEM).
|
||||
|
||||
Copyright (c) 2015, Arduino LLC
|
||||
Original code (pre-library): Copyright (c) 2011, Peter Barrett
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for
|
||||
any purpose with or without fee is hereby granted, provided that the
|
||||
above copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
|
||||
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
|
||||
BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
|
||||
OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
|
||||
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
|
||||
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef DYNAMIC_HID_h
|
||||
#define DYNAMIC_HID_h
|
||||
|
||||
#include <stdint.h>
|
||||
#include <Arduino.h>
|
||||
|
||||
#ifdef _VARIANT_ARDUINO_DUE_X_
|
||||
// The following values are the same as AVR's USBAPI.h
|
||||
// Reproduced here because SAM doesn't have these in
|
||||
// its own USBAPI.H
|
||||
#define USB_EP_SIZE 64
|
||||
#define TRANSFER_PGM 0x80
|
||||
|
||||
#include "USB/PluggableUSB.h"
|
||||
#else
|
||||
#include "PluggableUSB.h"
|
||||
#endif
|
||||
|
||||
#if defined(USBCON)
|
||||
|
||||
#define _USING_DYNAMIC_HID
|
||||
|
||||
// DYNAMIC_HID 'Driver'
|
||||
// ------------
|
||||
#define DYNAMIC_HID_GET_REPORT 0x01
|
||||
#define DYNAMIC_HID_GET_IDLE 0x02
|
||||
#define DYNAMIC_HID_GET_PROTOCOL 0x03
|
||||
#define DYNAMIC_HID_SET_REPORT 0x09
|
||||
#define DYNAMIC_HID_SET_IDLE 0x0A
|
||||
#define DYNAMIC_HID_SET_PROTOCOL 0x0B
|
||||
|
||||
#define DYNAMIC_HID_DESCRIPTOR_TYPE 0x21
|
||||
#define DYNAMIC_HID_REPORT_DESCRIPTOR_TYPE 0x22
|
||||
#define DYNAMIC_HID_PHYSICAL_DESCRIPTOR_TYPE 0x23
|
||||
|
||||
// HID subclass HID1.11 Page 8 4.2 Subclass
|
||||
#define DYNAMIC_HID_SUBCLASS_NONE 0
|
||||
#define DYNAMIC_HID_SUBCLASS_BOOT_INTERFACE 1
|
||||
|
||||
// HID Keyboard/Mouse bios compatible protocols HID1.11 Page 9 4.3 Protocols
|
||||
#define DYNAMIC_HID_PROTOCOL_NONE 0
|
||||
#define DYNAMIC_HID_PROTOCOL_KEYBOARD 1
|
||||
#define DYNAMIC_HID_PROTOCOL_MOUSE 2
|
||||
|
||||
// Normal or bios protocol (Keyboard/Mouse) HID1.11 Page 54 7.2.5 Get_Protocol Request
|
||||
// "protocol" variable is used for this purpose.
|
||||
#define DYNAMIC_HID_BOOT_PROTOCOL 0
|
||||
#define DYNAMIC_HID_REPORT_PROTOCOL 1
|
||||
|
||||
// HID Request Type HID1.11 Page 51 7.2.1 Get_Report Request
|
||||
#define DYNAMIC_HID_REPORT_TYPE_INPUT 1
|
||||
#define DYNAMIC_HID_REPORT_TYPE_OUTPUT 2
|
||||
#define DYNAMIC_HID_REPORT_TYPE_FEATURE 3
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint8_t len; // 9
|
||||
uint8_t dtype; // 0x21
|
||||
uint8_t addr;
|
||||
uint8_t versionL; // 0x101
|
||||
uint8_t versionH; // 0x101
|
||||
uint8_t country;
|
||||
uint8_t desctype; // 0x22 report
|
||||
uint8_t descLenL;
|
||||
uint8_t descLenH;
|
||||
} DYNAMIC_HIDDescDescriptor;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
InterfaceDescriptor hid;
|
||||
DYNAMIC_HIDDescDescriptor desc;
|
||||
EndpointDescriptor in;
|
||||
} DYNAMIC_HIDDescriptor;
|
||||
|
||||
class DynamicHIDSubDescriptor {
|
||||
public:
|
||||
DynamicHIDSubDescriptor *next = NULL;
|
||||
DynamicHIDSubDescriptor(const void *d, const uint16_t l, const bool ipm = true) : data(d), length(l), inProgMem(ipm) { }
|
||||
|
||||
const void* data;
|
||||
const uint16_t length;
|
||||
const bool inProgMem;
|
||||
};
|
||||
|
||||
class DynamicHID_ : public PluggableUSBModule
|
||||
{
|
||||
public:
|
||||
DynamicHID_(void);
|
||||
int begin(void);
|
||||
int SendReport(uint8_t id, const void* data, int len);
|
||||
void AppendDescriptor(DynamicHIDSubDescriptor* node);
|
||||
|
||||
protected:
|
||||
// Implementation of the PluggableUSBModule
|
||||
int getInterface(uint8_t* interfaceCount);
|
||||
int getDescriptor(USBSetup& setup);
|
||||
bool setup(USBSetup& setup);
|
||||
uint8_t getShortName(char* name);
|
||||
|
||||
private:
|
||||
#ifdef _VARIANT_ARDUINO_DUE_X_
|
||||
uint32_t epType[1];
|
||||
#else
|
||||
uint8_t epType[1];
|
||||
#endif
|
||||
|
||||
DynamicHIDSubDescriptor* rootNode;
|
||||
uint16_t descriptorSize;
|
||||
|
||||
uint8_t protocol;
|
||||
uint8_t idle;
|
||||
};
|
||||
|
||||
// Replacement for global singleton.
|
||||
// This function prevents static-initialization-order-fiasco
|
||||
// https://isocpp.org/wiki/faq/ctors#static-init-order-on-first-use
|
||||
DynamicHID_& DynamicHID();
|
||||
|
||||
#define D_HIDREPORT(length) { 9, 0x21, 0x01, 0x01, 0, 1, 0x22, lowByte(length), highByte(length) }
|
||||
|
||||
#endif // USBCON
|
||||
|
||||
#endif // DYNAMIC_HID_h
|
|
@ -0,0 +1,716 @@
|
|||
/*
|
||||
JoystickMH.cpp
|
||||
|
||||
Copyright (c) 2015-2017, Matthew Heironimus
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#include "JoystickMHX.h"
|
||||
|
||||
#if defined(_USING_DYNAMIC_HID)
|
||||
|
||||
#define JOYSTICK_REPORT_ID_INDEX 7
|
||||
#define JOYSTICK_AXIS_MINIMUM -32767
|
||||
#define JOYSTICK_AXIS_MAXIMUM 32767
|
||||
#define JOYSTICK_SIMULATOR_MINIMUM -32767
|
||||
#define JOYSTICK_SIMULATOR_MAXIMUM 32767
|
||||
|
||||
#define JOYSTICK_INCLUDE_X_AXIS B00000001
|
||||
#define JOYSTICK_INCLUDE_Y_AXIS B00000010
|
||||
#define JOYSTICK_INCLUDE_Z_AXIS B00000100
|
||||
#define JOYSTICK_INCLUDE_RX_AXIS B00001000
|
||||
#define JOYSTICK_INCLUDE_RY_AXIS B00010000
|
||||
#define JOYSTICK_INCLUDE_RZ_AXIS B00100000
|
||||
#define JOYSTICK_INCLUDE_S_AXIS B01000000
|
||||
#define JOYSTICK_INCLUDE_D_AXIS B10000000
|
||||
|
||||
#define JOYSTICK_INCLUDE_RUDDER B00000001
|
||||
#define JOYSTICK_INCLUDE_THROTTLE B00000010
|
||||
#define JOYSTICK_INCLUDE_ACCELERATOR B00000100
|
||||
#define JOYSTICK_INCLUDE_BRAKE B00001000
|
||||
#define JOYSTICK_INCLUDE_STEERING B00010000
|
||||
|
||||
Joystick_::Joystick_(
|
||||
uint8_t hidReportId,
|
||||
uint8_t joystickType,
|
||||
uint8_t buttonCount,
|
||||
uint8_t hatSwitchCount,
|
||||
bool includeXAxis,
|
||||
bool includeYAxis,
|
||||
bool includeZAxis,
|
||||
bool includeRxAxis,
|
||||
bool includeRyAxis,
|
||||
bool includeRzAxis,
|
||||
bool includeSlider,
|
||||
bool includeDial,
|
||||
bool includeRudder,
|
||||
bool includeThrottle,
|
||||
bool includeAccelerator,
|
||||
bool includeBrake,
|
||||
bool includeSteering)
|
||||
{
|
||||
// Set the USB HID Report ID
|
||||
_hidReportId = hidReportId;
|
||||
|
||||
// Save Joystick Settings
|
||||
_buttonCount = buttonCount;
|
||||
_hatSwitchCount = hatSwitchCount;
|
||||
_includeAxisFlags = 0;
|
||||
_includeAxisFlags |= (includeXAxis ? JOYSTICK_INCLUDE_X_AXIS : 0);
|
||||
_includeAxisFlags |= (includeYAxis ? JOYSTICK_INCLUDE_Y_AXIS : 0);
|
||||
_includeAxisFlags |= (includeZAxis ? JOYSTICK_INCLUDE_Z_AXIS : 0);
|
||||
_includeAxisFlags |= (includeRxAxis ? JOYSTICK_INCLUDE_RX_AXIS : 0);
|
||||
_includeAxisFlags |= (includeRyAxis ? JOYSTICK_INCLUDE_RY_AXIS : 0);
|
||||
_includeAxisFlags |= (includeRzAxis ? JOYSTICK_INCLUDE_RZ_AXIS : 0);
|
||||
_includeAxisFlags |= (includeSlider ? JOYSTICK_INCLUDE_S_AXIS : 0);
|
||||
_includeAxisFlags |= (includeDial ? JOYSTICK_INCLUDE_D_AXIS : 0);
|
||||
_includeSimulatorFlags = 0;
|
||||
_includeSimulatorFlags |= (includeRudder ? JOYSTICK_INCLUDE_RUDDER : 0);
|
||||
_includeSimulatorFlags |= (includeThrottle ? JOYSTICK_INCLUDE_THROTTLE : 0);
|
||||
_includeSimulatorFlags |= (includeAccelerator ? JOYSTICK_INCLUDE_ACCELERATOR : 0);
|
||||
_includeSimulatorFlags |= (includeBrake ? JOYSTICK_INCLUDE_BRAKE : 0);
|
||||
_includeSimulatorFlags |= (includeSteering ? JOYSTICK_INCLUDE_STEERING : 0);
|
||||
|
||||
// Build Joystick HID Report Description
|
||||
|
||||
// Button Calculations
|
||||
uint8_t buttonsInLastByte = _buttonCount % 8;
|
||||
uint8_t buttonPaddingBits = 0;
|
||||
if (buttonsInLastByte > 0)
|
||||
{
|
||||
buttonPaddingBits = 8 - buttonsInLastByte;
|
||||
}
|
||||
|
||||
// Axis Calculations
|
||||
uint8_t axisCount = (includeXAxis == true)
|
||||
+ (includeYAxis == true)
|
||||
+ (includeZAxis == true)
|
||||
+ (includeRxAxis == true)
|
||||
+ (includeRyAxis == true)
|
||||
+ (includeRzAxis == true)
|
||||
+ (includeSlider == true)
|
||||
+ (includeDial == true);
|
||||
|
||||
uint8_t simulationCount = (includeRudder == true)
|
||||
+ (includeThrottle == true)
|
||||
+ (includeAccelerator == true)
|
||||
+ (includeBrake == true)
|
||||
+ (includeSteering == true);
|
||||
|
||||
uint8_t tempHidReportDescriptor[150];
|
||||
int hidReportDescriptorSize = 0;
|
||||
|
||||
// USAGE_PAGE (Generic Desktop)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x05;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01;
|
||||
|
||||
// USAGE (Joystick - 0x04; Gamepad - 0x05; Multi-axis Controller - 0x08)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = joystickType;
|
||||
|
||||
// COLLECTION (Application)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0xa1;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01;
|
||||
|
||||
// REPORT_ID (Default: 3)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x85;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = _hidReportId;
|
||||
|
||||
if (_buttonCount > 0) {
|
||||
|
||||
// USAGE_PAGE (Button)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x05;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
|
||||
|
||||
// USAGE_MINIMUM (Button 1)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x19;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01;
|
||||
|
||||
// USAGE_MAXIMUM (Button 32)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x29;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = _buttonCount;
|
||||
|
||||
// LOGICAL_MINIMUM (0)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x15;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x00;
|
||||
|
||||
// LOGICAL_MAXIMUM (1)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x25;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01;
|
||||
|
||||
// REPORT_SIZE (1)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x75;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01;
|
||||
|
||||
// REPORT_COUNT (# of buttons)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x95;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = _buttonCount;
|
||||
|
||||
// UNIT_EXPONENT (0)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x55;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x00;
|
||||
|
||||
// UNIT (None)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x65;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x00;
|
||||
|
||||
// INPUT (Data,Var,Abs)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x81;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x02;
|
||||
|
||||
if (buttonPaddingBits > 0) {
|
||||
|
||||
// REPORT_SIZE (1)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x75;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01;
|
||||
|
||||
// REPORT_COUNT (# of padding bits)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x95;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = buttonPaddingBits;
|
||||
|
||||
// INPUT (Const,Var,Abs)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x81;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x03;
|
||||
|
||||
} // Padding Bits Needed
|
||||
|
||||
} // Buttons
|
||||
|
||||
if ((axisCount > 0) || (_hatSwitchCount > 0)) {
|
||||
|
||||
// USAGE_PAGE (Generic Desktop)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x05;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01;
|
||||
|
||||
}
|
||||
|
||||
if (_hatSwitchCount > 0) {
|
||||
|
||||
// USAGE (Hat Switch)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x39;
|
||||
|
||||
// LOGICAL_MINIMUM (0)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x15;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x00;
|
||||
|
||||
// LOGICAL_MAXIMUM (7)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x25;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x07;
|
||||
|
||||
// PHYSICAL_MINIMUM (0)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x35;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x00;
|
||||
|
||||
// PHYSICAL_MAXIMUM (315)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x46;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x3B;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01;
|
||||
|
||||
// UNIT (Eng Rot:Angular Pos)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x65;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x14;
|
||||
|
||||
// REPORT_SIZE (4)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x75;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x04;
|
||||
|
||||
// REPORT_COUNT (1)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x95;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01;
|
||||
|
||||
// INPUT (Data,Var,Abs)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x81;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x02;
|
||||
|
||||
if (_hatSwitchCount > 1) {
|
||||
|
||||
// USAGE (Hat Switch)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x39;
|
||||
|
||||
// LOGICAL_MINIMUM (0)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x15;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x00;
|
||||
|
||||
// LOGICAL_MAXIMUM (7)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x25;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x07;
|
||||
|
||||
// PHYSICAL_MINIMUM (0)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x35;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x00;
|
||||
|
||||
// PHYSICAL_MAXIMUM (315)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x46;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x3B;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01;
|
||||
|
||||
// UNIT (Eng Rot:Angular Pos)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x65;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x14;
|
||||
|
||||
// REPORT_SIZE (4)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x75;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x04;
|
||||
|
||||
// REPORT_COUNT (1)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x95;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01;
|
||||
|
||||
// INPUT (Data,Var,Abs)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x81;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x02;
|
||||
|
||||
} else {
|
||||
|
||||
// Use Padding Bits
|
||||
|
||||
// REPORT_SIZE (1)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x75;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01;
|
||||
|
||||
// REPORT_COUNT (4)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x95;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x04;
|
||||
|
||||
// INPUT (Const,Var,Abs)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x81;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x03;
|
||||
|
||||
} // One or Two Hat Switches?
|
||||
|
||||
} // Hat Switches
|
||||
|
||||
if (axisCount > 0) {
|
||||
|
||||
// USAGE (Pointer)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01;
|
||||
|
||||
// LOGICAL_MINIMUM (-32767)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x16;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x80;
|
||||
|
||||
// LOGICAL_MAXIMUM (+32767)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x26;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0xFF;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x7F;
|
||||
|
||||
// REPORT_SIZE (16)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x75;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x10;
|
||||
|
||||
// REPORT_COUNT (axisCount)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x95;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = axisCount;
|
||||
|
||||
// COLLECTION (Physical)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0xA1;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x00;
|
||||
|
||||
if (includeXAxis == true) {
|
||||
// USAGE (X)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x30;
|
||||
}
|
||||
|
||||
if (includeYAxis == true) {
|
||||
// USAGE (Y)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x31;
|
||||
}
|
||||
|
||||
if (includeZAxis == true) {
|
||||
// USAGE (Z)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x32;
|
||||
}
|
||||
|
||||
if (includeRxAxis == true) {
|
||||
// USAGE (Rx)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x33;
|
||||
}
|
||||
|
||||
if (includeRyAxis == true) {
|
||||
// USAGE (Ry)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x34;
|
||||
}
|
||||
|
||||
if (includeRzAxis == true) {
|
||||
// USAGE (Rz)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x35;
|
||||
}
|
||||
|
||||
if (includeSlider == true) {
|
||||
// USAGE (Rz)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x36;
|
||||
}
|
||||
|
||||
if (includeDial == true) {
|
||||
// USAGE (Rz)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x37;
|
||||
}
|
||||
|
||||
// INPUT (Data,Var,Abs)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x81;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x02;
|
||||
|
||||
// END_COLLECTION (Physical)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0xc0;
|
||||
|
||||
} // X, Y, Z, Rx, Ry, and Rz Axis
|
||||
|
||||
if (simulationCount > 0) {
|
||||
|
||||
// USAGE_PAGE (Simulation Controls)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x05;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x02;
|
||||
|
||||
// LOGICAL_MINIMUM (-32767)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x16;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x80;
|
||||
|
||||
// LOGICAL_MAXIMUM (+32767)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x26;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0xFF;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x7F;
|
||||
|
||||
// REPORT_SIZE (16)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x75;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x10;
|
||||
|
||||
// REPORT_COUNT (simulationCount)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x95;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = simulationCount;
|
||||
|
||||
// COLLECTION (Physical)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0xA1;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x00;
|
||||
|
||||
if (includeRudder == true) {
|
||||
// USAGE (Rudder)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0xBA;
|
||||
}
|
||||
|
||||
if (includeThrottle == true) {
|
||||
// USAGE (Throttle)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0xBB;
|
||||
}
|
||||
|
||||
if (includeAccelerator == true) {
|
||||
// USAGE (Accelerator)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0xC4;
|
||||
}
|
||||
|
||||
if (includeBrake == true) {
|
||||
// USAGE (Brake)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0xC5;
|
||||
}
|
||||
|
||||
if (includeSteering == true) {
|
||||
// USAGE (Steering)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0xC8;
|
||||
}
|
||||
|
||||
// INPUT (Data,Var,Abs)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x81;
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x02;
|
||||
|
||||
// END_COLLECTION (Physical)
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0xc0;
|
||||
|
||||
} // Simulation Controls
|
||||
|
||||
// END_COLLECTION
|
||||
tempHidReportDescriptor[hidReportDescriptorSize++] = 0xc0;
|
||||
|
||||
// Create a copy of the HID Report Descriptor template that is just the right size
|
||||
uint8_t *customHidReportDescriptor = new uint8_t[hidReportDescriptorSize];
|
||||
memcpy(customHidReportDescriptor, tempHidReportDescriptor, hidReportDescriptorSize);
|
||||
|
||||
// Register HID Report Description
|
||||
DynamicHIDSubDescriptor *node = new DynamicHIDSubDescriptor(customHidReportDescriptor, hidReportDescriptorSize, false);
|
||||
DynamicHID().AppendDescriptor(node);
|
||||
|
||||
// Setup Joystick State
|
||||
if (buttonCount > 0) {
|
||||
_buttonValuesArraySize = _buttonCount / 8;
|
||||
if ((_buttonCount % 8) > 0) {
|
||||
_buttonValuesArraySize++;
|
||||
}
|
||||
_buttonValues = new uint8_t[_buttonValuesArraySize];
|
||||
}
|
||||
|
||||
// Calculate HID Report Size
|
||||
_hidReportSize = _buttonValuesArraySize;
|
||||
_hidReportSize += (_hatSwitchCount > 0);
|
||||
_hidReportSize += (axisCount * 2);
|
||||
_hidReportSize += (simulationCount * 2);
|
||||
|
||||
// Initalize Joystick State
|
||||
_xAxis = 0;
|
||||
_yAxis = 0;
|
||||
_zAxis = 0;
|
||||
_xAxisRotation = 0;
|
||||
_yAxisRotation = 0;
|
||||
_zAxisRotation = 0;
|
||||
_slider = 0;
|
||||
_dial = 0;
|
||||
_throttle = 0;
|
||||
_rudder = 0;
|
||||
_accelerator = 0;
|
||||
_brake = 0;
|
||||
_steering = 0;
|
||||
for (int index = 0; index < JOYSTICK_HATSWITCH_COUNT_MAXIMUM; index++)
|
||||
{
|
||||
_hatSwitchValues[index] = JOYSTICK_HATSWITCH_RELEASE;
|
||||
}
|
||||
for (int index = 0; index < _buttonValuesArraySize; index++)
|
||||
{
|
||||
_buttonValues[index] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void Joystick_::begin(bool initAutoSendState)
|
||||
{
|
||||
_autoSendState = initAutoSendState;
|
||||
sendState();
|
||||
}
|
||||
|
||||
void Joystick_::end()
|
||||
{
|
||||
}
|
||||
|
||||
void Joystick_::setButton(uint8_t button, uint8_t value)
|
||||
{
|
||||
if (value == 0)
|
||||
{
|
||||
releaseButton(button);
|
||||
}
|
||||
else
|
||||
{
|
||||
pressButton(button);
|
||||
}
|
||||
}
|
||||
void Joystick_::pressButton(uint8_t button)
|
||||
{
|
||||
if (button >= _buttonCount) return;
|
||||
|
||||
int index = button / 8;
|
||||
int bit = button % 8;
|
||||
|
||||
bitSet(_buttonValues[index], bit);
|
||||
if (_autoSendState) sendState();
|
||||
}
|
||||
void Joystick_::releaseButton(uint8_t button)
|
||||
{
|
||||
if (button >= _buttonCount) return;
|
||||
|
||||
int index = button / 8;
|
||||
int bit = button % 8;
|
||||
|
||||
bitClear(_buttonValues[index], bit);
|
||||
if (_autoSendState) sendState();
|
||||
}
|
||||
|
||||
void Joystick_::setXAxis(int16_t value)
|
||||
{
|
||||
_xAxis = value;
|
||||
if (_autoSendState) sendState();
|
||||
}
|
||||
void Joystick_::setYAxis(int16_t value)
|
||||
{
|
||||
_yAxis = value;
|
||||
if (_autoSendState) sendState();
|
||||
}
|
||||
void Joystick_::setZAxis(int16_t value)
|
||||
{
|
||||
_zAxis = value;
|
||||
if (_autoSendState) sendState();
|
||||
}
|
||||
|
||||
void Joystick_::setRxAxis(int16_t value)
|
||||
{
|
||||
_xAxisRotation = value;
|
||||
if (_autoSendState) sendState();
|
||||
}
|
||||
void Joystick_::setRyAxis(int16_t value)
|
||||
{
|
||||
_yAxisRotation = value;
|
||||
if (_autoSendState) sendState();
|
||||
}
|
||||
void Joystick_::setRzAxis(int16_t value)
|
||||
{
|
||||
_zAxisRotation = value;
|
||||
if (_autoSendState) sendState();
|
||||
}
|
||||
void Joystick_::setSlider(int16_t value)
|
||||
{
|
||||
_slider = value;
|
||||
if (_autoSendState) sendState();
|
||||
}
|
||||
void Joystick_::setDial(int16_t value)
|
||||
{
|
||||
_dial = value;
|
||||
if (_autoSendState) sendState();
|
||||
}
|
||||
|
||||
|
||||
void Joystick_::setRudder(int16_t value)
|
||||
{
|
||||
_rudder = value;
|
||||
if (_autoSendState) sendState();
|
||||
}
|
||||
void Joystick_::setThrottle(int16_t value)
|
||||
{
|
||||
_throttle = value;
|
||||
if (_autoSendState) sendState();
|
||||
}
|
||||
void Joystick_::setAccelerator(int16_t value)
|
||||
{
|
||||
_accelerator = value;
|
||||
if (_autoSendState) sendState();
|
||||
}
|
||||
void Joystick_::setBrake(int16_t value)
|
||||
{
|
||||
_brake = value;
|
||||
if (_autoSendState) sendState();
|
||||
}
|
||||
void Joystick_::setSteering(int16_t value)
|
||||
{
|
||||
_steering = value;
|
||||
if (_autoSendState) sendState();
|
||||
}
|
||||
|
||||
void Joystick_::setHatSwitch(int8_t hatSwitchIndex, int16_t value)
|
||||
{
|
||||
if (hatSwitchIndex >= _hatSwitchCount) return;
|
||||
|
||||
_hatSwitchValues[hatSwitchIndex] = value;
|
||||
if (_autoSendState) sendState();
|
||||
}
|
||||
|
||||
int Joystick_::buildAndSet16BitValue(bool includeValue, int16_t value, int16_t valueMinimum, int16_t valueMaximum, int16_t actualMinimum, int16_t actualMaximum, uint8_t dataLocation[])
|
||||
{
|
||||
int16_t convertedValue;
|
||||
uint8_t highByte;
|
||||
uint8_t lowByte;
|
||||
int16_t realMinimum = min(valueMinimum, valueMaximum);
|
||||
int16_t realMaximum = max(valueMinimum, valueMaximum);
|
||||
|
||||
if (includeValue == false) return 0;
|
||||
|
||||
if (value < realMinimum) {
|
||||
value = realMinimum;
|
||||
}
|
||||
if (value > realMaximum) {
|
||||
value = realMaximum;
|
||||
}
|
||||
|
||||
if (valueMinimum > valueMaximum) {
|
||||
// Values go from a larger number to a smaller number (e.g. 1024 to 0)
|
||||
value = realMaximum - value + realMinimum;
|
||||
}
|
||||
|
||||
convertedValue = map(value, realMinimum, realMaximum, actualMinimum, actualMaximum);
|
||||
|
||||
highByte = (uint8_t)(convertedValue >> 8);
|
||||
lowByte = (uint8_t)(convertedValue & 0x00FF);
|
||||
|
||||
dataLocation[0] = lowByte;
|
||||
dataLocation[1] = highByte;
|
||||
|
||||
return 2;
|
||||
}
|
||||
|
||||
int Joystick_::buildAndSetAxisValue(bool includeAxis, int16_t axisValue, int16_t axisMinimum, int16_t axisMaximum, uint8_t dataLocation[])
|
||||
{
|
||||
return buildAndSet16BitValue(includeAxis, axisValue, axisMinimum, axisMaximum, JOYSTICK_AXIS_MINIMUM, JOYSTICK_AXIS_MAXIMUM, dataLocation);
|
||||
}
|
||||
|
||||
int Joystick_::buildAndSetSimulationValue(bool includeValue, int16_t value, int16_t valueMinimum, int16_t valueMaximum, uint8_t dataLocation[])
|
||||
{
|
||||
return buildAndSet16BitValue(includeValue, value, valueMinimum, valueMaximum, JOYSTICK_SIMULATOR_MINIMUM, JOYSTICK_SIMULATOR_MAXIMUM, dataLocation);
|
||||
}
|
||||
|
||||
void Joystick_::sendState()
|
||||
{
|
||||
uint8_t data[_hidReportSize];
|
||||
int index = 0;
|
||||
|
||||
// Load Button State
|
||||
for (; index < _buttonValuesArraySize; index++)
|
||||
{
|
||||
data[index] = _buttonValues[index];
|
||||
}
|
||||
|
||||
// Set Hat Switch Values
|
||||
if (_hatSwitchCount > 0) {
|
||||
|
||||
// Calculate hat-switch values
|
||||
uint8_t convertedHatSwitch[JOYSTICK_HATSWITCH_COUNT_MAXIMUM];
|
||||
for (int hatSwitchIndex = 0; hatSwitchIndex < JOYSTICK_HATSWITCH_COUNT_MAXIMUM; hatSwitchIndex++)
|
||||
{
|
||||
if (_hatSwitchValues[hatSwitchIndex] < 0)
|
||||
{
|
||||
convertedHatSwitch[hatSwitchIndex] = 8;
|
||||
}
|
||||
else
|
||||
{
|
||||
convertedHatSwitch[hatSwitchIndex] = (_hatSwitchValues[hatSwitchIndex] % 360) / 45;
|
||||
}
|
||||
}
|
||||
|
||||
// Pack hat-switch states into a single byte
|
||||
data[index++] = (convertedHatSwitch[1] << 4) | (B00001111 & convertedHatSwitch[0]);
|
||||
|
||||
} // Hat Switches
|
||||
|
||||
// Set Axis Values
|
||||
index += buildAndSetAxisValue(_includeAxisFlags & JOYSTICK_INCLUDE_X_AXIS, _xAxis, _xAxisMinimum, _xAxisMaximum, &(data[index]));
|
||||
index += buildAndSetAxisValue(_includeAxisFlags & JOYSTICK_INCLUDE_Y_AXIS, _yAxis, _yAxisMinimum, _yAxisMaximum, &(data[index]));
|
||||
index += buildAndSetAxisValue(_includeAxisFlags & JOYSTICK_INCLUDE_Z_AXIS, _zAxis, _zAxisMinimum, _zAxisMaximum, &(data[index]));
|
||||
index += buildAndSetAxisValue(_includeAxisFlags & JOYSTICK_INCLUDE_RX_AXIS, _xAxisRotation, _rxAxisMinimum, _rxAxisMaximum, &(data[index]));
|
||||
index += buildAndSetAxisValue(_includeAxisFlags & JOYSTICK_INCLUDE_RY_AXIS, _yAxisRotation, _ryAxisMinimum, _ryAxisMaximum, &(data[index]));
|
||||
index += buildAndSetAxisValue(_includeAxisFlags & JOYSTICK_INCLUDE_RZ_AXIS, _zAxisRotation, _rzAxisMinimum, _rzAxisMaximum, &(data[index]));
|
||||
index += buildAndSetAxisValue(_includeAxisFlags & JOYSTICK_INCLUDE_S_AXIS, _slider, _sliderMinimum, _sliderMaximum, &(data[index]));
|
||||
index += buildAndSetAxisValue(_includeAxisFlags & JOYSTICK_INCLUDE_D_AXIS, _dial, _dialMinimum, _dialMaximum, &(data[index]));
|
||||
|
||||
|
||||
// Set Simulation Values
|
||||
index += buildAndSetSimulationValue(_includeSimulatorFlags & JOYSTICK_INCLUDE_RUDDER, _rudder, _rudderMinimum, _rudderMaximum, &(data[index]));
|
||||
index += buildAndSetSimulationValue(_includeSimulatorFlags & JOYSTICK_INCLUDE_THROTTLE, _throttle, _throttleMinimum, _throttleMaximum, &(data[index]));
|
||||
index += buildAndSetSimulationValue(_includeSimulatorFlags & JOYSTICK_INCLUDE_ACCELERATOR, _accelerator, _acceleratorMinimum, _acceleratorMaximum, &(data[index]));
|
||||
index += buildAndSetSimulationValue(_includeSimulatorFlags & JOYSTICK_INCLUDE_BRAKE, _brake, _brakeMinimum, _brakeMaximum, &(data[index]));
|
||||
index += buildAndSetSimulationValue(_includeSimulatorFlags & JOYSTICK_INCLUDE_STEERING, _steering, _steeringMinimum, _steeringMaximum, &(data[index]));
|
||||
|
||||
DynamicHID().SendReport(_hidReportId, data, _hidReportSize);
|
||||
}
|
||||
|
||||
#endif
|
|
@ -0,0 +1,241 @@
|
|||
/*
|
||||
JoystickMH.h
|
||||
|
||||
Copyright (c) 2015-2017, Matthew Heironimus
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#ifndef JOYSTICKMHX_h
|
||||
#define JOYSTICKMHX_h
|
||||
|
||||
#include "DynamicHID/DynamicHID.h"
|
||||
|
||||
#if ARDUINO < 10606
|
||||
#error The Joystick library requires Arduino IDE 1.6.6 or greater. Please update your IDE.
|
||||
#endif // ARDUINO < 10606
|
||||
|
||||
#if ARDUINO > 10606
|
||||
#if !defined(USBCON)
|
||||
#error The Joystick library can only be used with a USB MCU (e.g. Arduino Leonardo, Arduino Micro, etc.).
|
||||
#endif // !defined(USBCON)
|
||||
#endif // ARDUINO > 10606
|
||||
|
||||
#if !defined(_USING_DYNAMIC_HID)
|
||||
|
||||
#warning "Using legacy HID core (non pluggable)"
|
||||
|
||||
#else // !defined(_USING_DYNAMIC_HID)
|
||||
|
||||
//================================================================================
|
||||
// Joystick (Gamepad)
|
||||
|
||||
#define JOYSTICK_DEFAULT_REPORT_ID 0x03
|
||||
#define JOYSTICK_DEFAULT_BUTTON_COUNT 32
|
||||
#define JOYSTICK_DEFAULT_AXIS_MINIMUM 0
|
||||
#define JOYSTICK_DEFAULT_AXIS_MAXIMUM 1023
|
||||
#define JOYSTICK_DEFAULT_SIMULATOR_MINIMUM 0
|
||||
#define JOYSTICK_DEFAULT_SIMULATOR_MAXIMUM 1023
|
||||
#define JOYSTICK_DEFAULT_HATSWITCH_COUNT 2
|
||||
#define JOYSTICK_HATSWITCH_COUNT_MAXIMUM 2
|
||||
#define JOYSTICK_HATSWITCH_RELEASE -1
|
||||
#define JOYSTICK_TYPE_JOYSTICK 0x04
|
||||
#define JOYSTICK_TYPE_GAMEPAD 0x05
|
||||
#define JOYSTICK_TYPE_MULTI_AXIS 0x08
|
||||
|
||||
class Joystick_
|
||||
{
|
||||
private:
|
||||
|
||||
// Joystick State
|
||||
int16_t _xAxis;
|
||||
int16_t _yAxis;
|
||||
int16_t _zAxis;
|
||||
int16_t _xAxisRotation;
|
||||
int16_t _yAxisRotation;
|
||||
int16_t _zAxisRotation;
|
||||
int16_t _slider;
|
||||
int16_t _dial;
|
||||
|
||||
int16_t _throttle;
|
||||
int16_t _rudder;
|
||||
int16_t _accelerator;
|
||||
int16_t _brake;
|
||||
int16_t _steering;
|
||||
int16_t _hatSwitchValues[JOYSTICK_HATSWITCH_COUNT_MAXIMUM];
|
||||
uint8_t *_buttonValues = NULL;
|
||||
|
||||
// Joystick Settings
|
||||
bool _autoSendState;
|
||||
uint8_t _buttonCount;
|
||||
uint8_t _buttonValuesArraySize = 0;
|
||||
uint8_t _hatSwitchCount;
|
||||
uint8_t _includeAxisFlags;
|
||||
uint8_t _includeSimulatorFlags;
|
||||
int16_t _xAxisMinimum = JOYSTICK_DEFAULT_AXIS_MINIMUM;
|
||||
int16_t _xAxisMaximum = JOYSTICK_DEFAULT_AXIS_MAXIMUM;
|
||||
int16_t _yAxisMinimum = JOYSTICK_DEFAULT_AXIS_MINIMUM;
|
||||
int16_t _yAxisMaximum = JOYSTICK_DEFAULT_AXIS_MAXIMUM;
|
||||
int16_t _zAxisMinimum = JOYSTICK_DEFAULT_AXIS_MINIMUM;
|
||||
int16_t _zAxisMaximum = JOYSTICK_DEFAULT_AXIS_MAXIMUM;
|
||||
int16_t _rxAxisMinimum = JOYSTICK_DEFAULT_AXIS_MINIMUM;
|
||||
int16_t _rxAxisMaximum = JOYSTICK_DEFAULT_AXIS_MAXIMUM;
|
||||
int16_t _ryAxisMinimum = JOYSTICK_DEFAULT_AXIS_MINIMUM;
|
||||
int16_t _ryAxisMaximum = JOYSTICK_DEFAULT_AXIS_MAXIMUM;
|
||||
int16_t _rzAxisMinimum = JOYSTICK_DEFAULT_AXIS_MINIMUM;
|
||||
int16_t _rzAxisMaximum = JOYSTICK_DEFAULT_AXIS_MAXIMUM;
|
||||
int16_t _sliderMinimum = JOYSTICK_DEFAULT_AXIS_MINIMUM;
|
||||
int16_t _sliderMaximum = JOYSTICK_DEFAULT_AXIS_MAXIMUM;
|
||||
int16_t _dialMinimum = JOYSTICK_DEFAULT_AXIS_MINIMUM;
|
||||
int16_t _dialMaximum = JOYSTICK_DEFAULT_AXIS_MAXIMUM;
|
||||
|
||||
int16_t _rudderMinimum = JOYSTICK_DEFAULT_SIMULATOR_MINIMUM;
|
||||
int16_t _rudderMaximum = JOYSTICK_DEFAULT_SIMULATOR_MAXIMUM;
|
||||
int16_t _throttleMinimum = JOYSTICK_DEFAULT_SIMULATOR_MINIMUM;
|
||||
int16_t _throttleMaximum = JOYSTICK_DEFAULT_SIMULATOR_MAXIMUM;
|
||||
int16_t _acceleratorMinimum = JOYSTICK_DEFAULT_SIMULATOR_MINIMUM;
|
||||
int16_t _acceleratorMaximum = JOYSTICK_DEFAULT_SIMULATOR_MAXIMUM;
|
||||
int16_t _brakeMinimum = JOYSTICK_DEFAULT_SIMULATOR_MINIMUM;
|
||||
int16_t _brakeMaximum = JOYSTICK_DEFAULT_SIMULATOR_MAXIMUM;
|
||||
int16_t _steeringMinimum = JOYSTICK_DEFAULT_SIMULATOR_MINIMUM;
|
||||
int16_t _steeringMaximum = JOYSTICK_DEFAULT_SIMULATOR_MAXIMUM;
|
||||
|
||||
uint8_t _hidReportId;
|
||||
uint8_t _hidReportSize;
|
||||
|
||||
protected:
|
||||
int buildAndSet16BitValue(bool includeValue, int16_t value, int16_t valueMinimum, int16_t valueMaximum, int16_t actualMinimum, int16_t actualMaximum, uint8_t dataLocation[]);
|
||||
int buildAndSetAxisValue(bool includeAxis, int16_t axisValue, int16_t axisMinimum, int16_t axisMaximum, uint8_t dataLocation[]);
|
||||
int buildAndSetSimulationValue(bool includeValue, int16_t value, int16_t valueMinimum, int16_t valueMaximum, uint8_t dataLocation[]);
|
||||
|
||||
public:
|
||||
Joystick_(
|
||||
uint8_t hidReportId = JOYSTICK_DEFAULT_REPORT_ID,
|
||||
uint8_t joystickType = JOYSTICK_TYPE_JOYSTICK,
|
||||
uint8_t buttonCount = JOYSTICK_DEFAULT_BUTTON_COUNT,
|
||||
uint8_t hatSwitchCount = JOYSTICK_DEFAULT_HATSWITCH_COUNT,
|
||||
bool includeXAxis = true,
|
||||
bool includeYAxis = true,
|
||||
bool includeZAxis = true,
|
||||
bool includeRxAxis = true,
|
||||
bool includeRyAxis = true,
|
||||
bool includeRzAxis = true,
|
||||
bool includeSlider = true,
|
||||
bool includeDial = true,
|
||||
bool includeRudder = true,
|
||||
bool includeThrottle = true,
|
||||
bool includeAccelerator = true,
|
||||
bool includeBrake = true,
|
||||
bool includeSteering = true);
|
||||
|
||||
void begin(bool initAutoSendState = true);
|
||||
void end();
|
||||
|
||||
// Set Range Functions
|
||||
inline void setXAxisRange(int16_t minimum, int16_t maximum)
|
||||
{
|
||||
_xAxisMinimum = minimum;
|
||||
_xAxisMaximum = maximum;
|
||||
}
|
||||
inline void setYAxisRange(int16_t minimum, int16_t maximum)
|
||||
{
|
||||
_yAxisMinimum = minimum;
|
||||
_yAxisMaximum = maximum;
|
||||
}
|
||||
inline void setZAxisRange(int16_t minimum, int16_t maximum)
|
||||
{
|
||||
_zAxisMinimum = minimum;
|
||||
_zAxisMaximum = maximum;
|
||||
}
|
||||
inline void setRxAxisRange(int16_t minimum, int16_t maximum)
|
||||
{
|
||||
_rxAxisMinimum = minimum;
|
||||
_rxAxisMaximum = maximum;
|
||||
}
|
||||
inline void setRyAxisRange(int16_t minimum, int16_t maximum)
|
||||
{
|
||||
_ryAxisMinimum = minimum;
|
||||
_ryAxisMaximum = maximum;
|
||||
}
|
||||
inline void setRzAxisRange(int16_t minimum, int16_t maximum)
|
||||
{
|
||||
_rzAxisMinimum = minimum;
|
||||
_rzAxisMaximum = maximum;
|
||||
}
|
||||
inline void setSliderRange(int16_t minimum, int16_t maximum)
|
||||
{
|
||||
_sliderMinimum = minimum;
|
||||
_sliderMaximum = maximum;
|
||||
}
|
||||
inline void setDialRange(int16_t minimum, int16_t maximum)
|
||||
{
|
||||
_dialMinimum = minimum;
|
||||
_dialMaximum = maximum;
|
||||
}
|
||||
|
||||
inline void setRudderRange(int16_t minimum, int16_t maximum)
|
||||
{
|
||||
_rudderMinimum = minimum;
|
||||
_rudderMaximum = maximum;
|
||||
}
|
||||
inline void setThrottleRange(int16_t minimum, int16_t maximum)
|
||||
{
|
||||
_throttleMinimum = minimum;
|
||||
_throttleMaximum = maximum;
|
||||
}
|
||||
inline void setAcceleratorRange(int16_t minimum, int16_t maximum)
|
||||
{
|
||||
_acceleratorMinimum = minimum;
|
||||
_acceleratorMaximum = maximum;
|
||||
}
|
||||
inline void setBrakeRange(int16_t minimum, int16_t maximum)
|
||||
{
|
||||
_brakeMinimum = minimum;
|
||||
_brakeMaximum = maximum;
|
||||
}
|
||||
inline void setSteeringRange(int16_t minimum, int16_t maximum)
|
||||
{
|
||||
_steeringMinimum = minimum;
|
||||
_steeringMaximum = maximum;
|
||||
}
|
||||
|
||||
// Set Axis Values
|
||||
void setXAxis(int16_t value);
|
||||
void setYAxis(int16_t value);
|
||||
void setZAxis(int16_t value);
|
||||
void setRxAxis(int16_t value);
|
||||
void setRyAxis(int16_t value);
|
||||
void setRzAxis(int16_t value);
|
||||
void setSlider(int16_t value);
|
||||
void setDial(int16_t value);
|
||||
|
||||
// Set Simuation Values
|
||||
void setRudder(int16_t value);
|
||||
void setThrottle(int16_t value);
|
||||
void setAccelerator(int16_t value);
|
||||
void setBrake(int16_t value);
|
||||
void setSteering(int16_t value);
|
||||
|
||||
void setButton(uint8_t button, uint8_t value);
|
||||
void pressButton(uint8_t button);
|
||||
void releaseButton(uint8_t button);
|
||||
|
||||
void setHatSwitch(int8_t hatSwitch, int16_t value);
|
||||
|
||||
void sendState();
|
||||
};
|
||||
|
||||
#endif // !defined(_USING_DYNAMIC_HID)
|
||||
#endif // JOYSTICKMHX_h
|
Binary file not shown.
After Width: | Height: | Size: 90 KiB |
|
@ -0,0 +1,13 @@
|
|||
## USB-джойстик из обычной радиоапппаратуры с приемником SBUS на arduino pro micro
|
||||
|
||||

|
||||
|
||||
Сигнал SBUS от приемника подключать на rx, нужен неинвертированный. Если у вас "обычный" инвертированный, вам придется использовать транзистор для инверсии.
|
||||
|
||||

|
||||
|
||||
Я использую "цифровые" транзисторы dtc144eka или bcr533 и не ставлю резистор в базу. В качестве передатчика и приемника sbus я использую qczek, вы можете использовать любой приемник.
|
||||
Его задача - передать значения каналов от аппаратуры до USB регистратора через [arduino](https://docs.arduino.cc/hardware/micro) на 32u4 ([ali](https://aliexpress.ru/item/1005003622414316.html)), которая определяется как hid-usb-joystick. При этом немного вырастает задержка за счет дополнительного
|
||||
процесса по приему и передаче значений, но вы не привязаны к регистратору проводом, что удобно.
|
||||
|
||||
Скетч, доработанная под 10 каналов осей библиотека джойстика (остальные каналы заполняются кнопками), [тут](https://github.com/OpenIPC/sandbox-fpv/tree/master/sbus-to-usb-joystick).
|
Binary file not shown.
After Width: | Height: | Size: 69 KiB |
|
@ -0,0 +1,73 @@
|
|||
#include "sbus.h"
|
||||
#include <JoystickMHX.h>
|
||||
|
||||
//#define SERIAL_DEBUG
|
||||
|
||||
//Joystic settings
|
||||
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_JOYSTICK,
|
||||
32, 2, // Button Count, Hat Switch Count
|
||||
true, true, true, // X and Y, Z Axis
|
||||
true, true, true, // Rx, Ry, Rz
|
||||
true, true, true, true, // slider, dial, rudder and throttle
|
||||
false, false, false); // accelerator, brake, and steering
|
||||
|
||||
bfs::SbusRx sbus_rx(&Serial1);
|
||||
bfs::SbusData data;
|
||||
|
||||
void setup() {
|
||||
#ifdef SERIAL_DEBUG
|
||||
Serial.begin(115200);
|
||||
while (!Serial) {}
|
||||
Serial.println("Started");
|
||||
#else
|
||||
Joystick.begin();
|
||||
//range
|
||||
Joystick.setXAxisRange(0, 2000);
|
||||
Joystick.setYAxisRange(0, 2000);
|
||||
Joystick.setZAxisRange(0, 2000);
|
||||
Joystick.setRxAxisRange(0, 2000);
|
||||
Joystick.setRyAxisRange(0, 2000);
|
||||
Joystick.setRzAxisRange(0, 2000);
|
||||
Joystick.setSliderRange(0, 2000);
|
||||
Joystick.setDialRange(0, 2000);
|
||||
Joystick.setRudderRange(0, 2000);
|
||||
Joystick.setThrottleRange(0, 2000);
|
||||
//default
|
||||
//Joystick.setRudder(1500);
|
||||
//Joystick.setAccelerator(0);
|
||||
//Joystick.setBrake(1500);
|
||||
//Joystick.setSteering(1500);
|
||||
#endif
|
||||
sbus_rx.Begin();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (sbus_rx.Read()) {
|
||||
data = sbus_rx.data();
|
||||
|
||||
#ifdef SERIAL_DEBUG
|
||||
for (int8_t i = 0; i < data.NUM_CH; i++) {
|
||||
Serial.print(data.ch[i]);
|
||||
Serial.print("\t");
|
||||
}
|
||||
Serial.println();
|
||||
#else
|
||||
Joystick.setXAxis (data.ch[0]);
|
||||
Joystick.setYAxis (data.ch[1]);
|
||||
Joystick.setZAxis (data.ch[2]);
|
||||
Joystick.setRxAxis (data.ch[3]);
|
||||
Joystick.setRyAxis (data.ch[4]);
|
||||
Joystick.setRzAxis (data.ch[5]);
|
||||
Joystick.setSlider (data.ch[6]);
|
||||
Joystick.setRudder (data.ch[7]);
|
||||
Joystick.setThrottle (data.ch[8]);
|
||||
Joystick.setDial (data.ch[9]);
|
||||
if(data.ch[10] > 1490) Joystick.setButton(0, 255); else Joystick.setButton(0, 0);
|
||||
if(data.ch[11] > 1490) Joystick.setButton(1, 255); else Joystick.setButton(1, 0);
|
||||
if(data.ch[12] > 1490) Joystick.setButton(2, 255); else Joystick.setButton(2, 0);
|
||||
if(data.ch[13] > 1490) Joystick.setButton(3, 255); else Joystick.setButton(3, 0);
|
||||
if(data.ch[14] > 1490) Joystick.setButton(4, 255); else Joystick.setButton(4, 0);
|
||||
Joystick.sendState();
|
||||
#endif
|
||||
} //sbus_rx.Read
|
||||
}
|
|
@ -0,0 +1,291 @@
|
|||
/*
|
||||
* Brian R Taylor
|
||||
* brian.taylor@bolderflight.com
|
||||
*
|
||||
* Copyright (c) 2022 Bolder Flight Systems Inc
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the “Software”), to
|
||||
* deal in the Software without restriction, including without limitation the
|
||||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
* IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "sbus.h" // NOLINT
|
||||
#if defined(ARDUINO)
|
||||
#include <Arduino.h>
|
||||
#else
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include "core/core.h"
|
||||
#endif
|
||||
|
||||
namespace bfs {
|
||||
|
||||
void SbusRx::Begin() {
|
||||
if (fast_) {
|
||||
baud_ = 200000;
|
||||
} else {
|
||||
baud_ = 100000;
|
||||
}
|
||||
/* Start the bus */
|
||||
/* Teensy 3.0 || Teensy 3.1/3.2 */
|
||||
#if defined(__MK20DX128__) || defined(__MK20DX256__)
|
||||
if (inv_) {
|
||||
uart_->begin(baud_, SERIAL_8E1_RXINV_TXINV);
|
||||
} else {
|
||||
uart_->begin(baud_, SERIAL_8E1);
|
||||
}
|
||||
/*
|
||||
* Teensy 3.5 || Teensy 3.6 ||
|
||||
* Teensy LC || Teensy 4.0/4.1 ||
|
||||
* Teensy 4.0 Beta
|
||||
*/
|
||||
#elif defined(__MK64FX512__) || defined(__MK66FX1M0__) || \
|
||||
defined(__MKL26Z64__) || defined(__IMXRT1062__) || \
|
||||
defined(__IMXRT1052__)
|
||||
if (inv_){
|
||||
uart_->begin(baud_, SERIAL_8E2_RXINV_TXINV);
|
||||
} else {
|
||||
uart_->begin(baud_, SERIAL_8E2);
|
||||
}
|
||||
/* STM32L4 */
|
||||
#elif defined(STM32L496xx) || defined(STM32L476xx) || \
|
||||
defined(STM32L433xx) || defined(STM32L432xx)
|
||||
if (inv_) {
|
||||
uart_->begin(baud_, SERIAL_8E2 | 0xC000ul);
|
||||
} else {
|
||||
uart_->begin(baud_, SERIAL_8E2);
|
||||
}
|
||||
/* ESP32 */
|
||||
#elif defined(ESP32)
|
||||
uart_->begin(baud_, SERIAL_8E2, rxpin_, txpin_, inv_);
|
||||
/* Everything else, with a hardware inverter */
|
||||
#else
|
||||
uart_->begin(baud_, SERIAL_8E2);
|
||||
#endif
|
||||
/* flush the bus */
|
||||
uart_->flush();
|
||||
}
|
||||
|
||||
bool SbusRx::Read() {
|
||||
/* Read through all available packets to get the newest */
|
||||
new_data_ = false;
|
||||
do {
|
||||
if (Parse()) {
|
||||
new_data_ = true;
|
||||
}
|
||||
} while (uart_->available());
|
||||
return new_data_;
|
||||
}
|
||||
|
||||
bool SbusRx::Parse() {
|
||||
/* Parse messages */
|
||||
while (uart_->available()) {
|
||||
cur_byte_ = uart_->read();
|
||||
if (state_ == 0) {
|
||||
if ((cur_byte_ == HEADER_) && ((prev_byte_ == FOOTER_) ||
|
||||
((prev_byte_ & 0x0F) == FOOTER2_))) {
|
||||
buf_[state_++] = cur_byte_;
|
||||
} else {
|
||||
state_ = 0;
|
||||
}
|
||||
} else if (state_ < PAYLOAD_LEN_ + HEADER_LEN_) {
|
||||
buf_[state_++] = cur_byte_;
|
||||
} else if (state_ < PAYLOAD_LEN_ + HEADER_LEN_ + FOOTER_LEN_) {
|
||||
state_ = 0;
|
||||
prev_byte_ = cur_byte_;
|
||||
if ((cur_byte_ == FOOTER_) || ((cur_byte_ & 0x0F) == FOOTER2_)) {
|
||||
/* Grab the channel data */
|
||||
data_.ch[0] = static_cast<int16_t>(buf_[1] |
|
||||
((buf_[2] << 8) & 0x07FF));
|
||||
data_.ch[1] = static_cast<int16_t>((buf_[2] >> 3) |
|
||||
((buf_[3] << 5) & 0x07FF));
|
||||
data_.ch[2] = static_cast<int16_t>((buf_[3] >> 6) |
|
||||
(buf_[4] << 2) |
|
||||
((buf_[5] << 10) & 0x07FF));
|
||||
data_.ch[3] = static_cast<int16_t>((buf_[5] >> 1) |
|
||||
((buf_[6] << 7) & 0x07FF));
|
||||
data_.ch[4] = static_cast<int16_t>((buf_[6] >> 4) |
|
||||
((buf_[7] << 4) & 0x07FF));
|
||||
data_.ch[5] = static_cast<int16_t>((buf_[7] >> 7) |
|
||||
(buf_[8] << 1) |
|
||||
((buf_[9] << 9) & 0x07FF));
|
||||
data_.ch[6] = static_cast<int16_t>((buf_[9] >> 2) |
|
||||
((buf_[10] << 6) & 0x07FF));
|
||||
data_.ch[7] = static_cast<int16_t>((buf_[10] >> 5) |
|
||||
((buf_[11] << 3) & 0x07FF));
|
||||
data_.ch[8] = static_cast<int16_t>(buf_[12] |
|
||||
((buf_[13] << 8) & 0x07FF));
|
||||
data_.ch[9] = static_cast<int16_t>((buf_[13] >> 3) |
|
||||
((buf_[14] << 5) & 0x07FF));
|
||||
data_.ch[10] = static_cast<int16_t>((buf_[14] >> 6) |
|
||||
(buf_[15] << 2) |
|
||||
((buf_[16] << 10) & 0x07FF));
|
||||
data_.ch[11] = static_cast<int16_t>((buf_[16] >> 1) |
|
||||
((buf_[17] << 7) & 0x07FF));
|
||||
data_.ch[12] = static_cast<int16_t>((buf_[17] >> 4) |
|
||||
((buf_[18] << 4) & 0x07FF));
|
||||
data_.ch[13] = static_cast<int16_t>((buf_[18] >> 7) |
|
||||
(buf_[19] << 1) |
|
||||
((buf_[20] << 9) & 0x07FF));
|
||||
data_.ch[14] = static_cast<int16_t>((buf_[20] >> 2) |
|
||||
((buf_[21] << 6) & 0x07FF));
|
||||
data_.ch[15] = static_cast<int16_t>((buf_[21] >> 5) |
|
||||
((buf_[22] << 3) & 0x07FF));
|
||||
/* CH 17 */
|
||||
data_.ch17 = buf_[23] & CH17_MASK_;
|
||||
/* CH 18 */
|
||||
data_.ch18 = buf_[23] & CH18_MASK_;
|
||||
/* Grab the lost frame */
|
||||
data_.lost_frame = buf_[23] & LOST_FRAME_MASK_;
|
||||
/* Grab the failsafe */
|
||||
data_.failsafe = buf_[23] & FAILSAFE_MASK_;
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
state_ = 0;
|
||||
}
|
||||
prev_byte_ = cur_byte_;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Needed for emulating two stop bytes on Teensy 3.0 and 3.1/3.2 */
|
||||
#if defined(__MK20DX128__) || defined(__MK20DX256__)
|
||||
namespace {
|
||||
IntervalTimer serial_timer;
|
||||
HardwareSerial *sbus_bus;
|
||||
uint8_t sbus_packet[25];
|
||||
volatile int send_index = 0;
|
||||
void SendByte() {
|
||||
if (send_index < 25) {
|
||||
sbus_bus->write(sbus_packet[send_index]);
|
||||
send_index++;
|
||||
} else {
|
||||
serial_timer.end();
|
||||
send_index = 0;
|
||||
}
|
||||
}
|
||||
} // namespace
|
||||
#endif
|
||||
|
||||
void SbusTx::Begin() {
|
||||
if (fast_) {
|
||||
baud_ = 200000;
|
||||
} else {
|
||||
baud_ = 100000;
|
||||
}
|
||||
/* Start the bus */
|
||||
/* Teensy 3.0 || Teensy 3.1/3.2 */
|
||||
#if defined(__MK20DX128__) || defined(__MK20DX256__)
|
||||
sbus_bus = uart_;
|
||||
if (inv_) {
|
||||
uart_->begin(baud_, SERIAL_8E1_RXINV_TXINV);
|
||||
} else {
|
||||
uart_->begin(baud_, SERIAL_8E1);
|
||||
}
|
||||
/*
|
||||
* Teensy 3.5 || Teensy 3.6 ||
|
||||
* Teensy LC || Teensy 4.0/4.1 ||
|
||||
* Teensy 4.0 Beta
|
||||
*/
|
||||
#elif defined(__MK64FX512__) || defined(__MK66FX1M0__) || \
|
||||
defined(__MKL26Z64__) || defined(__IMXRT1062__) || \
|
||||
defined(__IMXRT1052__)
|
||||
if (inv_){
|
||||
uart_->begin(baud_, SERIAL_8E2_RXINV_TXINV);
|
||||
} else {
|
||||
uart_->begin(baud_, SERIAL_8E2);
|
||||
}
|
||||
/* STM32L4 */
|
||||
#elif defined(STM32L496xx) || defined(STM32L476xx) || \
|
||||
defined(STM32L433xx) || defined(STM32L432xx)
|
||||
if (inv_) {
|
||||
uart_->begin(baud_, SERIAL_8E2 | 0xC000ul);
|
||||
} else {
|
||||
uart_->begin(baud_, SERIAL_8E2);
|
||||
}
|
||||
/* ESP32 */
|
||||
#elif defined(ESP32)
|
||||
uart_->begin(baud_, SERIAL_8E2, rxpin_, txpin_, inv_);
|
||||
/* Everything else, with a hardware inverter */
|
||||
#else
|
||||
uart_->begin(baud_, SERIAL_8E2);
|
||||
#endif
|
||||
}
|
||||
|
||||
void SbusTx::Write() {
|
||||
/* Assemble packet */
|
||||
buf_[0] = HEADER_;
|
||||
buf_[1] = static_cast<uint8_t>((data_.ch[0] & 0x07FF));
|
||||
buf_[2] = static_cast<uint8_t>((data_.ch[0] & 0x07FF) >> 8 |
|
||||
(data_.ch[1] & 0x07FF) << 3);
|
||||
buf_[3] = static_cast<uint8_t>((data_.ch[1] & 0x07FF) >> 5 |
|
||||
(data_.ch[2] & 0x07FF) << 6);
|
||||
buf_[4] = static_cast<uint8_t>((data_.ch[2] & 0x07FF) >> 2);
|
||||
buf_[5] = static_cast<uint8_t>((data_.ch[2] & 0x07FF) >> 10 |
|
||||
(data_.ch[3] & 0x07FF) << 1);
|
||||
buf_[6] = static_cast<uint8_t>((data_.ch[3] & 0x07FF) >> 7 |
|
||||
(data_.ch[4] & 0x07FF) << 4);
|
||||
buf_[7] = static_cast<uint8_t>((data_.ch[4] & 0x07FF) >> 4 |
|
||||
(data_.ch[5] & 0x07FF) << 7);
|
||||
buf_[8] = static_cast<uint8_t>((data_.ch[5] & 0x07FF) >> 1);
|
||||
buf_[9] = static_cast<uint8_t>((data_.ch[5] & 0x07FF) >> 9 |
|
||||
(data_.ch[6] & 0x07FF) << 2);
|
||||
buf_[10] = static_cast<uint8_t>((data_.ch[6] & 0x07FF) >> 6 |
|
||||
(data_.ch[7] & 0x07FF) << 5);
|
||||
buf_[11] = static_cast<uint8_t>((data_.ch[7] & 0x07FF) >> 3);
|
||||
buf_[12] = static_cast<uint8_t>((data_.ch[8] & 0x07FF));
|
||||
buf_[13] = static_cast<uint8_t>((data_.ch[8] & 0x07FF) >> 8 |
|
||||
(data_.ch[9] & 0x07FF) << 3);
|
||||
buf_[14] = static_cast<uint8_t>((data_.ch[9] & 0x07FF) >> 5 |
|
||||
(data_.ch[10] & 0x07FF) << 6);
|
||||
buf_[15] = static_cast<uint8_t>((data_.ch[10] & 0x07FF) >> 2);
|
||||
buf_[16] = static_cast<uint8_t>((data_.ch[10] & 0x07FF) >> 10 |
|
||||
(data_.ch[11] & 0x07FF) << 1);
|
||||
buf_[17] = static_cast<uint8_t>((data_.ch[11] & 0x07FF) >> 7 |
|
||||
(data_.ch[12] & 0x07FF) << 4);
|
||||
buf_[18] = static_cast<uint8_t>((data_.ch[12] & 0x07FF) >> 4 |
|
||||
(data_.ch[13] & 0x07FF) << 7);
|
||||
buf_[19] = static_cast<uint8_t>((data_.ch[13] & 0x07FF) >> 1);
|
||||
buf_[20] = static_cast<uint8_t>((data_.ch[13] & 0x07FF) >> 9 |
|
||||
(data_.ch[14] & 0x07FF) << 2);
|
||||
buf_[21] = static_cast<uint8_t>((data_.ch[14] & 0x07FF) >> 6 |
|
||||
(data_.ch[15] & 0x07FF) << 5);
|
||||
buf_[22] = static_cast<uint8_t>((data_.ch[15] & 0x07FF) >> 3);
|
||||
buf_[23] = 0x00 | (data_.ch17 * CH17_MASK_) | (data_.ch18 * CH18_MASK_) |
|
||||
(data_.failsafe * FAILSAFE_MASK_) |
|
||||
(data_.lost_frame * LOST_FRAME_MASK_);
|
||||
buf_[24] = FOOTER_;
|
||||
/* Send packet to servos */
|
||||
#if defined(__MK20DX128__) || defined(__MK20DX256__)
|
||||
/*
|
||||
* Use ISR to send byte at a time,
|
||||
* 130 us between bytes to emulate 2 stop bits
|
||||
*/
|
||||
__disable_irq();
|
||||
memcpy(sbus_packet, buf_, sizeof(buf_));
|
||||
__enable_irq();
|
||||
serial_timer.priority(255);
|
||||
serial_timer.begin(SendByte, 130);
|
||||
#else
|
||||
uart_->write(buf_, sizeof(buf_));
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace bfs
|
|
@ -0,0 +1,149 @@
|
|||
/*
|
||||
* Brian R Taylor
|
||||
* brian.taylor@bolderflight.com
|
||||
*
|
||||
* Copyright (c) 2022 Bolder Flight Systems Inc
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the “Software”), to
|
||||
* deal in the Software without restriction, including without limitation the
|
||||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
* IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef SRC_SBUS_H_
|
||||
#define SRC_SBUS_H_
|
||||
|
||||
#if defined(ARDUINO)
|
||||
#include <Arduino.h>
|
||||
#else
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include "core/core.h"
|
||||
#endif
|
||||
|
||||
namespace bfs {
|
||||
|
||||
struct SbusData {
|
||||
bool lost_frame;
|
||||
bool failsafe;
|
||||
bool ch17, ch18;
|
||||
static constexpr int8_t NUM_CH = 16;
|
||||
int16_t ch[NUM_CH];
|
||||
};
|
||||
|
||||
class SbusRx {
|
||||
public:
|
||||
#if defined(ESP32)
|
||||
SbusRx(HardwareSerial *bus, const int8_t rxpin, const int8_t txpin,
|
||||
const bool inv) : uart_(bus), inv_(inv), rxpin_(rxpin), txpin_(txpin)
|
||||
{}
|
||||
SbusRx(HardwareSerial *bus, const int8_t rxpin, const int8_t txpin,
|
||||
const bool inv, const bool fast) : uart_(bus), inv_(inv), fast_(fast),
|
||||
rxpin_(rxpin), txpin_(txpin) {}
|
||||
#else
|
||||
explicit SbusRx(HardwareSerial *bus) : uart_(bus) {}
|
||||
SbusRx(HardwareSerial *bus, const bool inv) : uart_(bus), inv_(inv) {}
|
||||
SbusRx(HardwareSerial *bus, const bool inv, const bool fast) : uart_(bus),
|
||||
inv_(inv),
|
||||
fast_(fast) {}
|
||||
#endif
|
||||
void Begin();
|
||||
bool Read();
|
||||
inline SbusData data() const {return data_;}
|
||||
|
||||
private:
|
||||
/* Communication */
|
||||
HardwareSerial *uart_;
|
||||
bool inv_ = true;
|
||||
bool fast_ = false;
|
||||
#if defined(ESP32)
|
||||
int8_t rxpin_, txpin_;
|
||||
#endif
|
||||
int32_t baud_ = 100000;
|
||||
/* Message len */
|
||||
static constexpr int8_t PAYLOAD_LEN_ = 23;
|
||||
static constexpr int8_t HEADER_LEN_ = 1;
|
||||
static constexpr int8_t FOOTER_LEN_ = 1;
|
||||
/* SBUS message defs */
|
||||
static constexpr int8_t NUM_SBUS_CH_ = 16;
|
||||
static constexpr uint8_t HEADER_ = 0x0F;
|
||||
static constexpr uint8_t FOOTER_ = 0x00;
|
||||
static constexpr uint8_t FOOTER2_ = 0x04;
|
||||
static constexpr uint8_t CH17_MASK_ = 0x01;
|
||||
static constexpr uint8_t CH18_MASK_ = 0x02;
|
||||
static constexpr uint8_t LOST_FRAME_MASK_ = 0x04;
|
||||
static constexpr uint8_t FAILSAFE_MASK_ = 0x08;
|
||||
/* Parsing state tracking */
|
||||
int8_t state_ = 0;
|
||||
uint8_t prev_byte_ = FOOTER_;
|
||||
uint8_t cur_byte_;
|
||||
/* Buffer for storing messages */
|
||||
uint8_t buf_[25];
|
||||
/* Data */
|
||||
bool new_data_;
|
||||
SbusData data_;
|
||||
bool Parse();
|
||||
};
|
||||
|
||||
class SbusTx {
|
||||
public:
|
||||
#if defined(ESP32)
|
||||
SbusTx(HardwareSerial *bus, const int8_t rxpin, const int8_t txpin,
|
||||
const bool inv) : uart_(bus), inv_(inv), rxpin_(rxpin), txpin_(txpin)
|
||||
{}
|
||||
SbusTx(HardwareSerial *bus, const int8_t rxpin, const int8_t txpin,
|
||||
const bool inv, const bool fast) : uart_(bus), inv_(inv), fast_(fast),
|
||||
rxpin_(rxpin), txpin_(txpin) {}
|
||||
#else
|
||||
explicit SbusTx(HardwareSerial *bus) : uart_(bus) {}
|
||||
SbusTx(HardwareSerial *bus, const bool inv) : uart_(bus), inv_(inv) {}
|
||||
SbusTx(HardwareSerial *bus, const bool inv, const bool fast) : uart_(bus),
|
||||
inv_(inv),
|
||||
fast_(fast) {}
|
||||
#endif
|
||||
void Begin();
|
||||
void Write();
|
||||
inline void data(const SbusData &data) {data_ = data;}
|
||||
inline SbusData data() const {return data_;}
|
||||
|
||||
private:
|
||||
/* Communication */
|
||||
HardwareSerial *uart_;
|
||||
bool inv_ = true;
|
||||
bool fast_ = false;
|
||||
#if defined(ESP32)
|
||||
int8_t rxpin_, txpin_;
|
||||
#endif
|
||||
int32_t baud_ = 100000;
|
||||
/* Message len */
|
||||
static constexpr int8_t BUF_LEN_ = 25;
|
||||
/* SBUS message defs */
|
||||
static constexpr int8_t NUM_SBUS_CH_ = 16;
|
||||
static constexpr uint8_t HEADER_ = 0x0F;
|
||||
static constexpr uint8_t FOOTER_ = 0x00;
|
||||
static constexpr uint8_t FOOTER2_ = 0x04;
|
||||
static constexpr uint8_t CH17_MASK_ = 0x01;
|
||||
static constexpr uint8_t CH18_MASK_ = 0x02;
|
||||
static constexpr uint8_t LOST_FRAME_MASK_ = 0x04;
|
||||
static constexpr uint8_t FAILSAFE_MASK_ = 0x08;
|
||||
/* Data */
|
||||
uint8_t buf_[BUF_LEN_];
|
||||
SbusData data_;
|
||||
};
|
||||
|
||||
} // namespace bfs
|
||||
|
||||
#endif // SRC_SBUS_H_
|
|
@ -0,0 +1,13 @@
|
|||
## USB joystick from regular radio equipment with SBUS receiver on arduino pro micro
|
||||
|
||||

|
||||
|
||||
Connect the SBUS signal from the receiver to rx, you need a non-inverted one. If you have a "regular" inverted one, you will have to use a transistor for inversion.
|
||||
|
||||

|
||||
|
||||
I use "digital" transistors dtc144eka or bcr533 and do not put a resistor in the base. As a transmitter and receiver sbus I use qczek, you can use any receiver.
|
||||
Its task is to transmit channel values from the equipment to the USB recorder via [arduino](https://docs.arduino.cc/hardware/micro) to 32u4 ([ali](https://aliexpress.ru/item/1005003622414316.html)), which is defined as hid-usb-joystick. At the same time, the delay increases slightly due to the additional
|
||||
process of receiving and transmitting values, but you are not tied to the recorder by a wire, which is convenient.
|
||||
|
||||
Sketch, a joystick library modified for 10 axis channels (the remaining channels are filled with buttons), [here](https://github.com/OpenIPC/sandbox-fpv/tree/master/sbus-to-usb-joystick).
|
Loading…
Reference in New Issue