Skip to content

Commit 691f105

Browse files
committed
Add to standalone/sensor example to start/stop BT
Fixes #502
1 parent 2d44306 commit 691f105

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

pico_w/bt/standalone/server.c

+25-13
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,28 @@ static void heartbeat_handler(struct btstack_timer_source *ts) {
4040
btstack_run_loop_add_timer(ts);
4141
}
4242

43+
static volatile bool key_pressed;
44+
void key_pressed_func(void *param) {
45+
int key = getchar_timeout_us(0); // get any pending key press but don't wait
46+
if (key == 's' || key == 'S') {
47+
key_pressed = true;
48+
}
49+
}
50+
4351
int main() {
4452
stdio_init_all();
4553

54+
restart:
4655
// initialize CYW43 driver architecture (will enable BT if/because CYW43_ENABLE_BLUETOOTH == 1)
4756
if (cyw43_arch_init()) {
4857
printf("failed to initialise cyw43_arch\n");
4958
return -1;
5059
}
5160

61+
// Get notified if the user presses a key
62+
printf("Press the \"S\" key to Stop bluetooth\n");
63+
stdio_set_chars_available_callback(key_pressed_func, NULL);
64+
5265
// Initialise adc for the temp sensor
5366
adc_init();
5467
adc_select_input(ADC_CHANNEL_TEMPSENSOR);
@@ -74,20 +87,19 @@ int main() {
7487
// turn on bluetooth!
7588
hci_power_control(HCI_POWER_ON);
7689

77-
// btstack_run_loop_execute is only required when using the 'polling' method (e.g. using pico_cyw43_arch_poll library).
78-
// This example uses the 'threadsafe background` method, where BT work is handled in a low priority IRQ, so it
79-
// is fine to call bt_stack_run_loop_execute() but equally you can continue executing user code.
80-
81-
#if 0 // btstack_run_loop_execute() is not required, so lets not use it
82-
btstack_run_loop_execute();
83-
#else
84-
// this core is free to do it's own stuff except when using 'polling' method (in which case you should use
85-
// btstacK_run_loop_ methods to add work to the run loop.
86-
87-
// this is a forever loop in place of where user code would go.
88-
while(true) {
90+
key_pressed = false;
91+
while(!key_pressed) {
92+
async_context_poll(cyw43_arch_async_context());
93+
async_context_wait_for_work_until(cyw43_arch_async_context(), at_the_end_of_time);
94+
}
95+
96+
cyw43_arch_deinit();
97+
98+
printf("Press the \"S\" key to Start bluetooth\n");
99+
key_pressed = false;
100+
while(!key_pressed) {
89101
sleep_ms(1000);
90102
}
91-
#endif
103+
goto restart;
92104
return 0;
93105
}

0 commit comments

Comments
 (0)