Use relative mouse reports for jitter screensaver mode

This commit is contained in:
Ellie T 2024-12-04 20:01:53 +11:00
parent cbb98e4a88
commit 85089fee11
1 changed files with 39 additions and 18 deletions

View File

@ -59,16 +59,44 @@ void usb_host_task(device_t *state) {
tuh_task(); 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. */ /* Have something fun and entertaining when idle. */
void screensaver_task(device_t *state) { void screensaver_task(device_t *state) {
const int mouse_move_delay = 5000; const int mouse_move_delay = 5000;
static int last_pointer_move = 0;
screensaver_t *screensaver = &state->config.output[BOARD_ROLE].screensaver; screensaver_t *screensaver = &state->config.output[BOARD_ROLE].screensaver;
uint64_t inactivity_period = time_us_64() - state->last_activity[BOARD_ROLE]; 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 we're not enabled, nothing to do here. */
if (screensaver->mode == DISABLED) if (screensaver->mode == DISABLED)
return; return;
@ -90,29 +118,22 @@ void screensaver_task(device_t *state) {
if ((time_us_32()) - last_pointer_move < mouse_move_delay) if ((time_us_32()) - last_pointer_move < mouse_move_delay)
return; return;
mouse_report_t *report;
switch (screensaver->mode) { switch (screensaver->mode) {
case PONG: case PONG:
/* Check if we are bouncing off the walls and reverse direction in that case. */ report = screensaver_pong(state);
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;
break; break;
case JITTER: case JITTER:
report.x = state->pointer_x + jitter; report = screensaver_jitter(state);
report.y = state->pointer_y + jitter;
jitter = -jitter;
break; break;
default:
return;
} }
/* Move mouse pointer */ /* Move mouse pointer */
queue_mouse_report(&report, state); queue_mouse_report(report, state);
/* Update timer of the last pointer move */ /* Update timer of the last pointer move */
last_pointer_move = time_us_32(); last_pointer_move = time_us_32();