Skip to content

Commit 8d847f9

Browse files
author
microbuilder
committed
Updated for latest Bluefruit LE Connect version with RGBW support
1 parent d1e86c6 commit 8d847f9

File tree

1 file changed

+39
-28
lines changed
  • libraries/Bluefruit52Lib/examples/Peripheral/neopixel

1 file changed

+39
-28
lines changed

libraries/Bluefruit52Lib/examples/Peripheral/neopixel/neopixel.ino

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,19 @@
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
3435
uint8_t *pixelBuffer = NULL;
3536
uint8_t width = 0;
3637
uint8_t height = 0;
37-
uint8_t components = 3; // only 3 and 4 are valid values
3838
uint8_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
4346
BLEDis 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

161164
void commandVersion() {
162165
Serial.println(F("Command: Version check"));
163-
sendResponse("Neopixel v1.0");
166+
sendResponse(NEOPIXEL_VERSION_STRING);
164167
}
165168

166169
void 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("\tsize: %dx%d\n", width, height);
178-
Serial.printf("\tcomponents: %d\n", components);
179184
Serial.printf("\tstride: %d\n", stride);
180-
Serial.printf("\tpixelType %d\n", pixelType );
181-
185+
Serial.printf("\tpixelType %d\n", pixelType);
186+
Serial.printf("\tcomponents: %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("\tcolor (%d, %d, %d)\n", color[0], color[1], color[2] );
246+
Serial.printf("\tclear (%d, %d, %d)\n", color[0], color[1], color[2] );
247+
}
248+
else {
249+
Serial.printf("\tclear (%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("\tcolor (%d, %d, %d)\n", pixelBuffer[pixelComponentOffset], pixelBuffer[pixelComponentOffset+1], pixelBuffer[pixelComponentOffset+2] );
280+
color = neopixel.Color( *pixelBufferPointer, *(pixelBufferPointer+1), *(pixelBufferPointer+2) );
281+
Serial.printf("\tcolor (%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("\tcolor (%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

Comments
 (0)