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 1a8f0d0
Showing 1 changed file with 9 additions and 12 deletions.
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 1a8f0d0

Please sign in to comment.