77#include <stdbool.h>
88#include <stdint.h>
99
10- #include <contiki.h>
11-
1210#include <pbio/os.h>
1311
14- #include <pbdrv/clock.h>
1512#include <pbsys/status.h>
1613
1714#include "hmi.h"
@@ -28,6 +25,26 @@ static struct {
2825 pbio_pybricks_user_program_id_t slot ;
2926} pbsys_status ;
3027
28+ /**
29+ * Let other processes and external hosts know that the status changed.
30+ */
31+ static void pbsys_status_update_emit (void ) {
32+
33+ uint8_t buf [PBIO_PYBRICKS_EVENT_STATUS_REPORT_SIZE ];
34+ pbio_pybricks_event_status_report (buf , pbsys_status .flags , pbsys_status .program_id , pbsys_status .slot );
35+
36+ // TODO: Here we will call pbsys_host_schedule_status_update(buf), which
37+ // sets the status in host drivers such as USB and bluetooth so they
38+ // can write it out when possible.
39+
40+ // Other processes may be awaiting status changes, so poll.
41+ pbio_os_request_poll ();
42+
43+ // REVISIT: Can be deleted once all processes that poll the status are
44+ // updated to use the new pbio os event loop.
45+ process_post (PROCESS_BROADCAST , PROCESS_EVENT_COM , NULL );
46+ }
47+
3148static void pbsys_status_update_flag (pbio_pybricks_status_flags_t status , bool set ) {
3249 uint32_t new_flags = set ? pbsys_status .flags | PBIO_PYBRICKS_STATUS_FLAG (status ) : pbsys_status .flags & ~PBIO_PYBRICKS_STATUS_FLAG (status );
3350
@@ -39,18 +56,11 @@ static void pbsys_status_update_flag(pbio_pybricks_status_flags_t status, bool s
3956 pbsys_status .flags = new_flags ;
4057 pbsys_status .changed_time [status ] = pbdrv_clock_get_ms ();
4158
42- // status light is the only subscriber to status changes, so call its handler
43- // directly. All other processes just poll the status as needed. If we ever
44- // need more subscribers, we could register callbacks and call them here.
45- pbsys_status_light_handle_status_change ();
46-
47- // Other processes may be awaiting status changes, so poll.
48- pbio_os_request_poll ();
49-
50- // REVISIT: Can be deleted once all processes that poll the status are
51- // updated to use the new pbio os event loop.
52- process_post (PROCESS_BROADCAST , PROCESS_EVENT_COM , NULL );
59+ // Let everyone know about new flags.
60+ pbsys_status_update_emit ();
5361
62+ // Status light may need updating if flags have changed.
63+ pbsys_status_light_handle_status_change ();
5464}
5565
5666/**
@@ -77,9 +87,11 @@ void pbsys_status_increment_selected_slot(bool increment) {
7787 #if PBSYS_CONFIG_HMI_NUM_SLOTS
7888 if (increment && pbsys_status .slot + 1 < PBSYS_CONFIG_HMI_NUM_SLOTS ) {
7989 pbsys_status .slot ++ ;
90+ pbsys_status_update_emit ();
8091 }
8192 if (!increment && pbsys_status .slot > 0 ) {
8293 pbsys_status .slot -- ;
94+ pbsys_status_update_emit ();
8395 }
8496 #endif
8597}
@@ -101,7 +113,12 @@ pbio_pybricks_user_program_id_t pbsys_status_get_selected_slot(void) {
101113 * @param [in] program_id The identifier to set.
102114 */
103115void pbsys_status_set_program_id (pbio_pybricks_user_program_id_t program_id ) {
116+ if (pbsys_status .program_id == program_id ) {
117+ return ;
118+ }
119+
104120 pbsys_status .program_id = program_id ;
121+ pbsys_status_update_emit ();
105122}
106123
107124/**
0 commit comments