Skip to content

Commit

Permalink
Simplify example of stdio_set_chars_available_callback (#495)
Browse files Browse the repository at this point in the history
The example in here uses an async callback to avoid a race when using
stdio_usb. But there's a fix available for this issue so simplify the
example.

To test pass -DPICO_STDIO_USB=1 on the cmake command line.

Fixes #461
  • Loading branch information
peterharperuk authored Jul 12, 2024
1 parent 69e63cc commit cbb5d61
Showing 1 changed file with 4 additions and 13 deletions.
17 changes: 4 additions & 13 deletions pico_w/wifi/iperf/picow_iperf.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,12 @@ static void iperf_report(void *arg, enum lwiperf_report_type report_type,
#endif
}

// This "worker" function is called to safely perform work when instructed by key_pressed_func
void key_pressed_worker_func(async_context_t *context, async_when_pending_worker_t *worker) {
printf("Disabling wifi\n");
cyw43_arch_disable_sta_mode();
}

static async_when_pending_worker_t key_pressed_worker = {
.do_work = key_pressed_worker_func
};

void key_pressed_func(void *param) {
int key = getchar_timeout_us(0); // get any pending key press but don't wait
if (key == 'd' || key == 'D') {
// We are probably in irq context so call wifi in a "worker"
async_context_set_work_pending((async_context_t*)param, &key_pressed_worker);
cyw43_arch_lwip_begin();
cyw43_arch_disable_sta_mode();
cyw43_arch_lwip_end();
}
}

Expand All @@ -63,7 +54,6 @@ int main() {
}

// Get notified if the user presses a key
async_context_add_when_pending_worker(cyw43_arch_async_context(), &key_pressed_worker);
stdio_set_chars_available_callback(key_pressed_func, cyw43_arch_async_context());

cyw43_arch_enable_sta_mode();
Expand Down Expand Up @@ -122,5 +112,6 @@ int main() {
}

cyw43_arch_deinit();
printf("Test complete\n");
return 0;
}

0 comments on commit cbb5d61

Please sign in to comment.