Skip to content

Commit ce10842

Browse files
committed
Update blink example to use status_led lib
1 parent b019767 commit ce10842

File tree

3 files changed

+67
-40
lines changed

3 files changed

+67
-40
lines changed

blink/CMakeLists.txt

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,34 @@ add_executable(blink
22
blink.c
33
)
44

5-
# pull in common dependencies
6-
target_link_libraries(blink pico_stdlib)
5+
# You can define PICO_DEFAULT_LED_PIN yourself to add a led to a different GPIO
6+
#target_compile_definitions(blink PRIVATE
7+
# PICO_DEFAULT_LED_PIN=15
8+
#)
79

8-
if (PICO_CYW43_SUPPORTED)
9-
target_link_libraries(blink pico_cyw43_arch_none)
10-
endif()
10+
# pull in common dependencies
11+
target_link_libraries(blink pico_stdlib pico_status_led)
1112

1213
# create map/bin/hex file etc.
1314
pico_add_extra_outputs(blink)
1415

1516
# add url via pico_set_program_url
1617
example_auto_set_url(blink)
18+
19+
add_executable(color_blink
20+
color_blink.c
21+
)
22+
23+
# You can define PICO_DEFAULT_WS2812_PIN yourself to add a WS2812 led to a normal GPIO
24+
#target_compile_definitions(color_blink PRIVATE
25+
# PICO_DEFAULT_WS2812_PIN=16
26+
#)
27+
28+
# pull in common dependencies
29+
target_link_libraries(color_blink pico_stdlib pico_status_led)
30+
31+
# create map/bin/hex file etc.
32+
pico_add_extra_outputs(color_blink)
33+
34+
# add url via pico_set_program_url
35+
example_auto_set_url(color_blink)

blink/blink.c

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,49 +5,22 @@
55
*/
66

77
#include "pico/stdlib.h"
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
8+
#include "pico/status_led.h"
149

1510
#ifndef LED_DELAY_MS
1611
#define LED_DELAY_MS 250
1712
#endif
1813

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-
4414
int main() {
45-
int rc = pico_led_init();
46-
hard_assert(rc == PICO_OK);
15+
bool rc = status_led_init();
16+
hard_assert(rc);
4717
while (true) {
48-
pico_set_led(true);
18+
status_led_set_state(true);
4919
sleep_ms(LED_DELAY_MS);
50-
pico_set_led(false);
20+
assert(status_led_get_state());
21+
status_led_set_state(false);
5122
sleep_ms(LED_DELAY_MS);
23+
assert(!status_led_get_state());
5224
}
25+
status_led_deinit();
5326
}

blink/color_blink.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
3+
*
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
*/
6+
7+
#include "pico/stdlib.h"
8+
#include "pico/status_led.h"
9+
10+
#if !PICO_COLORED_STATUS_LED_AVAILABLE
11+
#warning The color_blink example requires a board with a WS2812 LED
12+
#endif
13+
14+
#ifndef LED_DELAY_MS
15+
#define LED_DELAY_MS 250
16+
#endif
17+
18+
int main() {
19+
bool rc = status_led_init();
20+
hard_assert(rc);
21+
hard_assert(colored_status_led_supported()); // This assert fails if your board does not have WS2812 support
22+
uint32_t count = 0;
23+
while (true) {
24+
// flash red then green then blue
25+
uint32_t color = PICO_COLORED_STATUS_LED_COLOR_FROM_RGB(count % 3 == 0 ? 0xaa : 0, count % 3 == 1 ? 0xaa : 0, count % 3 == 2 ? 0xaa : 0);
26+
colored_status_led_set_on_with_color(color);
27+
count++;
28+
sleep_ms(LED_DELAY_MS);
29+
assert(colored_status_led_get_state());
30+
colored_status_led_set_state(false);
31+
sleep_ms(LED_DELAY_MS);
32+
assert(!colored_status_led_get_state());
33+
}
34+
status_led_deinit();
35+
}

0 commit comments

Comments
 (0)