4-axis USB controller for FPV sims, based on NXP FRDM-K22F

Dependencies:   mbed USBDevice

Files at this revision

API Documentation at this revision

Comitter:
wue
Date:
Thu Mar 05 20:57:25 2020 +0000
Commit message:
???

Changed in this revision

USBDevice.lib Show annotated file Show diff for this revision Revisions of this file
USBJoystick/USBJoystick.cpp Show annotated file Show diff for this revision Revisions of this file
USBJoystick/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.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBDevice.lib	Thu Mar 05 20:57:25 2020 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/wue/code/USBDevice/#092f8c67730c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBJoystick/USBJoystick.cpp	Thu Mar 05 20:57:25 2020 +0000
@@ -0,0 +1,100 @@
+/* mbed USBJoystick Library
+ * Copyright (c) 2012, v01:  Initial version, WH,
+ *                           Modified USBMouse code ARM Limited.
+ *                           (c) 2010-2011 mbed.org, MIT License
+ *               2016, v02:  Updated USBDevice Lib, Added waitForConnect, Updated 32 bits button 
+ *
+ * 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, inclumosig 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, INCLUmosiG 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 rx, int16_t ry) {
+
+   _x = x;
+   _y = y;   
+   _rx = rx;
+   _ry = ry;
+
+   return update();
+}
+ 
+bool USBJoystick::update() {
+   HID_REPORT report;
+
+   // Fill the report according to the Joystick Descriptor
+   report.data[0] = _x & 0xff;
+   report.data[1] = (_x >> 8) & 0xff;
+   report.data[2] = _y & 0xff;
+   report.data[3] = (_y >> 8) & 0xff;
+   report.data[4] = _rx & 0xff;
+   report.data[5] = (_rx >> 8) & 0xff;
+   report.data[6] = _ry & 0xff;
+   report.data[7] = (_ry >> 8) & 0xff;
+   report.length = 8;
+
+   return send(&report);
+}
+
+bool USBJoystick::move(int16_t x, int16_t y, int16_t rx, int16_t ry) {
+   _x = x;
+   _y = y;
+   _rx = rx;
+   _ry = ry;
+   return update();
+}
+
+void USBJoystick::_init() {
+   _x = 0x0000;
+   _y = 0x0000;
+   _rx = 0x0000;
+   _ry = 0x0000;
+}
+
+
+uint8_t * USBJoystick::reportDesc() {    
+         static uint8_t reportDescriptor[] = {
+            // value in () is the number of bytes.  These bytes follow the comma, least significant byte first
+            // see USBHID_Types.h for more info
+             USAGE_PAGE(1), 0x01,           // Generic Desktop                 
+             USAGE(1), 0x04,                // Usage (Joystick)
+             COLLECTION(1), 0x01,           // Application
+             
+// 6 Axes of Joystick
+               USAGE_PAGE(1), 0x01,            // Generic Desktop
+               USAGE(1), 0x01,                 // Usage (Pointer)
+               COLLECTION(1), 0x00,            // Physical
+                 USAGE(1), 0x30,                 // X
+                 USAGE(1), 0x31,                 // Y
+                 USAGE(1), 0x33,                 // RX
+                 USAGE(1), 0x34,                 // RY
+                 LOGICAL_MINIMUM(2), 0x00, 0x80, // -32768 (using 2's complement)
+                 LOGICAL_MAXIMUM(2), 0xff, 0x7f, // 32767 (0x7fff, least significant byte first)
+                 REPORT_SIZE(1), 0x10,  // REPORT_SIZE describes the number of bits in this element (16, in this case)
+                 REPORT_COUNT(1), 0x04,
+                 INPUT(1), 0x02,                 // Data, Variable, Absolute                
+               END_COLLECTION(0),
+             END_COLLECTION(0)
+      };
+
+      reportLength = sizeof(reportDescriptor);
+      return reportDescriptor;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBJoystick/USBJoystick.h	Thu Mar 05 20:57:25 2020 +0000
@@ -0,0 +1,104 @@
+/* mbed USBJoystick Library
+ * Copyright (c) 2012, v01:  Initial version, WH,
+ *                           Modified USBMouse code ARM Limited.
+ *                           (c) 2010-2011 mbed.org, MIT License
+ *               2016, v02:  Updated USBDevice Lib, Added waitForConnect, Updated 32 bits button 
+ *
+ * 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, inclumosig 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, INCLUmosiG 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.
+ */
+
+#ifndef USBJOYSTICK_H
+#define USBJOYSTICK_H
+
+#include "USBHID.h"
+
+#define REPORT_ID_JOYSTICK  4
+
+
+
+/* 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    (-32768)     /*!< The maximum value that we can move to the left on the x-axis */
+#define JY_MIN_ABS    (-32768)     /*!< The maximum value that we can move up on the y-axis */
+#define JT_MIN_ABS    (-32768)     /*!< The minimum value for the throttle */
+#define JX_MAX_ABS    (32767)      /*!< The maximum value that we can move to the right on the x-axis */
+#define JY_MAX_ABS    (32767)      /*!< The maximum value that we can move down on the y-axis */
+#define JT_MAX_ABS    (32767)      /*!< The maximum value for the throttle */
+
+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, int waitForConnect = true):    // 4 buttons, no padding on buttons
+//     USBJoystick(uint16_t vendor_id = 0x1234, uint16_t product_id = 0x0500, uint16_t product_release = 0x0001, int waitForConnect = true):    // 8 buttons, no padding on buttons
+     USBJoystick(uint16_t vendor_id = 0x1234, uint16_t product_id = 0x0702, uint16_t product_release = 0x0001, int waitForConnect = true):    // 32 buttons, no padding on buttons
+       USBHID(0, 0, vendor_id, product_id, product_release, false) {
+         _init();
+         connect(waitForConnect);
+     };
+         
+     /**
+       * Write state of the joystick
+       *
+       * @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 x, int16_t y, int16_t rx, int16_t ry);
+
+     /**
+       * Write state of the joystick
+       *
+       * @returns true if there is no error, false otherwise
+       */
+     bool update();       
+
+     /**
+       * 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, int16_t rx, int16_t ry);
+         
+
+     virtual uint8_t * reportDesc();
+
+   private:
+     int16_t _x;
+     int16_t _y;
+     int16_t _rx;
+     int16_t _ry;
+     
+     void _init();                 
+};
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Mar 05 20:57:25 2020 +0000
@@ -0,0 +1,37 @@
+#include "mbed.h"
+#include "USBJoystick.h"
+DigitalOut led_red(LED_RED);
+DigitalOut led_green(LED_GREEN);
+DigitalIn sw2(SW2);
+DigitalIn sw3(SW3);
+Serial pc(USBTX, USBRX);
+
+AnalogIn throt(A0);
+AnalogIn yaw(A1);
+AnalogIn pitch(A2);
+AnalogIn roll(A3);
+
+USBJoystick joystick;
+
+int main() {
+    int c;
+    led_green = 1;
+    led_red = 1;
+    pc.baud(115200);
+    pc.printf("Hello World from FRDM-K64F board.\n");
+
+    while (true) {
+        c += 1;
+        joystick.update(
+            throt.read() * 32767.0,
+            yaw.read() * 32767.0,
+            pitch.read() * 32767.0,
+            roll.read() * 32767.0
+        );
+
+        led_red = 1;
+        wait(0.005);
+        //led_red = 0;
+        //wait(0.01);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Thu Mar 05 20:57:25 2020 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400
\ No newline at end of file