Skip to content

Commit 414a899

Browse files
committed
pbio/sys/hmi: Save bluetooth enabled setting persistently.
1 parent ea54ebc commit 414a899

File tree

6 files changed

+19
-25
lines changed

6 files changed

+19
-25
lines changed

lib/pbio/include/pbsys/storage_settings.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,20 @@ void pbsys_storage_settings_set_defaults(pbsys_storage_settings_t *settings);
4848

4949
void pbsys_storage_settings_apply_loaded_settings(pbsys_storage_settings_t *settings);
5050

51-
bool pbsys_storage_settings_bluetooth_enabled(void);
51+
bool pbsys_storage_settings_bluetooth_enabled_get(void);
5252

53-
void pbsys_storage_settings_bluetooth_enabled_request_toggle(void);
53+
void pbsys_storage_settings_bluetooth_enabled_set(bool enable);
5454

5555
#else
5656

5757
static inline void pbsys_storage_settings_set_defaults(pbsys_storage_settings_t *settings) {
5858
}
5959
static inline void pbsys_storage_settings_apply_loaded_settings(pbsys_storage_settings_t *settings) {
6060
}
61-
static inline bool pbsys_storage_settings_bluetooth_enabled(void) {
61+
static inline bool pbsys_storage_settings_bluetooth_enabled_get(void) {
6262
return true;
6363
}
64-
static inline void pbsys_storage_settings_bluetooth_enabled_request_toggle(void) {
64+
static inline void pbsys_storage_settings_bluetooth_enabled_set(bool enable) {
6565
}
6666

6767
#endif // PBSYS_CONFIG_STORAGE

lib/pbio/sys/hmi.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ static pbio_error_t pbsys_hmi_monitor_bluetooth_state(pbio_os_state_t *state) {
183183

184184
// Begin advertising.
185185
pbdrv_bluetooth_start_advertising();
186+
pbsys_storage_settings_bluetooth_enabled_set(true);
186187
pbsys_status_clear(PBIO_PYBRICKS_STATUS_BLE_HOST_CONNECTED);
187188
pbsys_status_set(PBIO_PYBRICKS_STATUS_BLE_ADVERTISING);
188189

@@ -208,6 +209,8 @@ static pbio_error_t pbsys_hmi_monitor_bluetooth_state(pbio_os_state_t *state) {
208209
}
209210

210211
// Otherwise, we got here because the Bluetooth button was toggled.
212+
pbsys_storage_settings_bluetooth_enabled_set(false);
213+
211214
// Bluetooth is now off so we only have to wait for another button
212215
// press or a program started with the buttons.
213216
PBIO_OS_AWAIT_WHILE(state, pbdrv_button_get_pressed());

lib/pbio/sys/storage_settings.c

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ void pbsys_storage_settings_apply_loaded_settings(pbsys_storage_settings_t *sett
4545
#endif // PBIO_CONFIG_IMU
4646
}
4747

48-
bool pbsys_storage_settings_bluetooth_enabled(void) {
48+
bool pbsys_storage_settings_bluetooth_enabled_get(void) {
4949
#if PBSYS_CONFIG_BLUETOOTH_TOGGLE
5050
pbsys_storage_settings_t *settings = pbsys_storage_settings_get_settings();
5151
if (!settings) {
@@ -56,30 +56,21 @@ bool pbsys_storage_settings_bluetooth_enabled(void) {
5656
return true;
5757
#endif // PBSYS_CONFIG_BLUETOOTH_TOGGLE
5858
}
59-
60-
void pbsys_storage_settings_bluetooth_enabled_request_toggle(void) {
59+
void pbsys_storage_settings_bluetooth_enabled_set(bool enable) {
6160
#if PBSYS_CONFIG_BLUETOOTH_TOGGLE
6261
pbsys_storage_settings_t *settings = pbsys_storage_settings_get_settings();
6362

64-
// Ignore toggle request in all but idle system status.
65-
if (!settings
66-
|| pbsys_status_test(PBIO_PYBRICKS_STATUS_USER_PROGRAM_RUNNING)
67-
|| pbsys_status_test(PBIO_PYBRICKS_STATUS_POWER_BUTTON_PRESSED)
68-
|| pbsys_status_test(PBIO_PYBRICKS_STATUS_SHUTDOWN)
69-
|| pbsys_status_test(PBIO_PYBRICKS_STATUS_SHUTDOWN_REQUEST)
70-
// Ignore toggle is Bluetooth is currently being used in a connection.
71-
|| pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_LE)
72-
|| pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_PERIPHERAL)
73-
// Ignore if last request not yet finished processing.
74-
|| pbsys_storage_settings_bluetooth_enabled() != pbdrv_bluetooth_is_ready()
75-
) {
63+
bool current_value = pbsys_storage_settings_bluetooth_enabled_get();
64+
if (enable == current_value) {
7665
return;
7766
}
7867

79-
// Toggle the user enabled state and poll process to take action.
80-
settings->flags ^= PBSYS_STORAGE_SETTINGS_FLAGS_BLUETOOTH_ENABLED;
68+
if (enable) {
69+
settings->flags |= PBSYS_STORAGE_SETTINGS_FLAGS_BLUETOOTH_ENABLED;
70+
} else {
71+
settings->flags &= ~PBSYS_STORAGE_SETTINGS_FLAGS_BLUETOOTH_ENABLED;
72+
}
8173
pbsys_storage_request_write();
82-
pbsys_bluetooth_process_poll();
8374
#endif
8475
}
8576

pybricks/common/pb_type_ble.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ mp_obj_t pb_type_BLE_new(mp_obj_t broadcast_channel_in, mp_obj_t observe_channel
561561

562562
// Raise if Bluetooth is attempted to be used while not enabled.
563563
#if PBSYS_CONFIG_BLUETOOTH_TOGGLE
564-
if (!pbsys_storage_settings_bluetooth_enabled() && (num_observe_channels > 0 || broadcast_channel_in != mp_const_none)) {
564+
if (!pbsys_storage_settings_bluetooth_enabled_get() && (num_observe_channels > 0 || broadcast_channel_in != mp_const_none)) {
565565
mp_raise_msg(&mp_type_RuntimeError, MP_ERROR_TEXT("Bluetooth not enabled"));
566566
}
567567
#endif // PBSYS_CONFIG_BLUETOOTH_TOGGLE

pybricks/iodevices/pb_type_iodevices_lwp3device.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ static mp_obj_t pb_type_pupdevices_Remote_make_new(const mp_obj_type_t *type, si
405405
PB_ARG_DEFAULT_INT(timeout, 10000));
406406

407407
#if PBSYS_CONFIG_BLUETOOTH_TOGGLE
408-
if (!pbsys_storage_settings_bluetooth_enabled()) {
408+
if (!pbsys_storage_settings_bluetooth_enabled_get()) {
409409
mp_raise_msg(&mp_type_RuntimeError, MP_ERROR_TEXT("Bluetooth not enabled"));
410410
}
411411
#endif // PBSYS_CONFIG_BLUETOOTH_TOGGLE

pybricks/iodevices/pb_type_iodevices_xbox_controller.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ static mp_obj_t pb_type_xbox_make_new(const mp_obj_type_t *type, size_t n_args,
288288
);
289289

290290
#if PBSYS_CONFIG_BLUETOOTH_TOGGLE
291-
if (!pbsys_storage_settings_bluetooth_enabled()) {
291+
if (!pbsys_storage_settings_bluetooth_enabled_get()) {
292292
mp_raise_msg(&mp_type_RuntimeError, MP_ERROR_TEXT("Bluetooth not enabled"));
293293
}
294294
#endif // PBSYS_CONFIG_BLUETOOTH_TOGGLE

0 commit comments

Comments
 (0)