May not work well
Dependencies: USBDevice_edit mbed
Revision 0:7e061992d951, committed 2017-05-04
- Comitter:
- Ugly_Kid
- Date:
- Thu May 04 13:37:26 2017 +0000
- Commit message:
- Teste de USB_HID
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/README.txt Thu May 04 13:37:26 2017 +0000 @@ -0,0 +1,7 @@ +Este programa emula o funcionamento do controle dual shock 3 para um sistema ps3 + +USBHID_Types.h tem a definição de valores para o descriptor! + +USBJoystick.h tem vendoID e ProductID + +It WORKS!!!! \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/USBDevice_edit.lib Thu May 04 13:37:26 2017 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/users/Ugly_Kid/code/USBDevice_edit/#dc16be71dc2b
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/USBJoystick.cpp Thu May 04 13:37:26 2017 +0000 @@ -0,0 +1,312 @@ +/* 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] = _r & 0xff; + report.data[2] = _x & 0xff; + report.data[3] = __x & 0xff; + report.data[4] = ((_button & 0x0f) << 4) | (_hat & 0x0f) ; + report.length = 5; +*/ + // Fill the report according to the Joystick Descriptor +report.data[0] = 0x01; // report_id +report.data[1] = 0x00; //reserved +report.data[2] = (_button & 0x0f) << 4 | (_button & 0x0f) ; // buttons 1 - 8 ab +//report.data[3] = ((_button & 0x0f) << 4) | (_hat & 0x0f) ;; // buttons 17 - 19 / filling bits 1 -5 ef +report.data[3] = (_button & 0x0f) << 4 | (_button & 0x0f) ; //buttons 9 - 16 cd sq x 0 tr +report.data[4] = (_button & 0x0f); // buttons 17 - 19 / filling bits 1 -5 ef +report.data[5] = 0x00; // filling bits 6 - 13 / 32 bits total +report.data[6] = _t & 0xff; // X +report.data[7] = _r & 0xff; // Y +report.data[8] = _x & 0xff; // Z + report.data[9] = _y & 0xff; // Rz + report.data[10] = _y & 0xff; + report.data[11] = _y & 0xff; + report.data[12] = _y & 0xff;// directional analog up + report.data[13] = _y & 0xff;// directional analog left + report.data[14] = _y & 0xff;// directional analog down + report.data[15] = _y & 0xff;// directional analog right + report.data[16] = _y & 0xff;// analog left 2 + report.data[17] = _y & 0xff;// analog right 2 + report.data[18] = _y & 0xff;// analog left 1 + report.data[19] = _y & 0xff;// analog right 1 + report.data[20] = _y & 0xff;// triangle analog + report.data[21] = _y & 0xff;// circle analog + report.data[22] = _y & 0xff;// x analog + report.data[23] = _y & 0xff;// square analog + report.data[24] = _y & 0xff; + report.data[25] = _y & 0xff; + report.data[26] = _y & 0xff; + report.data[27] = _y & 0xff; // 03 ? + report.data[28] = _y & 0xff; // ef ? + report.data[29] = _y & 0xff; // 16 ? + report.data[30] = _y & 0xff; + report.data[31] = _y & 0xff; + report.data[32] = _y & 0xff; + report.data[33] = _y & 0xff; + report.data[34] = _y & 0xff;//Accel x 4 + report.data[35] = _y & 0xff;//Accel x 3 + report.data[36] = _y & 0xff;//Accel x 2 + report.data[37] = _y & 0xff;//Accel x 1 + report.data[38] = _y & 0xff;//Accel y 4 + report.data[39] = _y & 0xff;//Accel y 3 + report.data[40] = _y & 0xff;//Accel y 2 + report.data[41] = _y & 0xff;//Accel y 1 + report.data[42] = _y & 0xff;//Accel z 4 + report.data[43] = _y & 0xff;//Accel z 3 + report.data[44] = _y & 0xff;//Accel z 2 + report.data[45] = _y & 0xff;//Accel z 1 + report.data[46] = _y & 0xff; + report.data[47] = _y & 0xff; + report.data[48] = _y & 0xff; + + report.length = 49; + + + 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] = _r & 0xff; + report.data[2] = _x & 0xff; + report.data[3] = _y & 0xff; + report.data[4] = ((_button & 0x0f) << 4) | (_hat & 0x0f) ; + report.length = 5; + + return send(&report); +} +*/ +bool USBJoystick::update() { + HID_REPORT report; + + // Fill the report according to the Joystick Descriptor +/* //report.data[0] = 0x01; + report.data[0] = _t & 0xff; + report.data[1] = _r & 0xff; + report.data[2] = _x & 0xff; + report.data[3] = _y & 0xff; + report.data[4] = ((_button & 0x0f) << 4) | (_hat & 0x0f) ; + */ +// Fill the report according to the Joystick Descriptor +report.data[0] = 0x01; //reserved +report.data[1] = 0x00;; // buttons 1 - 8 +report.data[2] = 0x00; // buttons 9 - 16 +report.data[3] = 0x00; // buttons 17 - 19 / filling bits 1 -5 +report.data[4] = 0x00; // filling bits 6 - 13 / 32 bits total +report.data[5] = 0x7d; // X +report.data[6] = 0x7d; // Y +report.data[7] = 0x7d; // Z +report.data[8] = 0x80; // Rz + report.data[9] = 0x00; + report.data[10] = 0x00; + report.data[11] = 0x00; + report.data[12] = 0x00;// directional analog up + report.data[13] = 0x00;// directional analog left + report.data[14] = 0x00;// directional analog down + report.data[15] = 0x00;// directional analog right + report.data[16] = 0x00;// analog left 2 + report.data[17] = 0x00;// analog right 2 + report.data[18] = 0x00;// analog left 1 + report.data[19] = 0x00;// analog right 1 + report.data[20] = 0x00;// triangle analog + report.data[21] = 0x00;// circle analog + report.data[22] = 0x00;// x analog + report.data[23] = 0x00;// square analog + report.data[24] = 0x00; + report.data[25] = 0x00; + report.data[26] = 0x00; + report.data[27] = 0x00; // 03 ? + report.data[28] = 0x00; // ef ? + report.data[29] = 0x00; // 16 ? + report.data[30] = 0x00; + report.data[31] = 0x00; + report.data[32] = 0x00; + report.data[33] = 0x00; + report.data[34] = 0x00;//Accel x 4 + report.data[35] = 0x00;//Accel x 3 + report.data[36] = 0x00;//Accel x 2 + report.data[37] = 0x00;//Accel x 1 + report.data[38] = 0x00;//Accel y 4 + report.data[39] = 0x00;//Accel y 3 + report.data[40] = 0x00;//Accel y 2 + report.data[41] = 0x00;//Accel y 1 + report.data[42] = 0x00;//Accel z 4 + report.data[43] = 0x00;//Accel z 3 + report.data[44] = 0x00;//Accel z 2 + report.data[45] = 0x00;//Accel z 1 + report.data[46] = 0x00; + report.data[47] = 0x00; + report.data[48] = 0x00; + + report.length = 49; + + 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 = -127; + _r = -127; + _x = 0; + _y = 0; + _button = 0x0f; + _hat = 0x00; +} + + +uint8_t * USBJoystick::reportDesc() { + static uint8_t reportDescriptor[] = { + +0x05, 0x01, // Usage Page (Generic Desktop Ctrls) +0x09, 0x04, // Usage (Joystick) +0xA1, 0x01, // Collection (Physical) +0xA1, 0x02, // Collection (Application) +0x85, 0x01, // Report ID (1) +0x75, 0x08, // Report Size (8) +0x95, 0x01, // Report Count (1) //byte 00 +0x15, 0x00, // Logical Minimum (0) +0x26, 0xFF, 0x00, // Logical Maximum (255) +0x81, 0x03, // Input (Const,Var,Abs,No Wrap,Linear,Preferred State,No Null Position) + // NOTE: reserved byte +0x75, 0x01, // Report Size (1) +0x95, 0x13, // Report Count (19) +0x15, 0x00, // Logical Minimum (0) +0x25, 0x01, // Logical Maximum (1) +0x35, 0x00, // Physical Minimum (0) +0x45, 0x01, // Physical Maximum (1) +0x05, 0x09, // Usage Page (Button) +0x19, 0x01, // Usage Minimum (0x01) +0x29, 0x13, // Usage Maximum (0x13) +0x81, 0x02, // Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position) +0x75, 0x01, // Report Size (1) +0x95, 0x0D, // Report Count (13) // byte 1 - 4 +0x06, 0x00, 0xFF, // Usage Page (Vendor Defined 0xFF00) +0x81, 0x03, // Input (Const,Var,Abs,No Wrap,Linear,Preferred State,No Null Position) + // NOTE: 32 bit integer, where 0:18 are buttons and 19:31 are reserved +0x15, 0x00, // Logical Minimum (0) +0x26, 0xFF, 0x00, // Logical Maximum (255) +0x05, 0x01, // Usage Page (Generic Desktop Ctrls) +0x09, 0x01, // Usage (Pointer) +0xA1, 0x00, // Collection (Undefined) +0x75, 0x08, // Report Size (8) +0x95, 0x04, // Report Count (4) +0x35, 0x00, // Physical Minimum (0) +0x46, 0xFF, 0x00, // Physical Maximum (255) +0x09, 0x30, // Usage (X) +0x09, 0x31, // Usage (Y) +0x09, 0x32, // Usage (Z) +0x09, 0x35, // Usage (Rz) +0x81, 0x02, // Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position) + // NOTE: four joysticks +0xC0, // End Collection +0x05, 0x01, // Usage Page (Generic Desktop Ctrls) +0x75, 0x08, // Report Size (8) +0x95, 0x27, // Report Count (39) +0x09, 0x01, // Usage (Pointer) +0x81, 0x02, // Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position) +//0xC0, // End Collection +//0xC0 // End Collection + +0x75, 0x08, // Report Size (8) +0x95, 0x30, // Report Count (48) +0x09, 0x01, // Usage (Pointer) +0x91, 0x02, // Output (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile) +0x75, 0x08, // Report Size (8) +0x95, 0x30, // Report Count (48) +0x09, 0x01, // Usage (Pointer) +0xB1, 0x02, // Feature (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile) +0xC0, // End Collection +0xA1, 0x02, // Collection (Application) +0x85, 0x02, // Report ID (2) +0x75, 0x08, // Report Size (8) +0x95, 0x30, // Report Count (48) +0x09, 0x01, // Usage (Pointer) +0xB1, 0x02, // Feature (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile) +0xC0, // End Collection +0xA1, 0x02, // Collection (Application) +0x85, 0xEE, // Report ID (238) +0x75, 0x08, // Report Size (8) +0x95, 0x30, // Report Count (48) +0x09, 0x01, // Usage (Pointer) +0xB1, 0x02, // Feature (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile) +0xC0, // End Collection +0xA1, 0x02, // Collection (Application) +0x85, 0xEF, // Report ID (239) +0x75, 0x08, // Report Size (8) +0x95, 0x30, // Report Count (48) +0x09, 0x01, // Usage (Pointer) +0xB1, 0x02, // Feature (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile) +0xC0, // End Collection +0xC0, // End Collection + +// 148 bytes + + }; + + reportLength = sizeof(reportDescriptor); + return reportDescriptor; +} + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/USBJoystick.h Thu May 04 13:37:26 2017 +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 = 0x1233, uint16_t product_id = 0x003, 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: + int8_t _t; + int8_t _r; + int8_t _x; + int8_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 Thu May 04 13:37:26 2017 +0000 @@ -0,0 +1,112 @@ +#include "mbed.h" +//#include "USBMouse.h" +#include "USBJoystick.h" + +//USBMouse mouse; +USBJoystick joystick; + +// Variables for Heartbeat and Status monitoring +DigitalOut myled1(LED1); +DigitalOut myled2(LED2); +DigitalOut myled3(LED3); +DigitalOut heartbeatLED(LED4); +DigitalIn B1(p5); +DigitalIn B2(p6); +DigitalIn B3(p7); +DigitalIn B4(p8); +DigitalIn B5(p9); +AnalogIn IN(p20); +Ticker heartbeat; +//Serial pc(USBTX, USBRX); // tx, rx + +// Heartbeat monitor +void pulse() { + heartbeatLED = !heartbeatLED; +} + +void heartbeat_start() { + heartbeat.attach(&pulse, 0.5); +} + +void heartbeat_stop() { + heartbeat.detach(); +} + + +int main() { + //int16_t i = 0; + int8_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; + int counter = 0; + B1.mode(PullUp); + B2.mode(PullUp); + B3.mode(PullUp); + B4.mode(PullUp); + B5.mode(PullUp); + +// pc.printf("Hello World!\n\r"); + + heartbeat_start(); + + while (1) { + // Basic Joystick +// throttle = (i >> 8) & 0x00; // value -127 .. 128 +// rudder = (i >> 8) & 0x00; // value -127 .. 128 + throttle = IN.read_u16(); + // if (throttle == 0) + // throttle = 127; + // else throttle = 0;// value -127 .. 128 + //printf("%d \r\n", throttle); + rudder = i; // value -127 .. 128 + + + //button = (i >> 8) & 0x00; // value 0 .. 15, one bit per button + //button = i; // value 0 .. 15, one bit per button +// hat = (i >> 8) & 0x03; // value 0, 1, 2, 3 or 4 for neutral + hat = (i >> 8) & 0x00; // value 0..7 or 8 for neutral + //i++; + + //throttle = cos((double)angle*3.14/180.0)*radius; // value -127 .. 128 + //rudder = cos((double)angle*3.14/180.0)*radius; // value -127 .. 128 + + 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; + /* + if (!B1){ + //i = 1; + button = 2; + myled3=1; + } + else{ + //i = 0; + button = 0; + myled3=0; + } + */ + if (button == 0) + { + button = i; + i++; + } + + else button = 0; + joystick.update(throttle, rudder, x, y, button, hat); + if (counter == 50) + { + myled2 = !myled2; + counter = 0; + } + counter++; + wait(0.1); + } + + // pc.printf("Bye World!\n\r"); +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Thu May 04 13:37:26 2017 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/dbbf35b96557 \ No newline at end of file