Fix examples to build cleanly if PICO_DEFAULT_LED_PIN isn't defined

This commit is contained in:
Andrew Scheller
2021-03-04 11:32:56 +00:00
committed by Graham Sanderson
parent 482d9657d3
commit 4d7882bc59
5 changed files with 26 additions and 9 deletions

View File

@@ -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
}