Skip to content

Commit 6102eef

Browse files
committed
pbio/sys/status: Handle status change synchronously.
This was using broadcast, but there was ultimately only one receiver for this data. We can simplify this and avoid the risk of overrunning the event queue by calling it synchronously, which is safe for this event. After the refactoring in the previous commit, this is now a trivial change.
1 parent 8fbca1d commit 6102eef

File tree

3 files changed

+8
-16
lines changed

3 files changed

+8
-16
lines changed

lib/pbio/sys/core.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ PROCESS_THREAD(pbsys_system_process, ev, data) {
3131

3232
for (;;) {
3333
PROCESS_WAIT_EVENT();
34-
pbsys_hmi_handle_status_change((pbsys_status_change_t)ev, (pbio_pybricks_status_t)data);
34+
3535
if (ev == PROCESS_EVENT_TIMER && etimer_expired(&timer)) {
3636
etimer_reset(&timer);
3737
pbsys_battery_poll();

lib/pbio/sys/status.c

+7-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include <pbdrv/clock.h>
1313
#include <pbsys/status.h>
1414

15+
#include "hmi.h"
16+
1517
static struct {
1618
/** Status indications as bit flags */
1719
uint32_t flags;
@@ -31,9 +33,11 @@ static void pbsys_status_update_flag(pbio_pybricks_status_t status, bool set) {
3133

3234
pbsys_status.flags = new_flags;
3335
pbsys_status.changed_time[status] = pbdrv_clock_get_ms();
34-
// REVISIT: this can drop events if event queue is full
35-
process_post(PROCESS_BROADCAST, set ? PBSYS_STATUS_CHANGE_SET : PBSYS_STATUS_CHANGE_CLEARED,
36-
(process_data_t)status);
36+
37+
// hmi is the only subscriber to status changes, so call its handler
38+
// directly. All other processes just poll the status as needed. If we ever
39+
// need more subscribers, we could register callbacks and call them here.
40+
pbsys_hmi_handle_status_change(set ? PBSYS_STATUS_CHANGE_SET : PBSYS_STATUS_CHANGE_CLEARED, status);
3741
}
3842

3943
/**

lib/pbio/test/sys/test_status.c

-12
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,6 @@ static PT_THREAD(test_status(struct pt *pt)) {
4747
tt_want(!pbsys_status_test_debounce(test_flag, true, 10));
4848
tt_want(!pbsys_status_test_debounce(test_flag, false, 10));
4949

50-
// ensure that event was broadcast
51-
last_event = PROCESS_EVENT_NONE;
52-
PT_YIELD(pt);
53-
tt_want_uint_op(last_event, ==, PBSYS_STATUS_CHANGE_SET);
54-
tt_want_uint_op(last_data, ==, test_flag);
55-
5650
// ensure that debounce works
5751
pbio_test_clock_tick(9);
5852
PT_YIELD(pt);
@@ -78,12 +72,6 @@ static PT_THREAD(test_status(struct pt *pt)) {
7872
tt_want(!pbsys_status_test_debounce(test_flag, true, 10));
7973
tt_want(!pbsys_status_test_debounce(test_flag, false, 10));
8074

81-
// ensure that event was broadcast
82-
last_event = PROCESS_EVENT_NONE;
83-
PT_YIELD(pt);
84-
tt_want_uint_op(last_event, ==, PBSYS_STATUS_CHANGE_CLEARED);
85-
tt_want_uint_op(last_data, ==, test_flag);
86-
8775
// ensure that debounce works
8876
last_event = PROCESS_EVENT_NONE;
8977
pbio_test_clock_tick(9);

0 commit comments

Comments
 (0)