Skip to content

Commit 9851fda

Browse files
committed
pbio/sys/light_matrix: Handle animation synchronously with program.
The status event handler would only change this on program start and stop events, so we could just as well do it exactly there.
1 parent 220cb92 commit 9851fda

File tree

4 files changed

+13
-14
lines changed

4 files changed

+13
-14
lines changed

lib/pbio/sys/hmi.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,6 @@ void pbsys_hmi_init(void) {
175175

176176
void pbsys_hmi_handle_status_change(pbsys_status_change_t event, pbio_pybricks_status_t data) {
177177
pbsys_status_light_handle_status_change(event, data);
178-
pbsys_hub_light_matrix_handle_status_change(event, data);
179178
}
180179

181180
/**

lib/pbio/sys/light_matrix.c

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -143,22 +143,19 @@ static uint32_t pbsys_hub_light_matrix_user_program_animation_next(pbio_light_an
143143
return 40;
144144
}
145145

146-
void pbsys_hub_light_matrix_handle_status_change(pbsys_status_change_t event, pbio_pybricks_status_t data) {
147-
148-
pbio_pybricks_status_t status = (intptr_t)data;
149-
150-
// Only need to handle changing program running state.
151-
if (status != PBIO_PYBRICKS_STATUS_USER_PROGRAM_RUNNING) {
152-
return;
153-
}
154-
155-
if (event == PBSYS_STATUS_CHANGE_SET) {
146+
/**
147+
* Updates light matrix behavior when program is started or stopped.
148+
*
149+
* @param start @c true for start or @c false for stop.
150+
*/
151+
void pbsys_hub_light_matrix_handle_user_program_start(bool start) {
152+
if (start) {
156153
// The user animation updates only a subset of pixels to save time,
157154
// so the rest must be cleared before it starts.
158155
pbsys_hub_light_matrix_user_program_animation_clear();
159156
pbio_light_animation_init(&pbsys_hub_light_matrix->animation, pbsys_hub_light_matrix_user_program_animation_next);
160157
pbio_light_animation_start(&pbsys_hub_light_matrix->animation);
161-
} else if (event == PBSYS_STATUS_CHANGE_CLEARED) {
158+
} else {
162159
// If the user program has ended, show stop sign and selected slot.
163160
pbsys_hub_light_matrix_show_idle_ui(100);
164161
}

lib/pbio/sys/light_matrix.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
#if PBSYS_CONFIG_HUB_LIGHT_MATRIX
1212
void pbsys_hub_light_matrix_init(void);
1313
void pbsys_hub_light_matrix_deinit(void);
14-
void pbsys_hub_light_matrix_handle_status_change(pbsys_status_change_t event, pbio_pybricks_status_t data);
14+
void pbsys_hub_light_matrix_handle_user_program_start(bool start);
1515
void pbsys_hub_light_matrix_update_program_slot(void);
1616
#else
1717
#define pbsys_hub_light_matrix_init()
1818
#define pbsys_hub_light_matrix_deinit()
19-
#define pbsys_hub_light_matrix_handle_status_change(event, data)
19+
#define pbsys_hub_light_matrix_handle_user_program_start(start)
2020
#define pbsys_hub_light_matrix_update_program_slot()
2121
#endif
2222

lib/pbio/sys/main.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <pbsys/main.h>
1818
#include <pbsys/status.h>
1919

20+
#include "light_matrix.h"
2021
#include "program_stop.h"
2122
#include "storage.h"
2223
#include <pbsys/program_stop.h>
@@ -105,6 +106,7 @@ int main(int argc, char **argv) {
105106
pbsys_status_set_program_id(program.id);
106107
pbsys_status_set(PBIO_PYBRICKS_STATUS_USER_PROGRAM_RUNNING);
107108
pbsys_host_rx_set_callback(pbsys_main_stdin_event);
109+
pbsys_hub_light_matrix_handle_user_program_start(true);
108110

109111
// Handle pending events triggered by the status change, such as
110112
// starting status light animation.
@@ -118,6 +120,7 @@ int main(int argc, char **argv) {
118120
pbsys_status_clear(PBIO_PYBRICKS_STATUS_USER_PROGRAM_RUNNING);
119121
pbsys_host_rx_set_callback(NULL);
120122
pbsys_program_stop_set_buttons(PBIO_BUTTON_CENTER);
123+
pbsys_hub_light_matrix_handle_user_program_start(false);
121124
pbio_stop_all(true);
122125
program.start_request_type = PBSYS_MAIN_PROGRAM_START_REQUEST_TYPE_NONE;
123126

0 commit comments

Comments
 (0)