chipKIT® Development Platform

Inspired by Arduino™

MX7 as a Joystick

Created Mon, 23 Apr 2018 16:50:38 +0000 by aabfm


aabfm

Mon, 23 Apr 2018 16:50:38 +0000

Hi everyone, I've been looking at dozens of websites, forums, documents, datasheets, etc, etc, etc but there isn't one single one that explains, ideally with an example, how to create a simple USB joystick. The MX7 has what it takes to create one from the hardware point of view, but when it comes to software it's a different conversation. Does anyone know how or where to find a simple example of a "X,Y and Throttle" type of joystick with the MX7? Is there any library that can be used? I know that putting together a USB based peripheral is not as simple as reading a digital pin but there is a way for sure and I need to start somewhere even if it is just with the "Throttle"... :( Thanks in advance.


majenko

Tue, 24 Apr 2018 10:00:23 +0000

If your MX7 has a direct USB connection (not an FT232 chip) and the board config is set up to use it, then:

  1. Set the USB config in the options menu to "Custom / Disabled" (or is it "Disabled / Custom"...? I forget. Whatever...).
  2. Use some code similar to:
USBFS usbDevice;
USBManager USB(usbDevice, 0x04d8, 0x0f5c); // You can use your own VID/PID here if you like
HID_Joystick Joystick;

void setup() {
    USB.addDevice(Joystick);
    USB.begin();
}

void loop() {
    int x = analogRead(0) >> 2;
    int y = analogRead(1) >> 2;
    int z = analogRead(2) >> 2;
    
    Joystick.setPosition(x, y, z);
}

The default joystick has three positional axes, three rotational axes, an 8-way hat, and 16 buttons. The functions you can use are:

Joystick.setX(uint8_t x);
Joystick.setY(uint8_t y);
Joystick.setZ(uint8_t z);
Joystick.rotateX(uint8_t x);
Joystick.rotateY(uint8_t y);
Joystick.rotateZ(uint8_t z);
Joystick.setPosition(uint8_t x, uint8_t y, uint8_t z);
Joystick.setRotation(uint8_t x, uint8_t y, uint8_t z);
Joystick.press(uint8_t b);
Joystick.release(uint8_t b);
Joystick.etHat(uint8_t d);

aabfm

Tue, 24 Apr 2018 16:19:04 +0000

I think that my MX7 (PIC32MX795F512L) has a direct USB connection to pins P32_USBID, P32_D+ and P32_USB_D-, the board is the MX7cK.

Based on the libraries that are available from the UECIDE is any of the following two the correct one?

Plugins -> Libraries -> Communications -> Protocols -> Replacement USB library for chipKIT boards

or

Plugins -> Libraries -> Communications -> USB -> USB Host Shield Library

or

are all the libraries already installed with the UECIDE and I just need to use the code?


majenko

Tue, 24 Apr 2018 17:03:20 +0000

No libraries are needed. It's all in the core. Just use the current core version and you have everything you need.


aabfm

Tue, 24 Apr 2018 17:30:32 +0000

I'll give it a try and come back with feedback. Thanks


aabfm

Wed, 25 Apr 2018 09:02:39 +0000

Compiled and uploaded. However Windows7 keeps coming back with a message of not recognizing the chipKit Mx7 board... :( Shouldn't it be recognized as a joystick? Should I unplug the USB cable used for uploading the code and plug a standard PSU?


majenko

Wed, 25 Apr 2018 09:06:26 +0000

It should just be recognised as a USB joystick, yes. However, Windows is a strange beast and it's hard to know what it's thinking most of the time.

All a joystick is, is a HID device with a certain HID report descriptor. I pretty much copied that report descriptor off a real joystick, so it should work.


aabfm

Wed, 25 Apr 2018 10:04:58 +0000

Regarding the PSU, should maintain both USB plugs in or replace one of them by a standard PSU?

One other thing: if the USB cable on the micro USB socket is connected to the PC (the one to be recognized as a joystick) the board is constantly rebooting.


aabfm

Wed, 25 Apr 2018 11:12:32 +0000

:) Finally, it's working!

USB_UART (J2) used to program the board has to be disconnected. USB_Micro (J19) used as USB HID device port has to be connected. Power jumper J3 has to move from UART to USB.

On Windows7 "Devices and Printers" a new HID Gaming device will appear named as "Cerebot MX7CK".

Thanks a lot magenko! You're the best! ;)


majenko

Wed, 25 Apr 2018 16:02:43 +0000

Aha - that'll be it then :)

There's support for pretty much every device imaginable in the core now.