forked from McZonk/LEDRGBStrip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRGBStrip.ino
206 lines (148 loc) · 4.55 KB
/
RGBStrip.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
195
196
197
198
199
200
201
202
203
204
205
206
#include <SPI.h>
#include "Settings.h"
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <EthernetBonjour.h>
#include <LPD8806.h>
#include <TimerOne.h>
#if defined(USE_DHCP) && (USE_DHCP > 0)
#include <EthernetDHCP.h>
#endif
#include "ColorMessage.h"
#include "ColorArrayMessage.h"
#include "ColorListMessage.h"
#include "HSBColor.h"
#include "Gamma.h"
EthernetUDP socket;
LPD8806 ledStrip = LPD8806(LedStripLedCount, LedStripDataPin, LedStripClockPin);
HSBColor currentColors[LedStripLedCount];
//HSBColor targetColors[LedStripLedCount];
void setup()
{
ledStrip.begin();
#if defined(USE_DHCP) && (USE_DHCP > 0)
ledStrip.setPixelColor(0, 16, 0, 0);
ledStrip.show();
EthernetDHCP.begin(mac);
const byte* ip = EthernetDHCP.ipAddress();
#endif
ledStrip.setPixelColor(0, 16, 16, 0);
ledStrip.show();
Ethernet.begin(mac, ip);
socket.begin(port);
char serviceName[32];
snprintf(serviceName, sizeof(serviceName), "%s._rgbled", LocationName);
char txtRecord[64];
int index = 0;
{
int length = snprintf(txtRecord+index+1, sizeof(txtRecord)-(index+1), "leds=%u", LedStripLedCount);
txtRecord[index] = length;
index += 1 + length;
}
EthernetBonjour.begin("arduino");
EthernetBonjour.addServiceRecord(serviceName, port, MDNSServiceUDP, txtRecord);
ledStrip.setPixelColor(0, 0, 0, 0);
ledStrip.show();
Timer1.initialize(1000000 / 30);
Timer1.attachInterrupt(ledUpdate);
}
void loop()
{
#if defined(USE_DHCP) && (USE_DHCP > 0)
// Maintain DHCP
EthernetDHCP.maintain();
#endif
// Maintain Bonjour
EthernetBonjour.run();
int messageLength = socket.parsePacket();
if(messageLength > 0) {
RGBStrip::Message::Header header;
socket.readBytes((char*)&header, sizeof(header));
if(messageLength == header.length) {
noInterrupts();
switch(header.type) {
case RGBStrip::ColorMessage::Type :
void handleColorMessage(Stream& stream);
handleColorMessage(socket);
break;
case RGBStrip::ColorArrayMessage::Type :
void handleColorArrayMessage(Stream& stream);
handleColorArrayMessage(socket);
break;
case RGBStrip::ColorListMessage::Type :
void handleColorListMessage(Stream& stream);
handleColorListMessage(socket);
break;
default :
break;
}
interrupts();
}
// empty the buffer
while(socket.available() > 0) {
socket.read();
}
}
delay(1);
}
void ledUpdate() {
for(int i = 0; i < LedStripLedCount; ++i) {
HSBColor currentColor = currentColors[i];
RGBColor rgbColor = currentColor;
ledStrip.setPixelColor(i, GammaCorretion(rgbColor.r), GammaCorretion(rgbColor.g), GammaCorretion(rgbColor.b));
}
ledStrip.show();
}
void handleColorMessage(Stream& stream) {
uint16_t offset = 0;
stream.readBytes((char*)&offset, sizeof(offset));
uint16_t count = 0;
stream.readBytes((char*)&count, sizeof(count));
if(count > ledStrip.numPixels() - offset) {
count = ledStrip.numPixels() - offset;
}
HSBColor color;
stream.readBytes((char*)&color, sizeof(color));
for(int i = 0; i < count; ++i) {
currentColors[offset + i] = color;
}
}
void handleColorArrayMessage(Stream& stream) {
uint16_t offset = 0;
stream.readBytes((char*)&offset, sizeof(offset));
uint16_t count = 0;
stream.readBytes((char*)&count, sizeof(count));
if(count > ledStrip.numPixels() - offset) {
count = ledStrip.numPixels() - offset;
}
for(int i = 0; i < count; ++i) {
HSBColor color;
stream.readBytes((char*)&color, sizeof(color));
currentColors[offset + i] = color;
}
}
void handleColorListMessage(Stream& stream) {
uint16_t offset = 0;
stream.readBytes((char*)&offset, sizeof(offset));
uint16_t count = 0;
stream.readBytes((char*)&count, sizeof(count));
if(count > 0) {
RGBStrip::ColorListMessage::Key pKey;
stream.readBytes((char*)&pKey, sizeof(pKey));
for(int i = 1; i < count; ++i) {
RGBStrip::ColorListMessage::Key cKey;
stream.readBytes((char*)&cKey, sizeof(cKey));
int stepCount = cKey.index - pKey.index;
int stepIndex = 0;
for(int j = pKey.index; j <= cKey.index; ++j, ++stepIndex) {
if(offset + j >= LedStripLedCount) {
break;
}
int time = ((stepIndex << 8) / stepCount);
HSBColor color = lerp(pKey.color, cKey.color, time);
currentColors[offset + j] = color;
}
pKey = cKey;
}
}
}