-
Notifications
You must be signed in to change notification settings - Fork 0
/
led_strip.h
175 lines (156 loc) · 4.7 KB
/
led_strip.h
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
* The MIT License (MIT)
*
* Copyright (c) 2020 Ruslan V. Uss <unclerus@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/**
* @file led_strip.h
* @defgroup led_strip led_strip
* @{
*
* RMT-based ESP-IDF driver for WS2812B/SK6812/APA106 LED strips
*
* Copyright (c) 2020 Ruslan V. Uss <unclerus@gmail.com>
*
* MIT Licensed as described in the file LICENSE
*/
#ifndef __LED_STRIP_H__
#define __LED_STRIP_H__
#define CONFIG_LED_STRIP_FLUSH_TIMEOUT 0
#include <driver/gpio.h>
#include <esp_err.h>
#include <driver/rmt.h>
#include "color.h"
#ifdef __cplusplus
extern "C" {
#endif
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 4, 0)
#define LED_STRIP_BRIGHTNESS 1
#endif
/**
* LED type
*/
typedef enum
{
LED_STRIP_WS2812 = 0,
LED_STRIP_SK6812,
LED_STRIP_APA106
} led_strip_type_t;
/**
* LED strip descriptor
*/
typedef struct
{
led_strip_type_t type; ///< LED type
bool is_rgbw; ///< true for RGBW strips
#ifdef LED_STRIP_BRIGHTNESS
uint8_t brightness; ///< Brightness 0..255, call ::led_strip_flush() after change.
///< Supported only for ESP-IDF version >= 4.4
#endif
size_t length; ///< Number of LEDs in strip
gpio_num_t gpio; ///< Data GPIO pin
rmt_channel_t channel; ///< RMT channel
uint8_t *buf;
} led_strip_t;
/**
* @brief Setup library
*
* This method must be called before any other led_strip methods
*/
void led_strip_install();
/**
* @brief Initialize LED strip and allocate buffer memory
*
* @param strip Descriptor of LED strip
* @return `ESP_OK` on success
*/
esp_err_t led_strip_init(led_strip_t *strip);
/**
* @brief Deallocate buffer memory and release RMT channel
*
* @param strip Descriptor of LED strip
* @return `ESP_OK` on success
*/
esp_err_t led_strip_free(led_strip_t *strip);
/**
* @brief Send strip buffer to LEDs
*
* @param strip Descriptor of LED strip
* @return `ESP_OK` on success
*/
esp_err_t led_strip_flush(led_strip_t *strip);
/**
* @brief Check if associated RMT channel is busy
*
* @param strip Descriptor of LED strip
* @return true if RMT peripherals is busy
*/
bool led_strip_busy(led_strip_t *strip);
/**
* @brief Wait until RMT peripherals is free to send buffer to LEDs
*
* @param strip Descriptor of LED strip
* @param timeout Timeout in RTOS ticks
* @return `ESP_OK` on success
*/
esp_err_t led_strip_wait(led_strip_t *strip, TickType_t timeout);
/**
* @brief Set color of single LED in strip
*
* This function does not actually change colors of the LEDs.
* Call ::led_strip_flush() to send buffer to the LEDs.
*
* @param strip Descriptor of LED strip
* @param num LED number, 0..strip length - 1
* @param color RGB color
* @return `ESP_OK` on success
*/
esp_err_t led_strip_set_pixel(led_strip_t *strip, size_t num, rgb_t color);
/**
* @brief Set colors of multiple LEDs
*
* This function does not actually change colors of the LEDs.
* Call ::led_strip_flush() to send buffer to the LEDs.
*
* @param strip Descriptor of LED strip
* @param start First LED index, 0-based
* @param len Number of LEDs
* @param data Pointer to RGB data
* @return `ESP_OK` on success
*/
esp_err_t led_strip_set_pixels(led_strip_t *strip, size_t start, size_t len, rgb_t *data);
/**
* @brief Set multiple LEDs to the one color
*
* This function does not actually change colors of the LEDs.
* Call ::led_strip_flush() to send buffer to the LEDs.
*
* @param strip Descriptor of LED strip
* @param start First LED index, 0-based
* @param len Number of LEDs
* @param color RGB color
* @return `ESP_OK` on success
*/
esp_err_t led_strip_fill(led_strip_t *strip, size_t start, size_t len, rgb_t color);
#ifdef __cplusplus
}
#endif
/**@}*/
#endif /* __LED_STRIP_H__ */