MMlab / Mbed 2 deprecated PING_ultrasonic

Dependencies:   USBDevice mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers USBJoysteck.cpp Source File

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