Skip to content

Commit

Permalink
Merge pull request #181 from eltariel/relative-jitter
Browse files Browse the repository at this point in the history
Use relative mouse reports for jitter screensaver mode
  • Loading branch information
hrvach authored Dec 4, 2024
2 parents cbb98e4 + 85089fe commit fa4ccdf
Showing 1 changed file with 39 additions and 18 deletions.
57 changes: 39 additions & 18 deletions src/tasks.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down

0 comments on commit fa4ccdf

Please sign in to comment.