-
Notifications
You must be signed in to change notification settings - Fork 0
/
dotstar_strip.c
395 lines (359 loc) · 12.5 KB
/
dotstar_strip.c
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*****************************************************************************/
/* Legal */
/*****************************************************************************/
/*
** Copyright �2015. Lantronix, Inc. All Rights Reserved.
** By using this software, you are agreeing to the terms of the Software
** Development Kit (SDK) License Agreement included in the distribution package
** for this software (the �License Agreement�).
** Under the License Agreement, this software may be used solely to create
** custom applications for use on the Lantronix xPico Wi-Fi product.
** THIS SOFTWARE AND ANY ACCOMPANYING DOCUMENTATION IS PROVIDED "AS IS".
** LANTRONIX SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED
** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, NON-INFRINGEMENT AND FITNESS
** FOR A PARTICULAR PURPOSE.
** LANTRONIX HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
** ENHANCEMENTS, OR MODIFICATIONS TO THIS SOFTWARE.
** IN NO EVENT SHALL LANTRONIX BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
** SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
** ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
** LANTRONIX HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*****************************************************************************/
/* Documentation */
/*****************************************************************************/
/*!
** See README.md markdown file
*/
/*****************************************************************************/
/* Includes */
/*****************************************************************************/
#include <string.h>
#include "ltrx_compile_defines.h" /* Delivered with SDK. */
#include "main_module_libs.h" /* Delivered with SDK. */
#include "ltrx_cpm.h"
#include "dotstar_strip_module_defs.h" /* Automatically generated by make. */
#include "user_data_module_definitions.h" /* Delivered with SDK. */
/*****************************************************************************/
/* Defines */
/*****************************************************************************/
#define EXPECTED_MAIN_INTERFACE_VERSION 1
#define MAX_TRANSFER_LENGTH 4
#define LED_NUMBER 30
/*****************************************************************************/
/* Structs */
/*****************************************************************************/
struct thread_info
{
uint32_t zeroBasedIndex;
bool isRunning;
struct ltrx_ip_socket *socket;
struct ltrx_ip_socket *sock2;
struct ltrx_trigger eventTrigger;
struct ltrx_trigger dataTrigger;
};
/*****************************************************************************/
/* Prototypes */
/*****************************************************************************/
bool StartSpiDotStar(uint16_t zeroBasedIndex);
void StopSpiDotStar(uint16_t zeroBasedIndex);
/*****************************************************************************/
/* Local Constants */
/*****************************************************************************/
static const struct ltrx_spi_protocol s_spiProtocol = {
.protocolName = "DotStarLED",
.startProtocol = StartSpiDotStar,
.stopProtocol = StopSpiDotStar
};
static const uint8_t led_start[4] = "\0\0\0\0";
static const uint8_t led_end[4] = {0xFF, 0xFF, 0xFF, 0xFF};
/*****************************************************************************/
/* Local Variables */
/*****************************************************************************/
static struct thread_info *s_threadInfo[MAX_SPI_EXTERNAL];
static struct ltrx_thread *s_threadForSpi[MAX_SPI_EXTERNAL];
/*****************************************************************************/
/* Globals */
/*****************************************************************************/
const struct main_external_functions *g_mainExternalFunctionEntry_pointer = 0;
/*****************************************************************************/
/* Code */
/*****************************************************************************/
void dotstar_strip_module_initialization(
const struct main_external_functions *mef
)
{
if(
mef &&
mef->current_interface_version >= EXPECTED_MAIN_INTERFACE_VERSION &&
mef->backward_compatible_down_to_version <= EXPECTED_MAIN_INTERFACE_VERSION
)
{
g_mainExternalFunctionEntry_pointer = mef;
ltrx_module_register(&g_dotstar_stripModuleInfo);
ltrx_spi_register_protocol(&s_spiProtocol);
}
}
static bool getXmlValue(const struct xml_emit_value_specification *xevs,char *buffer,size_t size) {
struct output_stream_to_buffer osb;
ltrx_output_stream_init_to_buffer(
&osb, buffer, size, OUTPUT_STREAM_TO_BUFFER_MODE__ZERO_TERMINATE
);
return(ltrx_xml_emit_value(xevs, &osb.outStream));
}
static void sendSpi(uint8_t *txBuffer, uint32_t received) {
uint8_t rxBuffer[received];
struct ltrx_spi_descriptor lsd = {
.cmd_bytes = 4,
.cmd_buf = led_start,
.data_bytes = received,
.miso_buf = rxBuffer,
.mosi_buf = txBuffer
};
ltrx_spi_transfer(0, &lsd);
lsd.cmd_buf = led_end;
lsd.data_bytes = 0;
ltrx_spi_transfer(0, &lsd);
}
static void shiftLeft(uint8_t *buf,size_t shift, size_t size) {
uint8_t temp[shift];
memcpy(temp,buf,shift);
memcpy(buf,buf+shift,size-shift);
memcpy(buf+size-shift,temp,shift);
}
static void shiftRight(uint8_t *buf, size_t shift, size_t size) {
uint8_t temp[size-shift];
memcpy(temp,buf,size-shift);
memcpy(buf,buf+size-shift,shift);
memcpy(buf+shift,temp,size-shift);
}
static void dataReader(void *opaque) {
struct thread_info *ti = opaque;
uint32_t received;
uint8_t *txBuffer;
size_t size, part;
while(ti->isRunning) {
LTRX_TRIGGER_WAIT(&ti->dataTrigger,TIME_WAIT_FOREVER);
ltrx_trigger_clear(&ti->dataTrigger);
if(ltrx_ip_socket_has_data(ti->socket,0) || ltrx_ip_socket_has_data(ti->sock2,0) ) {
if(ltrx_ip_socket_has_data(ti->socket,0)) {
received = ltrx_ip_socket_receive(ti->socket, &txBuffer, 2048);
} else {
received = ltrx_ip_socket_receive(ti->sock2, &txBuffer, 2048);
}
switch(txBuffer[0]) {
case 0x00: // Send all the data after the initial 0
sendSpi(txBuffer+1,received-1);
break;
case 0x01: // Make all same pattern
size = txBuffer[1]*4;
part = received-2;
if (part > 3) {
uint8_t buf[size];
for(uint32_t i=0;i<size;i+=part) {
memcpy(buf+i,txBuffer+2,MINIMUM(part,size-i));
}
sendSpi(buf,size);
}
break;
case 0x02:
/* Rotating pattern
First byte: 02
Second byte: number of leds
Third byte: increment of 20ms for rotate
After that: pattern */
{
size = txBuffer[1]*4;
part = received-3;
uint8_t buf[size];
for(uint32_t i=0;i<size;i+=part) {
memcpy(buf+i,txBuffer+3,MINIMUM(part,size-i));
}
while(!LTRX_TRIGGER_WAIT(&ti->dataTrigger,txBuffer[2]*20)) {
sendSpi(buf,size);
shiftRight(buf,4,size);
}
}
break;
case 0x03:
{
size = txBuffer[1]*4;
part = received-3;
uint8_t buf[size];
memset(buf,0xE0,size);
memcpy(buf,txBuffer+3,part);
size_t pos=0;
bool right = true;
while(!LTRX_TRIGGER_WAIT(&ti->dataTrigger,txBuffer[2]*20)) {
sendSpi(buf,size);
if(right) {
shiftRight(buf,part,size);
pos+=part;
if(pos >= (size)-part) {
right = false;
}
} else {
shiftLeft(buf,part,size);
pos-=part;
if(pos < 1) {
right = true;
}
}
}
}
break;
}
}
}
}
static uint32_t getPortFromConfig(void) {
char buffer[6];
char *ptr;
static const struct xml_emit_value_specification s_xevs = {
.type = XML_EMIT_VALUE_TYPE__CONFIGURATION,
.groupName = "Custom",
.optGroupInstance = "Network",
.itemName = "Item",
.optItemInstance = "Port",
.optValueName = "Value"
};
if(getXmlValue(&s_xevs, buffer, sizeof(buffer))) {
TLOG(TLOG_SEVERITY_LEVEL__INFORMATIONAL, "Port: %s", buffer);
uint32_t port = (uint32_t)strtol(buffer,&ptr,10);
if(port > 0 && port < 64000) {
return port;
}
}
return 20000;
}
static void spiThread(void *opaque)
{
uint16_t zeroBasedIndex = (uint32_t)opaque;
bool loggedStartMessage = false;
struct thread_info ti =
{
.zeroBasedIndex = zeroBasedIndex,
.isRunning = true,
.socket = NULL
};
if(! ltrx_trigger_create(&ti.eventTrigger, s_spiProtocol.protocolName))
return;
if(! ltrx_trigger_create(&ti.dataTrigger, "data"))
return;
s_threadInfo[zeroBasedIndex] = &ti;
while(
ti.isRunning &&
! ltrx_spi_open(zeroBasedIndex, 1000)
);
if(ti.isRunning)
{
TLOG(
TLOG_SEVERITY_LEVEL__INFORMATIONAL,
"%s started on SPI %u",
s_spiProtocol.protocolName,
zeroBasedIndex + 1
);
loggedStartMessage = true;
}
if(ti.isRunning)
{
ltrx_trigger_clear(&ti.eventTrigger);
struct ltrx_udp_socket_options luso = {
.optLocalPort = getPortFromConfig(),
.optReceiveEventTrigger = &ti.dataTrigger,
.zeroBasedInterface = NETS_SOFTAP_START
};
if(luso.optLocalPort != 0) {
ltrx_thread_create("Data processor",dataReader,(void *)&ti,2000);
while(ti.socket == NULL) {
ti.socket = ltrx_udp_socket_open_with_options(&luso);
ltrx_thread_sleep(100);
}
luso.zeroBasedInterface = NETS_WLAN_START;
while(ti.sock2 == NULL) {
ti.sock2 = ltrx_udp_socket_open_with_options(&luso);
ltrx_thread_sleep(1000);
}
LTRX_TRIGGER_WAIT(&ti.eventTrigger, TIME_WAIT_FOREVER);
ltrx_trigger_signal(&ti.dataTrigger);
ltrx_thread_yield();
ltrx_udp_socket_close(ti.socket);
ltrx_udp_socket_close(ti.sock2);
ti.socket = NULL;
ti.sock2 = NULL;
}
}
if(loggedStartMessage)
{
TLOG(
TLOG_SEVERITY_LEVEL__INFORMATIONAL,
"%s stopped on SPI %u",
s_spiProtocol.protocolName,
zeroBasedIndex + 1
);
}
ltrx_spi_close(zeroBasedIndex);
s_threadInfo[zeroBasedIndex] = NULL;
s_threadForSpi[zeroBasedIndex] = 0;
}
bool StartSpiDotStar(uint16_t zeroBasedIndex)
{
if(s_threadInfo[zeroBasedIndex] || s_threadForSpi[zeroBasedIndex])
{
TLOG(
TLOG_SEVERITY_LEVEL__ERROR,
"%s thread already running for SPI %u",
s_spiProtocol.protocolName,
zeroBasedIndex + 1
);
return false;
}
s_threadForSpi[zeroBasedIndex] = ltrx_thread_create(
s_spiProtocol.protocolName,
spiThread,
(void *)(uint32_t)zeroBasedIndex,
3000
);
if(! s_threadForSpi[zeroBasedIndex])
{
TLOG(
TLOG_SEVERITY_LEVEL__ERROR,
"Failed to create %s thread for SPI %u",
s_spiProtocol.protocolName,
zeroBasedIndex + 1
);
return false;
}
return true;
}
void StopSpiDotStar(uint16_t zeroBasedIndex)
{
bool wasRunning = false;
struct thread_info *ti;
ltrx_block_preemption();
ti = s_threadInfo[zeroBasedIndex];
if(ti && ti->isRunning)
{
wasRunning = true;
ti->isRunning = false;
ltrx_trigger_signal(&ti->eventTrigger);
}
ltrx_unblock_preemption();
if(wasRunning)
{
struct ltrx_thread *lt;
while(
(lt = s_threadForSpi[zeroBasedIndex]) != NULL &&
lt != ltrx_threadid()
)
{
ltrx_thread_sleep(100);
}
}
}
/*!
** @}
*/
/*!
** @}
*/