2828#include < Adafruit_NeoPixel.h>
2929#include < bluefruit.h>
3030
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 */
3233
3334#define MAXCOMPONENTS 4
3435uint8_t *pixelBuffer = NULL ;
3536uint8_t width = 0 ;
3637uint8_t height = 0 ;
37- uint8_t components = 3 ; // only 3 and 4 are valid values
3838uint8_t stride;
39+ uint8_t componentsValue;
40+ bool is400Hz;
41+ uint8_t components = 3 ; // only 3 and 4 are valid values
3942
40- Adafruit_NeoPixel pixels = Adafruit_NeoPixel();
43+ Adafruit_NeoPixel neopixel = Adafruit_NeoPixel();
4144
4245// BLE Service
4346BLEDis bledis;
@@ -53,7 +56,7 @@ void setup()
5356 Serial.println (" Please connect using the Bluefruit Connect LE application" );
5457
5558 // Config Neopixels
56- pixels .begin ();
59+ neopixel .begin ();
5760
5861 // Init Bluefruit
5962 Bluefruit.begin ();
@@ -144,51 +147,53 @@ void swapBuffers()
144147 {
145148 for (int i = 0 ; i < width; i++) {
146149 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 )));
148151 }
149152 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 ) ));
151154 }
152155 base_addr+=components;
153156 pixelIndex++;
154157 }
155158 pixelIndex += stride - width; // Move pixelIndex to the next row (take into account the stride)
156159 }
157- pixels .show ();
160+ neopixel .show ();
158161
159162}
160163
161164void commandVersion () {
162165 Serial.println (F (" Command: Version check" ));
163- sendResponse (" Neopixel v1.0 " );
166+ sendResponse (NEOPIXEL_VERSION_STRING );
164167}
165168
166169void commandSetup () {
167170 Serial.println (F (" Command: Setup" ));
168171
169172 width = bleuart.read ();
170173 height = bleuart.read ();
171- components = bleuart.read ();
172174 stride = bleuart.read ();
175+ componentsValue = bleuart.read ();
176+ is400Hz = bleuart.read ();
177+
173178 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 ;
176182
177183 Serial.printf (" \t size: %dx%d\n " , width, height);
178- Serial.printf (" \t components: %d\n " , components);
179184 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);
182187
183188 if (pixelBuffer != NULL ) {
184189 delete[] pixelBuffer;
185190 }
186191
187192 uint32_t size = width*height;
188193 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);
192197
193198 // Done
194199 sendResponse (" OK" );
@@ -201,7 +206,7 @@ void commandSetBrightness() {
201206 uint8_t brightness = bleuart.read ();
202207
203208 // Set brightness
204- pixels .setBrightness (brightness);
209+ neopixel .setBrightness (brightness);
205210
206211 // Refresh pixels
207212 swapBuffers ();
@@ -238,7 +243,10 @@ void commandClearColor() {
238243
239244
240245 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 ] );
242250 }
243251
244252 // Done
@@ -253,9 +261,9 @@ void commandSetPixel() {
253261 uint8_t y = bleuart.read ();
254262
255263 // 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 ;
259267 for (int j = 0 ; j < components;) {
260268 if (bleuart.available ()) {
261269 *base_addr = bleuart.read ();
@@ -265,16 +273,19 @@ void commandSetPixel() {
265273 }
266274
267275 // Set colors
276+ uint32_t neopixelIndex = y*stride+x;
277+ uint8_t *pixelBufferPointer = pixelBuffer + pixelDataOffset;
278+ uint32_t color;
268279 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 ) );
273282 }
274283 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 ) );
276286 }
277- pixels.show ();
287+ neopixel.setPixelColor (neopixelIndex, color);
288+ neopixel.show ();
278289
279290 // Done
280291 sendResponse (" OK" );
0 commit comments