Skip to content

Commit a5b6b3c

Browse files
authored
Merge pull request #72 from Legion2/scale-segments
Function to scale multiple parts of a strip independently
2 parents 9734276 + 7946438 commit a5b6b3c

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

src/FastLEDControllerUtils.cpp

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
limitations under the License.
1515
*/
1616
#include "FastLEDControllerUtils.h"
17+
#include <math.h>
1718

1819
void CLP::transformLLFanToStrip(FastLEDController* controller, uint8_t channelIndex)
1920
{
@@ -33,9 +34,9 @@ void CLP::transformLLFanToStrip(FastLEDController* controller, uint8_t channelIn
3334
void CLP::scale(FastLEDController* controller, uint8_t channelIndex, int scaleToSize)
3435
{
3536
auto leds = controller->getLEDs(channelIndex);
36-
float scaleFactor = (float)controller->getLEDCount(channelIndex) / scaleToSize;
37+
const float scaleFactor = (float)controller->getLEDCount(channelIndex) / scaleToSize;
3738
for (int ledIndex = scaleToSize - 1; ledIndex >= 0; ledIndex--) {
38-
leds[ledIndex] = leds[(int)(ledIndex * scaleFactor)];
39+
leds[ledIndex] = leds[round(ledIndex * scaleFactor)];
3940
}
4041
}
4142

@@ -48,3 +49,34 @@ void CLP::repeat(FastLEDController* controller, uint8_t channelIndex, uint8_t ti
4849
memcpy(leds + (count * i), leds, sizeof(CRGB) * count);
4950
}
5051
}
52+
53+
void CLP::scaleSegments(FastLEDController* controller, uint8_t channelIndex, const SegmentScaling* const segments, int segmentsCount)
54+
{
55+
auto leds = controller->getLEDs(channelIndex);
56+
int ledStripIndexAfterScaling = 0;
57+
int ledStripIndexBeforeScaling = 0;
58+
for (int i = 0; i < segmentsCount; i++) {
59+
ledStripIndexAfterScaling += segments[i].scaleToSize;
60+
ledStripIndexBeforeScaling += segments[i].segmentLength;
61+
}
62+
63+
for (int i = segmentsCount - 1; i >= 0; i--) {
64+
const float scaleFactor = (float)segments[i].segmentLength / segments[i].scaleToSize;
65+
ledStripIndexAfterScaling -= segments[i].scaleToSize;
66+
ledStripIndexBeforeScaling -= segments[i].segmentLength;
67+
for (int ledIndex = segments[i].scaleToSize - 1; ledIndex >= 0; ledIndex--) {
68+
leds[ledStripIndexAfterScaling + ledIndex] = leds[ledStripIndexBeforeScaling + round(ledIndex * scaleFactor)];
69+
}
70+
}
71+
}
72+
73+
void CLP::reverse(FastLEDController* controller, uint8_t channelIndex)
74+
{
75+
auto leds = controller->getLEDs(channelIndex);
76+
auto maxIndex = controller->getLEDCount(channelIndex) - 1;
77+
for (int ledIndex = 0; ledIndex < maxIndex - ledIndex; ledIndex++) {
78+
CRGB temp = leds[ledIndex];
79+
leds[ledIndex] = leds[maxIndex - ledIndex];
80+
leds[maxIndex - ledIndex] = temp;
81+
}
82+
}

src/FastLEDControllerUtils.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,36 @@ namespace CLP
2828
void scale(FastLEDController* controller, uint8_t channelIndex, int scaleToSize);
2929
//Repeat a channel's leds color to control more leds than provided by iCUE.
3030
void repeat(FastLEDController* controller, uint8_t channelIndex, uint8_t times);
31+
32+
/**
33+
* Define the scaling information for a segment of a channel. A segment is a part of a channel that can be scale independently of other segments.
34+
*/
35+
struct SegmentScaling {
36+
/**
37+
* The length of the segment in iCUE for example 10 for the normal LED strips
38+
*/
39+
int segmentLength;
40+
/**
41+
* The size to which the segment will be scaled using Integer scaling
42+
*/
43+
int scaleToSize;
44+
};
45+
/**
46+
* Scales a channel's segments to given sizes. This can be used to apply different scaling factors to the different parts of a LED strip.
47+
* Integer scaling is used.
48+
*
49+
* @param controller the FastLEDController controlling the LEDs
50+
* @param channelIndex the index of the channel you want to scale the segments on
51+
* @param segments the segments defining the size before and after scaling
52+
* @param segmentsCount the number of segments
53+
*/
54+
void scaleSegments(FastLEDController* controller, uint8_t channelIndex, const SegmentScaling* const segments, int segmentsCount);
55+
56+
/**
57+
* Reverse the LEDs of a channel, after this operation, the first led is the last and the last is the first.
58+
*
59+
* @param controller the FastLEDController controlling the LEDs
60+
* @param channelIndex the index of the channel you want to reverse
61+
*/
62+
void reverse(FastLEDController* controller, uint8_t channelIndex);
3163
}

0 commit comments

Comments
 (0)