@@ -40,15 +40,28 @@ static void heartbeat_handler(struct btstack_timer_source *ts) {
40
40
btstack_run_loop_add_timer (ts );
41
41
}
42
42
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
+
43
51
int main () {
44
52
stdio_init_all ();
45
53
54
+ restart :
46
55
// initialize CYW43 driver architecture (will enable BT if/because CYW43_ENABLE_BLUETOOTH == 1)
47
56
if (cyw43_arch_init ()) {
48
57
printf ("failed to initialise cyw43_arch\n" );
49
58
return -1 ;
50
59
}
51
60
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
+
52
65
// Initialise adc for the temp sensor
53
66
adc_init ();
54
67
adc_select_input (ADC_CHANNEL_TEMPSENSOR );
@@ -74,20 +87,19 @@ int main() {
74
87
// turn on bluetooth!
75
88
hci_power_control (HCI_POWER_ON );
76
89
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 ) {
89
101
sleep_ms (1000 );
90
102
}
91
- #endif
103
+ goto restart ;
92
104
return 0 ;
93
105
}
0 commit comments