Repository navigation
Expand file tree
/
Copy pathLed.cpp
More file actions
135 lines (113 loc) · 3.18 KB
/
Copy pathLed.cpp
File metadata and controls
135 lines (113 loc) · 3.18 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
#include "Led.h"
#include "config.h"
namespace Led {
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_RGB + NEO_KHZ800);
static TaskHandle_t pulseTask = nullptr;
static volatile bool pulsing = false;
static uint8_t pulseR = 0, pulseG = 0, pulseB = 0;
static TaskHandle_t sequenceTask = nullptr;
static volatile bool sequencing = false;
static uint32_t sequence[SEQUENCE_MAX];
static int sequenceLength = 0;
static bool lightOn = false;
static uint32_t lightColors[LED_COUNT];
static uint8_t brightnessLevel = LED_BRIGHTNESS;
static void fill(uint32_t color) {
for (int i = 0; i < LED_COUNT; i++) strip.setPixelColor(i, color);
strip.show();
}
static void pulseLoop(void*) {
uint32_t start = millis();
while (pulsing) {
float phase = (float)((millis() - start) % PULSE_PERIOD_MS) / PULSE_PERIOD_MS;
float level = 0.5f - 0.5f * cosf(phase * 2.0f * PI);
uint8_t scale = (uint8_t)(level * 255.0f);
fill(strip.gamma32(strip.Color(pulseR * scale / 255, pulseG * scale / 255, pulseB * scale / 255)));
vTaskDelay(pdMS_TO_TICKS(PULSE_FRAME_MS));
}
pulseTask = nullptr;
vTaskDelete(nullptr);
}
static bool holdSequence(uint32_t ms) {
uint32_t start = millis();
while (sequencing && millis() - start < ms) vTaskDelay(pdMS_TO_TICKS(PULSE_FRAME_MS));
return sequencing;
}
static void sequenceLoop(void*) {
while (sequencing) {
for (int i = 0; i < sequenceLength; i++) {
fill(sequence[i]);
if (!holdSequence(SEQUENCE_ON_MS)) break;
fill(0);
if (!holdSequence(SEQUENCE_OFF_MS)) break;
}
holdSequence(SEQUENCE_PAUSE_MS);
}
sequenceTask = nullptr;
vTaskDelete(nullptr);
}
void begin() {
for (int i = 0; i < LED_COUNT; i++) lightColors[i] = LIGHT_COLOR;
strip.begin();
strip.setBrightness(brightnessLevel);
strip.clear();
strip.show();
}
void restore() {
if (pulseTask || sequenceTask) return;
if (lightOn) {
for (int i = 0; i < LED_COUNT; i++) strip.setPixelColor(i, lightColors[i]);
} else {
strip.clear();
}
strip.show();
}
void setLight(bool on) {
lightOn = on;
restore();
}
bool light() {
return lightOn;
}
void setLightColor(int index, uint32_t color) {
if (index < 0 || index >= LED_COUNT) return;
lightColors[index] = color & 0xFFFFFF;
restore();
}
uint32_t lightColor(int index) {
return index >= 0 && index < LED_COUNT ? lightColors[index] : 0;
}
void setBrightness(uint8_t value) {
brightnessLevel = value;
strip.setBrightness(brightnessLevel);
restore();
}
uint8_t brightness() {
return brightnessLevel;
}
void startPulse(uint8_t r, uint8_t g, uint8_t b) {
pulseR = r;
pulseG = g;
pulseB = b;
if (pulseTask) return;
pulsing = true;
xTaskCreate(pulseLoop, "ledPulse", 2048, nullptr, 1, &pulseTask);
}
void stopPulse() {
pulsing = false;
while (pulseTask) delay(PULSE_FRAME_MS);
restore();
}
void startSequence(const uint32_t* colors, int count) {
if (sequenceTask) return;
sequenceLength = min(count, SEQUENCE_MAX);
for (int i = 0; i < sequenceLength; i++) sequence[i] = colors[i];
sequencing = true;
xTaskCreate(sequenceLoop, "ledSequence", 2048, nullptr, 1, &sequenceTask);
}
void stopSequence() {
sequencing = false;
while (sequenceTask) delay(PULSE_FRAME_MS);
restore();
}
}