Skip to content

Commit

Permalink
pbio/sys/hmi: Run hmi only while user program inactive.
Browse files Browse the repository at this point in the history
The previous approach would go to the start but still run until the next yield. This meant that if the stop button was pressed while a program gracefully exists, it would already be in the "second stage" of the following

        PT_WAIT_UNTIL(pt, !button_pressed);
        PT_WAIT_UNTIL(pt, button_pressed);
        PT_WAIT_UNTIL(pt, !button_pressed);
        pbsys_main_program_request_start(selected_slot);

Then on release, the program would immediately restart.

PT_EXIT has the same effect of resetting the state to the start, but existing immediately after that.

Fixes pybricks/support#1863
  • Loading branch information
laurensvalk committed Oct 2, 2024
1 parent 2a45f4e commit a047055
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@
argument as already active ([support#1858]).
- Fixed lockup and reboot with f-strings and slice allocations in tight
loops ([support#1668]).
- Fixed program restarting if the stop button was held to end the program
without an exception ([support#1863]).

[support#1623]: https://github.com/pybricks/support/issues/1623
[support#1661]: https://github.com/pybricks/support/issues/1661
[support#1668]: https://github.com/pybricks/support/issues/1668
[support#1846]: https://github.com/pybricks/support/issues/1846
[support#1858]: https://github.com/pybricks/support/issues/1858
[support#1863]: https://github.com/pybricks/support/issues/1863

## [3.6.0b1] - 2024-09-24

Expand Down
21 changes: 9 additions & 12 deletions lib/pbio/sys/hmi.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,15 @@ static uint8_t selected_slot = 0;
*/
static PT_THREAD(update_program_run_button_wait_state(bool button_pressed)) {
struct pt *pt = &update_program_run_button_wait_state_pt;
// Creative use of protothread to reduce code size. This is the same
// as checking if the user program is running after each PT_WAIT.

// This should not be active while a program is running.
if (pbsys_status_test(PBIO_PYBRICKS_STATUS_USER_PROGRAM_RUNNING)) {
goto start;
PT_EXIT(pt);
}

PT_BEGIN(pt);

for (;;) {
start:
// button may still be pressed from power on or user program stop
PT_WAIT_UNTIL(pt, !button_pressed);
PT_WAIT_UNTIL(pt, button_pressed);
Expand All @@ -73,16 +72,15 @@ static struct pt update_bluetooth_button_wait_state_pt;
*/
static PT_THREAD(update_bluetooth_button_wait_state(bool button_pressed)) {
struct pt *pt = &update_bluetooth_button_wait_state_pt;
// Creative use of protothread to reduce code size. This is the same
// as checking if the user program is running after each PT_WAIT.

// This should not be active while a program is running.
if (pbsys_status_test(PBIO_PYBRICKS_STATUS_USER_PROGRAM_RUNNING)) {
goto start;
PT_EXIT(pt);
}

PT_BEGIN(pt);

for (;;) {
start:
// button may still be pressed during user program
PT_WAIT_UNTIL(pt, !button_pressed);
PT_WAIT_UNTIL(pt, button_pressed);
Expand Down Expand Up @@ -115,10 +113,10 @@ uint8_t pbsys_hmi_get_selected_program_slot(void) {
*/
static PT_THREAD(update_left_right_button_wait_state(bool left_button_pressed, bool right_button_pressed)) {
struct pt *pt = &update_left_right_button_wait_state_pt;
// Creative use of protothread to reduce code size. This is the same
// as checking if the user program is running after each PT_WAIT.

// This should not be active while a program is running.
if (pbsys_status_test(PBIO_PYBRICKS_STATUS_USER_PROGRAM_RUNNING)) {
goto start;
PT_EXIT(pt);
}

static uint8_t previous_slot;
Expand All @@ -127,7 +125,6 @@ static PT_THREAD(update_left_right_button_wait_state(bool left_button_pressed, b
PT_BEGIN(pt);

for (;;) {
start:
// Buttons may still be pressed during user program
PT_WAIT_UNTIL(pt, !left_button_pressed && !right_button_pressed);

Expand Down

0 comments on commit a047055

Please sign in to comment.