-
-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathLEDStrip.cpp
More file actions
329 lines (270 loc) · 7.85 KB
/
LEDStrip.cpp
File metadata and controls
329 lines (270 loc) · 7.85 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
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
/*---------------------------------------------------------*\
| Processing Code for Generic LED Strip Interface |
| |
| Adam Honse (calcprogrammer1@gmail.com), 12/11/2016 |
\*---------------------------------------------------------*/
#include "LEDStrip.h"
#include <fstream>
#include <iostream>
#include <string>
LEDStrip::LEDStrip()
{
num_leds = 30;
}
LEDStrip::~LEDStrip()
{
}
void LEDStrip::Initialize(char* ledstring)
{
strcpy(led_string, ledstring);
bool serial = TRUE;
LPSTR numleds = NULL;
LPSTR source = NULL;
LPSTR udpport_baud= NULL;
LPSTR next = NULL;
source = strtok_s(ledstring, ",", &next);
//Check if we are setting up a UDP or Serial interface
//UDP is denoted by udp:
if (strncmp(source, "udp:", 4) == 0)
{
source = source + 4;
serial = FALSE;
}
//Check for either the UDP port or the serial baud rate
if (strlen(next))
{
udpport_baud = strtok_s(next, ",", &next);
}
//Check for the number of LEDs
if (strlen(next))
{
numleds = strtok_s(next, ",", &next);
}
if (serial)
{
if (udpport_baud == NULL)
{
//Initialize with default baud rate
InitializeSerial(source, 115200);
}
else
{
//Initialize with custom baud rate
InitializeSerial(source, atoi(udpport_baud));
}
}
else
{
if (udpport_baud == NULL)
{
//Do something
}
else
{
//Initialize UDP port
InitializeUDP(source, udpport_baud);
}
}
if (numleds != NULL && strlen(numleds))
{
SetNumLEDs(atoi(numleds));
}
}
void LEDStrip::InitializeHuePlus(char* ledstring)
{
strcpy(led_string, ledstring);
LPSTR source = NULL;
LPSTR channels = NULL;
LPSTR numleds = NULL;
LPSTR next = NULL;
source = strtok_s(ledstring, ",", &next);
//Check for selected channel 0=both, 1= Ch.1, 2= Ch.2
if (strlen(next))
{
channels = strtok_s(next, ",", &next);
}
switch (atoi(channels))
{
case 0:
channel = 0x00;
break;
case 1:
channel = 0x01;
break;
case 2:
channel = 0x02;
break;
}
//Check for the number of LEDs, sets the corresponding variable with the counter for the fans
if (strlen(next))
{
numleds = strtok_s(next, ",", &next);
}
switch (atoi(numleds) / 8)
{
case 1:
fans = 0x00;
break;
case 2:
fans = 0x01;
break;
case 3:
fans = 0x02;
break;
case 4:
fans = 0x03;
break;
case 5:
fans = 0x04;
break;
}
//Initialize with default baud rate
InitializeSerial(source, 256000);
if (numleds != NULL && strlen(numleds))
{
SetNumLEDs(atoi(numleds));
}
}
void LEDStrip::InitializeSerial(char* portname, int baud)
{
portname = strtok(portname, "\r");
strcpy(port_name, portname);
baud_rate = baud;
serialport = new serial_port(port_name, baud_rate);
udpport = NULL;
}
void LEDStrip::InitializeUDP(char * clientname, char * port)
{
strcpy(client_name, clientname);
strcpy(port_name, port);
udpport = new net_port(client_name, port_name);
serialport = NULL;
}
char* LEDStrip::GetLEDString()
{
return(led_string);
}
void LEDStrip::SetNumLEDs(int numleds)
{
num_leds = numleds;
LEDStripXIndex = new int[num_leds];
LEDStripYIndex = new int[num_leds];
if ((num_leds % 2) == 0)
{
//Even number of LEDs
for (int i = 0; i < num_leds; i++)
{
LEDStripXIndex[i] = (int)((i * (256.0f / (num_leds - 1))));
if (i == (num_leds - 1))
{
LEDStripXIndex[i] = LEDStripXIndex[i] - 1;
}
LEDStripYIndex[i] = ROW_IDX_BAR_GRAPH;
}
}
else
{
//Odd number of LEDs
for (int i = 0; i < num_leds; i++)
{
LEDStripYIndex[i] = ROW_IDX_BAR_GRAPH;
if (i == (num_leds / 2))
{
LEDStripXIndex[i] = 128;
}
else if (i < ((num_leds / 2) + 1))
{
LEDStripXIndex[i] = (num_leds / 2) + ((i + 1) * (num_leds + 1));
}
else
{
LEDStripXIndex[i] = ((num_leds / 2) + 1) + (i * (num_leds + 1));
}
}
}
}
void LEDStrip::SetLEDs(COLORREF pixels[64][256])
{
if (serialport != NULL || udpport != NULL)
{
unsigned char *serial_buf;
serial_buf = new unsigned char[(num_leds * 3) + 3];
serial_buf[0] = 0xAA;
for (int idx = 0; idx < (num_leds * 3); idx += 3)
{
int pixel_idx = idx / 3;
COLORREF color = pixels[LEDStripYIndex[pixel_idx]][LEDStripXIndex[pixel_idx]];
serial_buf[idx + 1] = GetRValue(color);
serial_buf[idx + 2] = GetGValue(color);
serial_buf[idx + 3] = GetBValue(color);
}
unsigned short sum = 0;
for (int i = 0; i < (num_leds * 3) + 1; i++)
{
sum += serial_buf[i];
}
serial_buf[(num_leds * 3) + 1] = sum >> 8;
serial_buf[(num_leds * 3) + 2] = sum & 0x00FF;
if (serialport != NULL)
{
serialport->serial_write((char *)serial_buf, (num_leds * 3) + 3);
serialport->serial_flush_tx();
}
else if (udpport != NULL)
{
udpport->udp_write((char *)serial_buf, (num_leds * 3) + 3);
}
delete[] serial_buf;
}
}
void LEDStrip::SetLEDsXmas(COLORREF pixels[64][256])
{
unsigned char xmas_buf[5*25];
for (int idx = 0; idx < 25; idx++)
{
unsigned int xmas_color = ((GetBValue(pixels[ROW_IDX_BAR_GRAPH][(int)(idx * 5.12f)])/16)<<8)
| ((GetGValue(pixels[ROW_IDX_BAR_GRAPH][(int)(idx * 5.12f)])/16)<<4)
| ((GetRValue(pixels[ROW_IDX_BAR_GRAPH][(int)(idx * 5.12f)])/16));
xmas_buf[(idx * 5)] = 0x00;
xmas_buf[(idx * 5) + 1] = idx + 1;
xmas_buf[(idx * 5) + 2] = xmas_color >> 8;
xmas_buf[(idx * 5) + 3] = xmas_color & 0xFF;
xmas_buf[(idx * 5) + 4] = 0xFE;
if (idx == 24)
{
xmas_buf[(idx * 5) + 4] = 0xFF;
}
}
serialport->serial_write((char *)xmas_buf, 5*25);
serialport->serial_flush_tx();
};
void LEDStrip::SetLEDsHuePlus(COLORREF pixels[64][256])
{
if (serialport != NULL)
{
unsigned char *serial_buf;
serial_buf = new unsigned char[hueSize]; //Size of Message always 5 XX Blocks (Mode Selection) + 3 XX for each LED (1 color)
//-> max of 40 LEDs per Channel (or 5 Fans a 8 LEDs) -> 125 Blocks (empty LEDs are written, too
serial_buf[0] = 0x4b;
serial_buf[1] = channel;
serial_buf[2] = 0x0e;
serial_buf[3] = fans;
serial_buf[4] = 0x00;
for (int i = 5; i < hueSize; i++)
{
//clearing the buf otherwise sometimes strange things are written to the COM Port
serial_buf[i] = 0x00;
}
for (int idx = 0; idx < (num_leds * 3); idx += 3)
{
int pixel_idx = idx / 3;
COLORREF color = pixels[LEDStripYIndex[pixel_idx]][LEDStripXIndex[pixel_idx]];
serial_buf[idx + 5] = GetGValue(color);
serial_buf[idx + 6] = GetRValue(color);
serial_buf[idx + 7] = GetBValue(color);
}
serialport->serial_write((char *)serial_buf, hueSize);
serialport->serial_flush_tx();
delete[] serial_buf;
}
}