for whell

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 s) {
00031 
00032 
00033    _s = s;
00034     /*
00035    _t = t;
00036    _r = r;   
00037    _x = x;
00038    _y = y;
00039    _buttons = buttons;     
00040    _hat = hat;
00041    */
00042 
00043    return update();
00044 }
00045  
00046 bool USBJoystick::update() {
00047    HID_REPORT report;
00048 
00049    // Fill the report according to the Joystick Descriptor
00050    report.data[0] = _s & 0xff;            
00051    
00052    report.length = 1;
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::buttons(uint32_t buttons) {
00074    _buttons = buttons;
00075    return update();
00076 }
00077 
00078 bool USBJoystick::hat(uint8_t hat) {
00079    _hat = hat;
00080    return update();
00081 }
00082 
00083 bool USBJoystick::steering(int16_t s) {
00084      _s = s;
00085    return update();
00086 }
00087 
00088 void USBJoystick::_init() {
00089    _s = 0; 
00090    
00091    /*
00092    _t = -127;
00093    _r = -127;    
00094    _x = 0;                       
00095    _y = 0;     
00096    _buttons = 0x00000000;
00097    _hat = 0x00;     
00098    */         
00099 }
00100 
00101 
00102 uint8_t * USBJoystick::reportDesc() {    
00103          static uint8_t reportDescriptor[] = {
00104 
00105              USAGE_PAGE(1), 0x01,           // Generic Desktop           
00106              //LOGICAL_MINIMUM(1), 0x00,      // Logical_Minimum (0)             
00107              USAGE(1), 0x04,                // Usage (Joystick)
00108              COLLECTION(1), 0x01,           // Application
00109                USAGE_PAGE(1), 0x02,            // Simulation Controls
00110                USAGE(1), 0xC8,                 // Throttle                  
00111                LOGICAL_MINIMUM(1), 0x81,       // -127
00112                LOGICAL_MAXIMUM(1), 0x7f,       // 127
00113                REPORT_SIZE(1), 0x08,
00114                REPORT_COUNT(1), 0x01,
00115                INPUT(1), 0x02,                 // Data, Variable, Absolute               
00116                
00117              END_COLLECTION(0)
00118       };
00119 
00120       reportLength = sizeof(reportDescriptor);
00121       return reportDescriptor;
00122 }