Skip to content

Commit 1a8f0d0

Browse files
committed
pbio/sys/hmi: Run hmi only while user program inactive.
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
1 parent 2a45f4e commit 1a8f0d0

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

lib/pbio/sys/hmi.c

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,15 @@ static uint8_t selected_slot = 0;
4040
*/
4141
static PT_THREAD(update_program_run_button_wait_state(bool button_pressed)) {
4242
struct pt *pt = &update_program_run_button_wait_state_pt;
43-
// Creative use of protothread to reduce code size. This is the same
44-
// as checking if the user program is running after each PT_WAIT.
43+
44+
// This should not be active while a program is running.
4545
if (pbsys_status_test(PBIO_PYBRICKS_STATUS_USER_PROGRAM_RUNNING)) {
46-
goto start;
46+
PT_EXIT(pt);
4747
}
4848

4949
PT_BEGIN(pt);
5050

5151
for (;;) {
52-
start:
5352
// button may still be pressed from power on or user program stop
5453
PT_WAIT_UNTIL(pt, !button_pressed);
5554
PT_WAIT_UNTIL(pt, button_pressed);
@@ -73,16 +72,15 @@ static struct pt update_bluetooth_button_wait_state_pt;
7372
*/
7473
static PT_THREAD(update_bluetooth_button_wait_state(bool button_pressed)) {
7574
struct pt *pt = &update_bluetooth_button_wait_state_pt;
76-
// Creative use of protothread to reduce code size. This is the same
77-
// as checking if the user program is running after each PT_WAIT.
75+
76+
// This should not be active while a program is running.
7877
if (pbsys_status_test(PBIO_PYBRICKS_STATUS_USER_PROGRAM_RUNNING)) {
79-
goto start;
78+
PT_EXIT(pt);
8079
}
8180

8281
PT_BEGIN(pt);
8382

8483
for (;;) {
85-
start:
8684
// button may still be pressed during user program
8785
PT_WAIT_UNTIL(pt, !button_pressed);
8886
PT_WAIT_UNTIL(pt, button_pressed);
@@ -115,10 +113,10 @@ uint8_t pbsys_hmi_get_selected_program_slot(void) {
115113
*/
116114
static PT_THREAD(update_left_right_button_wait_state(bool left_button_pressed, bool right_button_pressed)) {
117115
struct pt *pt = &update_left_right_button_wait_state_pt;
118-
// Creative use of protothread to reduce code size. This is the same
119-
// as checking if the user program is running after each PT_WAIT.
116+
117+
// This should not be active while a program is running.
120118
if (pbsys_status_test(PBIO_PYBRICKS_STATUS_USER_PROGRAM_RUNNING)) {
121-
goto start;
119+
PT_EXIT(pt);
122120
}
123121

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

129127
for (;;) {
130-
start:
131128
// Buttons may still be pressed during user program
132129
PT_WAIT_UNTIL(pt, !left_button_pressed && !right_button_pressed);
133130

0 commit comments

Comments
 (0)