-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlightstripsex.cpp
379 lines (327 loc) · 13.6 KB
/
lightstripsex.cpp
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
376
377
378
379
#define FASTLED_ESP8266_RAW_PIN_ORDER
#define FASTLED_INTERNAL
#define FASTLED_INTERRUPT_RETRY_COUNT 0
#define FASTLED_ALLOW_INTERRUPTS 0
#include "FastLED.h"
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <DNSServer.h>
#include <ESP8266mDNS.h>
const char *softAP_ssid = "Spirit Hood";
const char *softAP_password = "12345678";
const char *myHostname = "Spirit Hood";
const byte DNS_PORT = 53;
DNSServer dnsServer;
// Web server
ESP8266WebServer server(80);
/* Soft AP network parameters */
IPAddress apIP(192, 168, 4, 1);
IPAddress netMsk(255, 255, 255, 0);
/** Last time I tried to connect to WLAN */
long lastConnectTry = 0;
/** Current WLAN status */
int status = WL_IDLE_STATUS;
#define FASTLED_ESP8266_RAW_PIN_ORDER
#if defined(FASTLED_VERSION) && (FASTLED_VERSION < 3001000)
#warning "Requires FastLED 3.1 or later; check github for latest code."
#endif
#define DATA_PIN 15
//#define CLK_PIN 4
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB
#define NUM_LEDS 60
CRGB leds[NUM_LEDS];
int BRIGHTNESS = 96;
#define FRAMES_PER_SECOND 120
uint8_t BeatsPerMinute = 62;
int r = 0;
int g = 0;
int b = 0;
bool homemade_method = false;
void setup() {
FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
FastLED.setBrightness(BRIGHTNESS);
Serial.begin(115200);
Serial.println();
Serial.print("Configuring access point...");
delay(1000);
WiFi.softAPConfig(apIP, apIP, netMsk);
WiFi.softAP(softAP_ssid, softAP_password);
delay(500); // Without delay I've seen the IP address blank
Serial.print("AP IP address: ");
Serial.println(WiFi.softAPIP());
dnsServer.setErrorReplyCode(DNSReplyCode::NoError);
dnsServer.start(DNS_PORT, "www.example.com", apIP);
server.on("/", handleRoot);
server.on("/rainbow", rainbow_call);
server.on("/rainbowWithGlitter", rainbowWithGlitter_call);
server.on("/confetti", confetti_call);
server.on("/sinelon", sinelon_call);
server.on("/juggle", juggle_call);
server.on("/next", next_call);
server.on("/bpm", [](){
int beats = server.arg("beats").toInt();
if (beats){
uint8_t BeatsPerMinute = beats;
}
bpm_call();
});
server.on("/solid", [](){
homemade_method = true;
r = server.arg("r").toInt();
g = server.arg("g").toInt();
b = server.arg("b").toInt();
solid_loop();
handleRoot();
});
server.on("/brightDown", [](){
int ammount = server.arg("a").toInt();
BRIGHTNESS -= ammount;
FastLED.setBrightness(BRIGHTNESS);
if(homemade_method){
solid_loop();
};
handleRoot();
});
server.on("/brightUp", [](){
int ammount = server.arg("a").toInt();
BRIGHTNESS -= ammount;
FastLED.setBrightness(BRIGHTNESS);
if(homemade_method){
solid_loop();
};
handleRoot();
});
server.on("/increase", [](){
int color = server.arg("c").toInt();
int ammount = server.arg("a").toInt();
if(color == 1){
r += ammount;
}
if(color == 2){
g+=ammount;
}
if(color == 3){
b+=ammount;
}
homemade_method = true;
solid_loop();
handleRoot();
});
server.on("/decrease", [](){
int color = server.arg("c").toInt();
int ammount = server.arg("a").toInt();
if(color == 1){
r -= ammount;
}
if(color == 2){
g -= ammount;
}
if(color == 3){
b -= ammount;
}
homemade_method = true;
solid_loop();
handleRoot();
});
server.onNotFound ( handleNotFound );
server.begin(); // Web server start
Serial.println("HTTP server started");
confetti_call();
}
String htmlTemplateTop = "<html><head><style>"
"* {box-sizing: border-box;}"
"body {font-family: Sans-serif;background: #edece7;}"
".switch {display: block;position: relative;width: 70px;height: 100px;margin: 70px auto;border-radius: 50px;background: #e6e3da;background: linear-gradient(#e6e3da, #fff);border: 1px solid rgba(0,0,0,0.1);box-shadow: inset 0 7px 0 #fdfdfd,0 2px 3px rgba(170, 160, 140,.3);cursor: pointer;}"
".switch:before {content: '';position: absolute;top: -10px; bottom: -10px;left: -5px; right: -5px;z-index: -1;background: #f2f1ed;border-radius: inherit;box-shadow:0 1px 1px rgba(#aea391,0.2),0 3px 3px rgba(170, 160, 140, 0.4),inset 0 1px 0 rgba(255,255,255,0.8),0 0 5px rgba(170, 160, 140, 0.5);}"
".switch:after {content: "";position:absolute;width: 60px;height: 70px;border-radius: 50%;z-index: -1;left: 18px;top: 10px;background: linear-gradient(160deg, rgba(170, 160, 140, 0.7), rgba(170, 160, 140, 0));background: -webkit-linear-gradient(290deg, rgba(170, 160, 140, 0.75), rgba(170, 160, 140, 0));-webkit-filter: blur(1px);}"
"#switch {clip: rect(0 0 0 0);position: absolute;visibility: hidden;}"
"#switch:checked ~ .switch {background: linear-gradient(#f7f6f4, #fff);box-shadow:inset 0 -5px 0 #dbd3c8,0 6px 5px rgba(170, 160, 140, 0.75),3px 16px 5px rgba(170,160,140, 0.3);border-bottom: none;}"
"#switch:checked ~ .switch:after {display: none;}"
"a {color:#000000;}"
"#title {width:100%;text-align:center;font-size:40px;padding:10px;background:#336699;border-bottom: 2px solid #333333;}"
"#network {width:100%;text-align:center;border-bottom: 2px solid #333333;}"
".network-item {font-size:30px;}"
"#lights {width:100%;text-align:center;background:#666666;padding:30px;border-bottom: 2px solid #333333;}"
"#light-status {border: 2px solid #333333;width:200px;margin:0 auto;background: #cccccc;border-radius: 20px;}"
".light-item {font-size:30px;margin:0 10px 10px 10px;}"
"</style></head><body><div id='page'><div id='title'>Ligh Strip ESP8266</div>";
String htmlTemplateBottom = "</div></body></html>";
String htmlLight = "<div id='lights'><p id='light-label' class='light-item'>Spirit Hood</p><p id='light-status' class='light-item'><input type='checkbox' name='switch' id='switch' onclick='window.location = '/lightSwitch''><label class='switch' for='switch'></label></p></div>";
String htmlNetworkOne = "<div id='network'><p id='current-connection' class='network-item'>You are connected through the wifi network: <b></br>";
String buttonsOne = "</br></br><a style='width: 20%; height 30px; border: 1px solid blue; padding: 2% 2%; margin: 3% 3%;' href='/rainbow'>Rainbow</a><a style='width: 20%; height 30px; border: 1px solid blue; padding: 2% 2%; margin: 3% 3%;' href='/rainbowWithGlitter'>Glitter</a><a style='width: 20%; height 30px; border: 1px solid blue; padding: 2% 2%; margin: 3% 3%;' href='/confetti'>Confetti</a><a style='width: 20%; height 30px; border: 1px solid blue; padding: 2% 2%; margin: 3% 3%;' href='/sinelon'>Sinelon</a> </br>";
String buttonsTwo = "</br></br><a style='width: 20%; height 30px; border: 1px solid orange; padding: 2% 2%; margin: 3% 3%;' href='/juggle'>Juggle</a><a style='width: 20%; height 30px; border: 1px solid orange; padding: 2% 2%; margin: 3% 3%;' href='/bpm?beats=116'>BPM</a><a style='width: 20%; height 30px; border: 1px solid orange; padding: 2% 2%; margin: 3% 3%;' href='/solid?r=0&b=255=&g=100'>Solid</a><a style='width: 20%; height 30px; border: 1px solid orange; padding: 2% 2%; margin: 3% 3%;' href='/next'>Next</a>";
String buttonsThree = "</br><br></br><a style='width: 10%; height 30px; border: 1px solid red; padding: 2% 0%; margin: 3% 1%;' href='/increase?c=1&a=10'>Up Red</a><a style='width: 10%; height 30px; border: 1px solid red; padding: 2% 0%; margin: 3% 1%;' href='/decrease?c=1&a=10'>Down Red</a><a style='width: 10%; height 30px; border: 1px solid blue; padding: 2% 0%; margin: 3% 1%;' href='/increase?c=2&a=10'>Up Blue</a><a style='width: 10%; height 30px; border: 1px solid blue; padding: 2% 0%; margin: 3% 1%;' href='/decrease?c=2&a=10'>Down Blue</a><a style='width: 10%; height 30px; border: 1px solid green; padding: 2% 0%; margin: 3% 1%;' href='/increase?c=3&a=10'>Up Green</a><a style='width: 10%; height 30px; border: 1px solid green; padding: 2% 0%; margin: 3% 1%;' href='/decrease?c=3&a=10'>Down Green</a>";
String buttonsFour = "</br></br><br><a style='width: 10%; height 30px; border: 1px solid red; padding: 2% 0%; margin: 3% 1%;' href='/brightUp?a=50'>Brighten</a><a style='width: 10%; height 30px; border: 1px solid red; padding: 2% 0%; margin: 3% 1%;' href='/brightDown?a=50'>Dim</a><a style='padding: 2% 20%; height 30px; border: 1px solid black;' href='/solid?r=0&g=0=&b=0'>Off</a>";
void handleRoot() {
//Start page content
server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate");
server.sendHeader("Pragma", "no-cache");
server.sendHeader("Expires", "-1");
server.setContentLength(CONTENT_LENGTH_UNKNOWN);
server.send(200, "text/html", ""); // Empty content inhibits Content-length header so we have to close the socket ourselves.
//Put together HTML Template & Light Color
server.sendContent(htmlTemplateTop + htmlNetworkOne);
server.sendContent(String(softAP_ssid));
server.sendContent(buttonsOne);
server.sendContent(buttonsTwo);
server.sendContent(buttonsThree);
server.sendContent(buttonsFour);
server.sendContent(htmlTemplateBottom);
server.client().stop(); // Stop is needed because we sent no content length
}
void handleNotFound() {
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += ( server.method() == HTTP_GET ) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for ( uint8_t i = 0; i < server.args(); i++ ) {
message += " " + server.argName ( i ) + ": " + server.arg ( i ) + "\n";
}
server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate");
server.sendHeader("Pragma", "no-cache");
server.sendHeader("Expires", "-1");
server.send ( 404, "text/plain", message );
}
// List of patterns to cycle through. Each is defined as a separate function below.
typedef void (*SimplePatternList[])();
SimplePatternList gPatterns = { rainbow, rainbowWithGlitter, confetti, sinelon, bpm, juggle };
uint8_t gCurrentPatternNumber = 0; // Index number of which pattern is current
uint8_t gHue = 0; // rotating "base color" used by many of the patterns
void ambient_loop(int a, int b, int c){
for(int k = 0; k < NUM_LEDS; k++) {
leds[k].setRGB(a, c, b);FastLED.show();
}
}
void solid_loop(){
for(int k = 0; k < NUM_LEDS; k++) {
leds[k].setRGB(r, b, g);FastLED.show();
}
}
void loop()
{
//DNS
dnsServer.processNextRequest();
//HTTP
server.handleClient();
// Call the current pattern function once, updating the 'leds' array
if (homemade_method == false){
FastLED.setBrightness(BRIGHTNESS);
gPatterns[gCurrentPatternNumber]();
// send the 'leds' array out to the actual LED strip
FastLED.show();
// insert a delay to keep the framerate modest
FastLED.delay(1000/FRAMES_PER_SECOND);
// do some periodic updates
EVERY_N_MILLISECONDS( 20 ) { gHue++; } // slowly cycle the "base color" through the rainbow
}
// EVERY_N_SECONDS( 100 ) { nextPattern(); } // change patterns periodically
}
#define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
void nextPattern()
{
// add one to the current pattern number, and wrap around at the end
gCurrentPatternNumber = (gCurrentPatternNumber + 1) % ARRAY_SIZE( gPatterns);
}
void rainbow_call()
{
homemade_method = false;
gCurrentPatternNumber = (1-1) % ARRAY_SIZE( gPatterns);
// FastLED's built-in rainbow generator
handleRoot();
}
void rainbowWithGlitter_call()
{
homemade_method = false;
gCurrentPatternNumber = (2-1) % ARRAY_SIZE( gPatterns);
// built-in FastLED rainbow, plus some random sparkly glitter
handleRoot();
}
void confetti_call()
{
homemade_method = false;
gCurrentPatternNumber = (3-1) % ARRAY_SIZE( gPatterns);
// random colored speckles that blink in and fade smoothly
handleRoot();;
}
void sinelon_call()
{
homemade_method = false;
gCurrentPatternNumber = (4-1) % ARRAY_SIZE( gPatterns);
// a colored dot sweeping back and forth, with fading trails
handleRoot();
}
void bpm_call()
{
homemade_method = false;
gCurrentPatternNumber = (5-1) % ARRAY_SIZE( gPatterns);
// built-in FastLED rainbow, plus some random sparkly glitter
handleRoot();
}
void juggle_call()
{
homemade_method = false;
gCurrentPatternNumber = (6-1) % ARRAY_SIZE( gPatterns);
// a colored dot sweeping back and forth, with fading trails
handleRoot();
}
void next_call()
{
homemade_method = false;
nextPattern();
handleRoot();
}
void rainbow()
{
// FastLED's built-in rainbow generator
fill_rainbow( leds, NUM_LEDS, gHue, 7);
}
void rainbowWithGlitter()
{
// built-in FastLED rainbow, plus some random sparkly glitter
rainbow();
addGlitter(80);
}
void addGlitter( fract8 chanceOfGlitter)
{
if( random8() < chanceOfGlitter) {
leds[ random16(NUM_LEDS) ] += CRGB::White;
}
}
void confetti()
{
// random colored speckles that blink in and fade smoothly
fadeToBlackBy( leds, NUM_LEDS, 10);
int pos = random16(NUM_LEDS);
leds[pos] += CHSV( gHue + random8(64), 200, 255);
}
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)
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;
}
}