-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgirland.c
700 lines (623 loc) · 25.4 KB
/
girland.c
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
/**
* Example of ws2812_i2s library usage.
*
* This example shows light that travels in circle with fading tail.
* As ws2812_i2s library using hardware I2S the output pin is GPIO3 and
* can not be changed.
*
* This sample code is in the public domain.,
*/
#include "espressif/esp_common.h"
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "esp/uart.h"
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include "esp8266.h"
#include <math.h>
#include <lwip/api.h>
#include "ws2812_i2s/ws2812_i2s.h"
#include "dhcpserver.h"
// #include "spiffs.h"
// #include "esp_spiffs.h"
#define HEIGHT 9
#define WIDTH 17
#define LED_NUMBER 153
#define RENDER_FREQ 30
#define SNOW_COLOR 0xB900FF
#define FORWARD true
#define BACKWARD false
#define FAST 10
#define MIDDLE 5
#define SLOW 2
#define NOT_MIRROR false
#define MIRROR true
#define IS_RAINBOW_VERTICAL true
#define IS_RAINBOW_HORISONTAL false
#define TORNADO_TAIL_LENGTH 8
#define SNOW_LIFE 30*2 // 5sec;
#define SNOW_NUMBER 15
#define RAINBOW_MODE 0
#define WAVE_MODE 1
#define TAPES_MODE 2
#define SNOW_MODE 3
#define TORNADO_MODE 4
#define DISABLE_MODE -1
#define PAGE_BUFFER_LENGTH 3400
int currentMode = RAINBOW_MODE;
int currentSpeed = FAST;
bool currentDirection = FORWARD;
int currentSnowNumber = SNOW_NUMBER;
int currentSnowLife = SNOW_LIFE;
uint32_t currentColor = SNOW_COLOR;
bool currentMirror = NOT_MIRROR;
int currentTornadoTailLength = TORNADO_TAIL_LENGTH;
bool rainbowAlign = IS_RAINBOW_HORISONTAL;
int currentHue = 196;
int currentTailHue = 16;
int currentTailLength = 8;
bool connectionFault = true;
QueueHandle_t render_queue;
typedef struct {
double h; // angle in degrees
double s; // a fraction between 0 and 1
double v; // a fraction between 0 and 1
} hsv;
typedef struct {
int maxAge;
int age;
int x;
int y;
} snow_item;
ws2812_pixel_t hexToRGB (uint32_t hex) {
ws2812_pixel_t data;
data.red = (hex & 0xFF0000) >> 16;
data.green = (hex & 0x00FF00) >> 8;
data.blue = (hex & 0x0000FF) >> 0;
return data;
}
bool getAuthData();
ws2812_pixel_t hsv2rgb(hsv in)
{
double hh, p, q, t, ff;
long i;
ws2812_pixel_t out;
if(in.s <= 0.0) { // < is bogus, just shuts up warnings
out.red = (int)(in.v * 255);
out.green = (int)(in.v * 255);
out.blue = (int)(in.v * 255);
return out;
}
hh = in.h;
if(hh >= 360.0) hh = 0.0;
hh /= 60.0;
i = (long)hh;
ff = hh - i;
p = in.v * (1.0 - in.s);
q = in.v * (1.0 - (in.s * ff));
t = in.v * (1.0 - (in.s * (1.0 - ff)));
switch(i) {
case 0:
out.red = (int)(in.v * 255);
out.green = (int)(t * 255);
out.blue = (int)(p * 255);
break;
case 1:
out.red = (int)(q * 255);
out.green = (int)(in.v * 255);
out.blue = (int)(p * 255);
break;
case 2:
out.red = (int)(p * 255);
out.green = (int)(in.v * 255);
out.blue = (int)(t * 255);
break;
case 3:
out.red = (int)(p * 255);
out.green = (int)(q * 255);
out.blue = (int)(in.v * 255);
break;
case 4:
out.red = (int)(t * 255);
out.green = (int)(p * 255);
out.blue = (int)(in.v * 255);
break;
case 5:
default:
out.red = (int)(in.v * 255);
out.green = (int)(p * 255);
out.blue = (int)(q * 255);
break;
}
return out;
}
void generateRainbow(int frame, ws2812_pixel_t* pixels, bool vertical, int speed, bool direction) {
int ledsInGradient = vertical ? HEIGHT : WIDTH;
hsv data;
for (int x = 0; x < WIDTH; x++)
{
int locX = direction ? x : WIDTH - x - 1;
int pos = 0;
if(!vertical) pos = (int)(((double)frame)*speed + (double)locX*360/ledsInGradient) % 360;
for (int y = 0; y < HEIGHT; y++)
{
int locY = (locX%2) ? y : HEIGHT - y - 1;
locY = direction ? locY : HEIGHT - locY - 1;
if(vertical) pos = (int)(((double)frame)*speed + (double)locY*360/ledsInGradient) % 360;
data.h = (double)pos;
data.s = 1.0;
data.v = 1.0;
pixels[x * HEIGHT + y] = hsv2rgb(data);
}
}
}
void generateWave(int frame, ws2812_pixel_t* pixels, bool direction, int speed, bool mirror) {
for (int x = 0; x < WIDTH; x++)
{
int locX = mirror ? x : WIDTH - x - 1;
for (int y = 0; y < HEIGHT; y++)
{
int locY = (x%2) ? y : HEIGHT - y - 1;
locY = direction ? HEIGHT - locY - 1 : locY;
if(((y + frame/(11 - speed))%HEIGHT >= (x/3 + 3)) && ((y + frame/(11-speed))%HEIGHT < (x/3 + 6))) {
hsv data;
data.h = currentHue;
data.s = 1;
data.v = 1;
pixels[locX * HEIGHT + locY] = hsv2rgb(data);
} else {
pixels[locX * HEIGHT + locY] = hexToRGB(0x000000);
}
}
}
}
void generateTapes(int frame, ws2812_pixel_t* pixels, int speed) {
for (int x = 0; x < WIDTH; x++) {
// int locX = mirror ? x : WIDTH - x - 1;
for (int y = 0; y < HEIGHT; y++)
{
int locY = (x%2) ? y : HEIGHT - y - 1;
// locY = direction ? HEIGHT - locY - 1 : locY;
if(y == (int)(x/3 + 1 + frame/(12 - speed))%HEIGHT || y == (int)(HEIGHT - x/3 + frame/(12 - speed))%HEIGHT) {
hsv data;
data.h = currentHue;
data.s = 1;
data.v = 1;
pixels[x * HEIGHT + locY] = hsv2rgb(data);
} else {
pixels[x * HEIGHT + locY] = hexToRGB(0x000000);
}
}
}
}
snow_item snowArr[SNOW_NUMBER];
bool vacantPos[SNOW_NUMBER] = { true, true, true, true, true, true, true };
uint32_t max = 0;
void generateSnow(ws2812_pixel_t* pixels) {
printf("RND: %d\n", max);
int vacance = -1;
for (int i = 0; i < SNOW_NUMBER; i++)
{
if(vacantPos[i]){
vacance = i;
break;
}
}
if(vacance != -1) {
if((int)((uint32_t)random())%10 > 4) {
;
} else {
snow_item item;
item.x = (int)((uint32_t)random())%WIDTH;
item.y = (int)((uint32_t)random())%HEIGHT;
item.age = SNOW_LIFE + (int)((uint32_t)random())%35;
item.maxAge = item.age;
snowArr[vacance] = item;
vacantPos[vacance] = false;
}
}
for (int x = 0; x < WIDTH; x++)
{
for (int y = 0; y < HEIGHT; y++)
{
pixels[x * HEIGHT + y] = hexToRGB(0x000000);
}
}
for (int i = 0; i < SNOW_NUMBER; i++)
{
if(!vacantPos[i]) {
hsv data;
int maxAge = snowArr[i].maxAge;
float deca = snowArr[i].maxAge/5;
int age = snowArr[i].age;
int maxValueAge = (int)(maxAge - deca);
if(age > maxValueAge) data.v = (maxAge - age) / deca;
else data.v = (float)age/maxValueAge;
data.h = currentHue;
data.s = 1;
pixels[snowArr[i].x * HEIGHT + snowArr[i].y] = hsv2rgb(data);
if(age <= 0) vacantPos[i] = true;
else snowArr[i].age--;
}
}
}
void generateTornado (int frame, ws2812_pixel_t* pixels ) {
for (int x = 0; x < WIDTH; x++) {
for (int y = 0; y < HEIGHT; y++) {
pixels[x * HEIGHT + y] = hexToRGB(0x000000);
}
}
int currentPos = frame%LED_NUMBER;
int currentX = currentPos%WIDTH;
int currentY = (int)currentPos/WIDTH;
if(currentX%2) currentY = HEIGHT - currentY - 1;
for (int i = 0; i < currentTailLength; i++) {
int tailPos = currentPos - 1 - i;
if(tailPos < 0) tailPos = LED_NUMBER + tailPos;
int tailX = tailPos%WIDTH;
int tailY = (int)tailPos/WIDTH;
if(tailX%2) tailY = HEIGHT - tailY - 1;
int currTailPos = (tailX * HEIGHT + tailY) % LED_NUMBER;
hsv data;
data.h = currentTailHue;
data.s = 1;
data.v = (float)1/(i*i*0.5+1);
pixels[currTailPos] = hsv2rgb(data);
}
hsv data;
data.s = 1;
data.v = 1;
data.h = currentHue;
pixels[currentX * HEIGHT + currentY] = hsv2rgb(data);
}
void shutdown(ws2812_pixel_t* pixels) {
for (int x = 0; x < WIDTH; x++) {
for (int y = 0; y < HEIGHT; y++) {
pixels[x * HEIGHT + y] = hexToRGB(0x000000);
}
}
}
void render(int frame, ws2812_pixel_t* pixels) {
switch (currentMode) {
case RAINBOW_MODE:
generateRainbow(frame, pixels, rainbowAlign, currentSpeed, currentDirection);
break;
case WAVE_MODE:
generateWave(frame, pixels, currentDirection, currentSpeed, currentMirror);
break;
case TAPES_MODE:
generateTapes(frame, pixels, currentSpeed);
break;
case SNOW_MODE:
generateSnow(pixels);
break;
case TORNADO_MODE:
generateTornado(frame, pixels);
break;
case DISABLE_MODE:
shutdown(pixels);
break;
default:
generateRainbow(frame, pixels, rainbowAlign, currentSpeed, currentDirection);
break;
}
}
static void renderTask(void *pvParameters)
{
ws2812_pixel_t pixels[LED_NUMBER];
int head_index = 0;
ws2812_i2s_init(LED_NUMBER, PIXEL_RGB);
memset(pixels, 0, sizeof(ws2812_pixel_t) * LED_NUMBER);
int frame = 0;
while (1) {
int index;
if(xQueueReceive(render_queue, &index, portMAX_DELAY)){
render(frame, &pixels[0]);
ws2812_i2s_update(pixels, PIXEL_RGB);
frame++;
}
}
}
void timerINterruptHandler (void *arg) {
int index = 1;
xQueueSendFromISR(render_queue, &index, NULL);
}
void connect () {
sdk_wifi_set_opmode(STATION_MODE);
connectionFault = false;
sdk_wifi_station_set_auto_connect(1);
struct sdk_station_config config = {
.ssid = "Jesus",
.password = "8715387153",
};
sdk_wifi_station_set_config(&config);
// printf("SSID: %s, PWD: %s", config.ssid, config.password);
}
void startAp() {
sdk_wifi_set_opmode(SOFTAP_MODE);
struct ip_info ap_ip;
IP4_ADDR(&ap_ip.ip, 192, 168, 0, 1);
IP4_ADDR(&ap_ip.gw, 0, 0, 0, 0);
IP4_ADDR(&ap_ip.netmask, 255, 255, 0, 0);
sdk_wifi_set_ip_info(1, &ap_ip);
struct sdk_softap_config ap_config = { .ssid = "girland", .ssid_hidden = 0, .channel = 3, .ssid_len = strlen("girland"), .authmode =
AUTH_WPA_WPA2_PSK, .password = "12345678", .max_connection = 3, .beacon_interval = 100, };
sdk_wifi_softap_set_config(&ap_config);
}
// void removeConfig() {
// printf("Remooving config");
// SPIFFS_remove(&fs, "ssid.txt");
// SPIFFS_remove(&fs, "pwd.txt");
// }
// void onConnFault() {
// connectionFault = true;
// removeConfig();
// sdk_system_restart();
// }
void checkConnectionWifi(uint8_t stationStatus) {
switch (stationStatus)
{
case STATION_CONNECTING :
printf(".");
break;
case STATION_WRONG_PASSWORD:
printf("WRONG PASSWORD\n");
// onConnFault();
break;
case STATION_NO_AP_FOUND:
printf("NO AP FOUND\n");
// onConnFault();
break;
case STATION_CONNECT_FAIL:
printf("CONNECT FAIL\n");
// onConnFault();
break;
case STATION_GOT_IP:
printf("GOT IP\n");
break;
default:
printf("\nStatus undefined: %d\n", stationStatus);
// sdk_system_restart();
break;
}
}
void mainTask (void *parameters) {
uint8_t stationStatus = sdk_wifi_station_get_connect_status();
while (1)
{
if(!connectionFault) {
stationStatus = sdk_wifi_station_get_connect_status();
if(stationStatus != STATION_GOT_IP) {
checkConnectionWifi(stationStatus);
}
}
vTaskDelay(10000 / portTICK_PERIOD_MS);
}
}
void getWifiStingsPage(char *buf) {
const char *webpage = "<html><head><style></style></head><body><div class=\"root\"><form method=\"post\" id=\"settings\"><div><label>SSID<input type=\"text\" name=\"SSID\" placeholder=\"SSID\"></label></div><div><label>PASSWORD<input type=\"text\"nname=\"PWD\" placeholder=\"PASSWORD\"></label></div><button>Send</button></form></div></body><script>const form=document.getElementById('settings');form.addEventListener('submit',(e)=>{e.preventDefault();fetch(`/wifiset/${e.target[0].value},${e.target[1].value}`);});</script></html>";
snprintf(buf, PAGE_BUFFER_LENGTH, webpage);
}
void getMainPage(char *buf) {
const char *webpage = "<html><head> <title>HTTP Server</title> <style> div.main { font-family: Arial; padding: 0.01em 16px; box-shadow: 2px 2px 1px 1px #d2d2d2; background-color: #f1f1f1; } #controls>div { padding: 10px; border-bottom: 1px solid rgba(0, 0, 0, 0.3); } @media screen and (max-device-width: 500px) { html { font-size: 50px; } html button { font-size: 50px; width: 400px; /* height: 60px; */ } } input[type=\"button\"]{ color: #fff; background-color: #007bff; border-color: #007bff; font-weight: 400; text-align: center; border: 1px solid transparent; padding: .375rem .75rem; margin:7px 12px 5px; font-size: 1rem; line-height: 1.5; border-radius: .25rem; transition: color .15s ease-in-out, background-color .15s ease-in-out,border-color .15s ease-in-out,box-shadow .15s ease-in-out; } input[type=\"button\"]:hover{background-color: #0069d9;border-color: #0062cc;} input[type=\"button\"]:active{background-color: #0062cc;border-color: #005cbf;} input[type=\"button\"]:focus{background-color: #0069d9;border-color: #0062cc;box-shadow: 0 0 0 0.2rem rgba(38,143,255,.5)} </style></head><body> <div class='main'> <h3>Garland Control</h3> <p>Free heap: <span id='heap'></span></p> <div id='controls'> </div> </div></body><script> const g = [ { a: ['all'], n: 'Mode set', p: '/mode', c: [ { n: 'RAINBOW', t: 'button', }, { n: 'WAVE', t: 'button', }, { n: 'TAPES', t: 'button', }, { n: 'SNOW', t: 'button', }, { n: 'TORNADO', t: 'button', }, { n: 'DISABLE', t: 'button', } ] }, { a: [0, 1], n: 'Direction set', p: '/dir', c: [ { n: 'FORWARD', t: 'button', }, { n: 'BACKWARD', t: 'button', }, ] }, { a: [0, 1, 2], n: 'Speed set', p: '/speed', c: [ { n: 'FAST', t: 'button', }, { n: 'MIDDLE', t: 'button', }, { n: 'SLOW', t: 'button', }, ] }, { a: [0], n: 'Align set', p: '/align', c: [ { n: 'VERTICAL', t: 'button', }, { n: 'HORISONTAL', t: 'button', } ] }, { a: [1], n: 'Mirror set', p: '/mirror', c: [ { n: 'MIRROR', t: 'button', }, { n: 'NOT_MIRROR', t: 'button', } ] }, { a: [1,2,3,4], n: 'Main color set', p: '/maincolor', c: [ { n: 'MAIN_COLOR', t: 'range', f: 'ch', min: 0, max: 360, }, ] }, { a: [4], n: 'Tail set', p: '/tail', c: [ { n: 'COLOR', t: 'range', f: 'th', min: 0, max:360, }, { n: 'LENGTH', t: 'range', f: 'tl', min: 0, max: 100, }, ] }, ]; function s(p) { fetch(p).then(r => r.json().then(render)); }; function render(state) { console.log(state); document.getElementById('heap').innerText=state.fh; const root = document.getElementById('controls'); root.innerHTML = ''; g.forEach((cg => { if (cg.a.includes(state.m) || cg.a.includes('all')) { const gn = document.createElement('div'); gn.innerHTML = `<span>${cg.n}: </span><br/>`; cg.c.forEach(c=>{ const e = document.createElement('input'); e.setAttribute('type', c.t); if(c.t==='button') { e.addEventListener('click', ()=>s(`${cg.p}/${c.n}`)); gn.appendChild(e); e.setAttribute('value', c.n); } else if(c.t==='range') { const cr = document.createElement('div'); cr.appendChild(e); cr.innerText=c.n; gn.appendChild(cr); e.setAttribute('min', c.min); e.setAttribute('max', c.max); e.value = state[c.f]; e.addEventListener('input', (e)=>s(`${cg.p}/${c.n}/${String(e.target.value).padStart(3,0)}`)); } gn.appendChild(e); }); root.appendChild(gn); } })); }; s('/status');</script></html>";
snprintf(buf, PAGE_BUFFER_LENGTH, webpage);
}
// void saveAuthData(char*ssid, char*pwd) {
// char *ssidbuf = ssid;
// char *pwdbuf = pwd;
// printf("SVE SSID %s, PWD %s", ssidbuf, pwdbuf);
// int fd = open("ssid.txt", O_WRONLY|O_CREAT, 0);
// if (fd < 0) {
// printf("Error opening file\n");
// return;
// }
// int written = write(fd, ssidbuf, 0xF);
// printf("Written %d bytes\n", written);
// fd = open("pwd.txt", O_WRONLY|O_CREAT, 0);
// if (fd < 0) {
// printf("Error opening file\n");
// return;
// }
// written = write(fd, pwdbuf, 0xF);
// printf("Written %d bytes\n", written);
// close(fd);
// }
void httpd_task(void *pvParameters)
{
ip_addr_t first_client_ip;
IP4_ADDR(&first_client_ip, 192, 168, 0, 2);
dhcpserver_start(&first_client_ip, 4);
struct netconn *client = NULL;
struct netconn *nc = netconn_new(NETCONN_TCP);
if (nc == NULL) {
printf("Failed to allocate socket.\n");
vTaskDelete(NULL);
}
netconn_bind(nc, IP_ADDR_ANY, 80);
netconn_listen(nc);
char buf[PAGE_BUFFER_LENGTH];
while (1) {
err_t err = netconn_accept(nc, &client);
if (err == ERR_OK) {
struct netbuf *nb;
if ((err = netconn_recv(client, &nb)) == ERR_OK) {
void *data;
char *subBuff;
subBuff = malloc(4);
u16_t len;
netbuf_data(nb, &data, &len);
/* check for a GET request */
if (!strncmp(data, "GET ", 4)) {
char uri[32];
const int max_uri_len = 32;
char *sp1, *sp2;
/* extract URI */
sp1 = data + 5;
sp2 = memchr(sp1, ' ', max_uri_len);
int len = sp2 - sp1;
memcpy(uri, sp1, len);
uri[len] = '\0';
printf("uri: %s\n", uri);
if (strstr(uri, "mode")) {
if(strstr(uri, "RAINBOW")) {
currentMode = RAINBOW_MODE;
} else if (strstr(uri, "WAVE")) {
currentMode = WAVE_MODE;
} else if (strstr(uri, "TAPES")) {
currentMode = TAPES_MODE;
} else if (strstr(uri, "SNOW")) {
currentMode = SNOW_MODE;
} else if (strstr(uri, "TORNADO")) {
currentMode = TORNADO_MODE;
} else if (strstr(uri, "DISABLE")) {
currentMode = DISABLE_MODE;
}
} else if (strstr(uri, "dir")) {
if(strstr(uri, "FORWARD")) {
currentDirection = FORWARD;
} else if (strstr(uri, "BACKWARD")) {
currentDirection = BACKWARD;
}
} else if (strstr(uri, "speed")) {
if(strstr(uri, "FAST")) {
currentSpeed = FAST;
} else if (strstr(uri, "MIDDLE")) {
currentSpeed = MIDDLE;
} else if (strstr(uri, "SLOW")) {
currentSpeed = SLOW;
}
} else if (strstr(uri, "mirror")) {
if (strstr(uri, "NOT_MIRROR")) {
currentMirror = NOT_MIRROR;
} else if(strstr(uri, "MIRROR")) {
currentMirror = MIRROR;
}
} else if (strstr(uri, "align")) {
if(strstr(uri, "VERTICAL")) {
rainbowAlign = IS_RAINBOW_VERTICAL;
} else if (strstr(uri, "HORISONTAL")) {
rainbowAlign = IS_RAINBOW_HORISONTAL;
}
} else if (strstr(uri, "maincolor")) {
int colorStart = memchr(uri+12, '/', max_uri_len) + 1;
memcpy(subBuff, colorStart, 3);
subBuff[3] = '\0';
sscanf(subBuff, "%d", ¤tHue);
} else if (strstr(uri, "tail")) {
if(strstr(uri, "COLOR")) {
int colorStart = memchr(uri+8, '/', max_uri_len) + 1;
memcpy(subBuff, colorStart, 3);
subBuff[3] = '\0';
sscanf(subBuff, "%d", ¤tTailHue);
} else if(strstr(uri, "LENGTH")) {
int colorStart = memchr(uri+8, '/', max_uri_len) + 1;
memcpy(subBuff, colorStart, 3);
subBuff[3] = '\0';
sscanf(subBuff, "%d", ¤tTailLength);
}
// } else if (strstr(uri, "wifiset")) {
// char * param1Addr = memchr(uri+5, '/', 32) + 1;
// char * dividerAddr = memchr(uri+5, ',', 32);
// char * end = memchr(uri+5, '\0', 32)+1;
// char SSID[32];
// char PWD[32];
// memcpy(SSID, param1Addr, dividerAddr-param1Addr);
// SSID[dividerAddr-param1Addr] = '\0';
// memcpy(PWD, dividerAddr + 1, end - dividerAddr-1);
// PWD[end - dividerAddr-1] = '\0';
// saveAuthData(SSID, PWD);
// vTaskDelay(500);
// getAuthData();
// // sdk_system_restart();
}
if(strstr(uri, "status")) {
snprintf(buf, sizeof(buf), "{\"m\":%d,\"fh\":%d,\"ch\":%d,\"th\":%d,\"tl\":%d}", currentMode, (int) xPortGetFreeHeapSize(), currentHue, currentTailHue, currentTailLength);
netconn_write(client, buf, strlen(buf), NETCONN_COPY);
} else if(strlen(uri) < 3) {
connectionFault ? getWifiStingsPage(&buf) : getMainPage(&buf);
printf("Buffer: \n %s\n", buf);
netconn_write(client, buf, strlen(buf), NETCONN_COPY);
} else {
snprintf(buf, sizeof(buf), "{\"m\":%d,\"fh\":%d,\"ch\":%d,\"th\":%d,\"tl\":%d}", currentMode, (int) xPortGetFreeHeapSize(), currentHue, currentTailHue, currentTailLength);
netconn_write(client, buf, strlen(buf), NETCONN_COPY);
}
}
free(subBuff);
}
netbuf_delete(nb);
}
printf("Closing connection\n");
netconn_close(client);
netconn_delete(client);
}
}
// bool getAuthData() {
// int fileLength = 0xF;
// char ssidbuf[fileLength];
// char pwdbuf[fileLength];
// spiffs_file fd = SPIFFS_open(&fs, "ssid.txt", SPIFFS_RDONLY, 0);
// if (fd < 0) {
// printf("Error opening file\n");
// return false;
// }
// int read_bytes = SPIFFS_read(&fs, fd, ssidbuf, fileLength);
// ssidbuf[read_bytes] = '\0';
// printf("Ssid: %s\n", ssidbuf);
// SPIFFS_close(&fs, fd);
// fd = SPIFFS_open(&fs, "pwd.txt", SPIFFS_RDONLY, 0);
// if (fd < 0) {
// printf("Error opening file\n");
// return false;
// }
// if(read_bytes < 2){
// return false;
// }
// read_bytes = SPIFFS_read(&fs, fd, pwdbuf, fileLength);
// pwdbuf[read_bytes] = '\0';
// printf("Pwd: %s\n", pwdbuf);
// SPIFFS_close(&fs, fd);
// if(read_bytes < 2){
// return false;
// } else {
// struct sdk_station_config config;
// memcpy(config.ssid, ssidbuf, strlen(ssidbuf));
// config.ssid[strlen(ssidbuf)] = '\0';
// memcpy(config.password, pwdbuf, strlen(pwdbuf));
// config.ssid[strlen(pwdbuf)] = '\0';
// printf("Config at getAuthData: %s, %s\n", config.ssid, config.password);
// sdk_wifi_station_set_config(&config);
// return true;
// }
// }
// void chaekSettings() {
// esp_spiffs_init();
// if (esp_spiffs_mount() != SPIFFS_OK) {
// printf("Error mount SPIFFS\n");
// }
// if(getAuthData()) {
// connect();
// } else {
// startAp();
// }
// }
void user_init(void)
{
uart_set_baud(0, 115200);
struct ip_info ipInfo;
/* required to call wifi_set_opmode before station_set_config */
render_queue = xQueueCreate(10, sizeof(int));
// sdk_wifi_set_opmode(STATIONAP_MODE);
connect();
// chaekSettings();
timer_set_interrupts(FRC1, false);
timer_set_run(FRC1, false);
_xt_isr_attach(INUM_TIMER_FRC1, timerINterruptHandler, NULL);
timer_set_frequency(FRC1, RENDER_FREQ);
timer_set_interrupts(FRC1, true);
timer_set_run(FRC1, true);
xTaskCreate(&renderTask, "ws2812_i2s", 2048, NULL, 10, NULL);
xTaskCreate(&mainTask, "main-task", 1024, NULL, 10, NULL);
xTaskCreate(&httpd_task, 'HTTP_DEAMON', 4096, NULL, 2, NULL);
}