-
Notifications
You must be signed in to change notification settings - Fork 2
/
sparkled-client-esp.ino
194 lines (156 loc) · 4.9 KB
/
sparkled-client-esp.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
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
#include "constants.h"
#include <ESP.h>
#if defined(ESP8266)
#include <ESP8266WiFi.h>
#elif defined(ESP32)
#include <WiFi.h>
#endif
#include <WiFiUdp.h>
#ifdef OTA_UPDATES_ENABLED
#include <ArduinoOTA.h>
#endif
#include <FastLED.h>
// Shared application state
WiFiUDP udp;
CRGB leds[STAGE_PROP_COUNT][LED_COUNT];
uint8_t packetBuffer[LED_BUFFER_SIZE];
boolean connected = false;
uint32_t lastSuccessfulPacketTime = 0;
uint32_t lastSubscribeTime = 0;
bool otaUpdating = false;
void setup() {
Serial.begin(115200);
#if CLOCK_PIN == -1
FastLED.addLeds<CHIPSET, DATA_PIN, RGB_ORDER>(leds[0], TOTAL_LED_COUNT).setCorrection(TypicalSMD5050);
#else
FastLED.addLeds<CHIPSET, DATA_PIN, CLOCK_PIN, RGB_ORDER>(leds[0], TOTAL_LED_COUNT).setCorrection(TypicalSMD5050);
#endif
connectToWiFi(NETWORK_SSID, NETWORK_PASSWORD);
}
void loop() {
uint32_t ms = millis();
if (!connected) {
return;
}
#ifdef OTA_UPDATES_ENABLED
ArduinoOTA.handle();
if (otaUpdating) {
return;
}
#endif
checkForNetworkFailure();
if (lastSubscribeTime == 0 || ms - lastSubscribeTime > SUBSCRIBE_INTERVAL_MS) {
for (uint8_t i = 0; i < STAGE_PROP_COUNT; i++) {
subscribe(STAGE_PROP_CODES[i], i);
}
lastSubscribeTime = ms;
}
receiveFrame();
}
void checkForNetworkFailure() {
if (millis() - lastSuccessfulPacketTime > MAX_CONNECTION_LOSS_MS) {
Serial.println("No packets received in " + String(MAX_CONNECTION_LOSS_MS) + "ms, restarting.");
ESP.restart();
while (true);
}
}
void subscribe(String stagePropCode, uint8_t clientId) {
uint32_t ms = millis();
if (!udp.beginPacket(SERVER_IP_ADDRESS, SERVER_UDP_PORT)) {
Serial.println("beginPacket() failed.");
return;
}
udp.printf(String(SUBSCRIBE_COMMAND + stagePropCode + ":" + clientId).c_str());
udp.endPacket();
}
void receiveFrame() {
uint32_t ms = millis();
uint16_t packetSize = udp.parsePacket();
if (packetSize > 0) {
udp.read(packetBuffer, LED_BUFFER_SIZE);
adjustBrightness();
renderLeds(packetSize);
lastSuccessfulPacketTime = millis();
}
}
void adjustBrightness() {
// Brightness is stored in the bottom 4 bits.
uint8_t brightness = map(packetBuffer[0] & 0b00001111, 0, 15, 0, UINT8_MAX);
FastLED.setBrightness(brightness);
}
void renderLeds(uint16_t packetSize) {
// Client ID is stored in the top 4 bits.
uint8_t clientId = (packetBuffer[0] & 0b11110000) >> 4;
fillLeds(clientId, CRGB::Black);
uint16_t packetLedCount = (packetSize - HEADER_SIZE) / BYTES_PER_LED;
for (uint16_t i = 0; i < _min(packetLedCount, LED_COUNT); i++) {
uint16_t bufferIndex = HEADER_SIZE + (i * BYTES_PER_LED);
leds[clientId][i].setRGB(packetBuffer[bufferIndex], packetBuffer[bufferIndex + 1], packetBuffer[bufferIndex + 2]);
}
FastLED.show();
}
void connectToWiFi(const String ssid, const String pwd) {
Serial.println("Connecting to network: " + ssid + "...");
showStatus(STATUS_CONNECTING);
WiFi.disconnect(true);
WiFi.onEvent(onWiFiEvent);
#ifdef STATIC_IP_ADDRESS
if (WiFi.config(IPAddress(STATIC_IP_ADDRESS), IPAddress(ROUTER_IP_ADDRESS), IPAddress(SUBNET_MASK), IPAddress(DNS_PRIMARY), IPAddress(DNS_SECONDARY))) {
Serial.println("Using static IP address.");
} else {
Serial.println("Failed to configure static IP address.");
}
#else
Serial.println("Using dynamic IP address.");
#endif
WiFi.begin(ssid.c_str(), pwd.c_str());
Serial.println("Waiting for network connection...");
}
void showStatus(CRGB statusColor) {
for (uint8_t clientId = 0; clientId < STAGE_PROP_COUNT; clientId++) {
fillLeds(clientId, CRGB::Black);
for (uint8_t i = 0; i < STATUS_LED_COUNT; i++) {
leds[clientId][i] = statusColor;
}
}
FastLED.show();
}
void fillLeds(uint8_t clientId, CRGB color) {
for (uint8_t i = 0; i < LED_COUNT; i++) {
leds[clientId][i] = color;
}
}
void onWiFiEvent(WiFiEvent_t event) {
switch (event) {
case EVENT_CONNECTED:
Serial.println("Connected to network.");
break;
case EVENT_GOT_IP:
if (WiFi.localIP() == IPAddress(0, 0, 0, 0)) {
Serial.println("Got IP Address of 0.0.0.0, waiting for proper IP address...");
} else {
if (!connected) {
Serial.print("Connected with IP address ");
Serial.println(WiFi.localIP());
#ifdef OTA_UPDATES_ENABLED
ArduinoOTA
.onStart([]() {
otaUpdating = true;
Serial.println("Over-the-air update has started.");
});
ArduinoOTA.begin();
Serial.println("Over-the-air updater is listening.");
#endif
udp.begin(SERVER_UDP_PORT);
connected = true;
lastSuccessfulPacketTime = millis();
}
}
break;
case EVENT_DISCONNECTED:
Serial.println("Lost network connection, attempting to reconnect...");
connected = false;
connectToWiFi(NETWORK_SSID, NETWORK_PASSWORD);
break;
}
}