Skip to content

Commit a8a1696

Browse files
committed
Improve wifi scan example
Use async context to initiate a scan in future instead of keeping track of time in the main loop. Detect a key press to exit the example.
1 parent c44c298 commit a8a1696

File tree

1 file changed

+30
-19
lines changed

1 file changed

+30
-19
lines changed

pico_w/wifi/wifi_scan/picow_wifi_scan.c

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,19 @@ static int scan_result(void *env, const cyw43_ev_scan_result_t *result) {
1919
return 0;
2020
}
2121

22+
// Start a wifi scan
23+
static void scan_worker_fn(async_context_t *context, async_at_time_worker_t *worker) {
24+
cyw43_wifi_scan_options_t scan_options = {0};
25+
int err = cyw43_wifi_scan(&cyw43_state, &scan_options, NULL, scan_result);
26+
if (err == 0) {
27+
bool *scan_started = (bool*)worker->user_data;
28+
*scan_started = true;
29+
printf("\nPerforming wifi scan\n");
30+
} else {
31+
printf("Failed to start scan: %d\n", err);
32+
}
33+
}
34+
2235
#include "hardware/vreg.h"
2336
#include "hardware/clocks.h"
2437

@@ -30,26 +43,24 @@ int main() {
3043
return 1;
3144
}
3245

46+
printf("Press 'q' to quit\n");
3347
cyw43_arch_enable_sta_mode();
3448

35-
absolute_time_t scan_time = nil_time;
36-
bool scan_in_progress = false;
37-
while(true) {
38-
if (absolute_time_diff_us(get_absolute_time(), scan_time) < 0) {
39-
if (!scan_in_progress) {
40-
cyw43_wifi_scan_options_t scan_options = {0};
41-
int err = cyw43_wifi_scan(&cyw43_state, &scan_options, NULL, scan_result);
42-
if (err == 0) {
43-
printf("\nPerforming wifi scan\n");
44-
scan_in_progress = true;
45-
} else {
46-
printf("Failed to start scan: %d\n", err);
47-
scan_time = make_timeout_time_ms(10000); // wait 10s and scan again
48-
}
49-
} else if (!cyw43_wifi_scan_active(&cyw43_state)) {
50-
scan_time = make_timeout_time_ms(10000); // wait 10s and scan again
51-
scan_in_progress = false;
52-
}
49+
// Start a scan immediately
50+
bool scan_started = false;
51+
async_at_time_worker_t scan_worker = { .do_work = scan_worker_fn, .user_data = &scan_started };
52+
hard_assert(async_context_add_at_time_worker_in_ms(cyw43_arch_async_context(), &scan_worker, 0));
53+
54+
bool exit = false;
55+
while(!exit) {
56+
int key = getchar_timeout_us(0);
57+
if (key == 'q' || key == 'Q') {
58+
exit = true;
59+
}
60+
if (!cyw43_wifi_scan_active(&cyw43_state) && scan_started) {
61+
// Start a scan in 10s
62+
scan_started = false;
63+
hard_assert(async_context_add_at_time_worker_in_ms(cyw43_arch_async_context(), &scan_worker, 10000));
5364
}
5465
// the following #ifdef is only here so this same example can be used in multiple modes;
5566
// you do not need it in your code
@@ -59,7 +70,7 @@ int main() {
5970
cyw43_arch_poll();
6071
// you can poll as often as you like, however if you have nothing else to do you can
6172
// choose to sleep until either a specified time, or cyw43_arch_poll() has work to do:
62-
cyw43_arch_wait_for_work_until(scan_time);
73+
cyw43_arch_wait_for_work_until(at_the_end_of_time);
6374
#else
6475
// if you are not using pico_cyw43_arch_poll, then WiFI driver and lwIP work
6576
// is done via interrupt in the background. This sleep is just an example of some (blocking)

0 commit comments

Comments
 (0)