Ries Twisk / Mbed 2 deprecated JoyStick

Dependencies:   USBDevice mbed-rtos mbed

Fork of JoyStick by Ries Twisk

Files at this revision

API Documentation at this revision

Comitter:
rvt
Date:
Wed Aug 28 02:33:03 2013 +0000
Child:
1:5b2ab44eb31f
Commit message:
Initial release with RTOS

Changed in this revision

AnalogFilterInterface.cpp Show annotated file Show diff for this revision Revisions of this file
AnalogFilterInterface.h Show annotated file Show diff for this revision Revisions of this file
AnalogInFiltered.cpp Show annotated file Show diff for this revision Revisions of this file
AnalogInFiltered.h Show annotated file Show diff for this revision Revisions of this file
AntiLog.cpp Show annotated file Show diff for this revision Revisions of this file
AntiLog.h Show annotated file Show diff for this revision Revisions of this file
LowPassFilter.cpp Show annotated file Show diff for this revision Revisions of this file
LowPassFilter.h Show annotated file Show diff for this revision Revisions of this file
MovingAverage.cpp Show annotated file Show diff for this revision Revisions of this file
MovingAverage.h Show annotated file Show diff for this revision Revisions of this file
USBDevice.lib Show annotated file Show diff for this revision Revisions of this file
USBJoystick.cpp Show annotated file Show diff for this revision Revisions of this file
USBJoystick.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/AnalogFilterInterface.cpp	Wed Aug 28 02:33:03 2013 +0000
@@ -0,0 +1,14 @@
+#include "AnalogFilterInterface.h"
+
+AnalogFilterInterface::AnalogFilterInterface(AnalogFilterInterface *chain) {
+    _chain = chain;
+}
+AnalogFilterInterface::AnalogFilterInterface() {
+}
+
+void AnalogFilterInterface::setData(long data){    
+            _data = data;
+        };
+long AnalogFilterInterface::getData(){
+            return _data;
+        };
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/AnalogFilterInterface.h	Wed Aug 28 02:33:03 2013 +0000
@@ -0,0 +1,21 @@
+#ifndef ANALOGFILTERINTERFACE_H
+#define ANALOGFILTERINTERFACE_H
+
+
+class AnalogFilterInterface
+{
+    private:
+        AnalogFilterInterface *_chain;
+        long _data;
+    public:
+        AnalogFilterInterface(AnalogFilterInterface *chain);
+        AnalogFilterInterface();
+        virtual void setData(long data);
+        virtual long getData();
+        virtual AnalogFilterInterface * getChain(){return _chain;};
+};
+
+#endif
+
+
+                                               
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/AnalogInFiltered.cpp	Wed Aug 28 02:33:03 2013 +0000
@@ -0,0 +1,32 @@
+#include "AnalogInFiltered.h"
+
+
+AnalogInFiltered::AnalogInFiltered(AnalogFilterInterface *filter, PinName pin) {
+    _ain = new AnalogIn(pin);
+    _filter = filter;
+    _lastValue=0;
+}
+
+AnalogInFiltered::~AnalogInFiltered() {
+    delete(_ain);
+    delete(_filter);
+}
+
+long AnalogInFiltered::getValue() {
+    return _filter->getData();
+}
+
+void AnalogInFiltered::measure () {
+    _filter->setData(_ain->read_u16() - 32768);
+}
+
+
+bool AnalogInFiltered::getIsChanged(int offSet) {
+    _filter->setData(_ain->read_u16() - 32768);
+    if (abs(_filter->getData() - _lastValue) > offSet) {
+        _lastValue = _filter->getData();
+        return true;
+    }
+    return false;
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/AnalogInFiltered.h	Wed Aug 28 02:33:03 2013 +0000
@@ -0,0 +1,20 @@
+#ifndef ANALOGINFILTERED_H
+#define ANALOGINFILTERED_H
+
+#include "mbed.h"
+#include "AnalogFilterInterface.h"
+
+class AnalogInFiltered {
+    private:
+        AnalogIn *_ain;
+        long   _lastValue;
+        AnalogFilterInterface   *_filter;
+    public:   
+        AnalogInFiltered(AnalogFilterInterface *filter, PinName pin);
+        ~AnalogInFiltered();
+        void measure ();
+        long getValue();
+        bool getIsChanged(int offSet);
+};
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/AntiLog.cpp	Wed Aug 28 02:33:03 2013 +0000
@@ -0,0 +1,21 @@
+#include "AntiLog.h"
+
+//#include "mbed.h"
+//Serial pc2(USBTX, USBRX); // tx, rx
+
+
+AntiLog::AntiLog(AnalogFilterInterface *chain, double _curve) : AnalogFilterInterface(chain) {
+
+}
+
+AntiLog::~AntiLog() {
+}
+
+void AntiLog::setData(long dataPoint) {
+    getChain()->setData(dataPoint);
+    antiLog = getChain()->getData();
+}
+
+long AntiLog::getData() {
+    return antiLog;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/AntiLog.h	Wed Aug 28 02:33:03 2013 +0000
@@ -0,0 +1,21 @@
+#ifndef ANTILOG_H
+#define ANTILOG_H
+#include "mbed.h"
+#include "AnalogFilterInterface.h"
+
+/**
+Class to remove the logaritmic function of a potentiometer
+**/
+class AntiLog : public AnalogFilterInterface  {
+    private:
+        long antiLog;
+        double curve;
+        int* _filterData;
+    public:   
+        AntiLog(AnalogFilterInterface *chain, double curve);
+        ~AntiLog();
+        virtual void setData(long data);
+        virtual long getData(); 
+};
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LowPassFilter.cpp	Wed Aug 28 02:33:03 2013 +0000
@@ -0,0 +1,21 @@
+#include "LowPassFilter.h"
+
+LowPassFilter::LowPassFilter(AnalogFilterInterface *chain,double alpha) :  AnalogFilterInterface(chain) {
+        _alpha = alpha;
+        _beta = 1.0 - _alpha;
+        smoothedValue = 0.0;
+}
+
+LowPassFilter::~LowPassFilter() {
+}
+
+
+void LowPassFilter::setData(long dataPoint) {
+    getChain()->setData(dataPoint);
+    smoothedValue = (getChain()->getData() * _beta) + (smoothedValue * _alpha);
+}
+
+long LowPassFilter::getData() {
+    return smoothedValue;
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LowPassFilter.h	Wed Aug 28 02:33:03 2013 +0000
@@ -0,0 +1,18 @@
+#ifndef LOWPASSFILTER_H
+#define LOWPASSFILTER_H
+#include "mbed.h"
+#include "AnalogFilterInterface.h"
+
+class LowPassFilter : public AnalogFilterInterface  {
+    private:
+        double _alpha;
+        double _beta;
+        double smoothedValue;
+    public:   
+        LowPassFilter(AnalogFilterInterface *chain, double alpha);
+        ~LowPassFilter();
+        virtual void setData(long data);
+        virtual long getData();
+};
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MovingAverage.cpp	Wed Aug 28 02:33:03 2013 +0000
@@ -0,0 +1,28 @@
+#include "MovingAverage.h"
+
+
+MovingAverage::MovingAverage(uint8_t size) {
+        _size = size;
+        _filterData=(double *)malloc(_size * sizeof(double));
+        for (uint8_t i=0; i<_size; i++) {
+            _filterData[ i ] = 0.0;
+        }
+}
+
+MovingAverage::~MovingAverage() {
+    delete(_filterData);
+}
+
+void MovingAverage::setData(long dataPoint) {
+        memmove (&_filterData[1], &_filterData[0], (_size * sizeof(double)) - sizeof(double));
+        _filterData[0] = dataPoint;
+}
+
+long MovingAverage::getData() {
+    double total=0.0;
+    for (uint8_t i=0; i<_size; i++) {
+        total += _filterData[ i ];
+    }
+    return (total / (double)_size);
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MovingAverage.h	Wed Aug 28 02:33:03 2013 +0000
@@ -0,0 +1,18 @@
+#ifndef MOVINGAVERAGE_H
+#define MOVINGAVERAGE_H
+#include "mbed.h"
+#include "AnalogFilterInterface.h"
+
+class MovingAverage : public AnalogFilterInterface  {
+    protected:
+        double* _filterData;
+        uint8_t _size;
+ 
+    public:   
+        MovingAverage(uint8_t size);
+        ~MovingAverage();
+        virtual void setData(long data);
+        virtual long getData();
+};
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBDevice.lib	Wed Aug 28 02:33:03 2013 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/USBDevice/#1e3d126a322b
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBJoystick.cpp	Wed Aug 28 02:33:03 2013 +0000
@@ -0,0 +1,179 @@
+/* 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 t, int16_t r, int16_t x, int16_t y, uint8_t button, uint8_t hat) {
+             HID_REPORT report;
+   _t = t;
+   _r = r;   
+   _x = x;
+   _y = y;
+   _button = button;     
+   _hat = hat;
+
+   // Fill the report according to the Joystick Descriptor
+   report.data[0] = _t & 0xff;
+   report.data[1] = (_t >> 8) & 0xff;
+   report.data[2] = _r & 0xff;
+   report.data[3] = (_r >> 8) & 0xff;
+   report.data[4] = _x & 0xff;
+   report.data[5] = (_x >> 8) & 0xff;
+   report.data[6] = _y & 0xff;
+   report.data[7] = (_y >> 8) & 0xff;
+   report.data[8] = ((_button & 0x0f) << 4) | (_hat & 0x0f) ;                                      
+   report.length = 9;
+   
+   return send(&report);
+}
+ 
+bool USBJoystick::update() {
+   HID_REPORT report;
+
+   // Fill the report according to the Joystick Descriptor
+   report.data[0] = _t & 0xff;
+   report.data[1] = (_t >> 8) & 0xff;
+   report.data[2] = _r & 0xff;
+   report.data[3] = (_r >> 8) & 0xff;
+   report.data[4] = _x & 0xff;
+   report.data[5] = (_x >> 8) & 0xff;
+   report.data[6] = _y & 0xff;
+   report.data[7] = (_y >> 8) & 0xff;
+   report.data[8] = ((_button & 0x0f) << 4) | (_hat & 0x0f) ;                                      
+   report.length = 9;
+
+   return send(&report);
+}
+
+bool USBJoystick::throttle(int16_t t) {
+     _t = t;
+     return update();
+}
+
+bool USBJoystick::rudder(int16_t r) {
+     _r = r;
+     return update();
+}
+
+bool USBJoystick::move(int16_t x, int16_t y) {
+     _x = x;
+     _y = y;
+     return update();
+}
+
+bool USBJoystick::button(uint8_t button) {
+     _button = button;
+     return update();
+}
+
+bool USBJoystick::hat(uint8_t hat) {
+     _hat = hat;
+     return update();
+}
+
+
+void USBJoystick::_init() {
+
+   _t = 0;
+   _r = 0;    
+   _x = 0;                       
+   _y = 0;     
+   _button = 0x00;
+   _hat = 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
+                          
+               USAGE_PAGE(1), 0x02,            // Simulation Controls
+               USAGE(1), 0xBB,                 // Throttle             
+               USAGE(1), 0xBA,                 // Rudder               
+               LOGICAL_MINIMUM(2), 0x01, 0x80,       // -32767
+                 LOGICAL_MAXIMUM(2), 0xff, 0x7f, // 32767
+               REPORT_SIZE(1), 0x10,
+               REPORT_COUNT(1), 0x02,
+               INPUT(1), 0x02,                 // Data, Variable, Absolute               
+
+               USAGE_PAGE(1), 0x01,            // Generic Desktop
+               USAGE(1), 0x01,                 // Usage (Pointer)
+               COLLECTION(1), 0x00,            // Physical
+                 USAGE(1), 0x30,                 // X
+                 USAGE(1), 0x31,                 // Y
+//  8 bit values
+//                 LOGICAL_MINIMUM(1), 0x81,       // -127
+//                 LOGICAL_MAXIMUM(1), 0x7f,       // 127
+//                 REPORT_SIZE(1), 0x08,
+//                 REPORT_COUNT(1), 0x02,
+//                 INPUT(1), 0x02,                 // Data, Variable, Absolute                  
+// 16 bit values
+               LOGICAL_MINIMUM(2), 0x01, 0x80,       // -32767
+                 LOGICAL_MAXIMUM(2), 0xff, 0x7f, // 32767
+                 REPORT_SIZE(1), 0x10,
+                 REPORT_COUNT(1), 0x02,
+                 INPUT(1), 0x02,                 // Data, Variable, Absolute                
+
+
+               END_COLLECTION(0),               
+// 4 Position Hat Switch
+//               USAGE(1), 0x39,                 // Usage (Hat switch)
+//               LOGICAL_MINIMUM(1), 0x00,       // 0
+//               LOGICAL_MAXIMUM(1), 0x03,       // 3
+//               PHYSICAL_MINIMUM(1), 0x00,      // Physical_Minimum (0)
+//               PHYSICAL_MAXIMUM(2), 0x0E, 0x01, // Physical_Maximum (270)
+//               UNIT(1), 0x14,                  // Unit (Eng Rot:Angular Pos)                            
+//               REPORT_SIZE(1), 0x04,
+//               REPORT_COUNT(1), 0x01,
+//               INPUT(1), 0x02,                 // Data, Variable, Absolute               
+// 8 Position Hat Switch
+               USAGE(1), 0x39,                 // Usage (Hat switch)
+               LOGICAL_MINIMUM(1), 0x00,       // 0
+               LOGICAL_MAXIMUM(1), 0x07,       // 7
+               PHYSICAL_MINIMUM(1), 0x00,      // Physical_Minimum (0)
+               PHYSICAL_MAXIMUM(2), 0x3B, 0x01, // Physical_Maximum (315)
+               UNIT(1), 0x14,                  // Unit (Eng Rot:Angular Pos)                            
+               REPORT_SIZE(1), 0x04,
+               REPORT_COUNT(1), 0x01,
+               INPUT(1), 0x02,                 // Data, Variable, Absolute               
+//
+               USAGE_PAGE(1), 0x09,            // Buttons
+               USAGE_MINIMUM(1), 0x01,         // 1
+               USAGE_MAXIMUM(1), 0x04,         // 4
+               LOGICAL_MINIMUM(1), 0x00,       // 0
+               LOGICAL_MAXIMUM(1), 0x01,       // 1
+               REPORT_SIZE(1), 0x01,
+               REPORT_COUNT(1), 0x04,
+               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;
+}
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBJoystick.h	Wed Aug 28 02:33:03 2013 +0000
@@ -0,0 +1,210 @@
+/* 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,     
+};
+
+#if(0)
+enum JOY_HAT {
+     JOY_HAT_UP      = 0,
+     JOY_HAT_RIGHT   = 1,
+     JOY_HAT_DOWN    = 2,
+     JOY_HAT_LEFT    = 3,     
+     JOY_HAT_NEUTRAL = 4,          
+};
+#else
+enum JOY_HAT {
+     JOY_HAT_UP         = 0,     
+     JOY_HAT_UP_RIGHT   = 1,
+     JOY_HAT_RIGHT      = 2,
+     JOY_HAT_RIGHT_DOWN = 3,
+     JOY_HAT_DOWN       = 4,
+     JOY_HAT_DOWN_LEFT  = 5,     
+     JOY_HAT_LEFT       = 6,     
+     JOY_HAT_LEFT_UP    = 7,          
+     JOY_HAT_NEUTRAL    = 8,          
+};
+#endif
+
+/* X, Y and T limits */
+/* These values do not directly map to screen pixels */
+/* Zero may be interpreted as meaning 'no movement' */
+#define JX_MIN_ABS    (-127)     /*!< The maximum value that we can move to the left on the x-axis */
+#define JY_MIN_ABS    (-127)     /*!< The maximum value that we can move up on the y-axis */
+#define JT_MIN_ABS    (-127)     /*!< The minimum value for the throttle */
+#define JX_MAX_ABS    (127)      /*!< The maximum value that we can move to the right on the x-axis */
+#define JY_MAX_ABS    (127)      /*!< The maximum value that we can move down on the y-axis */
+#define JT_MAX_ABS    (127)      /*!< The maximum value for the throttle */
+
+/**
+ *
+ * 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
+         * @param hat hat state 0 (up), 1 (right, 2 (down), 3 (left) or 4 (neutral)
+         * @returns true if there is no error, false otherwise
+         */
+         bool update(int16_t t, int16_t r, int16_t x, int16_t y, uint8_t buttons, uint8_t hat);
+
+         /**
+         * 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(uint8_t button);
+         
+         /**
+         * Press hat
+         *
+         * @param hat hat state
+         * @returns true if there is no error, false otherwise
+         */
+         bool hat(uint8_t hat);
+         
+         /*
+         * 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;     
+         uint8_t _button;
+         uint8_t _hat; 
+         
+         void _init();                 
+};
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Aug 28 02:33:03 2013 +0000
@@ -0,0 +1,158 @@
+#include "mbed.h"
+#include "USBHID.h"
+#include "USBJoystick.h"
+#include "MovingAverage.h"
+#include "LowPassFilter.h"
+#include "AntiLog.h"
+#include "AnalogInFiltered.h"
+#include "rtos.h"
+
+
+#define TTY_DEBUG
+
+// Value that defines when to start sending data this prevents the noise sending loads's of data over HID
+#define DATA_CHANGE_TRIGGER 64
+
+Serial pc(USBTX, USBRX); // tx, rx
+
+// Activity led for HID data transmissions
+DigitalOut HIDActivity(LED3);
+
+//This report will contain data to be sent
+HID_REPORT send_report;
+HID_REPORT recv_report;
+
+//USBJoyStick;
+USBJoystick joystick;
+
+// Structure that hold's the dataset of the input's
+Mutex analogValueMutex;
+struct AnalogData {
+    long value1;
+    long value2;
+    long value3;
+    long value4;
+    long value5;
+} analogData;
+
+void debug_thread(void const *args)
+{
+    while (true) {
+        // Make a local copy
+        AnalogData localCopy;
+        AnalogData previous;
+        analogValueMutex.lock();
+        memcpy (&localCopy, &analogData, sizeof(AnalogData));
+        analogValueMutex.unlock();
+
+        // Send to USB
+        pc.printf("\x1B[0;0H");
+        pc.printf("Yoke and Pedals!\n\r");
+        pc.printf("Analog in p20: %d  diff: %d    \n\r",localCopy.value1,localCopy.value1-previous.value1);
+        pc.printf("Analog in p19: %d  diff: %d    \n\r",localCopy.value2,localCopy.value2-previous.value2);
+        pc.printf("Analog in p18: %d  diff: %d    \n\r",localCopy.value3,localCopy.value3-previous.value3);
+        pc.printf("Analog in p17: %d  diff: %d    \n\r",localCopy.value4,localCopy.value4-previous.value4);
+        pc.printf("Analog in p16: %d  diff: %d    \n\r",localCopy.value5,localCopy.value5-previous.value5);
+        
+        memcpy (&previous, &localCopy, sizeof(AnalogData));
+
+
+        Thread::wait(1000);
+    }
+}
+
+void hid_thread(void const *args)
+{
+    while (true) {
+
+        // TODO read buttons
+        uint8_t buttons=0;
+        uint8_t hat=0;
+        AnalogData localCopy;
+
+        // Wait for analog in to have some data
+        Thread::signal_wait(0x1);
+
+        // Make a local copy of the data
+        analogValueMutex.lock();
+        memcpy (&localCopy, &analogData, sizeof(AnalogData));
+        analogValueMutex.unlock();
+
+        // Update joystick's info
+        joystick.update(
+            localCopy.value2,
+            localCopy.value3,
+            localCopy.value4,
+            localCopy.value5,
+            buttons,
+            hat);
+
+        HIDActivity=!HIDActivity;
+
+        // Wait 50 ms to send a other USB update
+        Thread::wait(50);
+    }
+}
+
+
+
+int main()
+{
+    analogData.value1=0;
+    analogData.value2=0;
+    analogData.value3=0;
+    analogData.value4=0;
+    analogData.value5=0;
+
+#ifdef TTY_DEBUG
+    Thread _debugThread(debug_thread);
+#endif
+    Thread _hid_thread(hid_thread);
+
+    // Initialise moving average filters
+    LowPassFilter lowPassFilter1(new AntiLog(new AnalogFilterInterface(),-5.0),0.95f);   // The close the alpha value is to 1, the lower the cut-off frequency
+    LowPassFilter lowPassFilter2(new AntiLog(new AnalogFilterInterface(),-5.0),0.95f);
+    LowPassFilter lowPassFilter3(new AntiLog(new AnalogFilterInterface(),-5.0),0.95f);
+    LowPassFilter lowPassFilter4(new AntiLog(new AnalogFilterInterface(),-5.0),0.95f);
+    LowPassFilter lowPassFilter5(new AntiLog(new AnalogFilterInterface(),-5.0),0.95f);
+
+    // Initialise analog input and tell it what fulters to use
+    AnalogInFiltered ai1(&lowPassFilter1, p20);
+    AnalogInFiltered ai2(&lowPassFilter2, p19);
+    AnalogInFiltered ai3(&lowPassFilter3, p18);
+    AnalogInFiltered ai4(&lowPassFilter4, p17);
+    AnalogInFiltered ai5(&lowPassFilter5, p16);
+
+    while (true) {
+        // Measure analog in's
+        ai1.measure();
+        ai2.measure();
+        ai3.measure();
+        ai4.measure();
+        ai5.measure();
+
+        // test of any of the values have been changed, so we only update when data was actually changed
+        if (
+                false
+                || ai1.getIsChanged(DATA_CHANGE_TRIGGER) // Value of 4 seems to wobble a bit, 8 is good
+                || ai2.getIsChanged(DATA_CHANGE_TRIGGER)
+                || ai3.getIsChanged(DATA_CHANGE_TRIGGER)
+                || ai4.getIsChanged(DATA_CHANGE_TRIGGER)
+//                || ai5.getIsChanged(8)
+           ) {
+            // Copy analog data to global data
+            analogValueMutex.lock();
+            analogData.value1 = ai1.getValue();
+            analogData.value2 = ai2.getValue();
+            analogData.value3 = ai3.getValue();
+            analogData.value4 = ai4.getValue();
+            analogData.value5 = ai5.getValue();
+            analogValueMutex.unlock();
+
+            // Signal that data has been changed
+            _hid_thread.signal_set(0x1);
+        }
+        Thread::wait(1);
+    }
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-rtos.lib	Wed Aug 28 02:33:03 2013 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed-rtos/#869ef732a8a2
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Wed Aug 28 02:33:03 2013 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/9c8f0e3462fb
\ No newline at end of file