Skip to content

Commit

Permalink
Refactor vusb to protocol use pre/post task (#14944)
Browse files Browse the repository at this point in the history
  • Loading branch information
zvecr authored Mar 14, 2024
1 parent 4bbfeca commit 63dd131
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 60 deletions.
35 changes: 18 additions & 17 deletions quantum/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,9 @@ void protocol_pre_task(void);
void protocol_post_task(void);

// Bodge as refactoring this area sucks....
void protocol_init(void) __attribute__((weak));
void protocol_init(void) {
protocol_pre_init();

keyboard_init();

protocol_post_init();
}

void protocol_task(void) __attribute__((weak));
void protocol_task(void) {
protocol_pre_task();

void protocol_keyboard_task(void) __attribute__((weak));
void protocol_keyboard_task(void) {
keyboard_task();

protocol_post_task();
}

/** \brief Main
Expand All @@ -53,11 +40,25 @@ int main(void) {
protocol_setup();
keyboard_setup();

protocol_init();
protocol_pre_init();
keyboard_init();
protocol_post_init();

/* Main loop */
while (true) {
protocol_task();
protocol_pre_task();
protocol_keyboard_task();
protocol_post_task();

#ifdef RAW_ENABLE
void raw_hid_task(void);
raw_hid_task();
#endif

#ifdef CONSOLE_ENABLE
void console_task(void);
console_task();
#endif

#ifdef QUANTUM_PAINTER_ENABLE
// Run Quantum Painter task
Expand Down
13 changes: 0 additions & 13 deletions tmk_core/protocol/chibios/chibios.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,6 @@ host_driver_t chibios_driver = {keyboard_leds, send_keyboard, send_nkro, send_mo
void virtser_task(void);
#endif

#ifdef RAW_ENABLE
void raw_hid_task(void);
#endif

#ifdef CONSOLE_ENABLE
void console_task(void);
#endif
#ifdef MIDI_ENABLE
void midi_ep_task(void);
#endif
Expand Down Expand Up @@ -209,17 +202,11 @@ void protocol_pre_task(void) {
}

void protocol_post_task(void) {
#ifdef CONSOLE_ENABLE
console_task();
#endif
#ifdef MIDI_ENABLE
midi_ep_task();
#endif
#ifdef VIRTSER_ENABLE
virtser_task();
#endif
#ifdef RAW_ENABLE
raw_hid_task();
#endif
usb_idle_task();
}
6 changes: 1 addition & 5 deletions tmk_core/protocol/lufa/lufa.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ __attribute__((weak)) void raw_hid_receive(uint8_t *data, uint8_t length) {
*
* FIXME: Needs doc
*/
static void raw_hid_task(void) {
void raw_hid_task(void) {
// Create a temporary buffer to hold the read in data from the host
uint8_t data[RAW_EPSIZE];
bool data_read = false;
Expand Down Expand Up @@ -865,10 +865,6 @@ void protocol_post_task(void) {
CDC_Device_USBTask(&cdc_device);
#endif

#ifdef RAW_ENABLE
raw_hid_task();
#endif

#if !defined(INTERRUPT_CONTROL_ENDPOINT)
USB_USBTask();
#endif
Expand Down
30 changes: 6 additions & 24 deletions tmk_core/protocol/vusb/protocol.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,6 @@
# include "sleep_led.h"
#endif

#ifdef CONSOLE_ENABLE
void console_task(void);
#endif

#ifdef RAW_ENABLE
void raw_hid_task(void);
#endif

/* This is from main.c of USBaspLoader */
static void initForUsbConnectivity(void) {
uint8_t i = 0;
Expand Down Expand Up @@ -136,7 +128,7 @@ static inline bool should_do_suspend(void) {
return vusb_suspended;
}

void protocol_task(void) {
void protocol_pre_task(void) {
#if !defined(NO_USB_STARTUP_CHECK)
if (should_do_suspend()) {
dprintln("suspending keyboard");
Expand All @@ -159,28 +151,18 @@ void protocol_task(void) {
vusb_wakeup();
}
#endif
}

void protocol_keyboard_task(void) {
usbPoll();

// TODO: configuration process is inconsistent. it sometime fails.
// To prevent failing to configure NOT scan keyboard during configuration
if (usbConfiguration && usbInterruptIsReady()) {
keyboard_task();
}
}

#ifdef RAW_ENABLE
usbPoll();

if (usbConfiguration && usbInterruptIsReady4()) {
raw_hid_task();
}
#endif

#ifdef CONSOLE_ENABLE
usbPoll();

if (usbConfiguration && usbInterruptIsReady3()) {
console_task();
}
#endif
void protocol_post_task(void) {
// do nothing
}
10 changes: 9 additions & 1 deletion tmk_core/protocol/vusb/vusb.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@ __attribute__((weak)) void raw_hid_receive(uint8_t *data, uint8_t length) {
}

void raw_hid_task(void) {
usbPoll();

if (!usbConfiguration || !usbInterruptIsReady4()) {
return;
}

if (raw_output_received_bytes == RAW_BUFFER_SIZE) {
raw_hid_receive(raw_output_buffer, RAW_BUFFER_SIZE);
raw_output_received_bytes = 0;
Expand All @@ -182,7 +188,9 @@ int8_t sendchar(uint8_t c) {
}

void console_task(void) {
if (!usbConfiguration) {
usbPoll();

if (!usbConfiguration || !usbInterruptIsReady3()) {
return;
}

Expand Down

0 comments on commit 63dd131

Please sign in to comment.