Skip to content

Commit

Permalink
pbsys/status: Include program id in status report.
Browse files Browse the repository at this point in the history
  • Loading branch information
laurensvalk committed Sep 2, 2024
1 parent 42d64cb commit b959321
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 24 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
backwards compatibility. For now, this is added to start various builtin
programs, but it prepares for the ability to start different downloaded
programs too ([pybricks-micropython#254]).
- Added program identifier to the hub status report to the host.

### Changed

Expand All @@ -42,8 +43,7 @@
### Removed
- Removed `loop_time` argument to `pybricks.tools.run_task` as this wasn't
having the desired effect, and would cause loop round trips to take `10 ms`
for every `await wait(1)` ([support#1460]). Passing the argument is still
allowed for backwards compatibility, but it won't do anything.
for every `await wait(1)` ([support#1460]). This was an undocumented feature.

[pybricks-micropython#250]: https://github.com/pybricks/pybricks-micropython/pull/250
[pybricks-micropython#253]: https://github.com/pybricks/pybricks-micropython/pull/253
Expand Down
9 changes: 4 additions & 5 deletions lib/pbio/include/pbio/protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,11 @@ typedef enum {
/**
* Status report event.
*
* The payload is a 32-bit little-endian unsigned integer containing
* ::pbio_pybricks_status_t flags.
* The payload is one 32-bit little-endian unsigned integer containing
* ::pbio_pybricks_status_t flags and a one byte program identifier
* representing the currently active program if it is running.
*
* @since Pybricks Profile v1.0.0
* @since Pybricks Profile v1.0.0. Program identifier added in Pybricks Profile v1.4.0.
*/
PBIO_PYBRICKS_EVENT_STATUS_REPORT = 0,

Expand Down Expand Up @@ -341,8 +342,6 @@ typedef enum {
*/
#define PBIO_PYBRICKS_STATUS_FLAG(status) (1 << status)

uint32_t pbio_pybricks_event_status_report(uint8_t *buf, uint32_t flags);

/**
* Application-specific feature flag supported by a hub.
*/
Expand Down
2 changes: 2 additions & 0 deletions lib/pbio/include/pbsys/status.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@

#include <pbio/protocol.h>

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);
bool pbsys_status_test(pbio_pybricks_status_t status);
bool pbsys_status_test_debounce(pbio_pybricks_status_t status, bool state, uint32_t ms);
uint32_t pbsys_status_get_flags(void);
uint32_t pbsys_status_write_status_report(uint8_t *buf);

#endif // _PBSYS_STATUS_H_

Expand Down
13 changes: 0 additions & 13 deletions lib/pbio/src/protocol/pybricks.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,6 @@
_Static_assert(NUM_PBIO_PYBRICKS_STATUS <= sizeof(uint32_t) * 8,
"oh no, we added too many status flags");

/**
* Writes Pybricks status report command to @p buf
*
* @param [in] buf The buffer to hold the binary data.
* @param [in] flags The status flags.
* @return The number of bytes written to @p buf.
*/
uint32_t pbio_pybricks_event_status_report(uint8_t *buf, uint32_t flags) {
buf[0] = PBIO_PYBRICKS_EVENT_STATUS_REPORT;
pbio_set_uint32_le(&buf[1], flags);
return 5;
}

/**
* Encodes the value of the Pybricks hub capabilities characteristic.
*
Expand Down
8 changes: 4 additions & 4 deletions lib/pbio/sys/bluetooth.c
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ static void reset_all(void) {

static PT_THREAD(pbsys_bluetooth_monitor_status(struct pt *pt)) {
static struct etimer timer;
static uint32_t old_status_flags, new_status_flags;
static uint32_t old_status_flags;
static send_msg_t msg;

PT_BEGIN(pt);
Expand All @@ -265,16 +265,16 @@ static PT_THREAD(pbsys_bluetooth_monitor_status(struct pt *pt)) {

for (;;) {
// wait for status to change or timeout
PT_WAIT_UNTIL(pt, (new_status_flags = pbsys_status_get_flags()) != old_status_flags || etimer_expired(&timer));
PT_WAIT_UNTIL(pt, pbsys_status_get_flags() != old_status_flags || etimer_expired(&timer));

etimer_restart(&timer);

// send the message
msg.context.size = pbio_pybricks_event_status_report(&msg.payload[0], new_status_flags);
msg.context.size = pbsys_status_write_status_report(&msg.payload[0]);
msg.context.connection = PBDRV_BLUETOOTH_CONNECTION_PYBRICKS;
list_add(send_queue, &msg);
msg.is_queued = true;
old_status_flags = new_status_flags;
old_status_flags = pbsys_status_get_flags();

// wait for message to be sent - note: it is possible to miss status changes
// if the status changes and then changes back to old_status_flags while we
Expand Down
1 change: 1 addition & 0 deletions lib/pbio/sys/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ int main(int argc, char **argv) {
}

// Prepare pbsys for running the program.
pbsys_status_set_program_id(program.id);
pbsys_status_set(PBIO_PYBRICKS_STATUS_USER_PROGRAM_RUNNING);
pbsys_bluetooth_rx_set_callback(pbsys_main_stdin_event);

Expand Down
26 changes: 26 additions & 0 deletions lib/pbio/sys/status.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ static struct {
uint32_t flags;
/** Timestamp of when status last changed */
uint32_t changed_time[NUM_PBIO_PYBRICKS_STATUS];
/** Currently active program identifier, if it is running according to the flags. */
pbio_pybricks_user_program_id_t program_id;
} pbsys_status;

static void pbsys_status_update_flag(pbio_pybricks_status_t status, bool set) {
Expand All @@ -35,6 +37,30 @@ static void pbsys_status_update_flag(pbio_pybricks_status_t status, bool set) {
(process_data_t)status);
}

/**
* Writes Pybricks status report command to @p buf
*
* @param [in] buf The buffer to hold the binary data.
* @return The number of bytes written to @p buf.
*/
uint32_t pbsys_status_write_status_report(uint8_t *buf) {
buf[0] = PBIO_PYBRICKS_EVENT_STATUS_REPORT;
pbio_set_uint32_le(&buf[1], pbsys_status.flags);
buf[5] = pbsys_status.program_id;
return 6;
}

/**
* Sets the identifier for the currently active program status information.
*
* Value only meaningful if ::PBIO_PYBRICKS_STATUS_USER_PROGRAM_RUNNING is set.
*
* @param [in] program_id The identifier to set.
*/
void pbsys_status_set_program_id(pbio_pybricks_user_program_id_t program_id) {
pbsys_status.program_id = program_id;
}

/**
* Sets a system status status indication.
* @param [in] status The status indication to set.
Expand Down

0 comments on commit b959321

Please sign in to comment.