Skip to content

Commit 79349f8

Browse files
committed
sys/light_grid: optimize user animation
This replaces the large animation data array with a custom callback function that only pokes the pixels that are being animated and shares the same brightness data between all pixels.
1 parent 7617a6a commit 79349f8

File tree

1 file changed

+33
-45
lines changed

1 file changed

+33
-45
lines changed

lib/pbio/sys/light_grid.c

Lines changed: 33 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -48,54 +48,42 @@ void pbsys_hub_light_grid_init() {
4848
pbio_light_grid_init(pbsys_hub_light_grid, 5, &pbsys_hub_light_grid_funcs);
4949
}
5050

51-
// FIXME: compress / implement differently
52-
static const uint8_t pbsys_hub_light_grid_user_program_animation_cells[] = {
53-
0, 0, 0, 0, 0, 10, 61, 99, 79, 25, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
54-
0, 0, 0, 0, 0, 6, 53, 97, 85, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
55-
0, 0, 0, 0, 0, 3, 45, 94, 90, 39, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
56-
0, 0, 0, 0, 0, 1, 38, 89, 94, 47, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
57-
0, 0, 0, 0, 0, 0, 30, 84, 97, 55, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
58-
0, 0, 0, 0, 0, 1, 23, 78, 99, 63, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
59-
0, 0, 0, 0, 0, 2, 17, 71, 100, 70, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
60-
0, 0, 0, 0, 0, 5, 12, 63, 99, 77, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
61-
0, 0, 0, 0, 0, 9, 7, 56, 98, 83, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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
0, 0, 0, 0, 0, 43, 92, 92, 42, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
89-
0, 0, 0, 0, 0, 35, 88, 95, 50, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
90-
0, 0, 0, 0, 0, 28, 82, 98, 57, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
91-
0, 0, 0, 0, 0, 21, 76, 100, 65, 13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
92-
0, 0, 0, 0, 0, 15, 68, 100, 72, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
93-
};
51+
static uint32_t pbsys_hub_light_grid_user_program_animation_next(pbio_light_animation_t *animation) {
52+
// The indexes of pixels to light up
53+
static const uint8_t indexes[] = { 6, 7, 8, 13, 18, 17, 16, 11 };
54+
// sine wave for brightness
55+
static const uint8_t data[] = {
56+
0, 1, 2, 3, 5, 7, 10, 13, 17, 21, 25, 30, 35, 40, 45, 50,
57+
56, 61, 66, 71, 75, 80, 84, 88, 91, 94, 96, 98, 99, 100, 100, 100,
58+
99, 98, 96, 94, 91, 88, 84, 80, 75, 71, 66, 61, 56, 50, 45, 40,
59+
35, 30, 25, 21, 17, 13, 10, 7, 5, 3, 2, 1, 0, 0, 0, 0,
60+
};
61+
// keeps track of where we are in the animation
62+
static uint8_t offset = 0;
63+
64+
pbdrv_led_array_dev_t *array;
65+
if (pbdrv_led_array_get_dev(0, &array) == PBIO_SUCCESS) {
66+
for (int i = 0; i < PBIO_ARRAY_SIZE(indexes); i++) {
67+
// Pixels are offset equally from each other in the data array so that
68+
// we get an animation that looks like the brightess is smoothly moving
69+
// from pixel to pixel.
70+
uint8_t brightness = data[(i * PBIO_ARRAY_SIZE(indexes) + offset++) % PBIO_ARRAY_SIZE(data)];
71+
pbdrv_led_array_set_brightness(array, indexes[i], brightness);
72+
// As long as PBIO_ARRAY_SIZE(data) is a multiple of the max offset (+1),
73+
// offset wraps around correctly when the addition overflows.
74+
_Static_assert((1 << (sizeof(offset) * 8)) % PBIO_ARRAY_SIZE(data) == 0, "needed for correct wraparound");
75+
}
76+
}
77+
78+
return 100;
79+
}
9480

9581
void pbsys_hub_light_grid_handle_event(process_event_t event, process_data_t data) {
9682
if (event == PBIO_EVENT_STATUS_SET && (pbsys_status_t)data == PBSYS_STATUS_USER_PROGRAM_RUNNING) {
97-
pbio_light_grid_start_animation(pbsys_hub_light_grid, pbsys_hub_light_grid_user_program_animation_cells,
98-
PBIO_ARRAY_SIZE(pbsys_hub_light_grid_user_program_animation_cells) / 25, 25);
83+
uint8_t rows[5] = {0};
84+
pbio_light_grid_set_rows(pbsys_hub_light_grid, rows);
85+
pbio_light_animation_init(&pbsys_hub_light_grid->animation, pbsys_hub_light_grid_user_program_animation_next);
86+
pbio_light_animation_start(&pbsys_hub_light_grid->animation);
9987
}
10088
if (event == PBIO_EVENT_STATUS_CLEARED && (pbsys_status_t)data == PBSYS_STATUS_USER_PROGRAM_RUNNING) {
10189
uint8_t rows[5] = {0};

0 commit comments

Comments
 (0)