USB HID Device that emulates a Gamecontroller

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers USBJoystick.cpp Source File

USBJoystick.cpp

00001 /* Copyright (c) 2010-2011 mbed.org, MIT License
00002 * Modified Mouse code for Joystick - WH 2012
00003 *
00004 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00005 * and associated documentation files (the "Software"), to deal in the Software without
00006 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
00007 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
00008 * Software is furnished to do so, subject to the following conditions:
00009 *
00010 * The above copyright notice and this permission notice shall be included in all copies or
00011 * substantial portions of the Software.
00012 *
00013 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00014 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00015 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00016 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00017 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00018 */
00019 
00020 #include "stdint.h"
00021 #include "USBJoystick.h"
00022 
00023 bool USBJoystick::update(int16_t t, int16_t r, int16_t x, int16_t y, uint8_t button, uint8_t hat) {
00024              HID_REPORT report;
00025    _t = t;
00026    _r = r;   
00027    _x = x;
00028    _y = y;
00029    _button = button;     
00030    _hat = hat;
00031 
00032    // Fill the report according to the Joystick Descriptor
00033    report.data[0] = _t & 0xff;            
00034    report.data[1] = _r & 0xff;               
00035    report.data[2] = _x & 0xff;            
00036    report.data[3] = _y & 0xff;            
00037    report.data[4] = ((_button & 0x0f) << 4) | (_hat & 0x0f) ;                                      
00038    report.length = 5; 
00039            
00040    return send(&report);
00041 }
00042  
00043 bool USBJoystick::update() {
00044    HID_REPORT report;
00045 
00046    // Fill the report according to the Joystick Descriptor
00047    report.data[0] = _t & 0xff;            
00048    report.data[1] = _r & 0xff;               
00049    report.data[2] = _x & 0xff;            
00050    report.data[3] = _y & 0xff;            
00051    report.data[4] = ((_button & 0x0f) << 4) | (_hat & 0x0f) ;                                      
00052    report.length = 5; 
00053 
00054    return send(&report);
00055 }
00056 
00057 bool USBJoystick::throttle(int16_t t) {
00058      _t = t;
00059      return update();
00060 }
00061 
00062 bool USBJoystick::rudder(int16_t r) {
00063      _r = r;
00064      return update();
00065 }
00066 
00067 bool USBJoystick::move(int16_t x, int16_t y) {
00068      _x = x;
00069      _y = y;
00070      return update();
00071 }
00072 
00073 bool USBJoystick::button(uint8_t button) {
00074      _button = button;
00075      return update();
00076 }
00077 
00078 bool USBJoystick::hat(uint8_t hat) {
00079      _hat = hat;
00080      return update();
00081 }
00082 
00083 
00084 void USBJoystick::_init() {
00085 
00086    _t = -127;
00087    _r = -127;    
00088    _x = 0;                       
00089    _y = 0;     
00090    _button = 0x00;
00091    _hat = 0x00;              
00092 }
00093 
00094 
00095 uint8_t * USBJoystick::reportDesc() {    
00096          static uint8_t reportDescriptor[] = {
00097 
00098              USAGE_PAGE(1), 0x01,           // Generic Desktop           
00099              LOGICAL_MINIMUM(1), 0x00,      // Logical_Minimum (0)             
00100              USAGE(1), 0x04,                // Usage (Joystick)
00101              COLLECTION(1), 0x01,           // Application
00102                USAGE_PAGE(1), 0x02,            // Simulation Controls
00103                USAGE(1), 0xBB,                 // Throttle             
00104                USAGE(1), 0xBA,                 // Rudder               
00105                LOGICAL_MINIMUM(1), 0x81,       // -127
00106                LOGICAL_MAXIMUM(1), 0x7f,       // 127
00107                REPORT_SIZE(1), 0x08,
00108                REPORT_COUNT(1), 0x02,
00109                INPUT(1), 0x02,                 // Data, Variable, Absolute               
00110                USAGE_PAGE(1), 0x01,            // Generic Desktop
00111                USAGE(1), 0x01,                 // Usage (Pointer)
00112                COLLECTION(1), 0x00,            // Physical
00113                  USAGE(1), 0x30,                 // X
00114                  USAGE(1), 0x31,                 // Y
00115 //  8 bit values
00116                  LOGICAL_MINIMUM(1), 0x81,       // -127
00117                  LOGICAL_MAXIMUM(1), 0x7f,       // 127
00118                  REPORT_SIZE(1), 0x08,
00119                  REPORT_COUNT(1), 0x02,
00120                  INPUT(1), 0x02,                 // Data, Variable, Absolute                  
00121 // 16 bit values
00122 //                 LOGICAL_MINIMUM(1), 0x00,       // 0
00123 //                 LOGICAL_MAXIMUM(2), 0xff, 0x7f, // 32767
00124 //                 REPORT_SIZE(1), 0x10,
00125 //                 REPORT_COUNT(1), 0x02,
00126 //                 INPUT(1), 0x02,                 // Data, Variable, Absolute                
00127 
00128                END_COLLECTION(0),               
00129 // 4 Position Hat Switch
00130 //               USAGE(1), 0x39,                 // Usage (Hat switch)
00131 //               LOGICAL_MINIMUM(1), 0x00,       // 0
00132 //               LOGICAL_MAXIMUM(1), 0x03,       // 3
00133 //               PHYSICAL_MINIMUM(1), 0x00,      // Physical_Minimum (0)
00134 //               PHYSICAL_MAXIMUM(2), 0x0E, 0x01, // Physical_Maximum (270)
00135 //               UNIT(1), 0x14,                  // Unit (Eng Rot:Angular Pos)                            
00136 //               REPORT_SIZE(1), 0x04,
00137 //               REPORT_COUNT(1), 0x01,
00138 //               INPUT(1), 0x02,                 // Data, Variable, Absolute               
00139 // 8 Position Hat Switch
00140                USAGE(1), 0x39,                 // Usage (Hat switch)
00141                LOGICAL_MINIMUM(1), 0x00,       // 0
00142                LOGICAL_MAXIMUM(1), 0x07,       // 7
00143                PHYSICAL_MINIMUM(1), 0x00,      // Physical_Minimum (0)
00144                PHYSICAL_MAXIMUM(2), 0x3B, 0x01, // Physical_Maximum (315)
00145                UNIT(1), 0x14,                  // Unit (Eng Rot:Angular Pos)                            
00146                REPORT_SIZE(1), 0x04,
00147                REPORT_COUNT(1), 0x01,
00148                INPUT(1), 0x02,                 // Data, Variable, Absolute               
00149 //
00150                USAGE_PAGE(1), 0x09,            // Buttons
00151                USAGE_MINIMUM(1), 0x01,         // 1
00152                USAGE_MAXIMUM(1), 0x04,         // 4
00153                LOGICAL_MINIMUM(1), 0x00,       // 0
00154                LOGICAL_MAXIMUM(1), 0x01,       // 1
00155                REPORT_SIZE(1), 0x01,
00156                REPORT_COUNT(1), 0x04,
00157                UNIT_EXPONENT(1), 0x00,         // Unit_Exponent (0)
00158                UNIT(1), 0x00,                  // Unit (None)                                           
00159                INPUT(1), 0x02,                 // Data, Variable, Absolute
00160              END_COLLECTION(0)
00161 
00162         };
00163 
00164       reportLength = sizeof(reportDescriptor);
00165       return reportDescriptor;
00166 }
00167 
00168