USB device stack

Fork of USBDevice by mbed official

USBJoystick.cpp

Committer:
nikitamere
Date:
2016-09-16
Revision:
66:b6940e641a15
Parent:
65:e64cf9f9c595

File content as of revision 66:b6940e641a15:

/* Copyright (c) 2010-2011 mbed.org, MIT License
* Modified Mouse code for Joystick - WH 2012
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include "stdint.h"
#include "USBJoystick.h"

bool USBJoystick::update(int16_t x, int16_t y, int16_t z, int16_t rx, uint16_t ry, uint16_t rz, uint8_t button) {
             HID_REPORT report;
   _x = x;  
   _y = y;
   _z = z;
   _rx = rx;
   _ry = ry;
   _rz = rz;
   _button = button;     

   // Fill the report according to the Joystick Descriptor
   report.data[0] = _x & 0xff;            
   report.data[1] = _y & 0xff;               
   report.data[2] = _z & 0xff;            
   report.data[3] = _rx & 0xff;  
   report.data[4] = _ry & 0xff;     
   report.data[5] = _rz & 0xff;       
   report.data[6] = _button & 0xff;                                 
   report.length = 7; 
           
   return send(&report);
}
 
bool USBJoystick::update() {
   HID_REPORT report;

   // Fill the report according to the Joystick Descriptor
   report.data[0] = _x & 0xff;            
   report.data[1] = _y & 0xff;               
   report.data[2] = _z & 0xff;            
   report.data[3] = _rx & 0xff;  
   report.data[4] = _ry & 0xff;     
   report.data[5] = _rz & 0xff;       
   report.data[6] = _button & 0xff;                                 
   report.length = 7; 
   
   return send(&report);
}

bool USBJoystick::throttle(int16_t t) {

     return update();
}

bool USBJoystick::rudder(int16_t r) {

     return update();
}

bool USBJoystick::move(int16_t x, int16_t y) {
     _x = x;
     _y = y;
     return update();
}

bool USBJoystick::button(uint16_t button) {
     _button = button;
     return update();
}


void USBJoystick::_init() {

   _x = 0;                       
   _y = 0;    
   _z = 0; 
   _rx = 0;                       
   _ry = 0;    
   _rz = 0;   

   _button = 0x00;
         
}


uint8_t * USBJoystick::reportDesc() {    
         static uint8_t reportDescriptor[] = {

             USAGE_PAGE(1), 0x01,           // Generic Desktop           
             LOGICAL_MINIMUM(1), 0x00,      // Logical_Minimum (0)             
             USAGE(1), 0x04,                // Usage (Joystick)
             COLLECTION(1), 0x01,           // Application
            // Data, Variable, Absolute               
               USAGE_PAGE(1), 0x01,            // Generic Desktop
               USAGE(1), 0x01,                 // Usage (Pointer)
               COLLECTION(1), 0x00,            // Physical
////  3 Translations and 3 Rotations
                 USAGE(1), 0x30,                 // X
                 USAGE(1), 0x31,                 // Y
                 USAGE(1), 0x32,                 // Z
                 USAGE(1), 0x33,                 //Rx
                 USAGE(1), 0x34,                 //Ry
                 USAGE(1), 0x35,                 //Rz
                 
////  8 bit values
                 LOGICAL_MINIMUM(1), 0x81,       // -127
                 LOGICAL_MAXIMUM(1), 0x7f,       // 127
                 REPORT_SIZE(1), 0x08,
                 REPORT_COUNT(1), 0x06,
                 INPUT(1), 0x02,                 // Data, Variable, Absolute                  
// 16 bit values
//                 LOGICAL_MINIMUM(1), 0x00,       // 0
//                 LOGICAL_MAXIMUM(2), 0xff, 0x7f, // 32767
//                 REPORT_SIZE(1), 0x10,
//                 REPORT_COUNT(1), 0x06,
//                 INPUT(1), 0x02,                 // Data, Variable, Absolute                

               END_COLLECTION(0),               
            
// 8 Buttons
               USAGE_PAGE(1), 0x09,            // Buttons
               USAGE_MINIMUM(1), 0x01,         // 1
               USAGE_MAXIMUM(1), 0x08,         // 8
               LOGICAL_MINIMUM(1), 0x00,       // 0
               LOGICAL_MAXIMUM(1), 0x01,       // 1
               REPORT_SIZE(1), 0x01,
               REPORT_COUNT(1), 0x08,
               UNIT_EXPONENT(1), 0x00,         // Unit_Exponent (0)
               UNIT(1), 0x00,                  // Unit (None)                                           
               INPUT(1), 0x02,                 // Data, Variable, Absolute
               
             END_COLLECTION(0)

        };

      reportLength = sizeof(reportDescriptor);
      return reportDescriptor;
}