From 7915827d7a9c920e8196937e7c3c5838c9ea2747 Mon Sep 17 00:00:00 2001 From: Andrew Scheller Date: Thu, 4 Mar 2021 11:32:56 +0000 Subject: [PATCH] Fix examples to build cleanly if PICO_DEFAULT_LED_PIN isn't defined --- blink/blink.c | 4 ++++ picoboard/blinky/blinky.c | 20 ++++++++++++-------- picoboard/button/button.c | 2 +- pio/pwm/pwm.c | 5 +++++ system/hello_double_tap/hello_double_tap.c | 4 ++++ 5 files changed, 26 insertions(+), 9 deletions(-) diff --git a/blink/blink.c b/blink/blink.c index f413d50..d478d78 100644 --- a/blink/blink.c +++ b/blink/blink.c @@ -7,6 +7,9 @@ #include "pico/stdlib.h" int main() { +#ifndef PICO_DEFAULT_LED_PIN +#warning blink example requires a board with a regular LED +#else const uint LED_PIN = PICO_DEFAULT_LED_PIN; gpio_init(LED_PIN); gpio_set_dir(LED_PIN, GPIO_OUT); @@ -16,4 +19,5 @@ int main() { gpio_put(LED_PIN, 0); sleep_ms(250); } +#endif } diff --git a/picoboard/blinky/blinky.c b/picoboard/blinky/blinky.c index eb2c545..6d51aaf 100644 --- a/picoboard/blinky/blinky.c +++ b/picoboard/blinky/blinky.c @@ -8,7 +8,6 @@ #include "pico/stdlib.h" #include "hardware/gpio.h" -const uint LED_PIN = PICO_DEFAULT_LED_PIN; const uint DOT_PERIOD_MS = 100; const char *morse_letters[] = { @@ -40,25 +39,25 @@ const char *morse_letters[] = { "--.." // Z }; -void put_morse_letter(const char *pattern) { +void put_morse_letter(uint led_pin, const char *pattern) { for (; *pattern; ++pattern) { - gpio_put(LED_PIN, 1); + gpio_put(led_pin, 1); if (*pattern == '.') sleep_ms(DOT_PERIOD_MS); else sleep_ms(DOT_PERIOD_MS * 3); - gpio_put(LED_PIN, 0); + gpio_put(led_pin, 0); sleep_ms(DOT_PERIOD_MS * 1); } sleep_ms(DOT_PERIOD_MS * 2); } -void put_morse_str(const char *str) { +void put_morse_str(uint led_pin, const char *str) { for (; *str; ++str) { if (*str >= 'A' && *str < 'Z') { - put_morse_letter(morse_letters[*str - 'A']); + put_morse_letter(led_pin, morse_letters[*str - 'A']); } else if (*str >= 'a' && *str < 'z') { - put_morse_letter(morse_letters[*str - 'a']); + put_morse_letter(led_pin, morse_letters[*str - 'a']); } else if (*str == ' ') { sleep_ms(DOT_PERIOD_MS * 4); } @@ -66,10 +65,15 @@ void put_morse_str(const char *str) { } int main() { +#ifndef PICO_DEFAULT_LED_PIN +#warning picoboard/blinky example requires a board with a regular LED +#else + const uint LED_PIN = PICO_DEFAULT_LED_PIN; gpio_init(LED_PIN); gpio_set_dir(LED_PIN, GPIO_OUT); while (true) { - put_morse_str("Hello world"); + put_morse_str(LED_PIN, "Hello world"); sleep_ms(1000); } +#endif } diff --git a/picoboard/button/button.c b/picoboard/button/button.c index b113533..6a5a5dd 100644 --- a/picoboard/button/button.c +++ b/picoboard/button/button.c @@ -54,7 +54,7 @@ bool __no_inline_not_in_flash_func(get_bootsel_button)() { int main() { #ifndef PICO_DEFAULT_LED_PIN -#warning picobooard/button example requires a board with a regular LED +#warning picoboard/button example requires a board with a regular LED #else gpio_init(PICO_DEFAULT_LED_PIN); gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT); diff --git a/pio/pwm/pwm.c b/pio/pwm/pwm.c index f1cc46d..c3148a6 100644 --- a/pio/pwm/pwm.c +++ b/pio/pwm/pwm.c @@ -26,6 +26,10 @@ void pio_pwm_set_level(PIO pio, uint sm, uint32_t level) { int main() { stdio_init_all(); +#ifndef PICO_DEFAULT_LED_PIN +#warning pio/pwm example requires a board with a regular LED + puts("Default LED pin was not defined"); +#else // todo get free sm PIO pio = pio0; @@ -43,4 +47,5 @@ int main() { level = (level + 1) % 256; sleep_ms(10); } +#endif } diff --git a/system/hello_double_tap/hello_double_tap.c b/system/hello_double_tap/hello_double_tap.c index e64e8ae..bc06bf6 100644 --- a/system/hello_double_tap/hello_double_tap.c +++ b/system/hello_double_tap/hello_double_tap.c @@ -13,6 +13,9 @@ // `pico_bootsel_via_double_reset` library! int main() { +#ifndef PICO_DEFAULT_LED_PIN +#warning system/hello_double_tap example requires a board with a regular LED +#else const uint LED_PIN = PICO_DEFAULT_LED_PIN; gpio_init(LED_PIN); gpio_set_dir(LED_PIN, GPIO_OUT); @@ -22,4 +25,5 @@ int main() { gpio_put(LED_PIN, 0); sleep_ms(250); } +#endif }