Digistump Forums

The Digispark => Digispark Libraries => Topic started by: duckythescientist on December 26, 2012, 04:55:37 pm

Title: Working Joystick Library!!!!
Post by: duckythescientist 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
Title: Working Joystick Library!!!!
Post by: digistump on December 26, 2012, 05:18:40 pm
Awesome - can I add it to the Digispark Ready Arduino IDE package?
Title: Working Joystick Library!!!!
Post by: duckythescientist 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.
Title: Working Joystick Library!!!!
Post by: digistump 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!
Title: Working Joystick Library!!!!
Post by: Bluebie on December 27, 2012, 04:40:35 pm
You have just made me the happiest girl in the world! Thank you so much! <3
Title: Working Joystick Library!!!!
Post by: Bluebie 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.
Title: Working Joystick Library!!!!
Post by: duckythescientist 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.
Title: Working Joystick Library!!!!
Post by: Bluebie on December 28, 2012, 06:14:18 am
Awesome stuff!
Title: Working Joystick Library!!!!
Post by: digistump on December 30, 2012, 10:16:09 pm
This has been added to the newest update!
Title: Working Joystick Library!!!!
Post by: WelshMullet 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...?
Title: Working Joystick Library!!!!
Post by: duckythescientist 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.
Title: Working Joystick Library!!!!
Post by: WelshMullet 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?
Title: Working Joystick Library!!!!
Post by: duckythescientist 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.
Title: Working Joystick Library!!!!
Post by: duckythescientist 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).  
Title: Re: Working Joystick Library!!!!
Post by: SuperRoach on February 26, 2013, 02:19:10 am
Can't wait to see the fun projects that get made out of this!
Title: Buttons 9-16?
Post by: Sure on February 07, 2014, 02:56:16 am
Thanks for the library, I really like to use it to bring old Gameport- & ADB-Joysticks back to life.

Maybe I missed something in the documentation, but I still haven´t found out how to use buttons 9-16... is there anything like a second byte to set them?

Thx & regards,

Andreas
Title: Re: Working Joystick Library!!!!
Post by: HCO on December 24, 2016, 09:11:24 am
Hi, thanks lot of your librery

i need your help, i nned use pin 0 to 2 with interruption and PCINT0_VECT is used yet.
How can i get PIN 0 to 2 with interrupt.

best regards

thanks
Title: Re: Working Joystick Library!!!!
Post by: KASA on June 11, 2017, 02:38:30 am
Hello there!
Im not sure if the creator of the library is still active but if anybody wants to help me:
I wanted to change the axis resolution to 10 bit.
What i tried:
(1) changed the maximum logic level to 0x400
         result: same 8 bit resolution as before
(2) changed the maximum logic level to 0x3FC
         result: same 8 bit resolution as before
(3) changed packet size from 0x08 to 0x0A and tried 1 and 2 with that
         result: Axis don't move anymore

I don't know why it isn't working, maybe I have to change something in the function that sets the axis position?

EDIT:
I think GCN64_REPORT_SIZE  has also something to do with the USB Hid but changing it to 10 prevents the PC from recognising the Spark...

Thanks,

KASA
Title: Re: Working Joystick Library!!!!
Post by: brian on April 26, 2018, 04:01:44 am
so how exactly do you activate buttons?
0x00, 0x00 is for deactivating all but that is everything i figured out :/
Title: Re: Working Joystick Library!!!!
Post by: Adsixnine on August 08, 2018, 09:35:26 pm
Awesome  8)
Title: Re: Working Joystick Library!!!!
Post by: Adsixnine on August 30, 2018, 08:59:52 pm
Awesome bro  8)