Author Topic: Working Joystick Library!!!!  (Read 16859 times)

duckythescientist

  • Newbie
  • *
  • Posts: 27
Working Joystick Library!!!!
« on: December 26, 2012, 04:55:37 pm »
Hey all! I modified the DigiKeyboard library to work as a USB joystick. It isn't the prettiest code, but neither was the keyboard library. There may be mistakes, but I have confirmed success with it. See the example file for usage documentation.

The Device is 6 analog axis and 16 digital buttons.

The library should also be easily incorporated into a non-Digispark tiny85 project.

Here is the code (currently a dropbox link until I find better hosting): https://dl.dropbox.com/u/10989648/DigisparkJoystick.zip
« Last Edit: December 26, 2012, 04:55:37 pm by duckythescientist »

digistump

  • Administrator
  • Hero Member
  • *****
  • Posts: 1465
Working Joystick Library!!!!
« Reply #1 on: December 26, 2012, 05:18:40 pm »
Awesome - can I add it to the Digispark Ready Arduino IDE package?

duckythescientist

  • Newbie
  • *
  • Posts: 27
Working Joystick Library!!!!
« Reply #2 on: December 26, 2012, 06:05:31 pm »
Definitely! I\'d love it if you checked it out for yourself first though just to make sure it works.

I tried to commit it with Github, but I have never used it before.

digistump

  • Administrator
  • Hero Member
  • *****
  • Posts: 1465
Working Joystick Library!!!!
« Reply #3 on: December 26, 2012, 06:10:57 pm »
I\'ll give it a try later tonight (packing orders right now) - and if it works for me I\'ll add it and put it in the github repo - thanks for the very first contribution to the libraries!

Bluebie

  • Sr. Member
  • ****
  • Posts: 486
Working Joystick Library!!!!
« Reply #4 on: December 27, 2012, 04:40:35 pm »
You have just made me the happiest girl in the world! Thank you so much! <3

Bluebie

  • Sr. Member
  • ****
  • Posts: 486
Working Joystick Library!!!!
« Reply #5 on: December 27, 2012, 05:23:10 pm »
I\'d love to see an Xinput gamepad variant of this, specifically emulating the Xbox 360 controller. This controller has rapidly become an industry standard and is very well supported in games on Windows and Mac, with emulation layers available to adapt other sorts of gamepads to behave as Xinput devices. It has 15 buttons (one dpad, 4 regular buttons, back, system, start buttons, two shoulder buttons, and \'click in\' joystick buttons and 10 axis (two 2d sticks, two shoulder triggers)

An Xinput gamepad library would make it exceptionally easy to plug digisparks in to any modern game on any pc platform, and also on Xbox 360.

One possible hitch is the Microsoft Xbox 360 controller includes an audio jack (headset) and is a high speed usb device. Probably some of the other brand controllers don\'t have the audio feature and I\'d be surprised if PC games complained about the lack of audio interface. Xbox systems maybe a bit more picky.

duckythescientist

  • Newbie
  • *
  • Posts: 27
Working Joystick Library!!!!
« Reply #6 on: December 27, 2012, 06:15:17 pm »
I will definitely be looking into that. So far, I\'ve been mainly porting other code projects, but I have also started looking into HID report descriptors and how to make them. I\'ll maybe get a Windows VM setup and start sniffing USB traffic to see exactly what all messages are being passed. Right now, I\'m working on getting Raphaël Assénat\'s full Gamecube to USB code working on this. I think I\'m only the hardware away.

Bluebie

  • Sr. Member
  • ****
  • Posts: 486
Working Joystick Library!!!!
« Reply #7 on: December 28, 2012, 06:14:18 am »
Awesome stuff!

digistump

  • Administrator
  • Hero Member
  • *****
  • Posts: 1465
Working Joystick Library!!!!
« Reply #8 on: December 30, 2012, 10:16:09 pm »
This has been added to the newest update!

WelshMullet

  • Newbie
  • *
  • Posts: 3
Working Joystick Library!!!!
« Reply #9 on: January 10, 2013, 07:00:23 am »
Reading this with an idea for a project but i\'m confused.
How on earth do you connect 6 analog sensors and 16 digital buttons to a digispark...?

duckythescientist

  • Newbie
  • *
  • Posts: 27
Working Joystick Library!!!!
« Reply #10 on: January 10, 2013, 08:26:08 am »
WelshMullet, you don\'t directly. However, a project where this could actually happen could be something like if you have the Digispark connected to a few I2C devices such as accelerometers, gyroscopes, touchscreens, etc. Because of communication and expansion ability, it is quite possible to use more than the library can handle right now. Actually, I\'m working on a modification to this that lets me turn four Gamecube controllers to USB joysticks at the same time. So yes, you can definitely connect that many sensors and buttons.

WelshMullet

  • Newbie
  • *
  • Posts: 3
Working Joystick Library!!!!
« Reply #11 on: January 10, 2013, 09:53:37 am »
So it would be as simple as using a few i/o expanders? Interesting. Do you know how many analog/digital inputs is the max for a standard usb joystick?

duckythescientist

  • Newbie
  • *
  • Posts: 27
Working Joystick Library!!!!
« Reply #12 on: January 16, 2013, 10:25:17 am »
There isn\'t much of a standard really. Each HID joystick has a report descriptor (usually) hardcoded into it. I chose a descriptor with 6analog and 16digital as a starting point, but I have seen descriptors that go to 32digital and 6-or-so analog. The descriptor tells the computer how many axis/buttons the joystick has. In fact, you can work the descriptor so a single USB device enumerates as multiple joysticks. The limiting factor becomes code size on the processor. There may be a limit in the way a computer can handle USB, but it\'ll never be reached with a Digispark.

duckythescientist

  • Newbie
  • *
  • Posts: 27
Working Joystick Library!!!!
« Reply #13 on: January 16, 2013, 10:38:26 am »
Code: [Select]

unsigned char gcn64_usbHidReportDescriptor[] PROGMEM = {
    0x05, 0x01,                    // USAGE_PAGE (Generic Desktop)
    0x09, 0x05,                    // USAGE (Gamepad)
    0xa1, 0x01,                    // COLLECTION (Application)

    0x09, 0x01,                    //   USAGE (Pointer)    
0xa1, 0x00,                    //   COLLECTION (Physical)
0x05, 0x01,                    //     USAGE_PAGE (Generic Desktop)
    0x09, 0x30,                    //     USAGE (X)
    0x09, 0x31,                    //     USAGE (Y)

0x09, 0x33,   //     USAGE (Rx)
0x09, 0x34, //  USAGE (Ry)

0x09, 0x35, //  USAGE (Rz)
0x09, 0x36, //  USAGE (Slider)

    0x15, 0x00,                    //     LOGICAL_MINIMUM (0)
    0x26, 0xFF, 0x00,              //     LOGICAL_MAXIMUM (255)
    0x75, 0x08,                    //     REPORT_SIZE (8)
    0x95, 0x06,                    //     REPORT_COUNT (6)
    0x81, 0x02,                    //     INPUT (Data,Var,Abs)
0xc0,                          //   END_COLLECTION (Physical)

    0x05, 0x09,                    //   USAGE_PAGE (Button)
    0x19, 0x01,                    //   USAGE_MINIMUM (Button 1)
    0x29, 0x10,                    //   USAGE_MAXIMUM (Button 14)
    0x15, 0x00,                    //   LOGICAL_MINIMUM (0)
    0x25, 0x01,                    //   LOGICAL_MAXIMUM (1)
    0x75, 0x01,                    //   REPORT_SIZE (1)
    0x95, 0x10,                    //   REPORT_COUNT (16)
    0x81, 0x02,                    //   INPUT (Data,Var,Abs)

    0xc0                           // END_COLLECTION (Application)
};


This is the HID descriptor that I used in the library (borrowed from Raphael Assenat). Notice the REPORT_COUNT (6) and REPORT_COUNT (16) lines. Those could be changed (along with the data collection and reporting functions) to give this joystick more/fewer inputs.

However, looking at the USB HID specs, 6 axes (plus a slider and other special inputs) appears to be standard. http://www.usb.org/developers/devclass_docs/Hut1_12v2.pdf section 4.2 page 29

You could probably also include 7 vector types for joystick axes (section 4.4).  

SuperRoach

  • Newbie
  • *
  • Posts: 3
Re: Working Joystick Library!!!!
« Reply #14 on: February 26, 2013, 02:19:10 am »
Can't wait to see the fun projects that get made out of this!