Skip to content

Commit

Permalink
pbio/sys/light: Restart animation on program start.
Browse files Browse the repository at this point in the history
This ensures that the animation looks consistent
when you start a program, rather than picking up
where it stopped last time.

This makes it easier to see that a program was
started when you run it while connected, since the
light always switches from solid to off and then
fades.
  • Loading branch information
laurensvalk committed Sep 3, 2024
1 parent be91cac commit de12ee5
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions lib/pbio/sys/light.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,21 +234,25 @@ static void pbsys_status_light_handle_status_change(void) {
}
}

static uint8_t animation_progress;

static uint32_t default_user_program_light_animation_next(pbio_light_animation_t *animation) {
// The brightness pattern has the form /\ through which we cycle in N steps.
static uint8_t cycle = 0;
const uint8_t cycle_max = 200;
// It is reset back to the start when the user program starts.
const uint8_t animation_progress_max = 200;

pbio_color_hsv_t hsv = {
.h = PBIO_COLOR_HUE_BLUE,
.s = 100,
.v = cycle < cycle_max / 2 ? cycle : cycle_max - cycle,
.v = animation_progress < animation_progress_max / 2 ?
animation_progress :
animation_progress_max - animation_progress,
};

pbsys_status_light->funcs->set_hsv(pbsys_status_light, &hsv);

// This increment controls the speed of the pattern and wraps on completion
cycle = (cycle + 4) % cycle_max;
animation_progress = (animation_progress + 4) % animation_progress_max;

return 40;
}
Expand All @@ -258,6 +262,7 @@ void pbsys_status_light_handle_event(process_event_t event, process_data_t data)
pbsys_status_light_handle_status_change();
}
if (event == PBIO_EVENT_STATUS_SET && (pbio_pybricks_status_t)(intptr_t)data == PBIO_PYBRICKS_STATUS_USER_PROGRAM_RUNNING) {
animation_progress = 0;
pbio_light_animation_init(&pbsys_status_light->animation, default_user_program_light_animation_next);
pbio_light_animation_start(&pbsys_status_light->animation);
}
Expand Down

0 comments on commit de12ee5

Please sign in to comment.