diff --git a/src/tasks.c b/src/tasks.c index c802da1..49d1ad7 100644 --- a/src/tasks.c +++ b/src/tasks.c @@ -59,16 +59,44 @@ void usb_host_task(device_t *state) { tuh_task(); } +mouse_report_t *screensaver_pong(device_t *state) { + static mouse_report_t report = {0}; + static int dx = 20, dy = 25; + + /* Check if we are bouncing off the walls and reverse direction in that case. */ + if (report.x + dx < MIN_SCREEN_COORD || report.x + dx > MAX_SCREEN_COORD) + dx = -dx; + + if (report.y + dy < MIN_SCREEN_COORD || report.y + dy > MAX_SCREEN_COORD) + dy = -dy; + + report.x += dx; + report.y += dy; + + return &report; +} + +mouse_report_t *screensaver_jitter(device_t *state) { + const int16_t jitter_distance = 2; + static mouse_report_t report = { + .x = jitter_distance, + .y = jitter_distance, + .mode = RELATIVE, + }; + + report.x = -report.x; + report.y = -report.y; + + return &report; +} + /* Have something fun and entertaining when idle. */ void screensaver_task(device_t *state) { const int mouse_move_delay = 5000; + static int last_pointer_move = 0; screensaver_t *screensaver = &state->config.output[BOARD_ROLE].screensaver; uint64_t inactivity_period = time_us_64() - state->last_activity[BOARD_ROLE]; - static mouse_report_t report = {0}; - static int last_pointer_move = 0; - static int dx = 20, dy = 25, jitter = 1; - /* If we're not enabled, nothing to do here. */ if (screensaver->mode == DISABLED) return; @@ -90,29 +118,22 @@ void screensaver_task(device_t *state) { if ((time_us_32()) - last_pointer_move < mouse_move_delay) return; + mouse_report_t *report; switch (screensaver->mode) { case PONG: - /* Check if we are bouncing off the walls and reverse direction in that case. */ - if (report.x + dx < MIN_SCREEN_COORD || report.x + dx > MAX_SCREEN_COORD) - dx = -dx; - - if (report.y + dy < MIN_SCREEN_COORD || report.y + dy > MAX_SCREEN_COORD) - dy = -dy; - - report.x += dx; - report.y += dy; - + report = screensaver_pong(state); break; case JITTER: - report.x = state->pointer_x + jitter; - report.y = state->pointer_y + jitter; - jitter = -jitter; + report = screensaver_jitter(state); break; + + default: + return; } /* Move mouse pointer */ - queue_mouse_report(&report, state); + queue_mouse_report(report, state); /* Update timer of the last pointer move */ last_pointer_move = time_us_32();