-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDOPLED.cpp
More file actions
235 lines (195 loc) · 5.54 KB
/
DOPLED.cpp
File metadata and controls
235 lines (195 loc) · 5.54 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/**
* @file DOPLED.cpp
* @brief Driver for Data Over Power addressable LEDs.
*
* See https://github.com/jratke587/DOPLED for more information on supported hardware and usage.
*
* @note This library only supports ESP32 microcontrollers.
*
* @author John Ratke
* @date 2025
* @license MIT
*/
#include <DOPLED.h>
bool dopled_tx_done_callback(
rmt_channel_handle_t channel,
const rmt_tx_done_event_data_t *edata,
void *user_ctx)
{
DOPLED *self = static_cast<DOPLED *>(user_ctx);
self->_txDone = true;
return false;
}
DOPLED::DOPLED(uint8_t pin, uint8_t timeBase_us)
{
_pin = pin;
timebase = timeBase_us;
if (timebase == 0)
{
timebase = DEFAULT_TIMING_BASE_US;
}
tx_config = {
.loop_count = 0,
};
tx_config.flags.eot_level = 1;
rmt_tx_channel_config_t tx_chan_config = {
.gpio_num = (gpio_num_t)_pin,
.clk_src = RMT_CLK_SRC_DEFAULT, // select source clock
.resolution_hz = RMT_RESOLUTION_HZ,
.mem_block_symbols = 128, // increase the block size can make the LED less flickering
.trans_queue_depth = 4, // set the number of transactions that can be pending in the background
};
ESP_ERROR_CHECK(rmt_new_tx_channel(&tx_chan_config, &led_chan));
ESP_ERROR_CHECK(rmt_new_dopled_encoder(&dopled_encoder));
rmt_tx_event_callbacks_t cbs = {
.on_trans_done = dopled_tx_done_callback,
};
ESP_ERROR_CHECK(rmt_tx_register_event_callbacks(led_chan, &cbs, this));
}
DOPLED::DOPLED(uint8_t pin) : DOPLED(pin, DEFAULT_TIMING_BASE_US) {}
esp_err_t DOPLED::rmt_new_dopled_encoder(rmt_encoder_handle_t *ret_encoder)
{
dopled_encoder_t *led_encoder = NULL;
led_encoder = (dopled_encoder_t *)rmt_alloc_encoder_mem(sizeof(dopled_encoder_t));
if (led_encoder == NULL)
{
return ESP_ERR_NO_MEM;
}
led_encoder->base.encode = rmt_encode_dopled;
led_encoder->base.del = rmt_del_dopled_encoder;
led_encoder->base.reset = rmt_dopled_encoder_reset;
led_encoder->state = RMT_ENCODING_RESET;
led_encoder->leading_symbol = {
.duration0 = (timebase * PRE_PKT_L) - 1,
.level0 = 0,
.duration1 = 1,
.level1 = 0,
};
led_encoder->ending_symbol = {
.duration0 = (timebase * INTER_PKT_H) - 1,
.level0 = 1,
.duration1 = 1,
.level1 = 1,
};
rmt_bytes_encoder_config_t bytes_encoder_config = {
.bit0 = {
.duration0 = timebase * T0H,
.level0 = 1,
.duration1 = timebase * T0L,
.level1 = 0,
},
.bit1 = {
.duration0 = timebase * T1H,
.level0 = 1,
.duration1 = timebase * T1L,
.level1 = 0,
},
};
bytes_encoder_config.flags.msb_first = true;
ESP_ERROR_CHECK(rmt_new_bytes_encoder(&bytes_encoder_config, &led_encoder->bytes_encoder));
rmt_copy_encoder_config_t copy_encoder_config = {};
ESP_ERROR_CHECK(rmt_new_copy_encoder(©_encoder_config, &led_encoder->copy_encoder));
*ret_encoder = &led_encoder->base;
return ESP_OK;
}
void DOPLED::begin()
{
if (_initialized)
return;
// Hold signal HIGH after data is sent to allow LEDs to light
tx_config.flags.eot_level = 1;
ESP_ERROR_CHECK(rmt_enable(led_chan));
_initialized = true;
}
void DOPLED::end()
{
if (!_initialized)
return;
// Hold signal LOW after data is sent to turn off LEDs
tx_config.flags.eot_level = 0;
// Transmit a dummy symbol to update the EOT level
rmt_symbol_word_t low_symbol = {
.duration0 = 1,
.level0 = 0,
.duration1 = 1,
.level1 = 0,
};
rmt_transmit(led_chan, dopled_encoder, &low_symbol, sizeof(low_symbol), &tx_config);
ESP_ERROR_CHECK(rmt_disable(led_chan));
_initialized = false;
}
void DOPLED::sendRaw(uint8_t *data, size_t size)
{
if (!_initialized)
return;
if (data == NULL || size == 0)
return;
// Wait for previous packets to finish sending
ESP_ERROR_CHECK(rmt_tx_wait_all_done(led_chan, portMAX_DELAY));
_txDone = false;
ESP_ERROR_CHECK(rmt_transmit(
led_chan,
dopled_encoder,
data,
size,
&tx_config));
}
void DOPLED::fillMatchingAddresses(uint8_t mask, uint8_t address, uint8_t r, uint8_t g, uint8_t b)
{
uint8_t pkt[6];
pkt[0] = _setCommand;
pkt[1] = mask;
pkt[2] = address;
pkt[3] = r;
pkt[4] = g;
pkt[5] = b;
sendRaw(pkt, sizeof(pkt));
};
void DOPLED::writePixel(uint8_t address, uint8_t r, uint8_t g, uint8_t b)
{
fillMatchingAddresses(0xFF, address, r, g, b);
};
void DOPLED::fillAll(uint8_t r, uint8_t g, uint8_t b)
{
uint8_t pkt[4];
pkt[0] = _fillCommand;
pkt[1] = r;
pkt[2] = g;
pkt[3] = b;
sendRaw(pkt, sizeof(pkt));
};
void DOPLED::fillRandomGroups(uint8_t mask, uint8_t address, uint8_t r, uint8_t g, uint8_t b)
{
uint8_t pkt[5];
pkt[0] = _randFillCommand;
pkt[1] = ((mask & 0x0F) << 4) | (address & 0x0F);
pkt[2] = r;
pkt[3] = g;
pkt[4] = b;
sendRaw(pkt, sizeof(pkt));
};
void DOPLED::setFlags(uint8_t flagByte)
{
_setCommand = (_setCommand & 0b11111000) | (flagByte & 0b00000111);
_fillCommand = (_fillCommand & 0b11111000) | (flagByte & 0b00000111);
_randFillCommand = (_randFillCommand & 0b11111000) | (flagByte & 0b00000111);
};
bool DOPLED::transmitDone()
{
return _txDone;
}
DOPLED::~DOPLED()
{
if (led_chan)
{
if (_initialized)
{
rmt_disable(led_chan);
}
rmt_del_channel(led_chan);
}
if (dopled_encoder)
{
rmt_del_encoder(dopled_encoder);
}
}