1
0
Fork 0
forked from forks/qmk_firmware

LUFA: Consolidate report sending code (#18629)

This commit is contained in:
Ryan 2022-10-16 03:53:57 +11:00 committed by GitHub
parent 7c0116a3ec
commit 19e2dd742b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -87,6 +87,23 @@ static void send_mouse(report_mouse_t *report);
static void send_extra(report_extra_t *report); static void send_extra(report_extra_t *report);
host_driver_t lufa_driver = {keyboard_leds, send_keyboard, send_mouse, send_extra}; host_driver_t lufa_driver = {keyboard_leds, send_keyboard, send_mouse, send_extra};
void send_report(uint8_t endpoint, void *report, size_t size) {
uint8_t timeout = 255;
if (USB_DeviceState != DEVICE_STATE_Configured) return;
Endpoint_SelectEndpoint(endpoint);
/* Check if write ready for a polling interval around 10ms */
while (timeout-- && !Endpoint_IsReadWriteAllowed()) {
_delay_us(40);
}
if (!Endpoint_IsReadWriteAllowed()) return;
Endpoint_Write_Stream_LE(report, size, NULL);
Endpoint_ClearIN();
}
#ifdef VIRTSER_ENABLE #ifdef VIRTSER_ENABLE
// clang-format off // clang-format off
@ -121,30 +138,8 @@ USB_ClassInfo_CDC_Device_t cdc_device = {
* FIXME: Needs doc * FIXME: Needs doc
*/ */
void raw_hid_send(uint8_t *data, uint8_t length) { void raw_hid_send(uint8_t *data, uint8_t length) {
// TODO: implement variable size packet if (length != RAW_EPSIZE) return;
if (length != RAW_EPSIZE) { send_report(RAW_IN_EPNUM, data, RAW_EPSIZE);
return;
}
if (USB_DeviceState != DEVICE_STATE_Configured) {
return;
}
// TODO: decide if we allow calls to raw_hid_send() in the middle
// of other endpoint usage.
uint8_t ep = Endpoint_GetCurrentEndpoint();
Endpoint_SelectEndpoint(RAW_IN_EPNUM);
// Check to see if the host is ready to accept another packet
if (Endpoint_IsINReady()) {
// Write data
Endpoint_Write_Stream_LE(data, RAW_EPSIZE, NULL);
// Finalize the stream transfer to send the last packet
Endpoint_ClearIN();
}
Endpoint_SelectEndpoint(ep);
} }
/** \brief Raw HID Receive /** \brief Raw HID Receive
@ -249,29 +244,6 @@ static void Console_Task(void) {
} }
#endif #endif
/*******************************************************************************
* Joystick
******************************************************************************/
void send_joystick(report_joystick_t *report) {
#ifdef JOYSTICK_ENABLE
uint8_t timeout = 255;
/* Select the Joystick Report Endpoint */
Endpoint_SelectEndpoint(JOYSTICK_IN_EPNUM);
/* Check if write ready for a polling interval around 10ms */
while (timeout-- && !Endpoint_IsReadWriteAllowed())
_delay_us(40);
if (!Endpoint_IsReadWriteAllowed()) return;
/* Write Joystick Report Data */
Endpoint_Write_Stream_LE(report, sizeof(report_joystick_t), NULL);
/* Finalize the stream transfer to send the last packet */
Endpoint_ClearIN();
#endif
}
/******************************************************************************* /*******************************************************************************
* USB Events * USB Events
******************************************************************************/ ******************************************************************************/
@ -587,32 +559,23 @@ static uint8_t keyboard_leds(void) {
* FIXME: Needs doc * FIXME: Needs doc
*/ */
static void send_keyboard(report_keyboard_t *report) { static void send_keyboard(report_keyboard_t *report) {
uint8_t timeout = 255;
/* Select the Keyboard Report Endpoint */ /* Select the Keyboard Report Endpoint */
uint8_t ep = KEYBOARD_IN_EPNUM; uint8_t ep = KEYBOARD_IN_EPNUM;
uint8_t size = KEYBOARD_REPORT_SIZE; uint8_t size = KEYBOARD_REPORT_SIZE;
#ifdef NKRO_ENABLE
if (keyboard_protocol && keymap_config.nkro) {
ep = SHARED_IN_EPNUM;
size = sizeof(struct nkro_report);
}
#endif
Endpoint_SelectEndpoint(ep);
/* Check if write ready for a polling interval around 10ms */
while (timeout-- && !Endpoint_IsReadWriteAllowed())
_delay_us(40);
if (!Endpoint_IsReadWriteAllowed()) return;
/* If we're in Boot Protocol, don't send any report ID or other funky fields */ /* If we're in Boot Protocol, don't send any report ID or other funky fields */
if (!keyboard_protocol) { if (!keyboard_protocol) {
Endpoint_Write_Stream_LE(&report->mods, 8, NULL); send_report(ep, &report->mods, 8);
} else { } else {
Endpoint_Write_Stream_LE(report, size, NULL); #ifdef NKRO_ENABLE
} if (keymap_config.nkro) {
ep = SHARED_IN_EPNUM;
size = sizeof(struct nkro_report);
}
#endif
/* Finalize the stream transfer to send the last packet */ send_report(ep, report, size);
Endpoint_ClearIN(); }
keyboard_report_sent = *report; keyboard_report_sent = *report;
} }
@ -623,55 +586,35 @@ static void send_keyboard(report_keyboard_t *report) {
*/ */
static void send_mouse(report_mouse_t *report) { static void send_mouse(report_mouse_t *report) {
#ifdef MOUSE_ENABLE #ifdef MOUSE_ENABLE
uint8_t timeout = 255; send_report(MOUSE_IN_EPNUM, report, sizeof(report_mouse_t));
/* Select the Mouse Report Endpoint */
Endpoint_SelectEndpoint(MOUSE_IN_EPNUM);
/* Check if write ready for a polling interval around 10ms */
while (timeout-- && !Endpoint_IsReadWriteAllowed())
_delay_us(40);
if (!Endpoint_IsReadWriteAllowed()) return;
/* Write Mouse Report Data */
Endpoint_Write_Stream_LE(report, sizeof(report_mouse_t), NULL);
/* Finalize the stream transfer to send the last packet */
Endpoint_ClearIN();
#endif #endif
} }
#if defined(EXTRAKEY_ENABLE) || defined(PROGRAMMABLE_BUTTON_ENABLE)
static void send_report(void *report, size_t size) {
uint8_t timeout = 255;
if (USB_DeviceState != DEVICE_STATE_Configured) return;
Endpoint_SelectEndpoint(SHARED_IN_EPNUM);
/* Check if write ready for a polling interval around 10ms */
while (timeout-- && !Endpoint_IsReadWriteAllowed())
_delay_us(40);
if (!Endpoint_IsReadWriteAllowed()) return;
Endpoint_Write_Stream_LE(report, size, NULL);
Endpoint_ClearIN();
}
#endif
/** \brief Send Extra /** \brief Send Extra
* *
* FIXME: Needs doc * FIXME: Needs doc
*/ */
static void send_extra(report_extra_t *report) { static void send_extra(report_extra_t *report) {
#ifdef EXTRAKEY_ENABLE #ifdef EXTRAKEY_ENABLE
send_report(report, sizeof(report_extra_t)); send_report(SHARED_IN_EPNUM, report, sizeof(report_extra_t));
#endif
}
void send_joystick(report_joystick_t *report) {
#ifdef JOYSTICK_ENABLE
send_report(JOYSTICK_IN_EPNUM, report, sizeof(report_joystick_t));
#endif #endif
} }
void send_programmable_button(report_programmable_button_t *report) { void send_programmable_button(report_programmable_button_t *report) {
#ifdef PROGRAMMABLE_BUTTON_ENABLE #ifdef PROGRAMMABLE_BUTTON_ENABLE
send_report(report, sizeof(report_programmable_button_t)); send_report(SHARED_IN_EPNUM, report, sizeof(report_programmable_button_t));
#endif
}
void send_digitizer(report_digitizer_t *report) {
#ifdef DIGITIZER_ENABLE
send_report(DIGITIZER_IN_EPNUM, report, sizeof(report_digitizer_t));
#endif #endif
} }
@ -844,24 +787,6 @@ void virtser_send(const uint8_t byte) {
} }
#endif #endif
void send_digitizer(report_digitizer_t *report) {
#ifdef DIGITIZER_ENABLE
uint8_t timeout = 255;
if (USB_DeviceState != DEVICE_STATE_Configured) return;
Endpoint_SelectEndpoint(DIGITIZER_IN_EPNUM);
/* Check if write ready for a polling interval around 10ms */
while (timeout-- && !Endpoint_IsReadWriteAllowed())
_delay_us(40);
if (!Endpoint_IsReadWriteAllowed()) return;
Endpoint_Write_Stream_LE(report, sizeof(report_digitizer_t), NULL);
Endpoint_ClearIN();
#endif
}
/******************************************************************************* /*******************************************************************************
* main * main
******************************************************************************/ ******************************************************************************/