28
28
#include < Adafruit_NeoPixel.h>
29
29
#include < bluefruit.h>
30
30
31
- #define PIN 30 /* Pin used to drive the NeoPixels */
31
+ #define NEOPIXEL_VERSION_STRING " Neopixel v2.0"
32
+ #define PIN 30 /* Pin used to drive the NeoPixels */
32
33
33
34
#define MAXCOMPONENTS 4
34
35
uint8_t *pixelBuffer = NULL ;
35
36
uint8_t width = 0 ;
36
37
uint8_t height = 0 ;
37
- uint8_t components = 3 ; // only 3 and 4 are valid values
38
38
uint8_t stride;
39
+ uint8_t componentsValue;
40
+ bool is400Hz;
41
+ uint8_t components = 3 ; // only 3 and 4 are valid values
39
42
40
- Adafruit_NeoPixel pixels = Adafruit_NeoPixel();
43
+ Adafruit_NeoPixel neopixel = Adafruit_NeoPixel();
41
44
42
45
// BLE Service
43
46
BLEDis bledis;
@@ -53,7 +56,7 @@ void setup()
53
56
Serial.println (" Please connect using the Bluefruit Connect LE application" );
54
57
55
58
// Config Neopixels
56
- pixels .begin ();
59
+ neopixel .begin ();
57
60
58
61
// Init Bluefruit
59
62
Bluefruit.begin ();
@@ -144,51 +147,53 @@ void swapBuffers()
144
147
{
145
148
for (int i = 0 ; i < width; i++) {
146
149
if (components == 3 ) {
147
- pixels .setPixelColor (pixelIndex, pixels .Color (*base_addr, *(base_addr+1 ), *(base_addr+2 )));
150
+ neopixel .setPixelColor (pixelIndex, neopixel .Color (*base_addr, *(base_addr+1 ), *(base_addr+2 )));
148
151
}
149
152
else {
150
- Serial. println ( F ( " TODO: implement me " ));
153
+ neopixel. setPixelColor (pixelIndex, neopixel. Color (*base_addr, *(base_addr+ 1 ), *(base_addr+ 2 ), *(base_addr+ 3 ) ));
151
154
}
152
155
base_addr+=components;
153
156
pixelIndex++;
154
157
}
155
158
pixelIndex += stride - width; // Move pixelIndex to the next row (take into account the stride)
156
159
}
157
- pixels .show ();
160
+ neopixel .show ();
158
161
159
162
}
160
163
161
164
void commandVersion () {
162
165
Serial.println (F (" Command: Version check" ));
163
- sendResponse (" Neopixel v1.0 " );
166
+ sendResponse (NEOPIXEL_VERSION_STRING );
164
167
}
165
168
166
169
void commandSetup () {
167
170
Serial.println (F (" Command: Setup" ));
168
171
169
172
width = bleuart.read ();
170
173
height = bleuart.read ();
171
- components = bleuart.read ();
172
174
stride = bleuart.read ();
175
+ componentsValue = bleuart.read ();
176
+ is400Hz = bleuart.read ();
177
+
173
178
neoPixelType pixelType;
174
- pixelType = bleuart.read ();
175
- pixelType += bleuart.read ()<<8 ;
179
+ pixelType = componentsValue + (is400Hz ? NEO_KHZ400 : NEO_KHZ800);
180
+
181
+ components = (componentsValue == NEO_RGB || componentsValue == NEO_RBG || componentsValue == NEO_GRB || componentsValue == NEO_GBR || componentsValue == NEO_BRG || componentsValue == NEO_BGR) ? 3 :4 ;
176
182
177
183
Serial.printf (" \t size: %dx%d\n " , width, height);
178
- Serial.printf (" \t components: %d\n " , components);
179
184
Serial.printf (" \t stride: %d\n " , stride);
180
- Serial.printf (" \t pixelType %d\n " , pixelType );
181
-
185
+ Serial.printf (" \t pixelType %d\n " , pixelType);
186
+ Serial. printf ( " \t components: %d \n " , components);
182
187
183
188
if (pixelBuffer != NULL ) {
184
189
delete[] pixelBuffer;
185
190
}
186
191
187
192
uint32_t size = width*height;
188
193
pixelBuffer = new uint8_t [size*components];
189
- pixels .updateLength (size);
190
- pixels .updateType (pixelType);
191
- pixels .setPin (PIN);
194
+ neopixel .updateLength (size);
195
+ neopixel .updateType (pixelType);
196
+ neopixel .setPin (PIN);
192
197
193
198
// Done
194
199
sendResponse (" OK" );
@@ -201,7 +206,7 @@ void commandSetBrightness() {
201
206
uint8_t brightness = bleuart.read ();
202
207
203
208
// Set brightness
204
- pixels .setBrightness (brightness);
209
+ neopixel .setBrightness (brightness);
205
210
206
211
// Refresh pixels
207
212
swapBuffers ();
@@ -238,7 +243,10 @@ void commandClearColor() {
238
243
239
244
240
245
if (components == 3 ) {
241
- Serial.printf (" \t color (%d, %d, %d)\n " , color[0 ], color[1 ], color[2 ] );
246
+ Serial.printf (" \t clear (%d, %d, %d)\n " , color[0 ], color[1 ], color[2 ] );
247
+ }
248
+ else {
249
+ Serial.printf (" \t clear (%d, %d, %d, %d)\n " , color[0 ], color[1 ], color[2 ], color[3 ] );
242
250
}
243
251
244
252
// Done
@@ -253,9 +261,9 @@ void commandSetPixel() {
253
261
uint8_t y = bleuart.read ();
254
262
255
263
// Read colors
256
- uint32_t pixelIndex = y*width+x;
257
- uint32_t pixelComponentOffset = pixelIndex *components;
258
- uint8_t *base_addr = pixelBuffer+pixelComponentOffset ;
264
+ uint32_t pixelOffset = y*width+x;
265
+ uint32_t pixelDataOffset = pixelOffset *components;
266
+ uint8_t *base_addr = pixelBuffer+pixelDataOffset ;
259
267
for (int j = 0 ; j < components;) {
260
268
if (bleuart.available ()) {
261
269
*base_addr = bleuart.read ();
@@ -265,16 +273,19 @@ void commandSetPixel() {
265
273
}
266
274
267
275
// Set colors
276
+ uint32_t neopixelIndex = y*stride+x;
277
+ uint8_t *pixelBufferPointer = pixelBuffer + pixelDataOffset;
278
+ uint32_t color;
268
279
if (components == 3 ) {
269
- uint32_t pixelIndex = y*stride+x;
270
- pixels.setPixelColor (pixelIndex, pixels.Color (pixelBuffer[pixelComponentOffset], pixelBuffer[pixelComponentOffset+1 ], pixelBuffer[pixelComponentOffset+2 ]));
271
-
272
- Serial.printf (" \t color (%d, %d, %d)\n " , pixelBuffer[pixelComponentOffset], pixelBuffer[pixelComponentOffset+1 ], pixelBuffer[pixelComponentOffset+2 ] );
280
+ color = neopixel.Color ( *pixelBufferPointer, *(pixelBufferPointer+1 ), *(pixelBufferPointer+2 ) );
281
+ Serial.printf (" \t color (%d, %d, %d)\n " ,*pixelBufferPointer, *(pixelBufferPointer+1 ), *(pixelBufferPointer+2 ) );
273
282
}
274
283
else {
275
- Serial.println (F (" TODO: implement me" ));
284
+ color = neopixel.Color ( *pixelBufferPointer, *(pixelBufferPointer+1 ), *(pixelBufferPointer+2 ), *(pixelBufferPointer+3 ) );
285
+ Serial.printf (" \t color (%d, %d, %d, %d)\n " , *pixelBufferPointer, *(pixelBufferPointer+1 ), *(pixelBufferPointer+2 ), *(pixelBufferPointer+3 ) );
276
286
}
277
- pixels.show ();
287
+ neopixel.setPixelColor (neopixelIndex, color);
288
+ neopixel.show ();
278
289
279
290
// Done
280
291
sendResponse (" OK" );
0 commit comments