Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 0 additions & 17 deletions lib/pbio/include/pbio/event.h

This file was deleted.

10 changes: 10 additions & 0 deletions lib/pbio/include/pbsys/status.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@

#define PBSYS_STATUS_REPORT_SIZE 6

/**
* Status flag change.
*/
typedef enum {
/** System status indicator was set. */
PBSYS_STATUS_CHANGE_SET,
/** System status indicator was cleared. */
PBSYS_STATUS_CHANGE_CLEARED,
} pbsys_status_change_t;

void pbsys_status_set_program_id(pbio_pybricks_user_program_id_t program_id);
void pbsys_status_set(pbio_pybricks_status_t status);
void pbsys_status_clear(pbio_pybricks_status_t status);
Expand Down
1 change: 0 additions & 1 deletion lib/pbio/sys/bluetooth.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

#include <pbdrv/bluetooth.h>
#include <pbio/error.h>
#include <pbio/event.h>
#include <pbio/protocol.h>
#include <pbio/util.h>
#include <pbsys/host.h>
Expand Down
3 changes: 2 additions & 1 deletion lib/pbio/sys/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <pbsys/battery.h>
#include <pbsys/host.h>
#include <pbsys/status.h>

#include "core.h"
#include "hmi.h"
Expand All @@ -29,7 +30,7 @@ PROCESS_THREAD(pbsys_system_process, ev, data) {

for (;;) {
PROCESS_WAIT_EVENT();
pbsys_hmi_handle_event(ev, data);

if (ev == PROCESS_EVENT_TIMER && etimer_expired(&timer)) {
etimer_reset(&timer);
pbsys_battery_poll();
Expand Down
11 changes: 5 additions & 6 deletions lib/pbio/sys/hmi.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#include <pbdrv/led.h>
#include <pbio/button.h>
#include <pbio/color.h>
#include <pbio/event.h>
#include <pbio/light.h>
#include <pbsys/config.h>
#include <pbsys/main.h>
Expand Down Expand Up @@ -174,17 +173,17 @@ void pbsys_hmi_init(void) {
#endif // PBSYS_CONFIG_BLUETOOTH_TOGGLE
}

void pbsys_hmi_handle_event(process_event_t event, process_data_t data) {
pbsys_status_light_handle_event(event, data);
pbsys_hub_light_matrix_handle_event(event, data);
void pbsys_hmi_handle_status_change(pbsys_status_change_t event, pbio_pybricks_status_t data) {
pbsys_status_light_handle_status_change(event, data);
pbsys_hub_light_matrix_handle_status_change(event, data);

#if PBSYS_CONFIG_BATTERY_CHARGER
// On the Technic Large hub, USB can keep the power on even though we are
// "shutdown", so if the button is pressed again, we reset to turn back on
if (
pbsys_status_test(PBIO_PYBRICKS_STATUS_SHUTDOWN)
&& event == PBIO_EVENT_STATUS_SET
&& (pbio_pybricks_status_t)data == PBIO_PYBRICKS_STATUS_POWER_BUTTON_PRESSED
&& event == PBSYS_STATUS_CHANGE_SET
&& data == PBIO_PYBRICKS_STATUS_POWER_BUTTON_PRESSED
) {
pbdrv_reset(PBDRV_RESET_ACTION_RESET);
}
Expand Down
3 changes: 2 additions & 1 deletion lib/pbio/sys/hmi.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@

#include <contiki.h>
#include <pbsys/config.h>
#include <pbsys/status.h>

void pbsys_hmi_init(void);
void pbsys_hmi_handle_event(process_event_t event, process_data_t data);
void pbsys_hmi_handle_status_change(pbsys_status_change_t event, pbio_pybricks_status_t data);
void pbsys_hmi_poll(void);

#if PBSYS_CONFIG_HMI_NUM_SLOTS
Expand Down
11 changes: 5 additions & 6 deletions lib/pbio/sys/light.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include <pbdrv/led.h>
#include <pbio/color.h>
#include <pbio/error.h>
#include <pbio/event.h>
#include <pbio/light.h>
#include <pbio/util.h>
#include <pbsys/config.h>
Expand Down Expand Up @@ -183,7 +182,7 @@ void pbsys_status_light_init(void) {
#endif
}

static void pbsys_status_light_handle_status_change(void) {
static void pbsys_status_light_update_patterns(void) {

// Warning pattern precedence.
pbsys_status_light_indication_warning_t warning_indication = PBSYS_STATUS_LIGHT_INDICATION_WARNING_NONE;
Expand Down Expand Up @@ -248,11 +247,11 @@ static uint32_t default_user_program_light_animation_next(pbio_light_animation_t
}
#endif // PBSYS_CONFIG_STATUS_LIGHT_STATE_ANIMATIONS

void pbsys_status_light_handle_event(process_event_t event, process_data_t data) {
if (event == PBIO_EVENT_STATUS_SET || event == PBIO_EVENT_STATUS_CLEARED) {
pbsys_status_light_handle_status_change();
void pbsys_status_light_handle_status_change(pbsys_status_change_t event, pbio_pybricks_status_t data) {
if (event == PBSYS_STATUS_CHANGE_SET || event == PBSYS_STATUS_CHANGE_CLEARED) {
pbsys_status_light_update_patterns();
}
if (event == PBIO_EVENT_STATUS_SET && (pbio_pybricks_status_t)(intptr_t)data == PBIO_PYBRICKS_STATUS_USER_PROGRAM_RUNNING) {
if (event == PBSYS_STATUS_CHANGE_SET && (pbio_pybricks_status_t)(intptr_t)data == PBIO_PYBRICKS_STATUS_USER_PROGRAM_RUNNING) {
#if PBSYS_CONFIG_STATUS_LIGHT_STATE_ANIMATIONS
animation_progress = 0;
pbio_light_animation_init(&pbsys_status_light_main->animation, default_user_program_light_animation_next);
Expand Down
4 changes: 2 additions & 2 deletions lib/pbio/sys/light.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@

#if PBSYS_CONFIG_STATUS_LIGHT
void pbsys_status_light_init(void);
void pbsys_status_light_handle_event(process_event_t event, process_data_t data);
void pbsys_status_light_handle_status_change(pbsys_status_change_t event, pbio_pybricks_status_t data);
void pbsys_status_light_poll(void);
#else
#define pbsys_status_light_init()
#define pbsys_status_light_handle_event(event, data)
#define pbsys_status_light_handle_status_change(event, data)
#define pbsys_status_light_poll()
#endif

Expand Down
7 changes: 3 additions & 4 deletions lib/pbio/sys/light_matrix.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

#include <pbdrv/led.h>
#include <pbio/error.h>
#include <pbio/event.h>
#include <pbio/light_matrix.h>
#include <pbio/util.h>
#include <pbsys/config.h>
Expand Down Expand Up @@ -139,8 +138,8 @@ static uint32_t pbsys_hub_light_matrix_user_program_animation_next(pbio_light_an
return 40;
}

void pbsys_hub_light_matrix_handle_event(process_event_t event, process_data_t data) {
if (event == PBIO_EVENT_STATUS_SET) {
void pbsys_hub_light_matrix_handle_status_change(pbsys_status_change_t event, pbio_pybricks_status_t data) {
if (event == PBSYS_STATUS_CHANGE_SET) {
pbio_pybricks_status_t status = (intptr_t)data;

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

// The user program has ended.
Expand Down
4 changes: 2 additions & 2 deletions lib/pbio/sys/light_matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@

#if PBSYS_CONFIG_HUB_LIGHT_MATRIX
void pbsys_hub_light_matrix_init(void);
void pbsys_hub_light_matrix_handle_event(process_event_t event, process_data_t data);
void pbsys_hub_light_matrix_handle_status_change(pbsys_status_change_t event, pbio_pybricks_status_t data);
void pbsys_hub_light_matrix_update_program_slot(void);
#else
#define pbsys_hub_light_matrix_init()
#define pbsys_hub_light_matrix_handle_event(event, data)
#define pbsys_hub_light_matrix_handle_status_change(event, data)
#define pbsys_hub_light_matrix_update_program_slot()
#endif

Expand Down
21 changes: 17 additions & 4 deletions lib/pbio/sys/status.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@

#include <contiki.h>

#include <pbio/os.h>

#include <pbdrv/clock.h>
#include <pbio/event.h>
#include <pbsys/status.h>

#include "hmi.h"

static struct {
/** Status indications as bit flags */
uint32_t flags;
Expand All @@ -32,9 +35,19 @@ static void pbsys_status_update_flag(pbio_pybricks_status_t status, bool set) {

pbsys_status.flags = new_flags;
pbsys_status.changed_time[status] = pbdrv_clock_get_ms();
// REVISIT: this can drop events if event queue is full
process_post(PROCESS_BROADCAST, set ? PBIO_EVENT_STATUS_SET : PBIO_EVENT_STATUS_CLEARED,
(process_data_t)status);

// hmi is the only subscriber to status changes, so call its handler
// directly. All other processes just poll the status as needed. If we ever
// need more subscribers, we could register callbacks and call them here.
pbsys_hmi_handle_status_change(set ? PBSYS_STATUS_CHANGE_SET : PBSYS_STATUS_CHANGE_CLEARED, status);

// Other processes may be awaiting status changes, so poll.
pbio_os_request_poll();

// REVISIT: Can be deleted once all processes that poll the status are
// updated to use the new pbio os event loop.
process_post(PROCESS_BROADCAST, PROCESS_EVENT_COM, NULL);

}

/**
Expand Down
13 changes: 0 additions & 13 deletions lib/pbio/test/sys/test_status.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <tinytest.h>
#include <tinytest_macros.h>

#include <pbio/event.h>
#include <pbsys/status.h>
#include <test-pbio.h>

Expand Down Expand Up @@ -48,12 +47,6 @@ static PT_THREAD(test_status(struct pt *pt)) {
tt_want(!pbsys_status_test_debounce(test_flag, true, 10));
tt_want(!pbsys_status_test_debounce(test_flag, false, 10));

// ensure that event was broadcast
last_event = PROCESS_EVENT_NONE;
PT_YIELD(pt);
tt_want_uint_op(last_event, ==, PBIO_EVENT_STATUS_SET);
tt_want_uint_op(last_data, ==, test_flag);

// ensure that debounce works
pbio_test_clock_tick(9);
PT_YIELD(pt);
Expand All @@ -79,12 +72,6 @@ static PT_THREAD(test_status(struct pt *pt)) {
tt_want(!pbsys_status_test_debounce(test_flag, true, 10));
tt_want(!pbsys_status_test_debounce(test_flag, false, 10));

// ensure that event was broadcast
last_event = PROCESS_EVENT_NONE;
PT_YIELD(pt);
tt_want_uint_op(last_event, ==, PBIO_EVENT_STATUS_CLEARED);
tt_want_uint_op(last_data, ==, test_flag);

// ensure that debounce works
last_event = PROCESS_EVENT_NONE;
pbio_test_clock_tick(9);
Expand Down