-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlight_controller.cpp
More file actions
717 lines (600 loc) · 16.3 KB
/
Copy pathlight_controller.cpp
File metadata and controls
717 lines (600 loc) · 16.3 KB
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
#include "light_controller.h"
#include "music_mode.h"
#include "audio_analyzer.h"
LightController::LightController()
{
mqtt = nullptr;
musicMode = nullptr;
audioAnalyzer = nullptr;
strip = nullptr;
numPixels = DEFAULT_NUM_PIXELS;
state = LIGHT_OFF;
mode = MODE_IDLE;
debugData = nullptr;
debugModeActive = false;
debugSelectedIndex = -1;
lastBreathUpdate = 0;
breathDirection = 1;
breathBrightness = 0;
suppressMqttFeedback = false;
idleColor = 0x0000FF; // 默认蓝色
}
LightController::~LightController()
{
if (strip != nullptr)
{
delete strip;
strip = nullptr;
}
if (debugData != nullptr)
{
delete[] debugData;
debugData = nullptr;
}
}
void LightController::begin(MQTTManager *mqttManager, int pixelCount)
{
mqtt = mqttManager;
numPixels = constrain(pixelCount, 1, MAX_NUM_PIXELS);
Serial.println("[LightController] Initializing...");
if (strip != nullptr)
{
delete strip;
}
strip = new Adafruit_NeoPixel(numPixels, NEOPIXEL_PIN, NEO_GRB + NEO_KHZ800);
strip->begin();
strip->clear();
strip->show();
if (debugData != nullptr)
{
delete[] debugData;
}
debugData = new PixelDebugData[numPixels];
for (int i = 0; i < numPixels; i++)
{
debugData[i].color = 0xFFFFFF;
debugData[i].brightness = 255;
debugData[i].isOverridden = false;
}
Serial.print("[LightController] ✓ Initialized with ");
Serial.print(numPixels);
Serial.print(" pixel(s) on pin ");
Serial.println(NEOPIXEL_PIN);
}
void LightController::setMusicMode(MusicMode *music, AudioAnalyzer *audio)
{
musicMode = music;
audioAnalyzer = audio;
Serial.println("[LightController] Music mode and audio analyzer configured");
}
void LightController::setActive(bool active)
{
if (!active)
{
strip->clear();
strip->show();
Serial.println("[LightController] Controller deactivated");
}
else
{
updateLEDs();
Serial.println("[LightController] Controller activated");
}
}
void LightController::setNumPixels(int count)
{
count = constrain(count, 1, MAX_NUM_PIXELS);
if (count != numPixels)
{
numPixels = count;
delete strip;
strip = new Adafruit_NeoPixel(numPixels, NEOPIXEL_PIN, NEO_GRB + NEO_KHZ800);
strip->begin();
delete[] debugData;
debugData = new PixelDebugData[numPixels];
for (int i = 0; i < numPixels; i++)
{
debugData[i].color = 0xFFFFFF;
debugData[i].brightness = 255;
debugData[i].isOverridden = false;
}
Serial.print("[LightController] ✓ Pixel count updated to: ");
Serial.println(numPixels);
if (state == LIGHT_ON)
{
updateLEDs();
}
else
{
strip->clear();
strip->show();
}
}
}
void LightController::loop()
{
if (state != LIGHT_ON)
{
return; // 灯关闭时不更新
}
if (mode == MODE_IDLE)
{
updateBreathingEffect();
}
else if (mode == MODE_MUSIC)
{
// Music 模式:持续更新 VU 表
updateLEDs();
}
}
void LightController::handleMQTTMessage(char *topic, byte *payload, unsigned int length)
{
String message = "";
for (unsigned int i = 0; i < length; i++)
{
message += (char)payload[i];
}
Serial.print("[LightController] Received [");
Serial.print(topic);
Serial.print("]: ");
Serial.println(message);
String topicStr = String(topic);
if (topicStr.endsWith("/status"))
{
if (message == "on" || message == "ON" || message == "1")
{
Serial.println("[LightController] Setting state to ON");
state = LIGHT_ON;
updateLEDs();
}
else if (message == "off" || message == "OFF" || message == "0")
{
Serial.println("[LightController] Setting state to OFF");
state = LIGHT_OFF;
updateLEDs();
}
}
else if (topicStr.endsWith("/mode"))
{
message.toLowerCase();
if (message == "timer")
{
mode = MODE_TIMER;
}
else if (message == "weather")
{
mode = MODE_WEATHER;
}
else if (message == "idle")
{
mode = MODE_IDLE;
breathBrightness = 0;
breathDirection = 1;
lastBreathUpdate = millis();
}
else if (message == "music")
{
mode = MODE_MUSIC;
}
const char *modeNames[] = {"timer", "weather", "idle", "music"};
Serial.print("[LightController] Setting mode to: ");
Serial.println(modeNames[mode]);
applyModeColor();
if (state == LIGHT_ON)
{
updateLEDs();
}
}
else if (topicStr.endsWith("/debug/color"))
{
int colonPos = message.indexOf(':');
if (colonPos > 0)
{
int index = message.substring(0, colonPos).toInt();
String colorStr = message.substring(colonPos + 1);
debugSetColor(index, colorStr);
}
else
{
for (int i = 0; i < numPixels; i++)
{
debugSetColor(i, message);
}
}
}
else if (topicStr.endsWith("/debug/brightness"))
{
int colonPos = message.indexOf(':');
if (colonPos > 0)
{
int index = message.substring(0, colonPos).toInt();
int brightness = message.substring(colonPos + 1).toInt();
debugSetBrightness(index, brightness);
}
else
{
int brightness = message.toInt();
for (int i = 0; i < numPixels; i++)
{
debugSetBrightness(i, brightness);
}
}
}
else if (topicStr.endsWith("/debug/index"))
{
if (message == "clear" || message == "CLEAR")
{
clearDebugMode();
}
else
{
debugSetIndex(message.toInt());
}
}
// 新增:IDLE 模式自定义颜色
else if (topicStr.endsWith("/idle/color"))
{
// 接收格式: #RRGGBB
uint32_t newColor = hexToColor(message);
idleColor = newColor;
Serial.print("[LightController] IDLE color set to: ");
Serial.println(message);
// 如果当前是 IDLE 模式且灯是开启的,立即更新显示
if (mode == MODE_IDLE && state == LIGHT_ON)
{
applyModeColor();
updateLEDs();
}
// 发布确认消息
if (mqtt && !suppressMqttFeedback)
{
mqtt->publishInfo("idle/color", message.c_str(), true);
}
}
}
void LightController::turnOn()
{
Serial.println("[LightController] Turning ON");
state = LIGHT_ON;
updateLEDs();
if (mqtt && !suppressMqttFeedback)
{
mqtt->publishStatus("on");
}
}
void LightController::turnOff()
{
Serial.println("[LightController] Turning OFF");
state = LIGHT_OFF;
updateLEDs();
if (mqtt && !suppressMqttFeedback)
{
mqtt->publishStatus("off");
}
}
void LightController::setMode(String modeName)
{
modeName.toLowerCase();
LightMode oldMode = mode;
if (modeName == "timer")
{
mode = MODE_TIMER;
}
else if (modeName == "weather")
{
mode = MODE_WEATHER;
}
else if (modeName == "idle")
{
mode = MODE_IDLE;
breathBrightness = 0;
breathDirection = 1;
lastBreathUpdate = millis();
}
else if (modeName == "music")
{
mode = MODE_MUSIC;
}
else
{
Serial.print("[LightController] ⚠ Unknown mode: ");
Serial.println(modeName);
suppressMqttFeedback = false;
return;
}
const char *modeNames[] = {"timer", "weather", "idle", "music"};
Serial.print("[LightController] Mode set to: ");
Serial.println(modeNames[mode]);
applyModeColor();
if (state == LIGHT_ON)
{
updateLEDs();
}
if (mqtt && !suppressMqttFeedback)
{
mqtt->publishMode(modeNames[mode]);
}
}
void LightController::applyModeColor()
{
uint32_t modeColor;
switch (mode)
{
case MODE_TIMER:
modeColor = 0xFF0000;
Serial.println("[LightController] Mode color: RED");
break;
case MODE_WEATHER:
modeColor = 0x00FF00;
Serial.println("[LightController] Mode color: GREEN");
break;
case MODE_IDLE:
modeColor = idleColor; // 使用自定义颜色
Serial.print("[LightController] Mode color: IDLE (Custom #");
Serial.print(idleColor, HEX);
Serial.println(")");
break;
case MODE_MUSIC:
modeColor = 0xFFFFFF;
Serial.println("[LightController] Mode color: WHITE (Music Mode)");
break;
}
if (strip != nullptr)
{
for (int i = 0; i < numPixels; i++)
{
if (!debugData[i].isOverridden)
{
setPixel(i, modeColor, 255);
}
}
}
}
void LightController::setPixel(int index, uint32_t pixelColor, int pixelBrightness)
{
if (strip == nullptr || index < 0 || index >= numPixels)
return;
uint8_t r = (pixelColor >> 16) & 0xFF;
uint8_t g = (pixelColor >> 8) & 0xFF;
uint8_t b = pixelColor & 0xFF;
r = (r * pixelBrightness) / 255;
g = (g * pixelBrightness) / 255;
b = (b * pixelBrightness) / 255;
strip->setPixelColor(index, strip->Color(r, g, b));
}
void LightController::debugSetColor(int index, String hexColor)
{
if (index < 0 || index >= numPixels)
return;
uint32_t color = hexToColor(hexColor);
debugData[index].color = color;
debugData[index].isOverridden = true;
debugModeActive = true;
Serial.print("[LightController] DEBUG: Pixel ");
Serial.print(index);
Serial.print(" color set to ");
Serial.println(hexColor);
if (state == LIGHT_ON)
{
updateLEDs();
}
}
void LightController::debugSetBrightness(int index, int brightness)
{
if (index < 0 || index >= numPixels)
return;
brightness = constrain(brightness, 0, 255);
debugData[index].brightness = brightness;
debugData[index].isOverridden = true;
debugModeActive = true;
Serial.print("[LightController] DEBUG: Pixel ");
Serial.print(index);
Serial.print(" brightness set to ");
Serial.println(brightness);
if (state == LIGHT_ON)
{
updateLEDs();
}
}
void LightController::debugSetIndex(int index)
{
debugSelectedIndex = index;
Serial.print("[LightController] DEBUG: Selected index ");
Serial.println(index);
}
void LightController::clearDebugMode()
{
Serial.println("[LightController] DEBUG: Clearing debug mode");
for (int i = 0; i < numPixels; i++)
{
debugData[i].isOverridden = false;
}
debugModeActive = false;
debugSelectedIndex = -1;
applyModeColor();
if (state == LIGHT_ON)
{
updateLEDs();
}
}
uint32_t LightController::hexToColor(String hexColor)
{
if (hexColor.startsWith("#"))
{
hexColor = hexColor.substring(1);
}
long number = strtol(hexColor.c_str(), NULL, 16);
return (uint32_t)number;
}
void LightController::updateBreathingEffect()
{
unsigned long now = millis();
if (now - lastBreathUpdate > 20)
{
breathBrightness += breathDirection * 2;
if (breathBrightness >= 255)
{
breathBrightness = 255;
breathDirection = -1;
}
else if (breathBrightness <= 0)
{
breathBrightness = 0;
breathDirection = 1;
}
updateLEDs();
lastBreathUpdate = now;
}
}
void LightController::updateLEDs()
{
if (strip == nullptr)
return;
if (state == LIGHT_OFF)
{
strip->clear();
strip->show();
return;
}
// Music 模式:使用 VU 表显示
if (mode == MODE_MUSIC && musicMode != nullptr && audioAnalyzer != nullptr)
{
updateMusicVU();
return;
}
// 其他模式:原有逻辑
for (int i = 0; i < numPixels; i++)
{
if (debugData[i].isOverridden)
{
uint8_t brightness = debugData[i].brightness;
if (mode == MODE_IDLE)
{
brightness = (brightness * breathBrightness) / 255;
}
setPixel(i, debugData[i].color, brightness);
}
else
{
uint32_t modeColor;
switch (mode)
{
case MODE_TIMER:
modeColor = 0xFF0000;
break;
case MODE_WEATHER:
modeColor = 0x00FF00;
break;
case MODE_IDLE:
modeColor = idleColor; // 使用自定义颜色
break;
case MODE_MUSIC:
modeColor = 0xFFFFFF;
break;
}
int brightness = (mode == MODE_IDLE) ? breathBrightness : 255;
setPixel(i, modeColor, brightness);
}
}
strip->show();
}
void LightController::updateMusicVU()
{
if (!musicMode || !audioAnalyzer)
{
return;
}
// 检查音量是否低于下限,如果是则全灭
float volumeDb = audioAnalyzer->getVolumeDecibel();
float minDb, maxDb;
audioAnalyzer->getVolumeRange(minDb, maxDb);
if (volumeDb <= minDb)
{
// 音量低于下限,全部熄灭
for (int i = 0; i < numPixels && i < 8; i++)
{
setPixel(i, 0x000000, 0);
}
strip->show();
// 调试输出
static unsigned long lastDebug = 0;
if (millis() - lastDebug > 3000)
{
Serial.print("[LightController] Volume below threshold: ");
Serial.print(volumeDb, 1);
Serial.print(" dB <= ");
Serial.print(minDb, 1);
Serial.println(" dB (All LEDs OFF)");
lastDebug = millis();
}
return;
}
// 获取精确的音量级别(0.0 - 8.0,对应 8 个灯)
float exactLevel = audioAnalyzer->getVolume() * 8.0;
if (exactLevel > 8.0)
exactLevel = 8.0;
// 完全点亮的灯数量(向下取整)
int fullLights = (int)exactLevel;
// 部分点亮的灯亮度(0.0 - 1.0)
float partialBrightness = exactLevel - fullLights;
// 调试:每 2 秒打印一次
static unsigned long lastDebug2 = 0;
if (millis() - lastDebug2 > 2000)
{
Serial.print("[LightController] Exact Level: ");
Serial.print(exactLevel, 2);
Serial.print(" (Full: ");
Serial.print(fullLights);
Serial.print(", Partial: ");
Serial.print(partialBrightness, 2);
Serial.print("), Volume: ");
Serial.print(volumeDb, 1);
Serial.println(" dB");
lastDebug2 = millis();
}
// VU 表颜色方案(从低到高)
uint32_t vuColors[8] = {
0x00FF00, // 0: 绿色(安静)
0x33FF00, // 1: 黄绿
0x66FF00, // 2: 黄绿
0x99FF00, // 3: 黄色
0xFFFF00, // 4: 黄色
0xFFCC00, // 5: 橙色
0xFF6600, // 6: 橙红
0xFF0000 // 7: 红色(爆音)
};
for (int i = 0; i < numPixels && i < 8; i++)
{
if (debugData[i].isOverridden)
{
// DEBUG 模式优先
setPixel(i, debugData[i].color, debugData[i].brightness);
}
else if (i < fullLights)
{
// 完全点亮的灯:全亮度
setPixel(i, vuColors[i], 255);
}
else if (i == fullLights && partialBrightness > 0.05)
{
// 部分点亮的灯:使用亮度控制平滑过渡
uint8_t brightness = (uint8_t)(partialBrightness * 255);
setPixel(i, vuColors[i], brightness);
}
else
{
// 熄灭未达到的灯
setPixel(i, 0x000000, 0);
}
}
strip->show();
}
void LightController::publishState()
{
if (!mqtt)
return;
Serial.println("[LightController] Publishing state...");
mqtt->publishStatus(state == LIGHT_ON ? "on" : "off");
const char *modeNames[] = {"timer", "weather", "idle", "music"};
mqtt->publishMode(modeNames[mode]);
}