Format imported KeyboardioHID code with clang-format

Signed-off-by: Michael Richters <gedankenexperimenter@gmail.com>
f/driver/keyboardiohid
Michael Richters 3 years ago
parent eb94f08c35
commit 04fba96786
No known key found for this signature in database
GPG Key ID: 1288FD13E4EEF0C0

@ -19,18 +19,13 @@
#include "kaleidoscope/device/virtual/Virtual.h" #include "kaleidoscope/device/virtual/Virtual.h"
// From Arduino:
#include <EEPROM.h> // for EEPROMClass, EERef #include <EEPROM.h> // for EEPROMClass, EERef
// From KeyboardioHID:
#include <HIDReportObserver.h> // for HIDReportObserver
// From system:
#include <stdint.h> // for uint8_t, uint16_t #include <stdint.h> // for uint8_t, uint16_t
#include <stdlib.h> // for exit, size_t #include <stdlib.h> // for exit, size_t
#include <virtual_io.h> // for getLineOfInput, isInte... #include <virtual_io.h> // for getLineOfInput, isInte...
#include <sstream> // for operator<<, string #include <sstream> // for operator<<, string
#include <string> // for operator==, char_traits #include <string> // for operator==, char_traits
// From Kaleidoscope:
#include "kaleidoscope/KeyAddr.h" // for MatrixAddr, MatrixAddr... #include "kaleidoscope/KeyAddr.h" // for MatrixAddr, MatrixAddr...
#include "kaleidoscope/device/virtual/DefaultHIDReportConsumer.h" // for DefaultHIDReportConsumer #include "kaleidoscope/device/virtual/DefaultHIDReportConsumer.h" // for DefaultHIDReportConsumer
#include "kaleidoscope/device/virtual/Logging.h" // for log_error, logging #include "kaleidoscope/device/virtual/Logging.h" // for log_error, logging

@ -82,26 +82,26 @@ static const uint8_t BOOT_KEYBOARD_EP_SIZE = USB_EP_SIZE;
#endif #endif
BootKeyboard_::BootKeyboard_(uint8_t protocol_) : PluggableUSBModule(1, 1, epType), default_protocol(protocol_), protocol(protocol_), idle(1), leds(0) { BootKeyboard_::BootKeyboard_(uint8_t protocol_)
: PluggableUSBModule(1, 1, epType), default_protocol(protocol_), protocol(protocol_), idle(1), leds(0) {
#ifdef ARCH_HAS_CONFIGURABLE_EP_SIZES #ifdef ARCH_HAS_CONFIGURABLE_EP_SIZES
epType[0] = EP_TYPE_INTERRUPT_IN(BOOT_KEYBOARD_EP_SIZE); // This is an 8 byte report, so ask for an 8 byte buffer, so reports aren't split epType[0] = EP_TYPE_INTERRUPT_IN(BOOT_KEYBOARD_EP_SIZE); // This is an 8 byte report, so ask for an 8 byte buffer, so reports aren't split
#else #else
epType[0] = EP_TYPE_INTERRUPT_IN; epType[0] = EP_TYPE_INTERRUPT_IN;
#endif #endif
PluggableUSB().plug(this); PluggableUSB().plug(this);
} }
int BootKeyboard_::getInterface(uint8_t* interfaceCount) { int BootKeyboard_::getInterface(uint8_t *interfaceCount) {
*interfaceCount += 1; // uses 1 *interfaceCount += 1; // uses 1
HIDDescriptor hidInterface = { HIDDescriptor hidInterface = {
D_INTERFACE(pluggedInterface, 1, USB_DEVICE_CLASS_HUMAN_INTERFACE, HID_SUBCLASS_BOOT_INTERFACE, HID_PROTOCOL_KEYBOARD), D_INTERFACE(pluggedInterface, 1, USB_DEVICE_CLASS_HUMAN_INTERFACE, HID_SUBCLASS_BOOT_INTERFACE, HID_PROTOCOL_KEYBOARD),
D_HIDREPORT(sizeof(boot_keyboard_hid_descriptor_)), D_HIDREPORT(sizeof(boot_keyboard_hid_descriptor_)),
D_ENDPOINT(USB_ENDPOINT_IN(pluggedEndpoint), USB_ENDPOINT_TYPE_INTERRUPT, BOOT_KEYBOARD_EP_SIZE, 0x01) D_ENDPOINT(USB_ENDPOINT_IN(pluggedEndpoint), USB_ENDPOINT_TYPE_INTERRUPT, BOOT_KEYBOARD_EP_SIZE, 0x01)};
};
return USB_SendControl(0, &hidInterface, sizeof(hidInterface)); return USB_SendControl(0, &hidInterface, sizeof(hidInterface));
} }
int BootKeyboard_::getDescriptor(USBSetup& setup) { int BootKeyboard_::getDescriptor(USBSetup &setup) {
// Check if this is a HID Class Descriptor request // Check if this is a HID Class Descriptor request
if (setup.bmRequestType != REQUEST_DEVICETOHOST_STANDARD_INTERFACE) { if (setup.bmRequestType != REQUEST_DEVICETOHOST_STANDARD_INTERFACE) {
return 0; return 0;
@ -138,13 +138,12 @@ void BootKeyboard_::end() {
} }
bool BootKeyboard_::setup(USBSetup &setup) {
bool BootKeyboard_::setup(USBSetup& setup) {
if (pluggedInterface != setup.wIndex) { if (pluggedInterface != setup.wIndex) {
return false; return false;
} }
uint8_t request = setup.bRequest; uint8_t request = setup.bRequest;
uint8_t requestType = setup.bmRequestType; uint8_t requestType = setup.bmRequestType;
if (requestType == REQUEST_DEVICETOHOST_CLASS_INTERFACE) { if (requestType == REQUEST_DEVICETOHOST_CLASS_INTERFACE) {
@ -267,10 +266,10 @@ size_t BootKeyboard_::press(uint8_t k) {
// Add k to the key report only if it's not already present // Add k to the key report only if it's not already present
// and if there is an empty slot. // and if there is an empty slot.
for (uint8_t i = 0; i < sizeof(report_.keycodes); i++) { for (uint8_t i = 0; i < sizeof(report_.keycodes); i++) {
if (report_.keycodes[i] != k) { // is k already in list? if (report_.keycodes[i] != k) { // is k already in list?
if (0 == report_.keycodes[i]) { // have we found an empty slot? if (0 == report_.keycodes[i]) { // have we found an empty slot?
report_.keycodes[i] = k; report_.keycodes[i] = k;
done = 1; done = 1;
break; break;
} }
} else { } else {
@ -316,7 +315,7 @@ size_t BootKeyboard_::release(uint8_t k) {
while (current < sizeof(report_.keycodes)) { while (current < sizeof(report_.keycodes)) {
if (report_.keycodes[current]) { if (report_.keycodes[current]) {
uint8_t tmp = report_.keycodes[nextpos]; uint8_t tmp = report_.keycodes[nextpos];
report_.keycodes[nextpos] = report_.keycodes[current]; report_.keycodes[nextpos] = report_.keycodes[current];
report_.keycodes[current] = tmp; report_.keycodes[current] = tmp;
++nextpos; ++nextpos;
@ -359,7 +358,6 @@ bool BootKeyboard_::wasKeyPressed(uint8_t k) {
} }
/* Returns true if the modifer key passed in will be sent during this key report /* Returns true if the modifer key passed in will be sent during this key report
* Returns false in all other cases * Returns false in all other cases
* */ * */

@ -71,9 +71,9 @@ class BootKeyboard_ : public PluggableUSBModule {
HID_BootKeyboardReport_Data_t report_, last_report_; HID_BootKeyboardReport_Data_t report_, last_report_;
// Implementation of the PUSBListNode // Implementation of the PUSBListNode
int getInterface(uint8_t* interfaceCount); int getInterface(uint8_t *interfaceCount);
int getDescriptor(USBSetup& setup); int getDescriptor(USBSetup &setup);
bool setup(USBSetup& setup); bool setup(USBSetup &setup);
EPTYPE_DESCRIPTOR_SIZE epType[1]; EPTYPE_DESCRIPTOR_SIZE epType[1];
uint8_t protocol; uint8_t protocol;

@ -25,121 +25,120 @@ THE SOFTWARE.
#pragma once #pragma once
#define D_MULTIBYTE(n) (n + 0x01) #define D_MULTIBYTE(n) (n + 0x01)
#define D_USAGE_PAGE 0x05 #define D_USAGE_PAGE 0x05
#define D_USAGE 0x09 #define D_USAGE 0x09
#define D_REPORT_ID 0x85 #define D_REPORT_ID 0x85
#define D_USAGE_MINIMUM 0x19 #define D_USAGE_MINIMUM 0x19
#define D_USAGE_MAXIMUM 0x29 #define D_USAGE_MAXIMUM 0x29
#define D_LOGICAL_MINIMUM 0x15 #define D_LOGICAL_MINIMUM 0x15
#define D_LOGICAL_MAXIMUM 0x25 #define D_LOGICAL_MAXIMUM 0x25
#define D_PHYSICAL_MINIMUM 0x35 #define D_PHYSICAL_MINIMUM 0x35
#define D_PHYSICAL_MAXIMUM 0x45 #define D_PHYSICAL_MAXIMUM 0x45
#define D_REPORT_SIZE 0x75 #define D_REPORT_SIZE 0x75
#define D_REPORT_COUNT 0x95 #define D_REPORT_COUNT 0x95
#define D_PUSH 0xa4 #define D_PUSH 0xa4
#define D_POP 0xb4 #define D_POP 0xb4
// USB HID DCD 1.11 section 6.2.2.4 - Main items // USB HID DCD 1.11 section 6.2.2.4 - Main items
// //
// each of these are the type description + 0x01 for a single byte item // each of these are the type description + 0x01 for a single byte item
#define D_INPUT 0x81 #define D_INPUT 0x81
#define D_OUTPUT 0x91 #define D_OUTPUT 0x91
#define D_FEATURE 0xb1 #define D_FEATURE 0xb1
#define D_COLLECTION 0xa1 #define D_COLLECTION 0xa1
#define D_END_COLLECTION 0xc0 #define D_END_COLLECTION 0xc0
// The bits that make up inputs, outputs and features // The bits that make up inputs, outputs and features
// Bit 0 // Bit 0
#define D_DATA 0b00000000 #define D_DATA 0b00000000
#define D_CONSTANT 0b00000001 #define D_CONSTANT 0b00000001
// Bit 1 // Bit 1
#define D_ARRAY 0b00000000 #define D_ARRAY 0b00000000
#define D_VARIABLE 0b00000010 #define D_VARIABLE 0b00000010
// Bit 2 // Bit 2
#define D_ABSOLUTE 0b00000000 #define D_ABSOLUTE 0b00000000
#define D_RELATIVE 0b00000100 #define D_RELATIVE 0b00000100
// Bit 3 // Bit 3
#define D_NO_WRAP 0b00000000 #define D_NO_WRAP 0b00000000
#define D_WRAP 0b00001000 #define D_WRAP 0b00001000
// Bit 4 // Bit 4
#define D_LINEAR 0b00000000 #define D_LINEAR 0b00000000
#define D_NON_LINEAR 0b00010000 #define D_NON_LINEAR 0b00010000
// Bit 5 // Bit 5
#define D_PREFERRED_STATE 0b00000000 #define D_PREFERRED_STATE 0b00000000
#define D_NO_PREFERRED 0b00100000 #define D_NO_PREFERRED 0b00100000
// Bit 6 // Bit 6
#define D_NO_NULL_POSITION 0b00000000 #define D_NO_NULL_POSITION 0b00000000
#define D_NULL_STATE 0b01000000 #define D_NULL_STATE 0b01000000
// Bit 7 // Bit 7
#define D_NON_VOLATILE 0b00000000 #define D_NON_VOLATILE 0b00000000
#define D_VOLATILE 0b01000000 #define D_VOLATILE 0b01000000
// Bit 8 // Bit 8
#define D_BIT_FIELD 0b00000000 #define D_BIT_FIELD 0b00000000
#define D_BUFFERED_BYTES 0b10000000 #define D_BUFFERED_BYTES 0b10000000
// Collection types // Collection types
#define D_PHYSICAL 0x00 // (group of axes) #define D_PHYSICAL 0x00 // (group of axes)
#define D_APPLICATION 0x01 // (mouse, keyboard) #define D_APPLICATION 0x01 // (mouse, keyboard)
#define D_LOGICAL 0x02 // (interrelated data) #define D_LOGICAL 0x02 // (interrelated data)
#define D_REPORT 0x03 #define D_REPORT 0x03
#define D_NAMED_ARRAY 0x04 #define D_NAMED_ARRAY 0x04
#define D_USAGE_SWITCH 0x05 #define D_USAGE_SWITCH 0x05
#define D_USAGE_MODIFIER 0x06 #define D_USAGE_MODIFIER 0x06
// 0x07-0x7f - Reserved // 0x07-0x7f - Reserved
// 0x80-0xff - Vendor define // 0x80-0xff - Vendor define
#define D_PAGE_GENERIC_DESKTOP 0x01 #define D_PAGE_GENERIC_DESKTOP 0x01
#define D_PAGE_SIMULATION 0x02 #define D_PAGE_SIMULATION 0x02
#define D_PAGE_VR 0x03 #define D_PAGE_VR 0x03
#define D_PAGE_SPORT 0x04 #define D_PAGE_SPORT 0x04
#define D_PAGE_GAME 0x05 #define D_PAGE_GAME 0x05
#define D_PAGE_GENERIC_DEVICE 0x06 #define D_PAGE_GENERIC_DEVICE 0x06
#define D_PAGE_KEYBOARD 0x07 #define D_PAGE_KEYBOARD 0x07
#define D_PAGE_LEDS 0x08 #define D_PAGE_LEDS 0x08
#define D_PAGE_BUTTON 0x09 #define D_PAGE_BUTTON 0x09
#define D_PAGE_ORDINAL 0x0A #define D_PAGE_ORDINAL 0x0A
#define D_PAGE_TELEPHONY 0x0B #define D_PAGE_TELEPHONY 0x0B
#define D_PAGE_CONSUMER 0x0C #define D_PAGE_CONSUMER 0x0C
#define D_PAGE_DIGITIZER 0x0D #define D_PAGE_DIGITIZER 0x0D
#define D_PAGE_RESERVED 0x0E #define D_PAGE_RESERVED 0x0E
#define D_PAGE_PID 0x0F #define D_PAGE_PID 0x0F
#define D_PAGE_UNICODE 0x10 #define D_PAGE_UNICODE 0x10
// 0x11-13 RESERVED // 0x11-13 RESERVED
#define D_PAGE_ALPHANUMERIC_DISPLAY 0x14 #define D_PAGE_ALPHANUMERIC_DISPLAY 0x14
#define D_PAGE_MEDICAL_INSTRUMENTS 0x40 #define D_PAGE_MEDICAL_INSTRUMENTS 0x40
// 0x80-83 MONITOR // 0x80-83 MONITOR
// 0x84-87 POWER // 0x84-87 POWER
#define D_PAGE_BAR_CODE_SCANNER 0x8C #define D_PAGE_BAR_CODE_SCANNER 0x8C
#define D_PAGE_SCALE 0x8D #define D_PAGE_SCALE 0x8D
#define D_PAGE_MSR 0x8E #define D_PAGE_MSR 0x8E
// 0x8F RESERVED POINT OF SALE // 0x8F RESERVED POINT OF SALE
#define D_PAGE_CAMERA_CONTROL 0x90 #define D_PAGE_CAMERA_CONTROL 0x90
#define D_PAGE_ARCADE 0x91 #define D_PAGE_ARCADE 0x91
// Generic Desktop Usages HUT Section 4 p27 // Generic Desktop Usages HUT Section 4 p27
#define D_USAGE_POINTER 0x01 #define D_USAGE_POINTER 0x01
#define D_USAGE_MOUSE 0x02 #define D_USAGE_MOUSE 0x02
// 0x03 is reserved // 0x03 is reserved
#define D_USAGE_JOYSTICK 0x04 #define D_USAGE_JOYSTICK 0x04
#define D_USAGE_GAMEPAD 0x05 #define D_USAGE_GAMEPAD 0x05
#define D_USAGE_KEYBOARD 0x06 #define D_USAGE_KEYBOARD 0x06
#define D_USAGE_KEYPAD 0x07 #define D_USAGE_KEYPAD 0x07
#define D_USAGE_MULITAXIS 0x08 #define D_USAGE_MULITAXIS 0x08

@ -93,7 +93,7 @@ class AbsoluteMouseAPI {
inline bool isPressed(uint8_t b = MOUSE_LEFT); inline bool isPressed(uint8_t b = MOUSE_LEFT);
// Sending is public in the base class for advanced users. // Sending is public in the base class for advanced users.
virtual void sendReport(void* data, int length) {} virtual void sendReport(void *data, int length) {}
protected: protected:
uint16_t x_axis_; uint16_t x_axis_;

@ -122,11 +122,11 @@ void USB_PackMessages(bool pack);
#define USB_Send USBDevice.send #define USB_Send USBDevice.send
#define USB_Flush USBDevice.flush #define USB_Flush USBDevice.flush
int USB_SendControl(void* y, uint8_t z); int USB_SendControl(void *y, uint8_t z);
int USB_SendControl(uint8_t x, const void* y, uint8_t z); int USB_SendControl(uint8_t x, const void *y, uint8_t z);
#define TRANSFER_PGM 0 #define TRANSFER_PGM 0
#define TRANSFER_RELEASE 0 #define TRANSFER_RELEASE 0
#elif defined(ARDUINO_ARCH_GD32) #elif defined(ARDUINO_ARCH_GD32)
@ -150,7 +150,6 @@ constexpr uint16_t EP_TYPE_INTERRUPT_OUT(uint8_t buffer_size) {
} }
#else #else
#error "Unsupported architecture" #error "Unsupported architecture"

@ -27,7 +27,7 @@ THE SOFTWARE.
#include "kaleidoscope/HIDTables.h" #include "kaleidoscope/HIDTables.h"
#define HID_FIRST_KEY HID_KEYBOARD_NO_EVENT #define HID_FIRST_KEY HID_KEYBOARD_NO_EVENT
#define HID_LAST_KEY HID_KEYPAD_HEXADECIMAL #define HID_LAST_KEY HID_KEYPAD_HEXADECIMAL
#define HID_KEYBOARD_FIRST_MODIFIER HID_KEYBOARD_LEFT_CONTROL #define HID_KEYBOARD_FIRST_MODIFIER HID_KEYBOARD_LEFT_CONTROL
#define HID_KEYBOARD_LAST_MODIFIER HID_KEYBOARD_RIGHT_GUI #define HID_KEYBOARD_LAST_MODIFIER HID_KEYBOARD_RIGHT_GUI

@ -28,12 +28,9 @@ THE SOFTWARE.
class HIDReportObserver { class HIDReportObserver {
public: public:
typedef void (*SendReportHook)(uint8_t id, const void *data, int len, int result);
typedef void(*SendReportHook)(uint8_t id, const void* data, static void observeReport(uint8_t id, const void *data, int len, int result) {
int len, int result);
static void observeReport(uint8_t id, const void* data,
int len, int result) {
if (send_report_hook_) { if (send_report_hook_) {
(*send_report_hook_)(id, data, len, result); (*send_report_hook_)(id, data, len, result);
} }
@ -45,11 +42,10 @@ class HIDReportObserver {
static SendReportHook resetHook(SendReportHook new_hook) { static SendReportHook resetHook(SendReportHook new_hook) {
auto previous_hook = send_report_hook_; auto previous_hook = send_report_hook_;
send_report_hook_ = new_hook; send_report_hook_ = new_hook;
return previous_hook; return previous_hook;
} }
private: private:
static SendReportHook send_report_hook_; static SendReportHook send_report_hook_;
}; };

@ -23,22 +23,21 @@
#if defined(USBCON) #if defined(USBCON)
HID_& HID() { HID_ &HID() {
static HID_ obj; static HID_ obj;
return obj; return obj;
} }
int HID_::getInterface(uint8_t* interfaceCount) { int HID_::getInterface(uint8_t *interfaceCount) {
*interfaceCount += 1; // uses 1 *interfaceCount += 1; // uses 1
HIDDescriptor hidInterface = { HIDDescriptor hidInterface = {
D_INTERFACE(pluggedInterface, 1, USB_DEVICE_CLASS_HUMAN_INTERFACE, HID_SUBCLASS_NONE, HID_PROTOCOL_NONE), D_INTERFACE(pluggedInterface, 1, USB_DEVICE_CLASS_HUMAN_INTERFACE, HID_SUBCLASS_NONE, HID_PROTOCOL_NONE),
D_HIDREPORT(descriptorSize), D_HIDREPORT(descriptorSize),
D_ENDPOINT(USB_ENDPOINT_IN(pluggedEndpoint), USB_ENDPOINT_TYPE_INTERRUPT, USB_EP_SIZE, 0x01) D_ENDPOINT(USB_ENDPOINT_IN(pluggedEndpoint), USB_ENDPOINT_TYPE_INTERRUPT, USB_EP_SIZE, 0x01)};
};
return USB_SendControl(0, &hidInterface, sizeof(hidInterface)); return USB_SendControl(0, &hidInterface, sizeof(hidInterface));
} }
int HID_::getDescriptor(USBSetup& setup) { int HID_::getDescriptor(USBSetup &setup) {
// Check if this is a HID Class Descriptor request // Check if this is a HID Class Descriptor request
if (setup.bmRequestType != REQUEST_DEVICETOHOST_STANDARD_INTERFACE) { if (setup.bmRequestType != REQUEST_DEVICETOHOST_STANDARD_INTERFACE) {
return 0; return 0;
@ -53,7 +52,7 @@ int HID_::getDescriptor(USBSetup& setup) {
} }
int total = 0; int total = 0;
HIDSubDescriptor* node; HIDSubDescriptor *node;
USB_PackMessages(true); USB_PackMessages(true);
for (node = rootNode; node; node = node->next) { for (node = rootNode; node; node = node->next) {
int res = USB_SendControl(TRANSFER_PGM, node->data, node->length); int res = USB_SendControl(TRANSFER_PGM, node->data, node->length);
@ -71,7 +70,8 @@ int HID_::getDescriptor(USBSetup& setup) {
} }
__attribute__((weak)) __attribute__((weak))
uint8_t HID_::getShortName(char *name) { uint8_t
HID_::getShortName(char *name) {
name[0] = 'k'; name[0] = 'k';
name[1] = 'b'; name[1] = 'b';
name[2] = 'i'; name[2] = 'i';
@ -94,13 +94,13 @@ void HID_::AppendDescriptor(HIDSubDescriptor *node) {
descriptorSize += node->length; descriptorSize += node->length;
} }
int HID_::SendReport(uint8_t id, const void* data, int len) { int HID_::SendReport(uint8_t id, const void *data, int len) {
auto result = SendReport_(id, data, len); auto result = SendReport_(id, data, len);
HIDReportObserver::observeReport(id, data, len, result); HIDReportObserver::observeReport(id, data, len, result);
return result; return result;
} }
int HID_::SendReport_(uint8_t id, const void* data, int len) { int HID_::SendReport_(uint8_t id, const void *data, int len) {
/* On SAMD, we need to send the whole report in one batch; sending the id, and /* On SAMD, we need to send the whole report in one batch; sending the id, and
* the report itself separately does not work, the report never arrives. Due * the report itself separately does not work, the report never arrives. Due
* to this, we merge the two into a single buffer, and send that. * to this, we merge the two into a single buffer, and send that.
@ -122,12 +122,12 @@ int HID_::SendReport_(uint8_t id, const void* data, int len) {
#endif #endif
} }
bool HID_::setup(USBSetup& setup) { bool HID_::setup(USBSetup &setup) {
if (pluggedInterface != setup.wIndex) { if (pluggedInterface != setup.wIndex) {
return false; return false;
} }
uint8_t request = setup.bRequest; uint8_t request = setup.bRequest;
uint8_t requestType = setup.bmRequestType; uint8_t requestType = setup.bmRequestType;
if (requestType == REQUEST_DEVICETOHOST_CLASS_INTERFACE) { if (requestType == REQUEST_DEVICETOHOST_CLASS_INTERFACE) {
@ -195,11 +195,14 @@ bool HID_::setup(USBSetup& setup) {
return false; return false;
} }
HID_::HID_() : PluggableUSBModule(1, 1, epType), HID_::HID_()
rootNode(NULL), descriptorSize(0), : PluggableUSBModule(1, 1, epType),
protocol(HID_REPORT_PROTOCOL), idle(1) { rootNode(NULL),
descriptorSize(0),
protocol(HID_REPORT_PROTOCOL),
idle(1) {
setReportData.reportId = 0; setReportData.reportId = 0;
setReportData.leds = 0; setReportData.leds = 0;
#ifdef ARCH_HAS_CONFIGURABLE_EP_SIZES #ifdef ARCH_HAS_CONFIGURABLE_EP_SIZES
epType[0] = EP_TYPE_INTERRUPT_IN(USB_EP_SIZE); epType[0] = EP_TYPE_INTERRUPT_IN(USB_EP_SIZE);

@ -28,30 +28,30 @@
// HID 'Driver' // HID 'Driver'
// ------------ // ------------
#define HID_GET_REPORT 0x01 #define HID_GET_REPORT 0x01
#define HID_GET_IDLE 0x02 #define HID_GET_IDLE 0x02
#define HID_GET_PROTOCOL 0x03 #define HID_GET_PROTOCOL 0x03
#define HID_SET_REPORT 0x09 #define HID_SET_REPORT 0x09
#define HID_SET_IDLE 0x0A #define HID_SET_IDLE 0x0A
#define HID_SET_PROTOCOL 0x0B #define HID_SET_PROTOCOL 0x0B
#define HID_HID_DESCRIPTOR_TYPE 0x21 #define HID_HID_DESCRIPTOR_TYPE 0x21
#define HID_REPORT_DESCRIPTOR_TYPE 0x22 #define HID_REPORT_DESCRIPTOR_TYPE 0x22
#define HID_PHYSICAL_DESCRIPTOR_TYPE 0x23 #define HID_PHYSICAL_DESCRIPTOR_TYPE 0x23
// HID subclass HID1.11 Page 8 4.2 Subclass // HID subclass HID1.11 Page 8 4.2 Subclass
#define HID_SUBCLASS_NONE 0 #define HID_SUBCLASS_NONE 0
#define HID_SUBCLASS_BOOT_INTERFACE 1 #define HID_SUBCLASS_BOOT_INTERFACE 1
// HID Keyboard/Mouse bios compatible protocols HID1.11 Page 9 4.3 Protocols // HID Keyboard/Mouse bios compatible protocols HID1.11 Page 9 4.3 Protocols
#define HID_PROTOCOL_NONE 0 #define HID_PROTOCOL_NONE 0
#define HID_PROTOCOL_KEYBOARD 1 #define HID_PROTOCOL_KEYBOARD 1
#define HID_PROTOCOL_MOUSE 2 #define HID_PROTOCOL_MOUSE 2
// Normal or bios protocol (Keyboard/Mouse) HID1.11 Page 54 7.2.5 Get_Protocol Request // Normal or bios protocol (Keyboard/Mouse) HID1.11 Page 54 7.2.5 Get_Protocol Request
// "protocol" variable is used for this purpose. // "protocol" variable is used for this purpose.
#define HID_BOOT_PROTOCOL 0 #define HID_BOOT_PROTOCOL 0
#define HID_REPORT_PROTOCOL 1 #define HID_REPORT_PROTOCOL 1
// HID Request Type HID1.11 Page 51 7.2.1 Get_Report Request // HID Request Type HID1.11 Page 51 7.2.1 Get_Report Request
#define HID_REPORT_TYPE_INPUT 1 #define HID_REPORT_TYPE_INPUT 1
@ -60,56 +60,57 @@
#pragma pack(push, 1) #pragma pack(push, 1)
typedef struct { typedef struct {
uint8_t len; // 9 uint8_t len; // 9
uint8_t dtype; // 0x21 uint8_t dtype; // 0x21
uint8_t addr; uint8_t addr;
uint8_t versionL; // 0x101 uint8_t versionL; // 0x101
uint8_t versionH; // 0x101 uint8_t versionH; // 0x101
uint8_t country; uint8_t country;
uint8_t desctype; // 0x22 report uint8_t desctype; // 0x22 report
uint8_t descLenL; uint8_t descLenL;
uint8_t descLenH; uint8_t descLenH;
} HIDDescDescriptor; } HIDDescDescriptor;
typedef struct { typedef struct {
InterfaceDescriptor hid; InterfaceDescriptor hid;
HIDDescDescriptor desc; HIDDescDescriptor desc;
EndpointDescriptor in; EndpointDescriptor in;
} HIDDescriptor; } HIDDescriptor;
#pragma pack(pop) #pragma pack(pop)
class HIDSubDescriptor { class HIDSubDescriptor {
public: public:
HIDSubDescriptor *next = NULL; HIDSubDescriptor *next = NULL;
HIDSubDescriptor(const void *d, const uint16_t l) : data(d), length(l) { } HIDSubDescriptor(const void *d, const uint16_t l)
: data(d), length(l) {}
const void* data; const void *data;
const uint16_t length; const uint16_t length;
}; };
class HID_ : public PluggableUSBModule { class HID_ : public PluggableUSBModule {
public: public:
HID_(); HID_();
int begin(); int begin();
int SendReport(uint8_t id, const void* data, int len); int SendReport(uint8_t id, const void *data, int len);
void AppendDescriptor(HIDSubDescriptor* node); void AppendDescriptor(HIDSubDescriptor *node);
uint8_t getLEDs() { uint8_t getLEDs() {
return setReportData.leds; return setReportData.leds;
} }
protected: protected:
// Implementation of the PluggableUSBModule // Implementation of the PluggableUSBModule
int getInterface(uint8_t* interfaceCount); int getInterface(uint8_t *interfaceCount);
int getDescriptor(USBSetup& setup); int getDescriptor(USBSetup &setup);
bool setup(USBSetup& setup); bool setup(USBSetup &setup);
uint8_t getShortName(char* name); uint8_t getShortName(char *name);
int SendReport_(uint8_t id, const void *data, int len);
int SendReport_(uint8_t id, const void* data, int len);
private: private:
EPTYPE_DESCRIPTOR_SIZE epType[1]; EPTYPE_DESCRIPTOR_SIZE epType[1];
HIDSubDescriptor* rootNode; HIDSubDescriptor *rootNode;
uint16_t descriptorSize; uint16_t descriptorSize;
uint8_t protocol; uint8_t protocol;
@ -123,8 +124,9 @@ class HID_ : public PluggableUSBModule {
// Replacement for global singleton. // Replacement for global singleton.
// This function prevents static-initialization-order-fiasco // This function prevents static-initialization-order-fiasco
// https://isocpp.org/wiki/faq/ctors#static-init-order-on-first-use // https://isocpp.org/wiki/faq/ctors#static-init-order-on-first-use
HID_& HID(); HID_ &HID();
#define D_HIDREPORT(length) { 9, 0x21, 0x01, 0x01, 0, 1, 0x22, lowByte(length), highByte(length) } #define D_HIDREPORT(length) \
{ 9, 0x21, 0x01, 0x01, 0, 1, 0x22, lowByte(length), highByte(length) }
#endif // USBCON #endif // USBCON

@ -25,11 +25,11 @@ THE SOFTWARE.
#pragma once #pragma once
#define MOUSE_LEFT (1 << 0) #define MOUSE_LEFT (1 << 0)
#define MOUSE_RIGHT (1 << 1) #define MOUSE_RIGHT (1 << 1)
#define MOUSE_MIDDLE (1 << 2) #define MOUSE_MIDDLE (1 << 2)
#define MOUSE_PREV (1 << 3) #define MOUSE_PREV (1 << 3)
#define MOUSE_NEXT (1 << 4) #define MOUSE_NEXT (1 << 4)
// actually this mouse report has 8 buttons (for smaller descriptor) // actually this mouse report has 8 buttons (for smaller descriptor)
// but the last 3 wont do anything from what I tested // but the last 3 wont do anything from what I tested
#define MOUSE_ALL (MOUSE_LEFT | MOUSE_RIGHT | MOUSE_MIDDLE | MOUSE_PREV | MOUSE_NEXT) #define MOUSE_ALL (MOUSE_LEFT | MOUSE_RIGHT | MOUSE_MIDDLE | MOUSE_PREV | MOUSE_NEXT)

@ -49,7 +49,7 @@ AbsoluteMouse_::AbsoluteMouse_() {
} }
void AbsoluteMouse_::sendReport(void* data, int length) { void AbsoluteMouse_::sendReport(void *data, int length) {
HID().SendReport(HID_REPORTID_MOUSE_ABSOLUTE, data, length); HID().SendReport(HID_REPORTID_MOUSE_ABSOLUTE, data, length);
} }

@ -37,7 +37,7 @@ class AbsoluteMouse_ : public AbsoluteMouseAPI {
protected: protected:
// Sending is public in the base class for advanced users. // Sending is public in the base class for advanced users.
virtual void sendReport(void* data, int length); virtual void sendReport(void *data, int length);
}; };
extern AbsoluteMouse_ AbsoluteMouse; extern AbsoluteMouse_ AbsoluteMouse;

@ -144,7 +144,7 @@ void Gamepad_::dPad2(int8_t d) {
report_.dPad2 = d; report_.dPad2 = d;
} }
void Gamepad_::sendReport(void* data, int length) { void Gamepad_::sendReport(void *data, int length) {
HID().SendReport(HID_REPORTID_GAMEPAD, data, length); HID().SendReport(HID_REPORTID_GAMEPAD, data, length);
} }

@ -31,15 +31,15 @@ THE SOFTWARE.
#include "kaleidoscope/driver/hid/keyboardio/usb/HID-Settings.h" #include "kaleidoscope/driver/hid/keyboardio/usb/HID-Settings.h"
// Dpad directions // Dpad directions
#define GAMEPAD_DPAD_CENTERED 0 #define GAMEPAD_DPAD_CENTERED 0
#define GAMEPAD_DPAD_UP 1 #define GAMEPAD_DPAD_UP 1
#define GAMEPAD_DPAD_UP_RIGHT 2 #define GAMEPAD_DPAD_UP_RIGHT 2
#define GAMEPAD_DPAD_RIGHT 3 #define GAMEPAD_DPAD_RIGHT 3
#define GAMEPAD_DPAD_DOWN_RIGHT 4 #define GAMEPAD_DPAD_DOWN_RIGHT 4
#define GAMEPAD_DPAD_DOWN 5 #define GAMEPAD_DPAD_DOWN 5
#define GAMEPAD_DPAD_DOWN_LEFT 6 #define GAMEPAD_DPAD_DOWN_LEFT 6
#define GAMEPAD_DPAD_LEFT 7 #define GAMEPAD_DPAD_LEFT 7
#define GAMEPAD_DPAD_UP_LEFT 8 #define GAMEPAD_DPAD_UP_LEFT 8
typedef union { typedef union {
@ -83,17 +83,17 @@ typedef union {
uint8_t button31 : 1; uint8_t button31 : 1;
uint8_t button32 : 1; uint8_t button32 : 1;
int16_t xAxis; int16_t xAxis;
int16_t yAxis; int16_t yAxis;
int16_t rxAxis; int16_t rxAxis;
int16_t ryAxis; int16_t ryAxis;
int8_t zAxis; int8_t zAxis;
int8_t rzAxis; int8_t rzAxis;
uint8_t dPad1 : 4; uint8_t dPad1 : 4;
uint8_t dPad2 : 4; uint8_t dPad2 : 4;
}; };
} HID_GamepadReport_Data_t; } HID_GamepadReport_Data_t;
@ -118,7 +118,8 @@ class Gamepad_ {
void dPad1(int8_t d); void dPad1(int8_t d);
void dPad2(int8_t d); void dPad2(int8_t d);
void sendReport(void* data, int length); void sendReport(void *data, int length);
protected: protected:
HID_GamepadReport_Data_t report_; HID_GamepadReport_Data_t report_;
}; };

@ -98,7 +98,8 @@ void Keyboard_::end() {
int Keyboard_::sendReportUnchecked() { int Keyboard_::sendReportUnchecked() {
return HID().SendReport(HID_REPORTID_NKRO_KEYBOARD, return HID().SendReport(HID_REPORTID_NKRO_KEYBOARD,
&last_report_, sizeof(last_report_)); &last_report_,
sizeof(last_report_));
} }
// Sending the current HID report to the host: // Sending the current HID report to the host:

@ -39,7 +39,7 @@ typedef union {
// Modifiers + keymap // Modifiers + keymap
struct { struct {
uint8_t modifiers; uint8_t modifiers;
uint8_t keys[KEY_BYTES ]; uint8_t keys[KEY_BYTES];
}; };
uint8_t allkeys[1 + KEY_BYTES]; uint8_t allkeys[1 + KEY_BYTES];
} HID_KeyboardReport_Data_t; } HID_KeyboardReport_Data_t;
@ -53,7 +53,7 @@ class Keyboard_ {
size_t press(uint8_t k); size_t press(uint8_t k);
size_t release(uint8_t k); size_t release(uint8_t k);
void releaseAll(); void releaseAll();
int sendReport(); int sendReport();
bool isKeyPressed(uint8_t k); bool isKeyPressed(uint8_t k);

@ -101,8 +101,8 @@ void Mouse_::click(uint8_t b) {
} }
void Mouse_::move(int8_t x, int8_t y, int8_t v_wheel, int8_t h_wheel) { void Mouse_::move(int8_t x, int8_t y, int8_t v_wheel, int8_t h_wheel) {
report_.xAxis = x; report_.xAxis = x;
report_.yAxis = y; report_.yAxis = y;
report_.vWheel = v_wheel; report_.vWheel = v_wheel;
report_.hWheel = h_wheel; report_.hWheel = h_wheel;
} }

@ -53,9 +53,9 @@ class Mouse_ {
// but also calls `sendReport()` at least twice. // but also calls `sendReport()` at least twice.
void click(uint8_t b = MOUSE_LEFT); void click(uint8_t b = MOUSE_LEFT);
void move(int8_t x, int8_t y, int8_t v_wheel = 0, int8_t h_wheel = 0); void move(int8_t x, int8_t y, int8_t v_wheel = 0, int8_t h_wheel = 0);
void press(uint8_t b = MOUSE_LEFT); // press LEFT by default void press(uint8_t b = MOUSE_LEFT); // press LEFT by default
void release(uint8_t b = MOUSE_LEFT); // release LEFT by default void release(uint8_t b = MOUSE_LEFT); // release LEFT by default
bool isPressed(uint8_t b = MOUSE_LEFT); // check LEFT by default bool isPressed(uint8_t b = MOUSE_LEFT); // check LEFT by default
/** getReport returns the current report. /** getReport returns the current report.
* *

@ -92,7 +92,7 @@ void SystemControl_::press(uint8_t s) {
} }
void SystemControl_::sendReport(void* data, int length) { void SystemControl_::sendReport(void *data, int length) {
HID().SendReport(HID_REPORTID_SYSTEMCONTROL, data, length); HID().SendReport(HID_REPORTID_SYSTEMCONTROL, data, length);
} }

@ -45,7 +45,7 @@ class SystemControl_ {
void press(uint8_t s); void press(uint8_t s);
void release(); void release();
void releaseAll(); void releaseAll();
void sendReport(void* data, int length); void sendReport(void *data, int length);
SystemControl_(); SystemControl_();
@ -53,5 +53,4 @@ class SystemControl_ {
}; };
extern SystemControl_ SystemControl; extern SystemControl_ SystemControl;

@ -46,8 +46,8 @@ static const uint8_t SINGLE_ABSOLUTEMOUSE_EP_SIZE = USB_EP_SIZE;
#endif #endif
SingleAbsoluteMouse_::SingleAbsoluteMouse_()
SingleAbsoluteMouse_::SingleAbsoluteMouse_() : PluggableUSBModule(1, 1, epType), protocol(HID_REPORT_PROTOCOL), idle(1) { : PluggableUSBModule(1, 1, epType), protocol(HID_REPORT_PROTOCOL), idle(1) {
#ifdef ARCH_HAS_CONFIGURABLE_EP_SIZES #ifdef ARCH_HAS_CONFIGURABLE_EP_SIZES
epType[0] = EP_TYPE_INTERRUPT_IN(SINGLE_ABSOLUTEMOUSE_EP_SIZE); epType[0] = EP_TYPE_INTERRUPT_IN(SINGLE_ABSOLUTEMOUSE_EP_SIZE);
@ -58,17 +58,16 @@ SingleAbsoluteMouse_::SingleAbsoluteMouse_() : PluggableUSBModule(1, 1, epType),
PluggableUSB().plug(this); PluggableUSB().plug(this);
} }
int SingleAbsoluteMouse_::getInterface(uint8_t* interfaceCount) { int SingleAbsoluteMouse_::getInterface(uint8_t *interfaceCount) {
*interfaceCount += 1; // uses 1 *interfaceCount += 1; // uses 1
HIDDescriptor hidInterface = { HIDDescriptor hidInterface = {
D_INTERFACE(pluggedInterface, 1, USB_DEVICE_CLASS_HUMAN_INTERFACE, HID_SUBCLASS_NONE, HID_PROTOCOL_NONE), D_INTERFACE(pluggedInterface, 1, USB_DEVICE_CLASS_HUMAN_INTERFACE, HID_SUBCLASS_NONE, HID_PROTOCOL_NONE),
D_HIDREPORT(sizeof(_hidSingleReportDescriptorAbsoluteMouse)), D_HIDREPORT(sizeof(_hidSingleReportDescriptorAbsoluteMouse)),
D_ENDPOINT(USB_ENDPOINT_IN(pluggedEndpoint), USB_ENDPOINT_TYPE_INTERRUPT, SINGLE_ABSOLUTEMOUSE_EP_SIZE, 0x01) D_ENDPOINT(USB_ENDPOINT_IN(pluggedEndpoint), USB_ENDPOINT_TYPE_INTERRUPT, SINGLE_ABSOLUTEMOUSE_EP_SIZE, 0x01)};
};
return USB_SendControl(0, &hidInterface, sizeof(hidInterface)); return USB_SendControl(0, &hidInterface, sizeof(hidInterface));
} }
int SingleAbsoluteMouse_::getDescriptor(USBSetup& setup) { int SingleAbsoluteMouse_::getDescriptor(USBSetup &setup) {
// Check if this is a HID Class Descriptor request // Check if this is a HID Class Descriptor request
if (setup.bmRequestType != REQUEST_DEVICETOHOST_STANDARD_INTERFACE) { if (setup.bmRequestType != REQUEST_DEVICETOHOST_STANDARD_INTERFACE) {
return 0; return 0;
@ -89,12 +88,12 @@ int SingleAbsoluteMouse_::getDescriptor(USBSetup& setup) {
return USB_SendControl(TRANSFER_PGM, _hidSingleReportDescriptorAbsoluteMouse, sizeof(_hidSingleReportDescriptorAbsoluteMouse)); return USB_SendControl(TRANSFER_PGM, _hidSingleReportDescriptorAbsoluteMouse, sizeof(_hidSingleReportDescriptorAbsoluteMouse));
} }
bool SingleAbsoluteMouse_::setup(USBSetup& setup) { bool SingleAbsoluteMouse_::setup(USBSetup &setup) {
if (pluggedInterface != setup.wIndex) { if (pluggedInterface != setup.wIndex) {
return false; return false;
} }
uint8_t request = setup.bRequest; uint8_t request = setup.bRequest;
uint8_t requestType = setup.bmRequestType; uint8_t requestType = setup.bmRequestType;
if (requestType == REQUEST_DEVICETOHOST_CLASS_INTERFACE) { if (requestType == REQUEST_DEVICETOHOST_CLASS_INTERFACE) {
@ -124,7 +123,7 @@ bool SingleAbsoluteMouse_::setup(USBSetup& setup) {
return false; return false;
} }
void SingleAbsoluteMouse_::sendReport(void* data, int length) { void SingleAbsoluteMouse_::sendReport(void *data, int length) {
auto result = USB_Send(pluggedEndpoint | TRANSFER_RELEASE, data, length); auto result = USB_Send(pluggedEndpoint | TRANSFER_RELEASE, data, length);
HIDReportObserver::observeReport(HID_REPORTID_MOUSE_ABSOLUTE, data, length, result); HIDReportObserver::observeReport(HID_REPORTID_MOUSE_ABSOLUTE, data, length, result);
} }

@ -40,14 +40,14 @@ class SingleAbsoluteMouse_ : public PluggableUSBModule, public AbsoluteMouseAPI
protected: protected:
// Implementation of the PUSBListNode // Implementation of the PUSBListNode
int getInterface(uint8_t* interfaceCount); int getInterface(uint8_t *interfaceCount);
int getDescriptor(USBSetup& setup); int getDescriptor(USBSetup &setup);
bool setup(USBSetup& setup); bool setup(USBSetup &setup);
EPTYPE_DESCRIPTOR_SIZE epType[1]; EPTYPE_DESCRIPTOR_SIZE epType[1];
uint8_t protocol; uint8_t protocol;
uint8_t idle; uint8_t idle;
inline void sendReport(void* data, int length) override; inline void sendReport(void *data, int length) override;
}; };
extern SingleAbsoluteMouse_ SingleAbsoluteMouse; extern SingleAbsoluteMouse_ SingleAbsoluteMouse;

@ -2,11 +2,11 @@
#ifdef ARDUINO_ARCH_SAMD #ifdef ARDUINO_ARCH_SAMD
int USB_SendControl(void* b, unsigned char c) { int USB_SendControl(void *b, unsigned char c) {
USBDevice.sendControl(b, c); USBDevice.sendControl(b, c);
} }
int USB_SendControl(uint8_t a, const void* b, uint8_t c) { int USB_SendControl(uint8_t a, const void *b, uint8_t c) {
USBDevice.sendControl(b, c); USBDevice.sendControl(b, c);
} }

Loading…
Cancel
Save