-
-
Notifications
You must be signed in to change notification settings - Fork 68
/
HyperSerialEsp8266.ino
304 lines (258 loc) · 8.79 KB
/
HyperSerialEsp8266.ino
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
#include <NeoPixelBus.h>
////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////// CONFIG SECTION STARTS /////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
#define THIS_IS_RGBW // RGBW SK6812, otherwise comment it
bool skipFirstLed = true; // if set the first led in the strip will be set to black (for level shifters)
int serialSpeed = 2000000; // serial port speed
////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////// CONFIG SECTION ENDS /////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
int pixelCount = 16; // This is dynamic, don't change it
#ifdef THIS_IS_RGBW
#define LED_TYPE NeoGrbwFeature
#else
#define LED_TYPE NeoGrbFeature
#endif
NeoPixelBus<LED_TYPE, NeoEsp8266Uart1800KbpsMethod>* strip = NULL;
void Init(int count)
{
if (strip != NULL)
delete strip;
pixelCount = count;
strip = new NeoPixelBus<LED_TYPE, NeoEsp8266Uart1800KbpsMethod>(pixelCount);
}
enum class AwaProtocol {
HEADER_A,
HEADER_w,
HEADER_a,
HEADER_HI,
HEADER_LO,
HEADER_CRC,
RED,
GREEN,
BLUE,
FLETCHER1,
FLETCHER2
};
// static data buffer for the loop
#define MAX_BUFFER 2048
uint8_t buffer[MAX_BUFFER];
AwaProtocol state = AwaProtocol::HEADER_A;
uint8_t CRC = 0;
uint16_t count = 0;
uint16_t currentPixel = 0;
uint16_t fletcher1 = 0;
uint16_t fletcher2 = 0;
#ifdef THIS_IS_RGBW
RgbwColor inputColor;
uint8_t rChannel[256];
uint8_t gChannel[256];
uint8_t bChannel[256];
#else
RgbColor inputColor;
#endif
// stats
unsigned long stat_start = 0;
uint16_t stat_good = 0;
uint16_t stat_frames = 0;
uint16_t stat_final_good = 0;
uint16_t stat_final_frames = 0;
void readSerialData()
{
unsigned long curTime = millis();
uint16_t bufferPointer = 0;
uint16_t internalIndex = min(Serial.available(), MAX_BUFFER);
if (internalIndex > 0)
internalIndex = Serial.readBytes(buffer, internalIndex);
// stats
if (internalIndex > 0 && curTime - stat_start > 1000)
{
if (stat_frames > 0)
{
stat_final_good = stat_good;
stat_final_frames = stat_frames;
}
stat_start = curTime;
stat_good = 0;
stat_frames = 0;
}
else if (curTime - stat_start > 5000)
{
stat_start = curTime;
stat_good = 0;
stat_frames = 0;
Serial.write("Stats for the last full frame.\r\n");
Serial.write("Frames per second: ");
Serial.print(stat_final_frames);
Serial.write("\r\nGood frames: ");
Serial.print(stat_final_good);
Serial.write("\r\nBad frames: ");
Serial.print(stat_final_frames - stat_final_good);
Serial.write("\r\n-------------------------\r\n");
}
while (bufferPointer < internalIndex)
{
byte input = buffer[bufferPointer++];
switch (state)
{
case AwaProtocol::HEADER_A:
if (input == 'A') state = AwaProtocol::HEADER_w;
break;
case AwaProtocol::HEADER_w:
if (input == 'w') state = AwaProtocol::HEADER_a;
else state = AwaProtocol::HEADER_A;
break;
case AwaProtocol::HEADER_a:
if (input == 'a') state = AwaProtocol::HEADER_HI;
else state = AwaProtocol::HEADER_A;
break;
case AwaProtocol::HEADER_HI:
stat_frames++;
currentPixel = 0;
count = input * 0x100;
CRC = input;
fletcher1 = 0;
fletcher2 = 0;
state = AwaProtocol::HEADER_LO;
break;
case AwaProtocol::HEADER_LO:
count += input;
CRC = CRC ^ input ^ 0x55;
state = AwaProtocol::HEADER_CRC;
break;
case AwaProtocol::HEADER_CRC:
if (CRC == input)
{
if (count+1 != pixelCount) Init(count+1);
state = AwaProtocol::RED;
}
else
state = AwaProtocol::HEADER_A;
break;
case AwaProtocol::RED:
inputColor.R = input;
fletcher1 = (fletcher1 + (uint16_t)input) % 255;
fletcher2 = (fletcher2 + fletcher1) % 255;
state = AwaProtocol::GREEN;
break;
case AwaProtocol::GREEN:
inputColor.G = input;
fletcher1 = (fletcher1 + (uint16_t)input) % 255;
fletcher2 = (fletcher2 + fletcher1) % 255;
state = AwaProtocol::BLUE;
break;
case AwaProtocol::BLUE:
inputColor.B = input;
#ifdef THIS_IS_RGBW
inputColor.W = min(rChannel[inputColor.R],
min(gChannel[inputColor.G],
bChannel[inputColor.B]));
inputColor.R -= rChannel[inputColor.W];
inputColor.G -= gChannel[inputColor.W];
inputColor.B -= bChannel[inputColor.W];
#endif
fletcher1 = (fletcher1 + (uint16_t)input) % 255;
fletcher2 = (fletcher2 + fletcher1) % 255;
if (currentPixel == 0 && skipFirstLed)
{
#ifdef THIS_IS_RGBW
strip->SetPixelColor(currentPixel++, RgbwColor(0, 0, 0, 0));
#else
strip->SetPixelColor(currentPixel++, RgbColor(0, 0, 0));
#endif
}
else
setStripPixel(currentPixel++, inputColor);
if (count-- > 0) state = AwaProtocol::RED;
else state = AwaProtocol::FLETCHER1;
break;
case AwaProtocol::FLETCHER1:
if (input != fletcher1) state = AwaProtocol::HEADER_A;
else state = AwaProtocol::FLETCHER2;
break;
case AwaProtocol::FLETCHER2:
if (input == fletcher2)
{
stat_good++;;
strip->Show();
}
state = AwaProtocol::HEADER_A;
break;
}
}
}
#ifdef THIS_IS_RGBW
void setStripPixel(uint16_t pix, RgbwColor& inputColor)
{
if (pix < pixelCount)
{
strip->SetPixelColor(pix, inputColor);
}
}
#else
void setStripPixel(uint16_t pix, RgbColor& inputColor)
{
if (pix < pixelCount)
{
strip->SetPixelColor(pix, inputColor);
}
}
#endif
void setup()
{
// Init serial port
Serial.begin(serialSpeed);
Serial.setTimeout(50);
Serial.setRxBufferSize(2048);
// Display config
Serial.write("\r\nWelcome!\r\nAwa driver.\r\n");
#ifdef THIS_IS_RGBW
Serial.write("Color mode: RGBW\r\n");
#else
Serial.write("Color mode: RGB\r\n");
#endif
if (skipFirstLed)
Serial.write("First LED: disabled\r\n");
else
Serial.write("First LED: enabled\r\n");
// Init NeoPixelBus
Init(pixelCount);
strip->Begin();
// Prepare calibration for RGBW
#ifdef THIS_IS_RGBW
// prepare
for (int i = 0; i < 256; i++)
{
// color calibration
uint32_t rCorrection = 0xB0 * (uint32_t)i; // adjust red -> white in 0-0xFF range
uint32_t gCorrection = 0xB0 * (uint32_t)i; // adjust green -> white in 0-0xFF range
uint32_t bCorrection = 0x70 * (uint32_t)i; // adjust blue -> white in 0-0xFF range
rCorrection /= 0xFF;
gCorrection /= 0xFF;
bCorrection /= 0xFF;
rChannel[i] = (uint8_t)rCorrection;
gChannel[i] = (uint8_t)gCorrection;
bChannel[i] = (uint8_t)bCorrection;
}
#endif
// Say ambilight like "Hello" to the world
for (int i = 0; i < 9; i++)
{
if (i < 3)
strip->SetPixelColor(0, RgbColor(((i + 1) * 80) % 255, 0, 0));
else if (i < 6)
strip->SetPixelColor(0, RgbColor(0, ((6 - i) * 80) % 255, 0));
else
strip->SetPixelColor(0, RgbColor(0, 0, ((i - 5) * 80) % 255));
strip->Show();
delay(200);
}
// Clear it
strip->SetPixelColor(0, RgbColor(0, 0, 0));
strip->Show();
}
void loop()
{
readSerialData();
}