|
6 | 6 |
|
7 | 7 | #include "pico/stdlib.h"
|
8 | 8 |
|
| 9 | +// Pico W devices use a GPIO on the WIFI chip for the LED, |
| 10 | +// so when building for Pico W, CYW43_WL_GPIO_LED_PIN will be defined |
| 11 | +#ifdef CYW43_WL_GPIO_LED_PIN |
| 12 | +#include "pico/cyw43_arch.h" |
| 13 | +#endif |
| 14 | + |
| 15 | +#ifndef LED_DELAY_MS |
| 16 | +#define LED_DELAY_MS 250 |
| 17 | +#endif |
| 18 | + |
| 19 | +// Perform initialisation |
| 20 | +int pico_led_init(void) { |
| 21 | +#if defined(PICO_DEFAULT_LED_PIN) |
| 22 | + // A device like Pico that uses a GPIO for the LED will define PICO_DEFAULT_LED_PIN |
| 23 | + // so we can use normal GPIO functionality to turn the led on and off |
| 24 | + gpio_init(PICO_DEFAULT_LED_PIN); |
| 25 | + gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT); |
| 26 | + return PICO_OK; |
| 27 | +#elif defined(CYW43_WL_GPIO_LED_PIN) |
| 28 | + // For Pico W devices we need to initialise the driver etc |
| 29 | + return cyw43_arch_init(); |
| 30 | +#endif |
| 31 | +} |
| 32 | + |
| 33 | +// Turn the led on or off |
| 34 | +void pico_set_led(bool led_on) { |
| 35 | +#if defined(PICO_DEFAULT_LED_PIN) |
| 36 | + // Just set the GPIO on or off |
| 37 | + gpio_put(PICO_DEFAULT_LED_PIN, led_on); |
| 38 | +#elif defined(CYW43_WL_GPIO_LED_PIN) |
| 39 | + // Ask the wifi "driver" to set the GPIO on or off |
| 40 | + cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, led_on); |
| 41 | +#endif |
| 42 | +} |
| 43 | + |
9 | 44 | int main() {
|
10 |
| -#ifndef PICO_DEFAULT_LED_PIN |
11 |
| -#warning blink example requires a board with a regular LED |
12 |
| -#else |
13 |
| - const uint LED_PIN = PICO_DEFAULT_LED_PIN; |
14 |
| - gpio_init(LED_PIN); |
15 |
| - gpio_set_dir(LED_PIN, GPIO_OUT); |
| 45 | + int rc = pico_led_init(); |
| 46 | + hard_assert(rc == PICO_OK); |
16 | 47 | while (true) {
|
17 |
| - gpio_put(LED_PIN, 1); |
18 |
| - sleep_ms(250); |
19 |
| - gpio_put(LED_PIN, 0); |
20 |
| - sleep_ms(250); |
| 48 | + pico_set_led(true); |
| 49 | + sleep_ms(LED_DELAY_MS); |
| 50 | + pico_set_led(false); |
| 51 | + sleep_ms(LED_DELAY_MS); |
21 | 52 | }
|
22 |
| -#endif |
23 | 53 | }
|
0 commit comments