1 /* Blink Example
2
3 This example code is in the Public Domain (or CC0 licensed, at your option.)
4
5 Unless required by applicable law or agreed to in writing, this
6 software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
7 CONDITIONS OF ANY KIND, either express or implied.
8 */
9 #include <stdio.h>
10 #include "freertos/FreeRTOS.h"
11 #include "freertos/task.h"
12 #include "driver/gpio.h"
13 #include "esp_log.h"
14 #include "led_strip.h"
15 #include "sdkconfig.h"
16
17 static const char *TAG = "example";
18
19 /* Use project configuration menu (idf.py menuconfig) to choose the GPIO to blink,
20 or you can edit the following line and set a number here.
21 */
22 #define BLINK_GPIO CONFIG_BLINK_GPIO
23
24 static uint8_t s_led_state = 0;
25
26 #ifdef CONFIG_BLINK_LED_RMT
27 static led_strip_t *pStrip_a;
28
blink_led(void)29 static void blink_led(void)
30 {
31 /* If the addressable LED is enabled */
32 if (s_led_state) {
33 /* Set the LED pixel using RGB from 0 (0%) to 255 (100%) for each color */
34 pStrip_a->set_pixel(pStrip_a, 0, 16, 16, 16);
35 /* Refresh the strip to send data */
36 pStrip_a->refresh(pStrip_a, 100);
37 } else {
38 /* Set all LED off to clear all pixels */
39 pStrip_a->clear(pStrip_a, 50);
40 }
41 }
42
configure_led(void)43 static void configure_led(void)
44 {
45 ESP_LOGI(TAG, "Example configured to blink addressable LED!");
46 /* LED strip initialization with the GPIO and pixels number*/
47 pStrip_a = led_strip_init(CONFIG_BLINK_LED_RMT_CHANNEL, BLINK_GPIO, 1);
48 /* Set all LED off to clear all pixels */
49 pStrip_a->clear(pStrip_a, 50);
50 }
51
52 #elif CONFIG_BLINK_LED_GPIO
53
blink_led(void)54 static void blink_led(void)
55 {
56 /* Set the GPIO level according to the state (LOW or HIGH)*/
57 gpio_set_level(BLINK_GPIO, s_led_state);
58 }
59
configure_led(void)60 static void configure_led(void)
61 {
62 ESP_LOGI(TAG, "Example configured to blink GPIO LED!");
63 gpio_reset_pin(BLINK_GPIO);
64 /* Set the GPIO as a push/pull output */
65 gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);
66 }
67
68 #endif
69
app_main(void)70 void app_main(void)
71 {
72
73 /* Configure the peripheral according to the LED type */
74 configure_led();
75
76 while (1) {
77 ESP_LOGI(TAG, "Turning the LED %s!", s_led_state == true ? "ON" : "OFF");
78 blink_led();
79 /* Toggle the LED state */
80 s_led_state = !s_led_state;
81 vTaskDelay(CONFIG_BLINK_PERIOD / portTICK_PERIOD_MS);
82 }
83 }
84