-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloxone-led2812-esp32.ino
166 lines (144 loc) · 4.43 KB
/
loxone-led2812-esp32.ino
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
#include "env_config.h"
#include "ESPAsyncWebServer.h"
#include "FastLED.h"
#include "WiFi.h"
// Init
CRGB leds[ENV_LEDS_NUMBER]; // Create LED array
char serprt_buffer[50]; // Create serial print buffer
AsyncWebServer server(80); // Create AsyncWebServer on port 80
const char *COLOR_INPUT = "color"; // GET request param query keyword
// Function pointers
void (*enableColorEffect)(uint8_t, uint8_t, uint8_t);
void (*disableColorEffect)(uint8_t, uint8_t, uint8_t);
// Color effects
void colorEnableFromCenter(uint8_t red, uint8_t green, uint8_t blue)
{
for (int i = ENV_LEDS_NUMBER / 2; i >= 0; i -= ENV_LED_GROUP_SIZE)
{
for (int j = i; j > i - ENV_LED_GROUP_SIZE && j >= 0; j--)
{
leds[j].setRGB(red, green, blue);
if ((ENV_LEDS_NUMBER - j) < ENV_LEDS_NUMBER)
{
leds[ENV_LEDS_NUMBER - j].setRGB(red, green, blue);
}
}
FastLED.show();
FastLED.delay(ENV_DELAY_TIME);
}
}
void colorDisableToCenter(uint8_t red, uint8_t green, uint8_t blue)
{
for (int i = 0; i <= ENV_LEDS_NUMBER / 2; i += ENV_LED_GROUP_SIZE)
{
for (int j = i; j < i + ENV_LED_GROUP_SIZE && j <= ENV_LEDS_NUMBER / 2; j++)
{
leds[j].setRGB(red, green, blue);
if ((ENV_LEDS_NUMBER - j) < ENV_LEDS_NUMBER)
{
leds[ENV_LEDS_NUMBER - j].setRGB(red, green, blue);
}
}
FastLED.show();
FastLED.delay(ENV_DELAY_TIME);
}
}
void colorEnableFromEdge(uint8_t red, uint8_t green, uint8_t blue)
{
for (int i = ENV_LEDS_NUMBER - 1; i >= 0; i -= ENV_LED_GROUP_SIZE)
{
for (int j = i; j > i - ENV_LED_GROUP_SIZE && j >= 0; j--)
{
leds[j].setRGB(red, green, blue);
}
FastLED.show();
FastLED.delay(ENV_DELAY_TIME);
}
}
void colorDisableToEdge(uint8_t red, uint8_t green, uint8_t blue)
{
for (int i = 0; i < ENV_LEDS_NUMBER; i += ENV_LED_GROUP_SIZE)
{
for (int j = i; j < i + ENV_LED_GROUP_SIZE && j < ENV_LEDS_NUMBER; j++)
{
leds[j].setRGB(red, green, blue);
}
FastLED.show();
FastLED.delay(ENV_DELAY_TIME);
}
}
// Handle Loxone color update
void handleColorUpdate(int color)
{
if (color == 0)
{
disableColorEffect(0, 0, 0);
return;
}
// Convert the input number to string
char colorString[10];
sprintf(colorString, "%09d", color);
// Print the color string with leading zeros
sprintf(serprt_buffer, "Loxone message (padded): %s", colorString);
Serial.println(serprt_buffer);
// Extract RED (0-100%)
uint8_t red = atoi(&colorString[6]);
// Extract GREEN (0-100%)
colorString[6] = '\0';
uint8_t green = atoi(&colorString[3]);
// Extract BLUE (0-100%)
colorString[3] = '\0';
uint8_t blue = atoi(colorString);
// Map percent values (0-100%) to RGB 8-bit values (0-255)
red = map(red, 0, 100, 0, 255);
green = map(green, 0, 100, 0, 255);
blue = map(blue, 0, 100, 0, 255);
sprintf(serprt_buffer, "Color values to LED STRIP - RED: %d, GREEN: %d, BLUE: %d", red, green, blue);
Serial.println(serprt_buffer);
// Call LED preset
enableColorEffect(red, green, blue);
}
void setup()
{
// Init LED strip and turn off all LEDs on boot
FastLED.addLeds<WS2812B, ENV_SIGNAL_PIN, GRB>(leds, ENV_LEDS_NUMBER);
FastLED.clear();
FastLED.show();
// Set function pointers based on ENV_EFFECT_SYMMETRICAL
if (ENV_EFFECT_SYMMETRICAL == 1)
{
enableColorEffect = colorEnableFromCenter;
disableColorEffect = colorDisableToCenter;
}
else
{
enableColorEffect = colorEnableFromEdge;
disableColorEffect = colorDisableToEdge;
}
// Start serial monitor
Serial.begin(115200);
// Connect to Wi-Fi adn show local IP address
const char *ssid = ENV_WIFI_SSID;
const char *password = ENV_WIFI_PASS;
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
Serial.println("Connecting to WiFi ...");
}
Serial.println(WiFi.localIP());
// Parse Loxone color to RGB values and send to LED strip
server.on("/update", HTTP_GET, [](AsyncWebServerRequest *request)
{
String inputMessage = request->getParam(COLOR_INPUT)->value();
int color = inputMessage.toInt();
sprintf(serprt_buffer, "Loxone message: %d", color);
Serial.println(serprt_buffer);
handleColorUpdate(color);
request->send(200, "text/plain", "OK"); });
// Start server
server.begin();
}
void loop()
{
}