USBJoystick updated for 32 buttons and added wait-for-connect.
Diff: USBJoystick.cpp
- Revision:
- 2:de0ca539fa79
- Parent:
- 1:f44ae6479bfe
- Child:
- 3:1f1c9178c55e
--- a/USBJoystick.cpp Sun Jul 25 18:01:15 2021 +0000 +++ b/USBJoystick.cpp Sun Aug 01 11:49:38 2021 +0000 @@ -26,6 +26,7 @@ #include "stdint.h" #include "USBJoystick.h" +#include "../buffer_builder.hpp" bool USBJoystick::update(int16_t t, int16_t r, int16_t x, int16_t y, uint32_t buttons, uint8_t hat) { @@ -42,30 +43,27 @@ 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] = _x & 0xff; - report.data[5] = _y & 0xff; - - -#if (BUTTONS4 == 1) + uint8_t* data = &report.data[0]; + buffer_builder::put_u16le(data, _t); + buffer_builder::put_u16le(data, _r); + buffer_builder::put_u16le(data, _x); + buffer_builder::put_u16le(data, _y); + buffer_builder::put_u16le(data, _t); + // buffer_builder::put_u16le(data, 0x5678); +#if (BUTTONS4 == 1 && 0) //Hat and 4 Buttons // report.data[4] = ((_buttons & 0x0f) << 4) | (_hat & 0x0f) ; // report.length = 5; //Use 4 bit padding for hat4 or hat8 - report.data[4] = (_hat & 0x0f) ; + buffer_builder::put_u8(data, _hat); //Use 4 bit padding for buttons - report.data[5] = (_buttons & 0x0f) ; - report.length = 6; + buffer_builder::put_u8(data, _buttons); #endif -#if (BUTTONS8 == 1) +#if (BUTTONS8 == 1 && 0) //Hat and first 4 Buttons // report.data[4] = ((_buttons & 0x0f) << 4) | (_hat & 0x0f) ; // @@ -74,25 +72,21 @@ // report.length = 6; //Use 4 bit padding for hat4 or hat8 - report.data[4] = (_hat & 0x0f) ; + buffer_builder::put_u8(data, _hat); //Use 8 bits for buttons - report.data[5] = (_buttons & 0xff) ; - report.length = 6; + buffer_builder::put_u8(data, _buttons); #endif -#if (BUTTONS32 == 1) +#if (BUTTONS32 == 1 && 0) //Use 4 bit padding for hat4 or hat8 - report.data[6] = (_hat & 0x0f) ; +// report.data[4] = (_hat & 0x0f) ; +// buffer_builder::put_u8(data, _hat); //No bit padding for 32 buttons - report.data[7] = (_buttons >> 0) & 0xff; - report.data[8] = (_buttons >> 8) & 0xff; - report.data[9] = (_buttons >> 16) & 0xff; - report.data[10] = (_buttons >> 24) & 0xff; - report.length = 11; + // buffer_builder::put_u32le(data, _buttons); #endif - + report.length = data - &report.data[0]; return send(&report); } @@ -137,40 +131,46 @@ 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 +#if 0 USAGE_PAGE(1), 0x02, // Simulation Controls USAGE(1), 0xBB, // Throttle - USAGE(1), 0xBA, // Rudder + USAGE(1), 0xBA, // Rudder + + /* LOGICAL_MINIMUM(1), 0x81, // -127 LOGICAL_MAXIMUM(1), 0x7f, // 127 REPORT_SIZE(1), 0x08, +*/ + 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 + INPUT(1), 0x02, // Data, Variable, Absolute +#endif +#if 1 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), 0x32, // Z USAGE(1), 0x33, // RX + USAGE(1), 0x34, // RY // 8 bit values - LOGICAL_MINIMUM(1), 0x81, // -127 - LOGICAL_MAXIMUM(1), 0x7f, // 127 - REPORT_SIZE(1), 0x08, - REPORT_COUNT(1), 0x04, - INPUT(1), 0x02, // Data, Variable, Absolute + // LOGICAL_MINIMUM(1), 0x81, // -127 + // LOGICAL_MAXIMUM(1), 0x7f, // 127 + // REPORT_SIZE(1), 0x08, + // REPORT_COUNT(1), 0x04, + // 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), 0x02, -// INPUT(1), 0x02, // Data, Variable, Absolute + LOGICAL_MINIMUM(2), 0x01, 0x80, // -32767 + LOGICAL_MAXIMUM(2), 0xff, 0x7f, // 32767 + REPORT_SIZE(1), 0x10, + REPORT_COUNT(1), 0x05, + INPUT(1), 0x02, // Data, Variable, Absolute - END_COLLECTION(0), - -#if (HAT4 == 1) +#endif +#if (HAT4 == 1 && 0) // 4 Position Hat Switch USAGE(1), 0x39, // Usage (Hat switch) LOGICAL_MINIMUM(1), 0x00, // 0 @@ -182,7 +182,7 @@ REPORT_COUNT(1), 0x01, INPUT(1), 0x02, // Data, Variable, Absolute #endif -#if (HAT8 == 1) +#if (HAT8 == 1 && 0) // 8 Position Hat Switch USAGE(1), 0x39, // Usage (Hat switch) LOGICAL_MINIMUM(1), 0x00, // 0 @@ -196,9 +196,9 @@ #endif // Padding 4 bits - REPORT_SIZE(1), 0x01, - REPORT_COUNT(1), 0x04, - INPUT(1), 0x01, // Constant +// REPORT_SIZE(1), 0x01, +// REPORT_COUNT(1), 0x04, +// INPUT(1), 0x01, // Constant #if (BUTTONS4 == 1) @@ -234,7 +234,7 @@ INPUT(1), 0x02, // Data, Variable, Absolute #endif -#if (BUTTONS32 == 1) +#if (BUTTONS32 == 1 && 0) // 32 Buttons USAGE_PAGE(1), 0x09, // Buttons USAGE_MINIMUM(1), 0x01, // 1