智能桌面改造

前言

一直对改造桌面有想法,放寒假很闲,于是采购了一片8266正式开始改造。(练手的小玩意)
1.为显示器加了一圈氛围灯,改写代码可实现流光溢彩的效果。
2.电脑实现远程启动,小爱同学启动(局域网网卡唤醒)。

代码

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
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762



//无线局域网音乐律动 远程启动电脑
#define BLINKER_WIFI
#define BLINKER_MIOT_LIGHT
#define BLINKER_MIOT_OUTLET
//#define BLINKER_ESP_SMARTCONFIG
#define BLINKER_WITHOUT_SSL
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <FastLED.h>
#include <Blinker.h>
char auth[] = "";
char ssid[] = "";
char pswd[] = "";

WiFiUDP Udp;
bool oState = false;

unsigned int localUdpPort = 6001;
char packetBuffer[256];
unsigned long triger_time;
BlinkerButton Button1("ButtonKey1");
BlinkerButton Button2("ButtonKey2");
BlinkerButton Button3("ButtonKey3");
BlinkerButton Button4("ButtonKey4");
BlinkerButton Button5("ButtonKey5");
BlinkerSlider Slider1("SliderKey");
BlinkerSlider Slider2("SliderKey2");
BlinkerRGB RGBWS2812("RGBKey");
int openState = 2;
unsigned int nowState = 2;
int freq_flash = 15;
uint8_t colorR = 250, colorG = 0, colorB = 0, colorW = 250;
bool wsState;
uint8_t wsMode = BLINKER_CMD_MIOT_DAY;
int brt_set = 100;

#include <Adafruit_NeoPixel.h>
#define PIN 5
#define NUMPIXELS 60
int pre_si = 0;
int drop_dot = NUMPIXELS - 1;
unsigned long drop_time;
unsigned long lift_time;
unsigned long up_time;
unsigned long down_time;
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
CRGBArray<NUMPIXELS> leds;
void pixelShow()
{

pixels.setBrightness(colorW);

for (int i = 0; i < NUMPIXELS; i++)
{
pixels.setPixelColor(i, colorR, colorG, colorB);

}
pixels.show();

}
void slider1_callback(int32_t value)
{

Blinker.vibrate();
BLINKER_LOG("get slider value: ", value);
freq_flash = value;
}
void slider2_callback(int32_t value)
{

Blinker.vibrate();
BLINKER_LOG("get slider value: ", value);


}
void ws2812_callback(uint8_t r_value, uint8_t g_value, uint8_t b_value, uint8_t bright_value)
{
button_clear();
BLINKER_LOG("R value: ", r_value);
BLINKER_LOG("G value: ", g_value);
BLINKER_LOG("B value: ", b_value);
BLINKER_LOG("Rrightness value: ", bright_value);
colorR = r_value;
colorG = g_value;
colorB = b_value;
colorW = bright_value;
openState = 6;
//Text1.print("灯光模式:" , "单颜色");
}
void button_clear()
{
Button1.print("off");
Button2.print("off");
Button3.print("off");
Button4.print("off");
Button5.print("off");
Button1.color("#008000");
Button2.color("#008000");
Button3.color("#008000");
Button4.color("#008000");
Button5.color("#008000");
}


void button1_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
if (state == BLINKER_CMD_ON) {
BLINKER_LOG("日光模式开启");
button_clear();
Button1.color("#DC143C");
Button1.print("on");
openState = 1;
}
else if (state == BLINKER_CMD_OFF) {
BLINKER_LOG("日光模式关闭!");
button_clear();
Button1.color("#008000");
openState = 0;
}
}
void button2_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
if (state == BLINKER_CMD_ON) {
BLINKER_LOG("月光模式开启");
button_clear();
Button2.print("on");
Button2.color("#DC143C");
openState = 2;
}
else if (state == BLINKER_CMD_OFF) {
BLINKER_LOG("月光模式关闭!");
button_clear();
openState = 0;
}
}
void button3_callback(const String & state)
{

BLINKER_LOG("get button state: ", state);

if (state == BLINKER_CMD_ON) {
BLINKER_LOG("温馨模式开启");
button_clear();
Button3.print("on");
Button3.color("#DC143C");
openState = 3;
brt_set = colorW;
}
else if (state == BLINKER_CMD_OFF) {
BLINKER_LOG("温馨模式关闭!");
button_clear();
openState = 0;
}

}
void button4_callback(const String & state)
{

BLINKER_LOG("get button state: ", state);

if (state == BLINKER_CMD_ON) {
BLINKER_LOG("电脑模式开启");

BlinkerMIOT.print();
pcawaking();

openState = 4;
}
else if (state == BLINKER_CMD_OFF) {
BLINKER_LOG("电脑模式关闭!");

BlinkerMIOT.print();
openState = 0;
}

}
void button5_callback(const String & state)
{

BLINKER_LOG("get button state: ", state);

if (state == BLINKER_CMD_ON) {
BLINKER_LOG("电视模式开启");

button_clear();
Button5.print("on");
Button5.color("#DC143C");
openState = 5;
}
else if (state == BLINKER_CMD_OFF) {
BLINKER_LOG("电视模式关闭!");

button_clear();
openState = 0;

}

}

void dataRead(const String & data)
{
BLINKER_LOG("Blinker readString: ", data);

Blinker.vibrate();

uint32_t BlinkerTime = millis();

Blinker.print("millis", BlinkerTime);
}
uint32_t getColor()
{

uint32_t color = colorR << 16 | colorG << 8 | colorB;

return color;
}
//小爱同学
void miotPowerState(const String & state)
{
BLINKER_LOG("need set power state: ", state);

if (state == BLINKER_CMD_ON) {

BlinkerMIOT.powerState("on");
BlinkerMIOT.print();
wsState = true;
if (colorW == 0) colorW = 255;
openState = 6;
}
else if (state == BLINKER_CMD_OFF) {
BlinkerMIOT.powerState("off");
BlinkerMIOT.print();
wsState = false;
openState = 0;
}
}

void miotColor(int32_t color)
{
BLINKER_LOG("need set color: ", color);

colorR = color >> 16 & 0xFF;
colorG = color >> 8 & 0xFF;
colorB = color & 0xFF;

BLINKER_LOG("colorR: ", colorR, ", colorG: ", colorG, ", colorB: ", colorB);
openState = 6;
BlinkerMIOT.color(color);
BlinkerMIOT.print();
}

void miotMode(uint8_t mode)
{
BLINKER_LOG("need set mode: ", mode);

if (mode == BLINKER_CMD_MIOT_DAY) {
button_clear();
Button1.print("on");
Button1.color("#DC143C");
openState = 1;

}
else if (mode == BLINKER_CMD_MIOT_NIGHT) {
button_clear();
Button2.print("on");
Button2.color("#DC143C");
openState = 2;
}
else if (mode == BLINKER_CMD_MIOT_COLOR) {
button_clear();
openState = 6;

}
else if (mode == BLINKER_CMD_MIOT_WARMTH) {
button_clear();
Button3.print("on");
Button3.color("#DC143C");
openState = 3;
}
else if (mode == BLINKER_CMD_MIOT_TV) {
button_clear();
Button5.print("on");
Button5.color("#DC143C");
openState = 5;
}
else if (mode == BLINKER_CMD_MIOT_READING) {
}
else if (mode == BLINKER_CMD_MIOT_COMPUTER) {
pcawaking();
button_clear();
Button1.print("on");
Button1.color("#DC143C");
openState = 4;
}

wsMode = mode;

BlinkerMIOT.mode(mode);
BlinkerMIOT.print();
}

void miotBright(const String & bright)
{
BLINKER_LOG("need set brightness: ", bright);

colorW = bright.toInt();

BLINKER_LOG("now set brightness: ", colorW);

pixelShow();

BlinkerMIOT.brightness(colorW);
BlinkerMIOT.print();
openState = 6;
}

void miotColoTemp(int32_t colorTemp)
{
BLINKER_LOG("need set colorTemperature: ", colorTemp);

BlinkerMIOT.colorTemp(colorTemp);
BlinkerMIOT.print();
}

void miotQuery(int32_t queryCode)
{
BLINKER_LOG("MIOT Query codes: ", queryCode);

switch (queryCode)
{
case BLINKER_CMD_QUERY_ALL_NUMBER :
BLINKER_LOG("MIOT Query All");
BlinkerMIOT.powerState(wsState ? "on" : "off");
BlinkerMIOT.color(0);
BlinkerMIOT.mode(0);
BlinkerMIOT.colorTemp(1000);
BlinkerMIOT.brightness(1);
BlinkerMIOT.print();
break;
case BLINKER_CMD_QUERY_POWERSTATE_NUMBER :
BLINKER_LOG("MIOT Query Power State");
BlinkerMIOT.powerState(wsState ? "on" : "off");
BlinkerMIOT.print();
break;
case BLINKER_CMD_QUERY_COLOR_NUMBER :
BLINKER_LOG("MIOT Query Color");
BlinkerMIOT.color(0);
BlinkerMIOT.print();
break;
case BLINKER_CMD_QUERY_MODE_NUMBER :
BLINKER_LOG("MIOT Query Mode");
BlinkerMIOT.mode(0);
BlinkerMIOT.print();
break;
case BLINKER_CMD_QUERY_COLORTEMP_NUMBER :
BLINKER_LOG("MIOT Query ColorTemperature");
BlinkerMIOT.colorTemp(1000);
BlinkerMIOT.print();
break;
case BLINKER_CMD_QUERY_BRIGHTNESS_NUMBER :
BLINKER_LOG("MIOT Query Brightness");
BlinkerMIOT.brightness(1);
BlinkerMIOT.print();
break;
default :
BlinkerMIOT.powerState(wsState ? "on" : "off");
BlinkerMIOT.color(0);
BlinkerMIOT.mode(0);
BlinkerMIOT.colorTemp(1000);
BlinkerMIOT.brightness(1);
BlinkerMIOT.print();
break;
}
}
// 开机
void pcawaking()
{
int i=0;
char mac[6]={0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
char pac[102];
char * Address = "192.168.0.255";
int Port = 3333;
for(i=0;i<6;i++)
{
pac[i]=0xFF;
}
for(i=6;i<102;i+=6)
{
memcpy(pac+i,mac,6);
}
Udp.beginPacket(Address, Port);
Udp.write((byte*)pac, 102);
Udp.endPacket();
}
bool active()
{
Blinker.run();
if (openState != nowState)
{
nowState = openState;
Serial.println("Operating in new mode " + String(openState));
return true;
}
if (openState == nowState)
{
//Serial.println("Operating in same mode " + String(openState));
return false;

}
}

uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if (WheelPos < 85) {
return pixels.Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
if (WheelPos < 170) {
WheelPos -= 85;
return pixels.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
WheelPos -= 170;
return pixels.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
void rainbow(uint8_t wait) {
uint16_t i, j;

for (j = 0; j < 256; j++) {
for (i = 0; i < pixels.numPixels(); i++) {
pixels.setPixelColor(i, Wheel((i + j) & 255));
}
if (active())
{
break;
}
pixels.show();
delay(freq_flash);
}
}

void colorWipe(uint32_t c, uint8_t wait) {
for (uint16_t i = 0; i < pixels.numPixels(); i++) {
pixels.setPixelColor(i, c);
pixels.show();
delay(wait);
if (active())
{
break;
}
}
}

void fadeall() {
for (int i = 0; i < NUMPIXELS; i++) {
leds[i].nscale8(250);
}
}


void cylon() {
static uint8_t hue = 0;


for (int i = 0; i < NUMPIXELS; i++) {
leds[i] = CHSV(hue++, 255, 255);
FastLED.show();
fadeall();
if (active())
{
break;
}
delay(freq_flash);
}
for (int i = (NUMPIXELS) - 1; i >= 0; i--) {
leds[i] = CHSV(hue++, 255, 255);
FastLED.show();
fadeall();
if (active())
{
break;
}
delay(freq_flash);
}
}
//局域网
void musicWrite2()
{
int si;
int delta = int(255 / NUMPIXELS );
String tmpString;
int packetSize = Udp.parsePacket();
if (packetSize) {

int len = Udp.read(packetBuffer, 256);
if (len > 0) {
packetBuffer[len] = 0;

si = uint8_t(packetBuffer[0]);
}
//si = tmpString.toInt();
Serial.println(si);
if (si > drop_dot and si < NUMPIXELS) {

if ((si + 3) >= NUMPIXELS)
{
drop_dot = NUMPIXELS - 1;
}
else {
drop_dot = si + 3;
}

}
if (si > pre_si)
{

for (int j = pre_si - 1; j < si + 1; j++)
{

while ((millis() - up_time ) < int(60 / (si - pre_si) + 2))
{
if ((drop_dot > si + 3) && (millis() - drop_time > 15))
{
pixels.setPixelColor(drop_dot + 1, pixels.Color(0, 0, 0));
pixels.setPixelColor(drop_dot, pixels.Color(colorR, colorG, colorB));//adjust color with APP color palette;
pixels.show();
drop_time = millis();
drop_dot--;
}
int j_fade = int(delta * j);
pixels.setPixelColor(j, Wheel(j_fade & 255)); //fade from BLUE-RED
pixels.show();
}
up_time = millis();

}
}
if (si < pre_si)
{

for (int k = pre_si + 1; k > si - 1; k--)
{

while ((millis() - down_time) < int(60 / (pre_si - si) + 2))
{

if ((drop_dot > si + 3) && (millis() - drop_time > 15))
{
pixels.setPixelColor(drop_dot + 1, pixels.Color(0, 0, 0));
pixels.setPixelColor(drop_dot, pixels.Color(colorR, colorG, colorB));//adjust color with APP color palette;
pixels.show();
drop_time = millis();
drop_dot--;
}


pixels.setPixelColor(k, pixels.Color(0, 0, 0));//
pixels.show();
}

down_time = millis();
}

}
pre_si = si;
Udp.stop();
Udp.begin(localUdpPort);

}


}
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 < NUMPIXELS; 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 = (NUMPIXELS - 1) - pixelnumber;

nblend( leds[pixelnumber], newcolor, 64);
}
FastLED.show();
}


void breath()
{
wsState = true;

for (int brt = 1; brt < brt_set + 1; brt++) {
colorW = brt;
pixelShow();
if (active())
{
break;
}
delay(freq_flash);
}
for (int brt = brt_set; brt > 0 ; brt--) {
colorW = brt;
pixelShow();
if (active())
{
break;
}
delay(freq_flash);
}

}

void setup()
{
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
LEDS.addLeds<WS2812, PIN, RGB>(leds, NUMPIXELS);
Blinker.begin(auth, ssid, pswd);
pixels.begin();
Udp.begin(localUdpPort);
Blinker.attachData(dataRead);
BlinkerMIOT.attachPowerState(miotPowerState);
BlinkerMIOT.attachColor(miotColor);
BlinkerMIOT.attachMode(miotMode);
BlinkerMIOT.attachBrightness(miotBright);
BlinkerMIOT.attachColorTemperature(miotColoTemp);
BlinkerMIOT.attachQuery(miotQuery);
pinMode(14, OUTPUT);
digitalWrite(14, HIGH);
pinMode(15, OUTPUT);
digitalWrite(15, HIGH);
Slider1.attach(slider1_callback);
Slider2.attach(slider2_callback);
RGBWS2812.attach(ws2812_callback);
Button1.attach(button1_callback);
Button2.attach(button2_callback);
Button3.attach(button3_callback);
Button4.attach(button4_callback);
Button5.attach(button5_callback);
pixels.setBrightness(250);
button_clear();

}

void mode_1()
{
if (colorW == 0)
{
colorW = 150;
}
cylon();
}

void mode_2()
{
musicWrite2();

}

void mode_3()
{
if (colorW == 0)
{
colorW = 150;
}
FastLED.setBrightness(colorW);
pride();
FastLED.show();

}

void mode_4()
{
if (colorW == 0)
{
colorW = 150;
}
rainbow(freq_flash);
//colorW = 50;
}
void mode_5()
{
if (colorW == 0)
{
colorW = 150;
}
breath();
}
void loop()
{

Blinker.run();
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("Connection Failed! Rebooting...");
delay(5000);
ESP.restart();
}

switch (openState)
{
case 0:
wsState = false;
openState = 0;
colorR = 255;
colorG = 255;
colorB = 255;
colorW = 0;
pixelShow();
break;
case 1:
mode_1();
break;
case 2:
mode_2();
break;
case 3:
mode_3();
break;
case 4:
mode_4();
break;
case 5:
mode_5();
break;
case 6:
pixelShow();
break;
default:
break;
}
}
  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!
表情 | 预览
快来做第一个评论的人吧~
Powered By Valine
v1.3.10

请我喝杯咖啡吧~

支付宝
微信