-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLumos_Board_Color.ino
375 lines (303 loc) · 9.79 KB
/
Lumos_Board_Color.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#include "FastLED.h"
FASTLED_USING_NAMESPACE
using namespace std;
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
#include "StringSplitter.h"
#include <cstring>
#include <cstdlib>
BLECharacteristic *pCharacteristic;
bool deviceConnected = false;
float txValue = 0;
// See the following for generating UUIDs:
// https://www.uuidgenerator.net/
#define SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID
#define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
#define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"
//led stuff
#define DATA_PIN 27
//#define CLK_PIN 4
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB
#define NUM_LEDS 300
CRGB leds[NUM_LEDS];
#define BRIGHTNESS 96
#define FRAMES_PER_SECOND 120
uint8_t gHue = 0; // rotating "base color" used by many of the patterns
String pattern;
String pat;
String sBrightFix;
int brightFixs;
String rxColorFix;
String sColorfix1;
String sColorFix;
uint8_t rgbR=128;
uint8_t rgbG=128;
uint8_t rgbB=128;
CRGB solidColor=CRGB::Blue;
#define FASTLED_SHOW_CORE 0
// -- Task handles for use in the notifications
static TaskHandle_t FastLEDshowTaskHandle = 0;
static TaskHandle_t userTaskHandle = 0;
/** show() for ESP32
Call this function instead of FastLED.show(). It signals core 0 to issue a show,
then waits for a notification that it is done.
*/
void FastLEDshowESP32()
{
if (userTaskHandle == 0) {
// -- Store the handle of the current task, so that the show task can
// notify it when it's done
userTaskHandle = xTaskGetCurrentTaskHandle();
// -- Trigger the show task
xTaskNotifyGive(FastLEDshowTaskHandle);
// -- Wait to be notified that it's done
const TickType_t xMaxBlockTime = pdMS_TO_TICKS( 200 );
ulTaskNotifyTake(pdTRUE, xMaxBlockTime);
userTaskHandle = 0;
}
}
/** show Task
This function runs on core 0 and just waits for requests to call FastLED.show()
*/
void FastLEDshowTask(void *pvParameters)
{
// -- Run forever...
for (;;) {
// -- Wait for the trigger
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
// -- Do the show (synchronously)
FastLED.show();
// -- Notify the calling task
xTaskNotifyGive(userTaskHandle);
}
}
void setSolidColor(uint8_t x, uint8_t y, uint8_t z, String sColorFix)
{
StringSplitter *splitter = new StringSplitter(sColorFix, ',', 3);
int itemCount = splitter->getItemCount();
Serial.println(itemCount);
int rgb[itemCount];
for (int i = 0; i < itemCount; i++) {
String item = splitter->getItemAtIndex(i);
rgb[i] = atoi(item.c_str());
Serial.println(rgb[i]);
}
rgbR=rgb[0];
rgbG=rgb[1];
rgbB=rgb[2];
}
void brightFix(String value) {
int bFix1;
value.remove(0, 11);
bFix1 = value.toInt();
FastLED.setBrightness(bFix1);
}
String setPattern(String pat) {
pattern = pat;
return pattern;
}
void pride()
{
static uint16_t sPseudotime = 0;
static uint16_t sLastMillis = 0;
static uint16_t sHue16 = 0;
uint8_t sat8 = beatsin88( 87, 220, 250);
uint8_t brightdepth = beatsin88( 341, 96, 224);
uint16_t brightnessthetainc16 = beatsin88( 203, (25 * 256), (40 * 256));
uint8_t msmultiplier = beatsin88(147, 23, 60);
uint16_t hue16 = sHue16;//gHue * 256;
uint16_t hueinc16 = beatsin88(113, 1, 3000);
uint16_t ms = millis();
uint16_t deltams = ms - sLastMillis ;
sLastMillis = ms;
sPseudotime += deltams * msmultiplier;
sHue16 += deltams * beatsin88( 400, 5, 9);
uint16_t brightnesstheta16 = sPseudotime;
for ( uint16_t i = 0 ; i < NUM_LEDS; i++) {
hue16 += hueinc16;
uint8_t hue8 = hue16 / 256;
brightnesstheta16 += brightnessthetainc16;
uint16_t b16 = sin16( brightnesstheta16 ) + 32768;
uint16_t bri16 = (uint32_t)((uint32_t)b16 * (uint32_t)b16) / 65536;
uint8_t bri8 = (uint32_t)(((uint32_t)bri16) * brightdepth) / 65536;
bri8 += (255 - brightdepth);
CRGB newcolor = CHSV( hue8, sat8, bri8);
uint16_t pixelnumber = i;
pixelnumber = (NUM_LEDS - 1) - pixelnumber;
nblend( leds[pixelnumber], newcolor, 64);
}
}
class MyServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;
};
void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
}
};
class MyCallbacks: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
std::string rxValue = pCharacteristic->getValue();
if (rxValue.length() > 0) {
Serial.println("*********");
Serial.print("Received Value: ");
for (int i = 0; i < rxValue.length(); i++) {
Serial.print(rxValue[i]);
}
Serial.println();
Serial.println("*********");
}
// Do stuff based on the command received from the app
// For some reason using rxValue.compare("A") == 0 doesn't work. Maybe
// there are hidden characters I'm not seeing?
if (rxValue.find("rainbow") != -1) {
Serial.println("Turning ON!");
pat = "rainbow";
setPattern(pat);
}
if (rxValue.find("brightness") != -1) {
sBrightFix = (rxValue.c_str());
brightFix(sBrightFix);
}
if (rxValue.find("juggle") != -1) {
Serial.println("Turning ON!");
pat = "juggle";
setPattern(pat);
}
if (rxValue.find("sinelon") != -1) {
Serial.println("Turning ON!");
pat = "sinelon";
setPattern(pat);
}
if (rxValue.find("bpm") != -1) {
Serial.println("Turning ON!");
pat = "bpm";
setPattern(pat);
}
if (rxValue.find("pride") != -1) {
Serial.println("Turning ON!");
pat = "pride";
setPattern(pat);
}
if (rxValue.find("color") != -1) {
sColorFix = (rxValue.c_str());
sColorFix.trim();
sColorFix.replace("color,", "");
sColorFix.replace(" ", "");
setSolidColor(rgbR,rgbG,rgbB,sColorFix);
Serial.println(rgbR);
pat = "color";
setPattern(pat);
}
else if (rxValue.find("off") != -1) {
Serial.println("Turning OFF!");
pat = "off";
setPattern(pat);
}
}
};
void setup() {
Serial.begin(115200);
FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
//FastLED.addLeds<LED_TYPE,DATA_PIN,CLK_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
// set master brightness control
FastLED.setBrightness(BRIGHTNESS);
// Create the BLE Device
BLEDevice::init("Lumos Board"); // Give it a name
// Create the BLE Server
BLEServer *pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
// Create the BLE Service
BLEService *pService = pServer->createService(SERVICE_UUID);
// Create a BLE Characteristic
pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID_TX,
BLECharacteristic::PROPERTY_NOTIFY
);
pCharacteristic->addDescriptor(new BLE2902());
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID_RX,
BLECharacteristic::PROPERTY_WRITE
);
pCharacteristic->setCallbacks(new MyCallbacks());
// Start the service
pService->start();
// Start advertising
pServer->getAdvertising()->start();
Serial.println("Waiting a client connection to notify...");
int core = xPortGetCoreID();
Serial.print("Main code running on core ");
Serial.println(core);
// -- Create the FastLED show task
xTaskCreatePinnedToCore(FastLEDshowTask, "FastLEDshowTask", 2048, NULL, 2, &FastLEDshowTaskHandle, FASTLED_SHOW_CORE);
}
void loop() {
EVERY_N_MILLISECONDS( 20 ) {
gHue++; // slowly cycle the "base color" through the rainbow
}
if (deviceConnected) {
// Fabricate some arbitrary junk for now...
if (pattern == "rainbow") {
// FastLED.show();
fill_rainbow( leds, NUM_LEDS, gHue, 7);
}
if (pattern == "off") {
// FastLED.show();
fill_solid(leds, NUM_LEDS, CRGB::Black);
}
if (pattern == "sinelon") {
// FastLED.show();
sinelon();
}
if (pattern == "bpm") {
// FastLED.show();
bpm();
}
if (pattern == "juggle") {
// FastLED.show();
juggle();
}
if (pattern == "pride") {
// FastLED.show();
pride();
}
if (pattern == "color") {
FastLED.show();
fill_solid(leds, NUM_LEDS, CRGB(rgbR,rgbG,rgbB));
}
}
if (!deviceConnected) {
FastLED.show();
fill_solid(leds, NUM_LEDS, CRGB::Black);
}
FastLEDshowESP32();
}
void sinelon()
{
// a colored dot sweeping back and forth, with fading trails
fadeToBlackBy( leds, NUM_LEDS, 20);
int pos = beatsin16( 13, 0, NUM_LEDS - 1 );
leds[pos] += CHSV( gHue, 255, 192);
}
void bpm()
{
// colored stripes pulsing at a defined Beats-Per-Minute (BPM)
uint8_t BeatsPerMinute = 62;
CRGBPalette16 palette = PartyColors_p;
uint8_t beat = beatsin8( BeatsPerMinute, 64, 255);
for ( int i = 0; i < NUM_LEDS; i++) { //9948
leds[i] = ColorFromPalette(palette, gHue + (i * 2), beat - gHue + (i * 10));
}
}
void juggle() {
// eight colored dots, weaving in and out of sync with each other
fadeToBlackBy( leds, NUM_LEDS, 20);
byte dothue = 0;
for ( int i = 0; i < 8; i++) {
leds[beatsin16( i + 7, 0, NUM_LEDS - 1 )] |= CHSV(dothue, 200, 255);
dothue += 32;
}
}