Modification of USB Joystick to work as a Hitbox-like game controller using the F401RE. Works reasonably well under Windows.

Dependencies:   USBDevice mbed

This is a hobby project trying to recreate a Hitbox Controller for fun .

The original code was based off of http://developer.mbed.org/users/wim/notebook/usb-joystick-device/ Slight modifications were made to the USB HID descriptor to up the number of buttons reported to 13.

Currently, this works well under Windows and Linux, but no luck getting it to work with Mac OS X or a PS3 so far.

If anybody has useful information, feel free to contribute. :)

p.s: My thread for hooking up an F401RE board as a USB device is here: http://developer.mbed.org/questions/5364/Cannot-get-the-Nucleo-F401RE-to-work-as-/

p.s.2: Cabling information and <del>pictures</del> will be uploaded in the near future.

p.s.3: 2015-04-20: added support for FRDM-KL25Z board. Due to the better usb functionality, this hitbox now works on Windows, Mac OS X, Linux and PS3 too! (minus the "PS" button...)

USBJoystick.cpp

Committer:
eimaiosatanas
Date:
2015-04-20
Revision:
2:278b7a590311
Parent:
0:c8e9c90ca7f4

File content as of revision 2:278b7a590311:

/* Copyright (c) 2010-2011 mbed.org, MIT License
* Modified Mouse code for Joystick - WH 2012
*
* 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 "stdint.h"
#include "USBJoystick.h"

bool USBJoystick::update(int16_t t, int16_t r, int16_t x, int16_t y, uint16_t button, uint8_t hat) {
             HID_REPORT report;
   _t = t;
   _r = r;   
   _x = x;
   _y = y;
   _button = button;     
   _hat = hat;

   // Fill the report according to the Joystick Descriptor
   report.data[0] = _t & 0xff;            
   report.data[1] = _r & 0xff;               
   report.data[2] = _x & 0xff;            
   report.data[3] = _y & 0xff;            
   report.data[4] = ((_button & 0x0f) << 4) | (_hat & 0x0f) ;
   report.data[5] = (_button >> 4);
   //report.data[6] = 0;
   report.data[6] = ( _button >> 0x0C) & 0x01;                                      
   report.length = 7; 
           
   return send(&report);
}
 
bool USBJoystick::update() {
   HID_REPORT report;

   // Fill the report according to the Joystick Descriptor
   report.data[0] = _t & 0xff;            
   report.data[1] = _r & 0xff;               
   report.data[2] = _x & 0xff;            
   report.data[3] = _y & 0xff;            
   report.data[4] = ((_button & 0x0f) << 4) | (_hat & 0x0f) ;                                      
   report.data[5] = ( _button >> 4);
   report.data[6] = ( _button >> 0x0C) & 0x01;                                      
   report.length = 7; 
   
   return send(&report);
}

bool USBJoystick::throttle(int16_t t) {
     _t = t;
     return update();
}

bool USBJoystick::rudder(int16_t r) {
     _r = r;
     return update();
}

bool USBJoystick::move(int16_t x, int16_t y) {
     _x = x;
     _y = y;
     return update();
}

bool USBJoystick::button(uint16_t button) {
     _button = button;
     return update();
}

bool USBJoystick::hat(uint8_t hat) {
     _hat = hat;
     return update();
}


void USBJoystick::_init() {

   _t = -127;
   _r = -127;    
   _x = 0;                       
   _y = 0;     
   _button = 0x00;
   _hat = 0x00;              
}


uint8_t * USBJoystick::reportDesc() {    
         static uint8_t reportDescriptor[] = {

             USAGE_PAGE(1), 0x01,           // Generic Desktop           
             LOGICAL_MINIMUM(1), 0x00,      // Logical_Minimum (0)             
             USAGE(1), 0x04,                // Usage (Joystick)
             COLLECTION(1), 0x01,           // Application
             //  USAGE_PAGE(1), 0x02,            // Simulation Controls
             //  USAGE(1), 0xBB,                 // Throttle             
             //  USAGE(1), 0xBA,                 // Rudder               
             //  LOGICAL_MINIMUM(1), 0x81,       // -127
             //  LOGICAL_MAXIMUM(1), 0x7f,       // 127
             //  REPORT_SIZE(1), 0x08,
             //  REPORT_COUNT(1), 0x02,
             //  INPUT(1), 0x02,                 // Data, Variable, Absolute               
               USAGE_PAGE(1), 0x01,            // Generic Desktop
               USAGE(1), 0x01,                 // Usage (Pointer)
               COLLECTION(1), 0x00,            // Physical
                 USAGE(1), 0x30,                 // X
                 USAGE(1), 0x31,                 // Y
                 USAGE(1), 0x32,                 //R
                 USAGE(1), 0x35,                 //Rz?
//  8 bit values
                 LOGICAL_MINIMUM(1), 0x81,       // -127
                 LOGICAL_MAXIMUM(1), 0x7f,       // 127
                 REPORT_SIZE(1), 0x08,
                 REPORT_COUNT(1), 0x04,
                 INPUT(1), 0x02,                 // Data, Variable, Absolute                  
// 16 bit values
//                 LOGICAL_MINIMUM(1), 0x00,       // 0
//                 LOGICAL_MAXIMUM(2), 0xff, 0x7f, // 32767
//                 REPORT_SIZE(1), 0x10,
//                 REPORT_COUNT(1), 0x02,
//                 INPUT(1), 0x02,                 // Data, Variable, Absolute                

               END_COLLECTION(0),               
// 4 Position Hat Switch
//               USAGE(1), 0x39,                 // Usage (Hat switch)
//               LOGICAL_MINIMUM(1), 0x00,       // 0
//               LOGICAL_MAXIMUM(1), 0x03,       // 3
//               PHYSICAL_MINIMUM(1), 0x00,      // Physical_Minimum (0)
//               PHYSICAL_MAXIMUM(2), 0x0E, 0x01, // Physical_Maximum (270)
//               UNIT(1), 0x14,                  // Unit (Eng Rot:Angular Pos)                            
//               REPORT_SIZE(1), 0x04,
//               REPORT_COUNT(1), 0x01,
//               INPUT(1), 0x02,                 // Data, Variable, Absolute               
// 8 Position Hat Switch
               USAGE(1), 0x39,                 // Usage (Hat switch)
               LOGICAL_MINIMUM(1), 0x00,       // 0
               LOGICAL_MAXIMUM(1), 0x07,       // 7
               PHYSICAL_MINIMUM(1), 0x00,      // Physical_Minimum (0)
               PHYSICAL_MAXIMUM(2), 0x3B, 0x01, // Physical_Maximum (315)
               UNIT(1), 0x14,                  // Unit (Eng Rot:Angular Pos)                            
               REPORT_SIZE(1), 0x04,
               REPORT_COUNT(1), 0x01,
               INPUT(1), 0x02,                 // Data, Variable, Absolute               
//
               USAGE_PAGE(1), 0x09,            // Buttons
               USAGE_MINIMUM(1), 0x01,         // 1
               USAGE_MAXIMUM(1), 0x0D,         // 13
               LOGICAL_MINIMUM(1), 0x00,       // 0
               LOGICAL_MAXIMUM(1), 0x01,       // 1
               REPORT_SIZE(1), 0x01,
               REPORT_COUNT(1), 0x0D,
               UNIT_EXPONENT(1), 0x00,         // Unit_Exponent (0)
               UNIT(1), 0x00,                  // Unit (None)                                           
               INPUT(1), 0x02,                 // Data, Variable, Absolute
               
               // Padding 7 bits
               REPORT_SIZE(1), 0x01,
               REPORT_COUNT(1), 0x07,
               INPUT(1), 0x01,  
             END_COLLECTION(0)

        };

      reportLength = sizeof(reportDescriptor);
      return reportDescriptor;
}