Skip to content

Commit 5a7a571

Browse files
committed
pbio/sys/hmi: Move bluetooth enable.
This completes the move of pulling the UI logic out of pbsys/bluetooth while ensuring that resetting Bluetooth happens at the same time as before.
1 parent 414a899 commit 5a7a571

File tree

6 files changed

+54
-42
lines changed

6 files changed

+54
-42
lines changed

lib/pbio/drv/bluetooth/bluetooth_btstack.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,15 @@ void pbdrv_bluetooth_init(void) {
603603
nordic_spp_service_server_init(nordic_spp_packet_handler);
604604
}
605605

606+
static bool pbdrv_bluetooth_powered_on;
607+
606608
void pbdrv_bluetooth_power_on(bool on) {
609+
610+
if (pbdrv_bluetooth_powered_on == on) {
611+
return;
612+
}
613+
pbdrv_bluetooth_powered_on = on;
614+
607615
hci_power_control(on ? HCI_POWER_ON : HCI_POWER_OFF);
608616

609617
// When powering off, cancel all pending tasks.
@@ -619,7 +627,7 @@ void pbdrv_bluetooth_power_on(bool on) {
619627
}
620628

621629
bool pbdrv_bluetooth_is_ready(void) {
622-
return hci_get_state() != HCI_STATE_OFF;
630+
return hci_get_state() != HCI_STATE_OFF || !pbdrv_bluetooth_powered_on;
623631
}
624632

625633
const char *pbdrv_bluetooth_get_hub_name(void) {

lib/pbio/drv/bluetooth/bluetooth_stm32_bluenrg.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,15 @@ void pbdrv_bluetooth_init(void) {
237237
spi_init();
238238
}
239239

240+
static bool pbdrv_bluetooth_powered_on;
241+
240242
void pbdrv_bluetooth_power_on(bool on) {
243+
244+
if (pbdrv_bluetooth_powered_on == on) {
245+
return;
246+
}
247+
pbdrv_bluetooth_powered_on = on;
248+
241249
if (on) {
242250
process_start(&pbdrv_bluetooth_spi_process);
243251
} else {

lib/pbio/drv/bluetooth/bluetooth_stm32_cc2640.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,15 @@ void pbdrv_bluetooth_init(void) {
262262

263263
// Public Bluetooth driver API implementation
264264

265+
static bool pbdrv_bluetooth_powered_on;
266+
265267
void pbdrv_bluetooth_power_on(bool on) {
268+
269+
if (pbdrv_bluetooth_powered_on == on) {
270+
return;
271+
}
272+
pbdrv_bluetooth_powered_on = on;
273+
266274
if (on) {
267275
process_start(&pbdrv_bluetooth_spi_process);
268276
} else {

lib/pbio/platform/prime_hub/pbdrvconfig.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@
107107
#define PBDRV_CONFIG_SOUND_STM32_HAL_DAC (1)
108108

109109
#define PBDRV_CONFIG_UART (1)
110-
#define PBDRV_CONFIG_UART_DEBUG_FIRST_PORT (0)
110+
#define PBDRV_CONFIG_UART_DEBUG_FIRST_PORT (1)
111111
#define PBDRV_CONFIG_UART_STM32F4_LL_IRQ (1)
112112
#define PBDRV_CONFIG_UART_STM32F4_LL_IRQ_NUM_UART (6)
113113

lib/pbio/sys/bluetooth.c

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,6 @@ static PT_THREAD(pbsys_bluetooth_monitor_status(struct pt *pt)) {
297297
}
298298

299299
PROCESS_THREAD(pbsys_bluetooth_process, ev, data) {
300-
static struct etimer timer;
301300
static struct pt status_monitor_pt;
302301

303302
PROCESS_BEGIN();
@@ -307,14 +306,6 @@ PROCESS_THREAD(pbsys_bluetooth_process, ev, data) {
307306

308307
while (!pbsys_status_test(PBIO_PYBRICKS_STATUS_SHUTDOWN_REQUEST)) {
309308

310-
// make sure the Bluetooth chip is in reset long enough to actually reset
311-
etimer_set(&timer, 150);
312-
PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_TIMER && etimer_expired(&timer));
313-
314-
// Enable Bluetooth.
315-
pbdrv_bluetooth_power_on(true);
316-
PROCESS_WAIT_UNTIL(pbdrv_bluetooth_is_ready());
317-
318309
// Now we are idle. We need to change the Bluetooth state and
319310
// indicators if a host connects to us, or a user program starts, or we
320311
// shut down, or Bluetooth is disabled by the user.
@@ -363,11 +354,6 @@ PROCESS_THREAD(pbsys_bluetooth_process, ev, data) {
363354
}
364355

365356
reset_all();
366-
PROCESS_WAIT_WHILE(pbsys_status_test(PBIO_PYBRICKS_STATUS_USER_PROGRAM_RUNNING));
367-
368-
// reset Bluetooth chip
369-
pbdrv_bluetooth_power_on(false);
370-
PROCESS_WAIT_WHILE(pbdrv_bluetooth_is_ready());
371357
}
372358

373359
PROCESS_END();

lib/pbio/sys/hmi.c

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -155,17 +155,10 @@ static pbio_error_t pbsys_hmi_monitor_bluetooth_state(pbio_os_state_t *state) {
155155

156156
PBIO_OS_ASYNC_BEGIN(state);
157157

158-
// FIXME: We can drop this once Bluetooth poweron-init is moved to pbdrv.
159-
// It waits for Bluetooth to be ready before staring pbsys.
160-
static pbio_os_timer_t timer;
161-
if (pbdrv_clock_get_ms() < 1000) {
162-
PBIO_OS_AWAIT_MS(state, &timer, 2000);
163-
}
164-
165158
for (;;) {
166159

167-
// No need to monitor buttons if connected, so just wait for program
168-
// start or disconnect and then re-assess.
160+
// No need to monitor Bluetooth button if connected, so just wait for
161+
// program start or disconnect.
169162
if (pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_LE)) {
170163
pbsys_status_set(PBIO_PYBRICKS_STATUS_BLE_HOST_CONNECTED);
171164
PBIO_OS_AWAIT_UNTIL(state,
@@ -175,16 +168,37 @@ static pbio_error_t pbsys_hmi_monitor_bluetooth_state(pbio_os_state_t *state) {
175168
if (pbsys_main_program_start_is_requested()) {
176169
// Done, ready to run the program.
177170
break;
178-
} else {
179-
// Disconnected, so start over.
180-
continue;
181171
}
182172
}
183173

184-
// Begin advertising.
174+
// Start with Bluetooth off.
175+
pbdrv_bluetooth_power_on(false);
176+
PBIO_OS_AWAIT_UNTIL(state, pbdrv_bluetooth_is_ready());
177+
// Hack: this is a remnant of pbsys/bluetooth. It needs to be included
178+
// in the pbdrv_bluetooth_power_on(false) once it is made awaitable.
179+
static pbio_os_timer_t timer;
180+
PBIO_OS_AWAIT_MS(state, &timer, 150);
181+
182+
// Since bluetooth is off, we just have to wait for a program start
183+
// with the buttons or until Bluetooth is enabled with the button or as
184+
// loaded from settings.
185+
pbsys_status_clear(PBIO_PYBRICKS_STATUS_BLE_HOST_CONNECTED);
186+
PBIO_OS_AWAIT_WHILE(state, pbdrv_button_get_pressed());
187+
PBIO_OS_AWAIT_UNTIL(state,
188+
pbsys_storage_settings_bluetooth_enabled_get() ||
189+
pbsys_hmi_bluetooth_button_is_pressed() ||
190+
pbsys_main_program_start_is_requested()
191+
);
192+
if (pbsys_main_program_start_is_requested()) {
193+
// Done, ready to run the program.
194+
break;
195+
}
196+
197+
// Enable bluetooth and begin advertising.
198+
pbdrv_bluetooth_power_on(true);
199+
PBIO_OS_AWAIT_UNTIL(state, pbdrv_bluetooth_is_ready());
185200
pbdrv_bluetooth_start_advertising();
186201
pbsys_storage_settings_bluetooth_enabled_set(true);
187-
pbsys_status_clear(PBIO_PYBRICKS_STATUS_BLE_HOST_CONNECTED);
188202
pbsys_status_set(PBIO_PYBRICKS_STATUS_BLE_ADVERTISING);
189203

190204
// Wait for connection, program run, or bluetooth toggle.
@@ -210,18 +224,6 @@ static pbio_error_t pbsys_hmi_monitor_bluetooth_state(pbio_os_state_t *state) {
210224

211225
// Otherwise, we got here because the Bluetooth button was toggled.
212226
pbsys_storage_settings_bluetooth_enabled_set(false);
213-
214-
// Bluetooth is now off so we only have to wait for another button
215-
// press or a program started with the buttons.
216-
PBIO_OS_AWAIT_WHILE(state, pbdrv_button_get_pressed());
217-
PBIO_OS_AWAIT_UNTIL(state,
218-
pbsys_hmi_bluetooth_button_is_pressed() ||
219-
pbsys_main_program_start_is_requested()
220-
);
221-
if (pbsys_main_program_start_is_requested()) {
222-
// Done, ready to run the program.
223-
break;
224-
}
225227
}
226228

227229
// Wait for all buttons to be released before starting under all conditions.

0 commit comments

Comments
 (0)