MARIOS SATANAS / Mbed 2 deprecated USBJoystick_mod

Dependencies:   USBDevice 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, uint16_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.data[5] = (_button >> 4);
00039    //report.data[6] = 0;
00040    report.data[6] = ( _button >> 0x0C) & 0x01;                                      
00041    report.length = 7; 
00042            
00043    return send(&report);
00044 }
00045  
00046 bool USBJoystick::update() {
00047    HID_REPORT report;
00048 
00049    // Fill the report according to the Joystick Descriptor
00050    report.data[0] = _t & 0xff;            
00051    report.data[1] = _r & 0xff;               
00052    report.data[2] = _x & 0xff;            
00053    report.data[3] = _y & 0xff;            
00054    report.data[4] = ((_button & 0x0f) << 4) | (_hat & 0x0f) ;                                      
00055    report.data[5] = ( _button >> 4);
00056    report.data[6] = ( _button >> 0x0C) & 0x01;                                      
00057    report.length = 7; 
00058    
00059    return send(&report);
00060 }
00061 
00062 bool USBJoystick::throttle(int16_t t) {
00063      _t = t;
00064      return update();
00065 }
00066 
00067 bool USBJoystick::rudder(int16_t r) {
00068      _r = r;
00069      return update();
00070 }
00071 
00072 bool USBJoystick::move(int16_t x, int16_t y) {
00073      _x = x;
00074      _y = y;
00075      return update();
00076 }
00077 
00078 bool USBJoystick::button(uint16_t button) {
00079      _button = button;
00080      return update();
00081 }
00082 
00083 bool USBJoystick::hat(uint8_t hat) {
00084      _hat = hat;
00085      return update();
00086 }
00087 
00088 
00089 void USBJoystick::_init() {
00090 
00091    _t = -127;
00092    _r = -127;    
00093    _x = 0;                       
00094    _y = 0;     
00095    _button = 0x00;
00096    _hat = 0x00;              
00097 }
00098 
00099 
00100 uint8_t * USBJoystick::reportDesc() {    
00101          static uint8_t reportDescriptor[] = {
00102 
00103              USAGE_PAGE(1), 0x01,           // Generic Desktop           
00104              LOGICAL_MINIMUM(1), 0x00,      // Logical_Minimum (0)             
00105              USAGE(1), 0x04,                // Usage (Joystick)
00106              COLLECTION(1), 0x01,           // Application
00107              //  USAGE_PAGE(1), 0x02,            // Simulation Controls
00108              //  USAGE(1), 0xBB,                 // Throttle             
00109              //  USAGE(1), 0xBA,                 // Rudder               
00110              //  LOGICAL_MINIMUM(1), 0x81,       // -127
00111              //  LOGICAL_MAXIMUM(1), 0x7f,       // 127
00112              //  REPORT_SIZE(1), 0x08,
00113              //  REPORT_COUNT(1), 0x02,
00114              //  INPUT(1), 0x02,                 // Data, Variable, Absolute               
00115                USAGE_PAGE(1), 0x01,            // Generic Desktop
00116                USAGE(1), 0x01,                 // Usage (Pointer)
00117                COLLECTION(1), 0x00,            // Physical
00118                  USAGE(1), 0x30,                 // X
00119                  USAGE(1), 0x31,                 // Y
00120                  USAGE(1), 0x32,                 //R
00121                  USAGE(1), 0x35,                 //Rz?
00122 //  8 bit values
00123                  LOGICAL_MINIMUM(1), 0x81,       // -127
00124                  LOGICAL_MAXIMUM(1), 0x7f,       // 127
00125                  REPORT_SIZE(1), 0x08,
00126                  REPORT_COUNT(1), 0x04,
00127                  INPUT(1), 0x02,                 // Data, Variable, Absolute                  
00128 // 16 bit values
00129 //                 LOGICAL_MINIMUM(1), 0x00,       // 0
00130 //                 LOGICAL_MAXIMUM(2), 0xff, 0x7f, // 32767
00131 //                 REPORT_SIZE(1), 0x10,
00132 //                 REPORT_COUNT(1), 0x02,
00133 //                 INPUT(1), 0x02,                 // Data, Variable, Absolute                
00134 
00135                END_COLLECTION(0),               
00136 // 4 Position Hat Switch
00137 //               USAGE(1), 0x39,                 // Usage (Hat switch)
00138 //               LOGICAL_MINIMUM(1), 0x00,       // 0
00139 //               LOGICAL_MAXIMUM(1), 0x03,       // 3
00140 //               PHYSICAL_MINIMUM(1), 0x00,      // Physical_Minimum (0)
00141 //               PHYSICAL_MAXIMUM(2), 0x0E, 0x01, // Physical_Maximum (270)
00142 //               UNIT(1), 0x14,                  // Unit (Eng Rot:Angular Pos)                            
00143 //               REPORT_SIZE(1), 0x04,
00144 //               REPORT_COUNT(1), 0x01,
00145 //               INPUT(1), 0x02,                 // Data, Variable, Absolute               
00146 // 8 Position Hat Switch
00147                USAGE(1), 0x39,                 // Usage (Hat switch)
00148                LOGICAL_MINIMUM(1), 0x00,       // 0
00149                LOGICAL_MAXIMUM(1), 0x07,       // 7
00150                PHYSICAL_MINIMUM(1), 0x00,      // Physical_Minimum (0)
00151                PHYSICAL_MAXIMUM(2), 0x3B, 0x01, // Physical_Maximum (315)
00152                UNIT(1), 0x14,                  // Unit (Eng Rot:Angular Pos)                            
00153                REPORT_SIZE(1), 0x04,
00154                REPORT_COUNT(1), 0x01,
00155                INPUT(1), 0x02,                 // Data, Variable, Absolute               
00156 //
00157                USAGE_PAGE(1), 0x09,            // Buttons
00158                USAGE_MINIMUM(1), 0x01,         // 1
00159                USAGE_MAXIMUM(1), 0x0D,         // 13
00160                LOGICAL_MINIMUM(1), 0x00,       // 0
00161                LOGICAL_MAXIMUM(1), 0x01,       // 1
00162                REPORT_SIZE(1), 0x01,
00163                REPORT_COUNT(1), 0x0D,
00164                UNIT_EXPONENT(1), 0x00,         // Unit_Exponent (0)
00165                UNIT(1), 0x00,                  // Unit (None)                                           
00166                INPUT(1), 0x02,                 // Data, Variable, Absolute
00167                
00168                // Padding 7 bits
00169                REPORT_SIZE(1), 0x01,
00170                REPORT_COUNT(1), 0x07,
00171                INPUT(1), 0x01,  
00172              END_COLLECTION(0)
00173 
00174         };
00175 
00176       reportLength = sizeof(reportDescriptor);
00177       return reportDescriptor;
00178 }
00179 
00180