Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add USB Joystick / Game Controller library to Arduino Leonardo and Arduino Micro #2843

Closed
wants to merge 10 commits into from

Conversation

MHeironimus
Copy link

The changes in this pull request add a Joystick library to the Arduino Leonardo and Arduino Micro. I attempted to model the methods in this library after the existing Mouse and Keyboard library. These code changes cause the Arduino Leonardo and Arduino Micro to appear as a generic Game Controller (as well as a generic keyboard and mouse) to the host computer.

The generic Game Controller provides the following:

  • X, Y, and Z axis
  • 32 buttons
  • X, Y, and Z axis rotation
  • Rudder
  • Throttle
  • 2 Point of View Hat Switches

The following is a summary of the methods provided by the Joystick library.

Joystick.begin(bool AutoSendState)

Starts emulating a game controller connected to a computer. By default all methods update the game controller state immediately. If AutoSendState 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.

Joystick.setXAxis(byte value)

Sets the X axis value. Range -127 to 127 (0 is center).

Joystick.setYAxis(byte value)

Sets the Y axis value. Range -127 to 127 (0 is center).

Joystick.setZAxis(byte value)

Sets the Z axis value. Range -127 to 127 (0 is center).

Joystick.setXAxisRotation(int value)

Sets the X axis rotation value. Range 0° to 360°.

Joystick.setyAxisRotation(int value)

Sets the Y axis rotation value. Range 0° to 360°.

Joystick.setZAxisRotation(int value)

Sets the Z axis rotation value. Range 0° to 360°.

Joystick.setButton(byte button, byte value)

Sets the state of the specified button. 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(byte button)

Press the indicated button. The button is the 0-based button number (i.e. button '1' is 0, button '2' is 1, etc.).

Joystick.releaseButton(byte button)

Release the indicated button. The button is the 0-based button number (i.e. button '1' is 0, button '2' is 1, etc.).

Joystick.setThrottle(byte value)

Sets the throttle value. Range 0 to 255.

Joystick.setRudder(byte value)

Sets the rudder value. Range 0 to 255.

Joystick.setHatSwitch(byte hatSwitch, int 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.).

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).

Please feel free to incorporate these additions into a future release of the Arduio software. Let me know if there is anything you think should be changed in this library.

Added a USB joystick to the Arduino Leonardo & Micro. Currently class is
very simple, but I plan on enhancing it in future versions.
Added button, throttle, rudder, x, y, and z axis methods. Still need to
do x, y, and z axis rotation and hat values.
Added the setHatSwitch function.
Added X, Y, and Z axis rotation methods.
Added a setButton method to make it easier to set the button state in
some situations.
Added the ability to automatically update the state of the joystick
whenever anything changes. This is the new default mode.
@q2dg
Copy link

q2dg commented Apr 2, 2015

+1 !!!!

@NicoHood
Copy link
Contributor

NicoHood commented Apr 2, 2015

I guess this will add Problems on some OS. See this issue:
NicoHood/HID#14

If you want a better USB-Core use the HID Project I suggest. Feel free to help me.
https://github.com/NicoHood/HID

Made a minor change to the layout of the Joystick HID report to simplify
the X, Y, and Z axis values.
@cmaglie cmaglie added feature request A request to make an enhancement (not a bug fix) Architecture: AVR Applies only to the AVR microcontrollers (Uno, etc.) Component: USB Device Opposed to USB Host. Related to the USB subsystem (SerialUSB, HID, ...) labels Apr 15, 2015
Update to match Version 1.6.5.
@facchinm
Copy link
Member

Closed with #1803

@facchinm facchinm closed this Sep 21, 2015
@ffissore ffissore added this to the Release 1.6.6 milestone Sep 23, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Architecture: AVR Applies only to the AVR microcontrollers (Uno, etc.) Component: USB Device Opposed to USB Host. Related to the USB subsystem (SerialUSB, HID, ...) feature request A request to make an enhancement (not a bug fix)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants