Wim Huiskamp / USBJoystick

Dependents:   USBJoystick_HelloWorld2 GamePortAdapter USBJoyFromRC Retro_Controller_Adaptor

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers USBJoystick.cpp Source File

USBJoystick.cpp

00001 /* mbed USBJoystick Library
00002  * Copyright (c) 2012, v01:  Initial version, WH,
00003  *                           Modified USBMouse code ARM Limited.
00004  *                           (c) 2010-2011 mbed.org, MIT License
00005  *               2016, v02:  Updated USBDevice Lib, Added waitForConnect, Updated 32 bits button 
00006  *
00007  * Permission is hereby granted, free of charge, to any person obtaining a copy
00008  * of this software and associated documentation files (the "Software"), to deal
00009  * in the Software without restriction, inclumosig without limitation the rights
00010  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00011  * copies of the Software, and to permit persons to whom the Software is
00012  * furnished to do so, subject to the following conditions:
00013  *
00014  * The above copyright notice and this permission notice shall be included in
00015  * all copies or substantial portions of the Software.
00016  *
00017  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00018  * IMPLIED, INCLUmosiG BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00019  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00020  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00021  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00022  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00023  * THE SOFTWARE.
00024  */
00025 
00026 
00027 #include "stdint.h"
00028 #include "USBJoystick.h"
00029 
00030 bool USBJoystick::update(int16_t t, int16_t r, int16_t x, int16_t y, uint32_t buttons, uint8_t hat) {
00031 
00032    _t = t;
00033    _r = r;   
00034    _x = x;
00035    _y = y;
00036    _buttons = buttons;     
00037    _hat = hat;
00038 
00039    return update();
00040 }
00041  
00042 bool USBJoystick::update() {
00043    HID_REPORT report;
00044 
00045    // Fill the report according to the Joystick Descriptor
00046    report.data[0] = _t & 0xff;            
00047    report.data[1] = _r & 0xff;               
00048    report.data[2] = _x & 0xff;            
00049    report.data[3] = _y & 0xff;
00050 
00051 
00052 #if (BUTTONS4 == 1)               
00053 //Hat and 4 Buttons
00054 //   report.data[4] = ((_buttons & 0x0f) << 4) | (_hat & 0x0f) ;                                      
00055 //   report.length = 5; 
00056 
00057 
00058 //Use 4 bit padding for hat4 or hat8
00059    report.data[4] = (_hat & 0x0f) ;                                      
00060 
00061 //Use 4 bit padding for buttons   
00062    report.data[5] = (_buttons & 0x0f) ;                                         
00063    report.length = 6; 
00064 #endif
00065 
00066 #if (BUTTONS8 == 1)               
00067 //Hat and first 4 Buttons
00068 //   report.data[4] = ((_buttons & 0x0f) << 4) | (_hat & 0x0f) ;                                      
00069 //
00070 //Use bit padding for last 4 Buttons
00071 //   report.data[5] =  (_buttons & 0xf0) >> 4;                                         
00072 //   report.length = 6; 
00073 
00074 //Use 4 bit padding for hat4 or hat8
00075    report.data[4] = (_hat & 0x0f) ;                                      
00076 
00077 //Use 8 bits for buttons   
00078    report.data[5] = (_buttons & 0xff) ;
00079    report.length = 6;
00080 #endif
00081 
00082 #if (BUTTONS32 == 1)               
00083 //Use 4 bit padding for hat4 or hat8
00084    report.data[4] = (_hat & 0x0f) ;
00085 
00086 //No bit padding for 32 buttons                                         
00087    report.data[5] = (_buttons >>  0) & 0xff;
00088    report.data[6] = (_buttons >>  8) & 0xff;
00089    report.data[7] = (_buttons >> 16) & 0xff;
00090    report.data[8] = (_buttons >> 24) & 0xff;
00091    report.length = 9;
00092 #endif
00093        
00094    return send(&report);
00095 }
00096 
00097 bool USBJoystick::throttle(int16_t t) {
00098      _t = t;
00099    return update();
00100 }
00101 
00102 bool USBJoystick::rudder(int16_t r) {
00103    _r = r;
00104    return update();
00105 }
00106 
00107 bool USBJoystick::move(int16_t x, int16_t y) {
00108    _x = x;
00109    _y = y;
00110    return update();
00111 }
00112 
00113 bool USBJoystick::buttons(uint32_t buttons) {
00114    _buttons = buttons;
00115    return update();
00116 }
00117 
00118 bool USBJoystick::hat(uint8_t hat) {
00119    _hat = hat;
00120    return update();
00121 }
00122 
00123 
00124 void USBJoystick::_init() {
00125    _t = -127;
00126    _r = -127;    
00127    _x = 0;                       
00128    _y = 0;     
00129    _buttons = 0x00000000;
00130    _hat = 0x00;              
00131 }
00132 
00133 
00134 uint8_t * USBJoystick::reportDesc() {    
00135          static uint8_t reportDescriptor[] = {
00136 
00137              USAGE_PAGE(1), 0x01,           // Generic Desktop           
00138              LOGICAL_MINIMUM(1), 0x00,      // Logical_Minimum (0)             
00139              USAGE(1), 0x04,                // Usage (Joystick)
00140              COLLECTION(1), 0x01,           // Application
00141                USAGE_PAGE(1), 0x02,            // Simulation Controls
00142                USAGE(1), 0xBB,                 // Throttle             
00143                USAGE(1), 0xBA,                 // Rudder               
00144                LOGICAL_MINIMUM(1), 0x81,       // -127
00145                LOGICAL_MAXIMUM(1), 0x7f,       // 127
00146                REPORT_SIZE(1), 0x08,
00147                REPORT_COUNT(1), 0x02,
00148                INPUT(1), 0x02,                 // Data, Variable, Absolute               
00149                USAGE_PAGE(1), 0x01,            // Generic Desktop
00150                USAGE(1), 0x01,                 // Usage (Pointer)
00151                COLLECTION(1), 0x00,            // Physical
00152                  USAGE(1), 0x30,                 // X
00153                  USAGE(1), 0x31,                 // Y
00154 //  8 bit values
00155                  LOGICAL_MINIMUM(1), 0x81,       // -127
00156                  LOGICAL_MAXIMUM(1), 0x7f,       // 127
00157                  REPORT_SIZE(1), 0x08,
00158                  REPORT_COUNT(1), 0x02,
00159                  INPUT(1), 0x02,                 // Data, Variable, Absolute                  
00160 // 16 bit values
00161 //                 LOGICAL_MINIMUM(1), 0x00,       // 0
00162 //                 LOGICAL_MAXIMUM(2), 0xff, 0x7f, // 32767
00163 //                 REPORT_SIZE(1), 0x10,
00164 //                 REPORT_COUNT(1), 0x02,
00165 //                 INPUT(1), 0x02,                 // Data, Variable, Absolute                
00166 
00167                END_COLLECTION(0),               
00168 
00169 #if (HAT4 == 1)
00170 // 4 Position Hat Switch
00171                USAGE(1), 0x39,                 // Usage (Hat switch)
00172                LOGICAL_MINIMUM(1), 0x00,       // 0
00173                LOGICAL_MAXIMUM(1), 0x03,       // 3
00174                PHYSICAL_MINIMUM(1), 0x00,      // Physical_Minimum (0)
00175                PHYSICAL_MAXIMUM(2), 0x0E, 0x01, // Physical_Maximum (270)
00176                UNIT(1), 0x14,                  // Unit (Eng Rot:Angular Pos)                            
00177                REPORT_SIZE(1), 0x04,
00178                REPORT_COUNT(1), 0x01,
00179                INPUT(1), 0x02,                 // Data, Variable, Absolute               
00180 #endif
00181 #if (HAT8 == 1)
00182 // 8 Position Hat Switch
00183                USAGE(1), 0x39,                 // Usage (Hat switch)
00184                LOGICAL_MINIMUM(1), 0x00,       // 0
00185                LOGICAL_MAXIMUM(1), 0x07,       // 7
00186                PHYSICAL_MINIMUM(1), 0x00,      // Physical_Minimum (0)
00187                PHYSICAL_MAXIMUM(2), 0x3B, 0x01, // Physical_Maximum (315)
00188                UNIT(1), 0x14,                  // Unit (Eng Rot:Angular Pos)                            
00189                REPORT_SIZE(1), 0x04,
00190                REPORT_COUNT(1), 0x01,
00191                INPUT(1), 0x02,                 // Data, Variable, Absolute               
00192 #endif
00193 
00194 // Padding 4 bits
00195                REPORT_SIZE(1), 0x01,
00196                REPORT_COUNT(1), 0x04,
00197                INPUT(1), 0x01,                 // Constant
00198 
00199 
00200 #if (BUTTONS4 == 1)
00201 // 4 Buttons
00202                USAGE_PAGE(1), 0x09,            // Buttons
00203                USAGE_MINIMUM(1), 0x01,         // 1
00204                USAGE_MAXIMUM(1), 0x04,         // 4
00205                LOGICAL_MINIMUM(1), 0x00,       // 0
00206                LOGICAL_MAXIMUM(1), 0x01,       // 1
00207                REPORT_SIZE(1), 0x01,
00208                REPORT_COUNT(1), 0x04,
00209                UNIT_EXPONENT(1), 0x00,         // Unit_Exponent (0)
00210                UNIT(1), 0x00,                  // Unit (None)                                           
00211                INPUT(1), 0x02,                 // Data, Variable, Absolute
00212 
00213 // Padding 4 bits
00214                REPORT_SIZE(1), 0x01,
00215                REPORT_COUNT(1), 0x04,
00216                INPUT(1), 0x01,                 // Constant
00217 
00218 #endif
00219 #if (BUTTONS8 == 1)
00220 // 8 Buttons
00221                USAGE_PAGE(1), 0x09,            // Buttons
00222                USAGE_MINIMUM(1), 0x01,         // 1
00223                USAGE_MAXIMUM(1), 0x08,         // 8
00224                LOGICAL_MINIMUM(1), 0x00,       // 0
00225                LOGICAL_MAXIMUM(1), 0x01,       // 1
00226                REPORT_SIZE(1), 0x01,
00227                REPORT_COUNT(1), 0x08,
00228                UNIT_EXPONENT(1), 0x00,         // Unit_Exponent (0)
00229                UNIT(1), 0x00,                  // Unit (None)                                           
00230                INPUT(1), 0x02,                 // Data, Variable, Absolute
00231 #endif
00232 
00233 #if (BUTTONS32 == 1)
00234 // 32 Buttons
00235                USAGE_PAGE(1), 0x09,            // Buttons
00236                USAGE_MINIMUM(1), 0x01,         // 1
00237                USAGE_MAXIMUM(1), 0x20,         // 32
00238                LOGICAL_MINIMUM(1), 0x00,       // 0
00239                LOGICAL_MAXIMUM(1), 0x01,       // 1
00240                REPORT_SIZE(1), 0x01,
00241                REPORT_COUNT(1), 0x20,
00242                UNIT_EXPONENT(1), 0x00,         // Unit_Exponent (0)
00243                UNIT(1), 0x00,                  // Unit (None)                                           
00244                INPUT(1), 0x02,                 // Data, Variable, Absolute
00245 #endif
00246 
00247              END_COLLECTION(0)
00248       };
00249 
00250       reportLength = sizeof(reportDescriptor);
00251       return reportDescriptor;
00252 }