Skip to content

Commit ec678d4

Browse files
committed
add Neopixel special sketch that work with bluefruit LE app
1 parent 01be130 commit ec678d4

File tree

3 files changed

+280
-248
lines changed

3 files changed

+280
-248
lines changed
Lines changed: 280 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,280 @@
1+
// Include Bluetooth
2+
3+
#include <Arduino.h>
4+
#include <Adafruit_NeoPixel.h>
5+
#include <bluefruit.h>
6+
7+
// Neopixel
8+
#define PIN 23 /* Pin used to drive the NeoPixels */
9+
10+
#define MAXCOMPONENTS 4
11+
uint8_t *pixelBuffer = NULL;
12+
uint8_t width = 0;
13+
uint8_t height = 0;
14+
uint8_t components = 3; // only 3 and 4 are valid values
15+
uint8_t stride;
16+
17+
Adafruit_NeoPixel pixels = Adafruit_NeoPixel();
18+
19+
BLEUart bleuart;
20+
21+
void setup()
22+
{
23+
Serial.begin(115200);
24+
Serial.println(F("Adafruit Bluefruit Neopixel Test"));
25+
Serial.println(F("------------------------------------"));
26+
27+
// Neopixels
28+
pixels.begin();
29+
30+
Bluefruit.begin();
31+
Bluefruit.setName("Bluefruit52");
32+
33+
// Configure and Start BLE Uart Service
34+
bleuart.begin();
35+
36+
// Set up Advertising Packet
37+
setupAdv();
38+
39+
// Start Advertising
40+
Bluefruit.Advertising.start();
41+
42+
}
43+
44+
void setupAdv(void)
45+
{
46+
//Bluefruit.Advertising.addTxPower();
47+
Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
48+
Bluefruit.Advertising.addTxPower();
49+
50+
// Include bleuart 128-bit uuid
51+
Bluefruit.Advertising.addService(bleuart);
52+
53+
// There is no room for Name in Advertising packet
54+
// Use Scan response for Name
55+
Bluefruit.ScanResponse.addName();
56+
}
57+
58+
59+
void loop()
60+
{
61+
// Echo received data
62+
if ( Bluefruit.connected() && bleuart.notifyEnabled() )
63+
{
64+
int command = bleuart.read();
65+
66+
switch (command) {
67+
case 'V': { // Get Version
68+
commandVersion();
69+
break;
70+
}
71+
72+
case 'S': { // Setup dimensions, components, stride...
73+
commandSetup();
74+
break;
75+
}
76+
77+
case 'C': { // Clear with color
78+
commandClearColor();
79+
break;
80+
}
81+
82+
case 'B': { // Set Brightness
83+
commandSetBrightness();
84+
break;
85+
}
86+
87+
case 'P': { // Set Pixel
88+
commandSetPixel();
89+
break;
90+
}
91+
92+
case 'I': { // Receive new image
93+
commandImage();
94+
break;
95+
}
96+
97+
}
98+
}
99+
}
100+
101+
void swapBuffers()
102+
{
103+
uint8_t *base_addr = pixelBuffer;
104+
int pixelIndex = 0;
105+
for (int j = 0; j < height; j++)
106+
{
107+
for (int i = 0; i < width; i++) {
108+
if (components == 3) {
109+
pixels.setPixelColor(pixelIndex, pixels.Color(*base_addr, *(base_addr+1), *(base_addr+2)));
110+
}
111+
else {
112+
Serial.println(F("TODO: implement me"));
113+
}
114+
base_addr+=components;
115+
pixelIndex++;
116+
}
117+
pixelIndex += stride - width; // move pixelIndex to the next row (take into account the stride)
118+
}
119+
pixels.show();
120+
121+
}
122+
123+
void commandVersion() {
124+
Serial.println(F("Command: Version check"));
125+
sendResponse("Neopixel v1.0");
126+
}
127+
128+
void commandSetup() {
129+
Serial.println(F("Command: Setup"));
130+
131+
width = bleuart.read();
132+
height = bleuart.read();
133+
components = bleuart.read();
134+
stride = bleuart.read();
135+
neoPixelType pixelType;
136+
pixelType = bleuart.read();
137+
pixelType += bleuart.read()<<8;
138+
139+
Serial.printf("\tsize: %dx%d\n", width, height);
140+
Serial.printf("\tcomponents: %d\n", components);
141+
Serial.printf("\tstride: %d\n", stride);
142+
Serial.printf("\tpixelType %d\n", pixelType );
143+
144+
145+
if (pixelBuffer != NULL) {
146+
delete[] pixelBuffer;
147+
}
148+
149+
uint32_t size = width*height;
150+
pixelBuffer = new uint8_t[size*components];
151+
pixels.updateLength(size);
152+
pixels.updateType(pixelType);
153+
pixels.setPin(PIN);
154+
155+
// Done
156+
sendResponse("OK");
157+
}
158+
159+
void commandSetBrightness() {
160+
Serial.println(F("Command: SetBrightness"));
161+
162+
// Read value
163+
uint8_t brightness = bleuart.read();
164+
165+
// Set brightness
166+
pixels.setBrightness(brightness);
167+
168+
// Refresh pixels
169+
swapBuffers();
170+
171+
// Done
172+
sendResponse("OK");
173+
}
174+
175+
void commandClearColor() {
176+
Serial.println(F("Command: ClearColor"));
177+
178+
// Read color
179+
uint8_t color[MAXCOMPONENTS];
180+
for (int j = 0; j < components;) {
181+
if (bleuart.available()) {
182+
color[j] = bleuart.read();
183+
j++;
184+
}
185+
}
186+
187+
// Set all leds to color
188+
int size = width * height;
189+
uint8_t *base_addr = pixelBuffer;
190+
for (int i = 0; i < size; i++) {
191+
for (int j = 0; j < components; j++) {
192+
*base_addr = color[j];
193+
base_addr++;
194+
}
195+
}
196+
197+
// Swap buffers
198+
Serial.println(F("ClearColor completed"));
199+
swapBuffers();
200+
201+
202+
if (components == 3) {
203+
Serial.printf("\tcolor (%d, %d, %d)\n", color[0], color[1], color[2] );
204+
}
205+
206+
// Done
207+
sendResponse("OK");
208+
}
209+
210+
void commandSetPixel() {
211+
Serial.println(F("Command: SetPixel"));
212+
213+
// Read position
214+
uint8_t x = bleuart.read();
215+
uint8_t y = bleuart.read();
216+
217+
// Read colors
218+
uint32_t pixelIndex = y*width+x;
219+
uint32_t pixelComponentOffset = pixelIndex*components;
220+
uint8_t *base_addr = pixelBuffer+pixelComponentOffset;
221+
for (int j = 0; j < components;) {
222+
if (bleuart.available()) {
223+
*base_addr = bleuart.read();
224+
base_addr++;
225+
j++;
226+
}
227+
}
228+
229+
// Set colors
230+
if (components == 3) {
231+
uint32_t pixelIndex = y*stride+x;
232+
pixels.setPixelColor(pixelIndex, pixels.Color(pixelBuffer[pixelComponentOffset], pixelBuffer[pixelComponentOffset+1], pixelBuffer[pixelComponentOffset+2]));
233+
234+
Serial.printf("\tcolor (%d, %d, %d)\n", pixelBuffer[pixelComponentOffset], pixelBuffer[pixelComponentOffset+1], pixelBuffer[pixelComponentOffset+2] );
235+
}
236+
else {
237+
Serial.println(F("TODO: implement me"));
238+
}
239+
pixels.show();
240+
241+
// Done
242+
sendResponse("OK");
243+
}
244+
245+
void commandImage() {
246+
Serial.printf("Command: Image %dx%d, %d, %d\n", width, height, components, stride);
247+
248+
// Receive new pixel buffer
249+
int size = width * height;
250+
uint8_t *base_addr = pixelBuffer;
251+
for (int i = 0; i < size; i++) {
252+
for (int j = 0; j < components;) {
253+
if (bleuart.available()) {
254+
*base_addr = bleuart.read();
255+
base_addr++;
256+
j++;
257+
}
258+
}
259+
260+
/*
261+
if (components == 3) {
262+
uint32_t index = i*components;
263+
Serial.printf("\tp%d (%d, %d, %d)\n", i, pixelBuffer[index], pixelBuffer[index+1], pixelBuffer[index+2] );
264+
}
265+
*/
266+
}
267+
268+
// Swap buffers
269+
Serial.println(F("Image received"));
270+
swapBuffers();
271+
272+
// Done
273+
sendResponse("OK");
274+
}
275+
276+
void sendResponse(char const *response) {
277+
Serial.printf("Send Response: %s\n", response);
278+
bleuart.write(response, strlen(response)*sizeof(char));
279+
}
280+

libraries/Bluefruit52Lib/examples/Peripheral/neopixel_picker/neopixel_picker.ino

Lines changed: 0 additions & 115 deletions
This file was deleted.

0 commit comments

Comments
 (0)