HID Joystick - For use with X-Plane or other programs that can read HID JoySticks

Dependencies:   USBDevice mbed-rtos mbed

Fork of JoyStick by Ries Twisk

This is a simple Joystick HID that I use for xplane and a home build yoke + paddels, see this forum with the look and feel of it : http://forums.x-plane.org/index.php?showtopic=70041

The analog input are filtered with a LowPass IIR filter and the digital input's will be derived from the analog input and de-bounced.

The analog values are read at a 1Khz interval and to ensure we don't push the USB stack to much at a maximum rate of 20 updates/sec HID data is send over USB only if any values where changed. The JoyStick will send 16Bit analog values as opposite of 8 bit values that are normally used to increase accuracy of the whole system. This is well noticeable within x-plane!

The JoyStick uses the JoyStick copied from Wim Huiskamp and modified to suite my needs and the MBED RTOS libraries for reading analog inputs, sending debug data over USB and sending HID data, 3 threads in total.

USBJoystick.h

Committer:
rvt
Date:
2016-06-22
Revision:
5:a0bb17c379ce
Parent:
4:2cc58c173de8

File content as of revision 5:a0bb17c379ce:

/* USBJoystick.h */
/* USB device example: Joystick*/
/* Copyright (c) 2011 ARM Limited. All rights reserved. */
/* Modified Mouse code for Joystick - WH 2012 */

#ifndef USBJOYSTICK_H
#define USBJOYSTICK_H

#include "USBHID.h"

#define REPORT_ID_JOYSTICK  4

/* Common usage */
enum JOY_BUTTON {
     JOY_B0 = 1,
     JOY_B1 = 2,
     JOY_B2 = 4,
     JOY_B3 = 8,     
     JOY_B4 = 16,     
};

/**
 *
 * USBJoystick example
 * @code
 * #include "mbed.h"
 * #include "USBJoystick.h"
 *
 * USBJoystick joystick;
 *
 * int main(void)
 * {
 *   while (1)
 *   {
 *      joystick.move(20, 0);
 *      wait(0.5);
 *   }
 * }
 *
 * @endcode
 *
 *
 * @code
 * #include "mbed.h"
 * #include "USBJoystick.h"
 * #include <math.h>
 *
 * USBJoystick joystick;
 *
 * int main(void)
 * {
 *   int16_t i = 0;
 *   int16_t throttle = 0;
 *   int16_t rudder = 0;    
 *   int16_t x = 0;
 *   int16_t y = 0;
 *   int32_t radius = 120;
 *   int32_t angle = 0;
 *   int8_t button = 0;    
 *   int8_t hat = 0;    
 *   
 *   while (1) {
 *       // Basic Joystick
 *       throttle = (i >> 8) & 0xFF; // value -127 .. 128
 *       rudder = (i >> 8) & 0xFF;   // value -127 .. 128        
 *       button = (i >> 8) & 0x0F;   // value    0 .. 15, one bit per button     
 *        hat    = (i >> 8) & 0x07;   // value 0..7 or 8 for neutral         
 *       i++;        
 *       
 *       x = cos((double)angle*3.14/180.0)*radius;  // value -127 .. 128
 *       y = sin((double)angle*3.14/180.0)*radius;  // value -127 .. 128
 *       angle += 3;        
 *
 *       joystick.update(throttle, rudder, x, y, button, hat);
 *
 *       wait(0.001);
 *   }
 * }
 * @endcode
 */


class USBJoystick: public USBHID {
   public:

        /**
         *   Constructor
         *
         * @param vendor_id Your vendor_id (default: 0x1234)
         * @param product_id Your product_id (default: 0x0002)
         * @param product_release Your product_release (default: 0x0001)
         */
         USBJoystick(uint16_t vendor_id = 0x1234, uint16_t product_id = 0x0100, uint16_t product_release = 0x0001): 
             USBHID(0, 0, vendor_id, product_id, product_release, false)
             { 
                 _init();
                 connect();
             };
         
         /**
         * Write a state of the mouse
         *
         * @param t throttle position
         * @param r rudder position         
         * @param x x-axis position
         * @param y y-axis position
         * @param buttons buttons state
         * @returns true if there is no error, false otherwise
         */
         bool update(int16_t t, int16_t r, int16_t x, int16_t y, uint32_t buttons);

         /**
         * Write a state of the mouse
         *
         * @returns true if there is no error, false otherwise
         */
         bool update();

         /**
         * Move the throttle position
         *
         * @param t throttle position
         * @returns true if there is no error, false otherwise
         */
         bool throttle(int16_t t);
         
         /**
         * Move the rudder position
         *
         * @param r rudder position
         * @returns true if there is no error, false otherwise
         */        
         bool rudder(int16_t r);         

         /**
         * Move the cursor to (x, y)
         *
         * @param x-axis position
         * @param y-axis position
         * @returns true if there is no error, false otherwise
         */
         bool move(int16_t x, int16_t y);
         
         /**
         * Press one or several buttons
         *
         * @param button button state
         * @returns true if there is no error, false otherwise
         */
         bool button(uint32_t button);
                  
         /*
         * To define the report descriptor. Warning: this method has to store the length of the report descriptor in reportLength.
         *
         * @returns pointer to the report descriptor
         */
         virtual uint8_t * reportDesc();

     private:
         int16_t _t;     
         int16_t _r;              
         int16_t _x;                       
         int16_t _y;     
         uint32_t _button;
         
         void _init();                 
};

#endif