Skip to content

Commit db311e1

Browse files
committed
Add LED grouping feature
1 parent 3b9da69 commit db311e1

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

Arduino/LEDstream_FastLED/LEDstream_FastLED.ino

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ const unsigned long
3939
const uint16_t
4040
SerialTimeout = 60; // time before LEDs are shut off if no data (in seconds), 0 to disable
4141

42+
// --- Group Settings (uncomment to add)
43+
// Grouping will set a group of LEDs to the data received for a single one. This
44+
// lets you send less data to the device, increasing throughput and therefore
45+
// framerate - at the cost of resolution. Grouping only works if your strip is
46+
// evenly divisible by the amount of data sent.
47+
// #define GROUPING // enable the automatic grouping feature
48+
4249
// --- Optional Settings (uncomment to add)
4350
#define SERIAL_FLUSH // Serial buffer cleared on LED latch
4451
// #define CLEAR_ON_START // LEDs are cleared on reset
@@ -89,6 +96,7 @@ const unsigned long Timebase = 1000; // time units per second
8996

9097
void headerMode(uint8_t c);
9198
void dataMode(uint8_t c);
99+
void groupProcessing();
92100
void timeouts();
93101

94102
// Macros initialized
@@ -226,6 +234,10 @@ void dataMode(uint8_t c){
226234

227235
// if all data has been read, write the output
228236
if (ledsRemaining == 0) {
237+
#if defined(GROUPING)
238+
groupProcessing();
239+
#endif
240+
229241
FastLED.show();
230242
channelIndex = 0; // reset channel tracking
231243
mode = Header; // begin next header search
@@ -236,6 +248,27 @@ void dataMode(uint8_t c){
236248
}
237249
}
238250

251+
void groupProcessing() {
252+
// if we've received the same amount of data as there
253+
// are LEDs in the strip, don't do any group processing
254+
if (Num_Leds == ledIndex) return;
255+
256+
const uint16_t GroupSize = Num_Leds / ledIndex;
257+
const uint16_t GroupRemainder = Num_Leds - (ledIndex * GroupSize);
258+
259+
// if the value isn't evenly divisible, don't bother trying to group
260+
// (it won't look right without significant processing, i.e. overhead)
261+
if (GroupRemainder != 0) return;
262+
263+
// otherwise, iterate backwards through the array, copying each LED color
264+
// forwards to the rest of its group
265+
for (uint16_t group = 1; group <= ledIndex; ++group) {
266+
const CRGB GroupColor = leds[ledIndex - group];
267+
const uint16_t GroupStart = Num_Leds - (group * GroupSize);
268+
fill_solid(&leds[GroupStart], GroupSize, GroupColor);
269+
}
270+
}
271+
239272
void timeouts(){
240273
const unsigned long t = now();
241274

0 commit comments

Comments
 (0)