forked from raspberrypi/pico-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blinky.c
79 lines (73 loc) · 1.85 KB
/
blinky.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/**
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h"
const uint DOT_PERIOD_MS = 100;
const char *morse_letters[] = {
".-", // A
"-...", // B
"-.-.", // C
"-..", // D
".", // E
"..-.", // F
"--.", // G
"....", // H
"..", // I
".---", // J
"-.-", // K
".-..", // L
"--", // M
"-.", // N
"---", // O
".--.", // P
"--.-", // Q
".-.", // R
"...", // S
"-", // T
"..-", // U
"...-", // V
".--", // W
"-..-", // X
"-.--", // Y
"--.." // Z
};
void put_morse_letter(uint led_pin, const char *pattern) {
for (; *pattern; ++pattern) {
gpio_put(led_pin, 1);
if (*pattern == '.')
sleep_ms(DOT_PERIOD_MS);
else
sleep_ms(DOT_PERIOD_MS * 3);
gpio_put(led_pin, 0);
sleep_ms(DOT_PERIOD_MS * 1);
}
sleep_ms(DOT_PERIOD_MS * 2);
}
void put_morse_str(uint led_pin, const char *str) {
for (; *str; ++str) {
if (*str >= 'A' && *str <= 'Z') {
put_morse_letter(led_pin, morse_letters[*str - 'A']);
} else if (*str >= 'a' && *str <= 'z') {
put_morse_letter(led_pin, morse_letters[*str - 'a']);
} else if (*str == ' ') {
sleep_ms(DOT_PERIOD_MS * 4);
}
}
}
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(LED_PIN, "Hello world");
sleep_ms(1000);
}
#endif
}