Ries Twisk / Mbed 2 deprecated JoyStick

Dependencies:   USBDevice mbed-rtos mbed

Fork of JoyStick by Ries Twisk

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