Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 5319e1f

Browse files
committedMar 19, 2025
pbio/sys: Refactor status events.
This refactors a few names without changing any code, setting the stage for the next commit where we update the sys background process. We never quite adopted events across pbio, but only had a status set/clear handler in pbsys, with only one top level handler at pbsys_hmi_handle_status_change. Later, we can call it directly without casting the event data value to void * and back.
1 parent e629d3e commit 5319e1f

File tree

12 files changed

+34
-45
lines changed

12 files changed

+34
-45
lines changed
 

‎lib/pbio/include/pbio/event.h

-17
This file was deleted.

‎lib/pbio/include/pbsys/status.h

+10
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@
1616

1717
#define PBSYS_STATUS_REPORT_SIZE 6
1818

19+
/**
20+
* Status flag change.
21+
*/
22+
typedef enum {
23+
/** System status indicator was set. */
24+
PBSYS_STATUS_CHANGE_SET,
25+
/** System status indicator was cleared. */
26+
PBSYS_STATUS_CHANGE_CLEARED,
27+
} pbsys_status_change_t;
28+
1929
void pbsys_status_set_program_id(pbio_pybricks_user_program_id_t program_id);
2030
void pbsys_status_set(pbio_pybricks_status_t status);
2131
void pbsys_status_clear(pbio_pybricks_status_t status);

‎lib/pbio/sys/bluetooth.c

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
#include <pbdrv/bluetooth.h>
1717
#include <pbio/error.h>
18-
#include <pbio/event.h>
1918
#include <pbio/protocol.h>
2019
#include <pbio/util.h>
2120
#include <pbsys/bluetooth.h>

‎lib/pbio/sys/core.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <pbsys/battery.h>
1111
#include <pbsys/bluetooth.h>
12+
#include <pbsys/status.h>
1213

1314
#include "core.h"
1415
#include "hmi.h"
@@ -30,7 +31,7 @@ PROCESS_THREAD(pbsys_system_process, ev, data) {
3031

3132
for (;;) {
3233
PROCESS_WAIT_EVENT();
33-
pbsys_hmi_handle_event(ev, data);
34+
pbsys_hmi_handle_status_change((pbsys_status_change_t)ev, (pbio_pybricks_status_t)data);
3435
if (ev == PROCESS_EVENT_TIMER && etimer_expired(&timer)) {
3536
etimer_reset(&timer);
3637
pbsys_battery_poll();

‎lib/pbio/sys/hmi.c

+5-6
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include <pbdrv/led.h>
1818
#include <pbio/button.h>
1919
#include <pbio/color.h>
20-
#include <pbio/event.h>
2120
#include <pbio/light.h>
2221
#include <pbsys/config.h>
2322
#include <pbsys/main.h>
@@ -174,17 +173,17 @@ void pbsys_hmi_init(void) {
174173
#endif // PBSYS_CONFIG_BLUETOOTH_TOGGLE
175174
}
176175

177-
void pbsys_hmi_handle_event(process_event_t event, process_data_t data) {
178-
pbsys_status_light_handle_event(event, data);
179-
pbsys_hub_light_matrix_handle_event(event, data);
176+
void pbsys_hmi_handle_status_change(pbsys_status_change_t event, pbio_pybricks_status_t data) {
177+
pbsys_status_light_handle_status_change(event, data);
178+
pbsys_hub_light_matrix_handle_status_change(event, data);
180179

181180
#if PBSYS_CONFIG_BATTERY_CHARGER
182181
// On the Technic Large hub, USB can keep the power on even though we are
183182
// "shutdown", so if the button is pressed again, we reset to turn back on
184183
if (
185184
pbsys_status_test(PBIO_PYBRICKS_STATUS_SHUTDOWN)
186-
&& event == PBIO_EVENT_STATUS_SET
187-
&& (pbio_pybricks_status_t)data == PBIO_PYBRICKS_STATUS_POWER_BUTTON_PRESSED
185+
&& event == PBSYS_STATUS_CHANGE_SET
186+
&& data == PBIO_PYBRICKS_STATUS_POWER_BUTTON_PRESSED
188187
) {
189188
pbdrv_reset(PBDRV_RESET_ACTION_RESET);
190189
}

‎lib/pbio/sys/hmi.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66

77
#include <contiki.h>
88
#include <pbsys/config.h>
9+
#include <pbsys/status.h>
910

1011
void pbsys_hmi_init(void);
11-
void pbsys_hmi_handle_event(process_event_t event, process_data_t data);
12+
void pbsys_hmi_handle_status_change(pbsys_status_change_t event, pbio_pybricks_status_t data);
1213
void pbsys_hmi_poll(void);
1314

1415
#if PBSYS_CONFIG_HMI_NUM_SLOTS

‎lib/pbio/sys/light.c

+5-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#include <pbdrv/led.h>
1212
#include <pbio/color.h>
1313
#include <pbio/error.h>
14-
#include <pbio/event.h>
1514
#include <pbio/light.h>
1615
#include <pbio/util.h>
1716
#include <pbsys/config.h>
@@ -183,7 +182,7 @@ void pbsys_status_light_init(void) {
183182
#endif
184183
}
185184

186-
static void pbsys_status_light_handle_status_change(void) {
185+
static void pbsys_status_light_update_patterns(void) {
187186

188187
// Warning pattern precedence.
189188
pbsys_status_light_indication_warning_t warning_indication = PBSYS_STATUS_LIGHT_INDICATION_WARNING_NONE;
@@ -248,11 +247,11 @@ static uint32_t default_user_program_light_animation_next(pbio_light_animation_t
248247
}
249248
#endif // PBSYS_CONFIG_STATUS_LIGHT_STATE_ANIMATIONS
250249

251-
void pbsys_status_light_handle_event(process_event_t event, process_data_t data) {
252-
if (event == PBIO_EVENT_STATUS_SET || event == PBIO_EVENT_STATUS_CLEARED) {
253-
pbsys_status_light_handle_status_change();
250+
void pbsys_status_light_handle_status_change(pbsys_status_change_t event, pbio_pybricks_status_t data) {
251+
if (event == PBSYS_STATUS_CHANGE_SET || event == PBSYS_STATUS_CHANGE_CLEARED) {
252+
pbsys_status_light_update_patterns();
254253
}
255-
if (event == PBIO_EVENT_STATUS_SET && (pbio_pybricks_status_t)(intptr_t)data == PBIO_PYBRICKS_STATUS_USER_PROGRAM_RUNNING) {
254+
if (event == PBSYS_STATUS_CHANGE_SET && (pbio_pybricks_status_t)(intptr_t)data == PBIO_PYBRICKS_STATUS_USER_PROGRAM_RUNNING) {
256255
#if PBSYS_CONFIG_STATUS_LIGHT_STATE_ANIMATIONS
257256
animation_progress = 0;
258257
pbio_light_animation_init(&pbsys_status_light_main->animation, default_user_program_light_animation_next);

‎lib/pbio/sys/light.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111

1212
#if PBSYS_CONFIG_STATUS_LIGHT
1313
void pbsys_status_light_init(void);
14-
void pbsys_status_light_handle_event(process_event_t event, process_data_t data);
14+
void pbsys_status_light_handle_status_change(pbsys_status_change_t event, pbio_pybricks_status_t data);
1515
void pbsys_status_light_poll(void);
1616
#else
1717
#define pbsys_status_light_init()
18-
#define pbsys_status_light_handle_event(event, data)
18+
#define pbsys_status_light_handle_status_change(event, data)
1919
#define pbsys_status_light_poll()
2020
#endif
2121

‎lib/pbio/sys/light_matrix.c

+3-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
#include <pbdrv/led.h>
1313
#include <pbio/error.h>
14-
#include <pbio/event.h>
1514
#include <pbio/light_matrix.h>
1615
#include <pbio/util.h>
1716
#include <pbsys/config.h>
@@ -139,8 +138,8 @@ static uint32_t pbsys_hub_light_matrix_user_program_animation_next(pbio_light_an
139138
return 40;
140139
}
141140

142-
void pbsys_hub_light_matrix_handle_event(process_event_t event, process_data_t data) {
143-
if (event == PBIO_EVENT_STATUS_SET) {
141+
void pbsys_hub_light_matrix_handle_status_change(pbsys_status_change_t event, pbio_pybricks_status_t data) {
142+
if (event == PBSYS_STATUS_CHANGE_SET) {
144143
pbio_pybricks_status_t status = (intptr_t)data;
145144

146145
if (status == PBIO_PYBRICKS_STATUS_USER_PROGRAM_RUNNING) {
@@ -156,7 +155,7 @@ void pbsys_hub_light_matrix_handle_event(process_event_t event, process_data_t d
156155
// first, which is handled below to avoid a race condition.
157156
pbsys_hub_light_matrix_start_power_animation();
158157
}
159-
} else if (event == PBIO_EVENT_STATUS_CLEARED) {
158+
} else if (event == PBSYS_STATUS_CHANGE_CLEARED) {
160159
pbio_pybricks_status_t status = (intptr_t)data;
161160

162161
// The user program has ended.

‎lib/pbio/sys/light_matrix.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010

1111
#if PBSYS_CONFIG_HUB_LIGHT_MATRIX
1212
void pbsys_hub_light_matrix_init(void);
13-
void pbsys_hub_light_matrix_handle_event(process_event_t event, process_data_t data);
13+
void pbsys_hub_light_matrix_handle_status_change(pbsys_status_change_t event, pbio_pybricks_status_t data);
1414
void pbsys_hub_light_matrix_update_program_slot(void);
1515
#else
1616
#define pbsys_hub_light_matrix_init()
17-
#define pbsys_hub_light_matrix_handle_event(event, data)
17+
#define pbsys_hub_light_matrix_handle_status_change(event, data)
1818
#define pbsys_hub_light_matrix_update_program_slot()
1919
#endif
2020

‎lib/pbio/sys/status.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include <contiki.h>
1111

1212
#include <pbdrv/clock.h>
13-
#include <pbio/event.h>
1413
#include <pbsys/status.h>
1514

1615
static struct {
@@ -33,7 +32,7 @@ static void pbsys_status_update_flag(pbio_pybricks_status_t status, bool set) {
3332
pbsys_status.flags = new_flags;
3433
pbsys_status.changed_time[status] = pbdrv_clock_get_ms();
3534
// REVISIT: this can drop events if event queue is full
36-
process_post(PROCESS_BROADCAST, set ? PBIO_EVENT_STATUS_SET : PBIO_EVENT_STATUS_CLEARED,
35+
process_post(PROCESS_BROADCAST, set ? PBSYS_STATUS_CHANGE_SET : PBSYS_STATUS_CHANGE_CLEARED,
3736
(process_data_t)status);
3837
}
3938

‎lib/pbio/test/sys/test_status.c

+2-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include <tinytest.h>
99
#include <tinytest_macros.h>
1010

11-
#include <pbio/event.h>
1211
#include <pbsys/status.h>
1312
#include <test-pbio.h>
1413

@@ -51,7 +50,7 @@ static PT_THREAD(test_status(struct pt *pt)) {
5150
// ensure that event was broadcast
5251
last_event = PROCESS_EVENT_NONE;
5352
PT_YIELD(pt);
54-
tt_want_uint_op(last_event, ==, PBIO_EVENT_STATUS_SET);
53+
tt_want_uint_op(last_event, ==, PBSYS_STATUS_CHANGE_SET);
5554
tt_want_uint_op(last_data, ==, test_flag);
5655

5756
// ensure that debounce works
@@ -82,7 +81,7 @@ static PT_THREAD(test_status(struct pt *pt)) {
8281
// ensure that event was broadcast
8382
last_event = PROCESS_EVENT_NONE;
8483
PT_YIELD(pt);
85-
tt_want_uint_op(last_event, ==, PBIO_EVENT_STATUS_CLEARED);
84+
tt_want_uint_op(last_event, ==, PBSYS_STATUS_CHANGE_CLEARED);
8685
tt_want_uint_op(last_data, ==, test_flag);
8786

8887
// ensure that debounce works

0 commit comments

Comments
 (0)
Please sign in to comment.