Skip to content

Commit 1e29752

Browse files
committed
pbio/drv/button: Simplify reading.
Most platforms have no way to report errors for reading the button. And even if we did, there is no way to handle it. It is better to assume that nothing is pressed if the buttons are broken.
1 parent 49f716f commit 1e29752

File tree

14 files changed

+51
-90
lines changed

14 files changed

+51
-90
lines changed

lib/pbio/drv/button/button_gpio.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,12 @@ static pbio_button_flags_t pbdrv_button_gpio_read(void) {
5252
return flags;
5353
}
5454

55-
56-
pbio_error_t pbdrv_button_is_pressed(pbio_button_flags_t *pressed) {
55+
pbio_button_flags_t pbdrv_button_get_pressed(void) {
5756
#if PBDRV_CONFIG_BUTTON_GPIO_DEBOUNCE
58-
*pressed = pbdrv_button_state;
57+
return pbdrv_button_state;
5958
#else
60-
*pressed = pbdrv_button_gpio_read();
59+
return pbdrv_button_gpio_read();
6160
#endif
62-
63-
return PBIO_SUCCESS;
6461
}
6562

6663
#if PBDRV_CONFIG_BUTTON_GPIO_DEBOUNCE

lib/pbio/drv/button/button_nxt.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,26 @@
1414
void pbdrv_button_init(void) {
1515
}
1616

17-
pbio_error_t pbdrv_button_is_pressed(pbio_button_flags_t *pressed) {
17+
pbio_button_flags_t pbdrv_button_get_pressed(void) {
1818

1919
nx_avr_button_t button = nx_avr_get_button();
2020

21-
*pressed = 0;
21+
pbio_button_flags_t pressed = 0;
2222

2323
if (button == BUTTON_OK) {
24-
*pressed |= PBIO_BUTTON_CENTER;
24+
pressed |= PBIO_BUTTON_CENTER;
2525
}
2626
if (button == BUTTON_LEFT) {
27-
*pressed |= PBIO_BUTTON_LEFT;
27+
pressed |= PBIO_BUTTON_LEFT;
2828
}
2929
if (button == BUTTON_RIGHT) {
30-
*pressed |= PBIO_BUTTON_RIGHT;
30+
pressed |= PBIO_BUTTON_RIGHT;
3131
}
3232
if (button == BUTTON_CANCEL) {
33-
*pressed |= PBIO_BUTTON_DOWN;
33+
pressed |= PBIO_BUTTON_DOWN;
3434
}
3535

36-
return PBIO_SUCCESS;
36+
return pressed;
3737
}
3838

3939
#endif // PBDRV_CONFIG_BUTTON_NXT

lib/pbio/drv/button/button_resistor_ladder.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,43 +14,43 @@
1414
void pbdrv_button_init(void) {
1515
}
1616

17-
pbio_error_t pbdrv_button_is_pressed(pbio_button_flags_t *pressed) {
17+
pbio_button_flags_t pbdrv_button_get_pressed(void) {
1818
pbdrv_resistor_ladder_ch_flags_t flags;
1919
pbio_error_t err;
2020

21-
*pressed = 0;
21+
pbio_button_flags_t pressed = 0;
2222

2323
// REVISIT: For now, this is hard-coded for SPIKE Prime. If more platforms
2424
// that use this are added, a resistor ladder flags to buttons map will
2525
// need to be added to platform data.
2626

2727
err = pbdrv_resistor_ladder_get(0, &flags);
2828
if (err != PBIO_SUCCESS) {
29-
return err;
29+
return pressed;
3030
}
3131

3232
if (flags & PBDRV_RESISTOR_LADDER_CH_1) {
33-
*pressed |= PBIO_BUTTON_CENTER;
33+
pressed |= PBIO_BUTTON_CENTER;
3434
}
3535

3636
err = pbdrv_resistor_ladder_get(1, &flags);
3737
if (err != PBIO_SUCCESS) {
38-
return err;
38+
return pressed;
3939
}
4040

4141
if (flags & PBDRV_RESISTOR_LADDER_CH_0) {
42-
*pressed |= PBIO_BUTTON_LEFT;
42+
pressed |= PBIO_BUTTON_LEFT;
4343
}
4444

4545
if (flags & PBDRV_RESISTOR_LADDER_CH_1) {
46-
*pressed |= PBIO_BUTTON_RIGHT;
46+
pressed |= PBIO_BUTTON_RIGHT;
4747
}
4848

4949
if (flags & PBDRV_RESISTOR_LADDER_CH_2) {
50-
*pressed |= PBIO_BUTTON_RIGHT_UP; // Bluetooth
50+
pressed |= PBIO_BUTTON_RIGHT_UP; // Bluetooth
5151
}
5252

53-
return PBIO_SUCCESS;
53+
return pressed;
5454
}
5555

5656
#endif // PBDRV_CONFIG_BUTTON_RESISTOR_LADDER

lib/pbio/drv/button/button_test.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ void pbio_test_button_set_pressed(pbio_button_flags_t flags) {
1919
void pbdrv_button_init(void) {
2020
}
2121

22-
pbio_error_t pbdrv_button_is_pressed(pbio_button_flags_t *pressed) {
23-
*pressed = pbio_test_button_flags;
24-
return PBIO_SUCCESS;
22+
pbio_button_flags_t pbdrv_button_get_pressed(void) {
23+
return pbio_test_button_flags;
2524
}
2625

2726
#endif // PBDRV_CONFIG_BUTTON_TEST

lib/pbio/include/pbdrv/button.h

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,15 @@
2222

2323
/**
2424
* Get bitmask indicating currently pressed buttons.
25-
* @param [out] pressed Bitmask indicating which buttons are pressed
26-
* @return ::PBIO_SUCCESS if the call was successful,
27-
* ::PBIO_ERROR_NO_DEV if port is valid but a device with buttons is not connected
28-
* ::PBIO_ERROR_IO if there was an I/O error
25+
*
26+
* @return Bitmask indicating which buttons are pressed
2927
*/
30-
pbio_error_t pbdrv_button_is_pressed(pbio_button_flags_t *pressed);
28+
pbio_button_flags_t pbdrv_button_get_pressed(void);
3129

3230
#else
3331

34-
static inline pbio_error_t pbdrv_button_is_pressed(pbio_button_flags_t *pressed) {
35-
*pressed = 0;
36-
return PBIO_ERROR_NOT_SUPPORTED;
32+
static inline pbio_button_flags_t pbdrv_button_get_pressed(void) {
33+
return 0;
3734
}
3835

3936
#endif

lib/pbio/include/pbio/button.h

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -100,30 +100,9 @@ typedef enum _pbio_button_flags_t {
100100

101101
} pbio_button_flags_t;
102102

103-
// Generic button state getter
104-
typedef pbio_error_t (*pbio_button_is_pressed_func_t)(pbio_button_flags_t *pressed);
105-
106-
#ifdef DOXYGEN
107-
108-
/**
109-
* Get bitmask indicating currently pressed buttons.
110-
* @param [out] pressed Bitmask indicating which buttons are pressed
111-
* @return ::PBIO_SUCCESS if the call was successful,
112-
* ::PBIO_ERROR_NO_DEV if port is valid but a device with buttons is not connected
113-
* ::PBIO_ERROR_IO if there was an I/O error
114-
*/
115-
pbio_error_t pbio_button_is_pressed(pbio_button_flags_t *pressed);
116-
117-
#else // DOXYGEN
118-
119-
// calling pbdrv function directly for efficiency
120-
#define pbio_button_is_pressed pbdrv_button_is_pressed
121-
122-
// include for pbdrv_button_is_pressed needs to be called after pbio_button_flags_t is defined
103+
// include for pbdrv_button_get_pressed needs to be called after pbio_button_flags_t is defined
123104
#include <pbdrv/button.h>
124105

125-
#endif // DOXYGEN
126-
127106
#endif // _PBIO_BUTTON_H_
128107

129108
/** @} */

lib/pbio/platform/ev3/platform.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -735,9 +735,7 @@ void SystemInit(void) {
735735
* as a very convenient way to power off the EV3 for fast iteration.
736736
*/
737737
void lazy_poweroff_hook(void) {
738-
pbio_button_flags_t flags;
739-
pbio_error_t err = pbio_button_is_pressed(&flags);
740-
if (err == PBIO_SUCCESS && (flags & PBIO_BUTTON_LEFT_UP)) {
738+
if (pbdrv_button_get_pressed() & PBIO_BUTTON_LEFT_UP) {
741739
pbdrv_reset_power_off();
742740
return;
743741
}

lib/pbio/sys/hmi.c

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -183,30 +183,28 @@ void pbsys_hmi_handle_status_change(pbsys_status_change_t event, pbio_pybricks_s
183183
* This is called periodically to update the current HMI state.
184184
*/
185185
void pbsys_hmi_poll(void) {
186-
pbio_button_flags_t btn;
186+
pbio_button_flags_t btn = pbdrv_button_get_pressed();
187187

188-
if (pbio_button_is_pressed(&btn) == PBIO_SUCCESS) {
189-
if (btn & PBIO_BUTTON_CENTER) {
190-
pbsys_status_set(PBIO_PYBRICKS_STATUS_POWER_BUTTON_PRESSED);
191-
update_program_run_button_wait_state(true);
188+
if (btn & PBIO_BUTTON_CENTER) {
189+
pbsys_status_set(PBIO_PYBRICKS_STATUS_POWER_BUTTON_PRESSED);
190+
update_program_run_button_wait_state(true);
192191

193-
// power off when button is held down for 2 seconds
194-
if (pbsys_status_test_debounce(PBIO_PYBRICKS_STATUS_POWER_BUTTON_PRESSED, true, 2000)) {
195-
pbsys_status_set(PBIO_PYBRICKS_STATUS_SHUTDOWN_REQUEST);
196-
}
197-
} else {
198-
pbsys_status_clear(PBIO_PYBRICKS_STATUS_POWER_BUTTON_PRESSED);
199-
update_program_run_button_wait_state(false);
192+
// power off when button is held down for 2 seconds
193+
if (pbsys_status_test_debounce(PBIO_PYBRICKS_STATUS_POWER_BUTTON_PRESSED, true, 2000)) {
194+
pbsys_status_set(PBIO_PYBRICKS_STATUS_SHUTDOWN_REQUEST);
200195
}
196+
} else {
197+
pbsys_status_clear(PBIO_PYBRICKS_STATUS_POWER_BUTTON_PRESSED);
198+
update_program_run_button_wait_state(false);
199+
}
201200

202-
#if PBSYS_CONFIG_BLUETOOTH_TOGGLE
203-
update_bluetooth_button_wait_state(btn & PBSYS_CONFIG_BLUETOOTH_TOGGLE_BUTTON);
204-
#endif // PBSYS_CONFIG_BLUETOOTH_TOGGLE
201+
#if PBSYS_CONFIG_BLUETOOTH_TOGGLE
202+
update_bluetooth_button_wait_state(btn & PBSYS_CONFIG_BLUETOOTH_TOGGLE_BUTTON);
203+
#endif // PBSYS_CONFIG_BLUETOOTH_TOGGLE
205204

206-
#if PBSYS_CONFIG_HMI_NUM_SLOTS
207-
update_left_right_button_wait_state(btn & PBIO_BUTTON_LEFT, btn & PBIO_BUTTON_RIGHT);
208-
#endif // PBSYS_CONFIG_HMI_NUM_SLOTS
209-
}
205+
#if PBSYS_CONFIG_HMI_NUM_SLOTS
206+
update_left_right_button_wait_state(btn & PBIO_BUTTON_LEFT, btn & PBIO_BUTTON_RIGHT);
207+
#endif // PBSYS_CONFIG_HMI_NUM_SLOTS
210208

211209
pbsys_status_light_poll();
212210
}

lib/pbio/sys/program_stop.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ void pbsys_program_stop_poll(void) {
6363
return;
6464
}
6565

66-
pbio_button_flags_t btn;
67-
pbio_button_is_pressed(&btn);
66+
pbio_button_flags_t btn = pbdrv_button_get_pressed();
6867

6968
if (!stop_buttons) {
7069
return;

pybricks/hubs/pb_type_ev3brick.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ typedef struct _hubs_EV3Brick_obj_t {
2424
} hubs_EV3Brick_obj_t;
2525

2626
static mp_obj_t pb_type_ev3brick_button_pressed(void) {
27-
pbio_button_flags_t flags;
28-
pb_assert(pbio_button_is_pressed(&flags));
27+
pbio_button_flags_t flags = pbdrv_button_get_pressed();
2928
mp_obj_t pressed[5];
3029
size_t num = 0;
3130
if (flags & PBIO_BUTTON_LEFT) {

0 commit comments

Comments
 (0)