2121#include <pbsys/bluetooth.h>
2222#include <pbsys/command.h>
2323#include <pbsys/status.h>
24+ #include <pbsys/storage.h>
2425
2526#include "light.h"
2627
@@ -202,10 +203,13 @@ bool pbsys_bluetooth_tx_is_idle(void) {
202203 return !send_busy && lwrb_get_full (& stdout_ring_buf ) == 0 ;
203204}
204205
205- static bool pbsys_bluetooth_user_enabled = true ;
206+ static pbsys_storage_settings_t * settings ;
206207
207208bool pbsys_bluetooth_is_user_enabled (void ) {
208- return pbsys_bluetooth_user_enabled ;
209+ if (!settings ) {
210+ return false;
211+ }
212+ return settings -> bluetooth_ble_user_enabled ;
209213}
210214
211215void pbsys_bluetooth_is_user_enabled_request_toggle (void ) {
@@ -219,13 +223,14 @@ void pbsys_bluetooth_is_user_enabled_request_toggle(void) {
219223 || pbdrv_bluetooth_is_connected (PBDRV_BLUETOOTH_CONNECTION_LE )
220224 || pbdrv_bluetooth_is_connected (PBDRV_BLUETOOTH_CONNECTION_PERIPHERAL )
221225 // Ignore if last request not yet finished processing.
222- || pbsys_bluetooth_user_enabled != pbdrv_bluetooth_is_ready ()
226+ || settings -> bluetooth_ble_user_enabled != pbdrv_bluetooth_is_ready ()
223227 ) {
224228 return ;
225229 }
226230
227231 // Toggle the user enabled state and poll process to take action.
228- pbsys_bluetooth_user_enabled = !pbsys_bluetooth_user_enabled ;
232+ settings -> bluetooth_ble_user_enabled = !settings -> bluetooth_ble_user_enabled ;
233+ pbsys_storage_request_settings_write ();
229234 pbsys_bluetooth_process_poll ();
230235}
231236
@@ -316,6 +321,8 @@ PROCESS_THREAD(pbsys_bluetooth_process, ev, data) {
316321
317322 PROCESS_BEGIN ();
318323
324+ PROCESS_WAIT_EVENT_UNTIL (pbsys_storage_get_settings (& settings ) == PBIO_SUCCESS );
325+
319326 pbdrv_bluetooth_set_on_event (pbsys_bluetooth_process_poll );
320327 pbdrv_bluetooth_set_receive_handler (handle_receive );
321328
@@ -325,7 +332,7 @@ PROCESS_THREAD(pbsys_bluetooth_process, ev, data) {
325332 PROCESS_WAIT_EVENT_UNTIL (ev == PROCESS_EVENT_TIMER && etimer_expired (& timer ));
326333
327334 // Wait until Bluetooth enabled requested by user, but stop waiting on shutdown.
328- PROCESS_WAIT_UNTIL (pbsys_bluetooth_user_enabled || pbsys_status_test (PBIO_PYBRICKS_STATUS_SHUTDOWN ));
335+ PROCESS_WAIT_UNTIL (settings -> bluetooth_ble_user_enabled || pbsys_status_test (PBIO_PYBRICKS_STATUS_SHUTDOWN ));
329336 if (pbsys_status_test (PBIO_PYBRICKS_STATUS_SHUTDOWN )) {
330337 break ;
331338 }
@@ -346,7 +353,7 @@ PROCESS_THREAD(pbsys_bluetooth_process, ev, data) {
346353 pbdrv_bluetooth_is_connected (PBDRV_BLUETOOTH_CONNECTION_LE )
347354 || pbsys_status_test (PBIO_PYBRICKS_STATUS_USER_PROGRAM_RUNNING )
348355 || pbsys_status_test (PBIO_PYBRICKS_STATUS_SHUTDOWN )
349- || !pbsys_bluetooth_user_enabled );
356+ || !settings -> bluetooth_ble_user_enabled );
350357
351358 // Now change the state depending on which of the above was triggered.
352359
@@ -365,7 +372,7 @@ PROCESS_THREAD(pbsys_bluetooth_process, ev, data) {
365372 // The Bluetooth enabled flag can only change while disconnected and
366373 // while no program is running. So here it just serves to skip the
367374 // Bluetooth loop below and go directly to the disable step below it.
368- while (pbsys_bluetooth_user_enabled
375+ while (settings -> bluetooth_ble_user_enabled
369376 && pbdrv_bluetooth_is_connected (PBDRV_BLUETOOTH_CONNECTION_LE )
370377 && !pbsys_status_test (PBIO_PYBRICKS_STATUS_SHUTDOWN )) {
371378
@@ -406,7 +413,7 @@ PROCESS_THREAD(pbsys_bluetooth_process, ev, data) {
406413
407414 // Indicate status only if user requested Bluetooth to be disabled to
408415 // avoid always flashing red in between program runs when disconnected.
409- if (!pbsys_bluetooth_user_enabled ) {
416+ if (!settings -> bluetooth_ble_user_enabled ) {
410417 pbsys_status_light_bluetooth_set_color (PBIO_COLOR_RED );
411418 }
412419
0 commit comments