Skip to content

Commit

Permalink
Always run USB polling from an interrupt to avoid lag from rendering
Browse files Browse the repository at this point in the history
Sending while text was scrolling was running very slowly
  • Loading branch information
stecman committed Sep 24, 2023
1 parent a7a2ffd commit dd2d457
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 23 deletions.
20 changes: 7 additions & 13 deletions firmware/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ class CodepointSender
{
// Start sending keys if it's not running yet
if (m_send_timer.alarm_id == 0) {
add_repeating_timer_ms(5, key_send_callback, this, &m_send_timer);
add_repeating_timer_ms(1, key_send_callback, this, &m_send_timer);
update();
}
}
Expand Down Expand Up @@ -273,6 +273,10 @@ int main()
usb_init();
stdio_usb_init();

// Run USB polling from an interrupt as the main loop slows down when rendering
static struct repeating_timer usb_timer;
add_repeating_timer_ms(5, background_usb_poll, NULL, &usb_timer);

printf("\n\nDevice has reset\n");

// GPIOs 0-11 as inputs with pull-up
Expand Down Expand Up @@ -332,16 +336,8 @@ int main()
// Scroll so 0,0 in memory is actually rendered in the top-left corner
st7789_vertical_scroll(300);

// Start the application
// Temporarily polls usb from an interrupt as app.load() blocks for a while
{
struct repeating_timer timer;
add_repeating_timer_ms(5, background_usb_poll, NULL, &timer);

app.load("fonts");

cancel_repeating_timer(&timer);
}
// Start the application (blocking until loaded)
app.load("fonts");

// Turn on data input LEDs (inverted as this drives a P-channel mosfet)
{
Expand All @@ -366,8 +362,6 @@ int main()
UserInput send_switch(PIN_SWITCH_SEND);

while (true) {
usb_poll();

if (needs_render) {
needs_render = false;

Expand Down
10 changes: 0 additions & 10 deletions firmware/usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,6 @@ void usb_poll(void)
{
tud_task();

// Send reports at a fixed interval
const uint32_t interval_ms = 5;
static uint32_t start_ms = 0;

if (board_millis() - start_ms < interval_ms) {
return; // not enough time
}

start_ms += interval_ms;

if (!tud_hid_ready()) {
return;
}
Expand Down

0 comments on commit dd2d457

Please sign in to comment.