-
Notifications
You must be signed in to change notification settings - Fork 5
/
esp32_digital_led_lib.h
145 lines (127 loc) · 4.22 KB
/
esp32_digital_led_lib.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
/*
* Library for driving digital RGB(W) LEDs using the ESP32's RMT peripheral
*
* Modifications Copyright (c) 2017-2019 Martin F. Falatic
*
* Based on public domain code created 19 Nov 2016 by Chris Osborn <[email protected]>
* http://insentricity.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.
*/
#ifndef ESP32_DIGITAL_LED_LIB_H
#define ESP32_DIGITAL_LED_LIB_H
#ifdef __cplusplus
extern "C"
{
#endif
#include <stdint.h>
#include <math.h>
#define DEBUG_ESP32_DIGITAL_LED_LIB 0
typedef union {
struct __attribute__((packed))
{
uint8_t r, g, b, w;
};
uint32_t num;
} pixelColor_t;
inline pixelColor_t pixelFromRGB(uint8_t r, uint8_t g, uint8_t b)
{
pixelColor_t v;
v.r = r;
v.g = g;
v.b = b;
// v.w = 0;
return v;
}
inline pixelColor_t pixelFromRGBW(uint8_t r, uint8_t g, uint8_t b, uint8_t w)
{
pixelColor_t v;
v.r = r;
v.g = g;
v.b = b;
v.w = w;
return v;
}
inline pixelColor_t pixelFromRGBL(uint8_t r, uint8_t g, uint8_t b,float l)
{
pixelColor_t v;
float light = (l/255.0);
v.r = ceil(r*light);
v.g = ceil(g*light);
v.b = ceil(b*light);
// v.r = r;
// v.g = g;
// v.b = b;
// v.w = 0;
return v;
}
typedef struct
{
int rmtChannel;
int gpioNum;
int ledType;
int brightLimit;
int numPixels;
pixelColor_t *pixels;
void *_stateVars;
} strand_t;
typedef struct
{
int bytesPerPixel;
uint32_t T0H;
uint32_t T1H;
uint32_t T0L;
uint32_t T1L;
uint32_t TRS;
} ledParams_t;
enum led_types
{
LED_WS2812_V1,
LED_WS2812B_V1,
LED_WS2812B_V2,
LED_WS2812B_V3,
LED_WS2813_V1,
LED_WS2813_V2,
LED_WS2813_V3,
LED_SK6812_V1,
LED_SK6812W_V1,
};
const ledParams_t ledParamsAll[] = {
// Still must match order of `led_types`
[LED_WS2812_V1] = {.bytesPerPixel = 3, .T0H = 350, .T1H = 700, .T0L = 800, .T1L = 600, .TRS = 50000}, // Various
[LED_WS2812B_V1] = {.bytesPerPixel = 3, .T0H = 350, .T1H = 900, .T0L = 900, .T1L = 350, .TRS = 50000}, // Older datasheet
[LED_WS2812B_V2] = {.bytesPerPixel = 3, .T0H = 400, .T1H = 850, .T0L = 850, .T1L = 400, .TRS = 50000}, // 2016 datasheet
[LED_WS2812B_V3] = {.bytesPerPixel = 3, .T0H = 450, .T1H = 850, .T0L = 850, .T1L = 450, .TRS = 50000}, // cplcpu test
[LED_WS2813_V1] = {.bytesPerPixel = 3, .T0H = 350, .T1H = 800, .T0L = 350, .T1L = 350, .TRS = 300000}, // Older datasheet
[LED_WS2813_V2] = {.bytesPerPixel = 3, .T0H = 270, .T1H = 800, .T0L = 800, .T1L = 270, .TRS = 300000}, // 2016 datasheet
[LED_WS2813_V3] = {.bytesPerPixel = 3, .T0H = 270, .T1H = 630, .T0L = 630, .T1L = 270, .TRS = 300000}, // 2017-05 WS datasheet
[LED_SK6812_V1] = {.bytesPerPixel = 3, .T0H = 300, .T1H = 600, .T0L = 900, .T1L = 600, .TRS = 80000}, // Various, all consistent
[LED_SK6812W_V1] = {.bytesPerPixel = 4, .T0H = 300, .T1H = 600, .T0L = 900, .T1L = 600, .TRS = 80000}, // Various, all consistent
};
extern int digitalLeds_initDriver();
extern int digitalLeds_addStrands(strand_t *strands[], int numStrands);
extern int digitalLeds_removeStrands(strand_t *strands[], int numStrands);
extern int digitalLeds_drawPixels(strand_t *strands[], int numStrands);
extern int digitalLeds_resetPixels(strand_t *strands[], int numStrands);
#ifdef __cplusplus
}
#endif
#endif /* ESP32_DIGITAL_LED_LIB_H */