|
| 1 | +/** |
| 2 | + * Copyright (c) 2026 mjcross |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: BSD-3-Clause |
| 5 | + */ |
| 6 | + |
| 7 | +#include "pico/stdlib.h" |
| 8 | +#include "pico/async_context_threadsafe_background.h" |
| 9 | +#include "pico/status_led.h" |
| 10 | + |
| 11 | +#ifndef LED_DELAY_MS |
| 12 | +#define LED_DELAY_MS 500 |
| 13 | +#endif |
| 14 | + |
| 15 | +// define a user data type to be passed to our worker's callback function |
| 16 | +typedef struct { |
| 17 | + bool led_state; |
| 18 | +} my_data_t; |
| 19 | + |
| 20 | + |
| 21 | +// define a callback function for our worker (MUST be safe to call from an IRQ) |
| 22 | +void worker_cb(async_context_t *p_ctx, async_at_time_worker_t *p_worker) { |
| 23 | + // unpack our user data instance from `(void *)worker.user_data` |
| 24 | + my_data_t *p_my_data = p_worker->user_data; |
| 25 | + |
| 26 | + // update the status LED from our user data and toggle it |
| 27 | + status_led_set_state(p_my_data->led_state); |
| 28 | + p_my_data->led_state = !p_my_data->led_state; |
| 29 | + |
| 30 | + // re-schedule the worker, so as to flash the LED continuously (an at_time worker is |
| 31 | + // automatically removed from the context just before it runs) |
| 32 | + async_context_add_at_time_worker_in_ms(p_ctx, p_worker, LED_DELAY_MS); |
| 33 | +} |
| 34 | + |
| 35 | +// create an instance of our user data type (MUST still exist when the worker runs) |
| 36 | +static my_data_t my_data = { |
| 37 | + .led_state = true |
| 38 | +}; |
| 39 | + |
| 40 | +// create an asynchronous at-time worker that points to our callback function and user |
| 41 | +// data (MUST still exist when the worker runs) |
| 42 | +static async_at_time_worker_t worker = { |
| 43 | + .do_work = &worker_cb, |
| 44 | + .user_data = &my_data |
| 45 | +}; |
| 46 | + |
| 47 | + |
| 48 | +int main() { |
| 49 | + // initialise the status LED |
| 50 | + hard_assert( |
| 51 | + status_led_init() == true |
| 52 | + ); |
| 53 | + |
| 54 | + // create and initialise an asynchronous background context with default settings |
| 55 | + // note: in a networking application we'd probably reuse the existing cyw43_arch |
| 56 | + // context directly with `async_context_t ctx = cyw43_arch_async_context()` |
| 57 | + async_context_threadsafe_background_t background; |
| 58 | + hard_assert( |
| 59 | + async_context_threadsafe_background_init_with_defaults(&background) == true |
| 60 | + ); |
| 61 | + |
| 62 | + // add our at-time worker to the background context, to run after LED_DELAY_MS |
| 63 | + // note: `background.core` is the async_context_t of our threadsafe background. If |
| 64 | + // we were using the cyw43 context instead (see above) we'd just put `&ctx` here. |
| 65 | + hard_assert( |
| 66 | + async_context_add_at_time_worker_in_ms(&background.core, &worker, LED_DELAY_MS) == true |
| 67 | + ); |
| 68 | + |
| 69 | + // the LED will flash in the background |
| 70 | + while(true) { |
| 71 | + sleep_ms(5000); |
| 72 | + } |
| 73 | +} |
0 commit comments