2323
2424 Modified heavily for WLED
2525*/
26-
26+ # include " wled.h "
2727#include " FX.h"
2828#include " palettes.h"
2929
4040 another example. Switches direction every 5 LEDs.
4141 {"map":[
4242 0, 1, 2, 3, 4, 9, 8, 7, 6, 5, 10, 11, 12, 13, 14,
43- 19, 18, 17, 16, 15, 20, 21, 22, 23, 24, 29, 28, 27, 26, 25]
43+ 19, 18, 17, 16, 15, 20, 21, 22, 23, 24, 29, 28, 27, 26, 25]}
4444*/
4545
4646// factory defaults LED setup
@@ -218,14 +218,13 @@ uint16_t WS2812FX::realPixelIndex(uint16_t i) {
218218 int16_t realIndex = iGroup;
219219 if (IS_REVERSE) {
220220 if (IS_MIRROR) {
221- realIndex = (SEGMENT.length () -1 ) / 2 - iGroup; // only need to index half the pixels
221+ realIndex = (SEGMENT.length () - 1 ) / 2 - iGroup; // only need to index half the pixels
222222 } else {
223- realIndex = SEGMENT.length () - iGroup - 1 ;
223+ realIndex = ( SEGMENT.length () - 1 ) - iGroup ;
224224 }
225225 }
226226
227227 realIndex += SEGMENT.start ;
228-
229228 return realIndex;
230229}
231230
@@ -246,7 +245,6 @@ void WS2812FX::setPixelColor(uint16_t i, byte r, byte g, byte b, byte w)
246245 }
247246
248247 if (SEGLEN) {// from segment
249-
250248 // color_blend(getpixel, col, _bri_t); (pseudocode for future blending of segments)
251249 if (_bri_t < 255 ) {
252250 r = scale8 (r, _bri_t );
@@ -256,12 +254,12 @@ void WS2812FX::setPixelColor(uint16_t i, byte r, byte g, byte b, byte w)
256254 }
257255 uint32_t col = ((w << 24 ) | (r << 16 ) | (g << 8 ) | (b));
258256
259- bool reversed = IS_REVERSE;
257+ /* Set all the pixels in the group */
260258 uint16_t realIndex = realPixelIndex (i);
261259 uint16_t len = SEGMENT.length ();
262260
263261 for (uint16_t j = 0 ; j < SEGMENT.grouping ; j++) {
264- int indexSet = realIndex + (reversed ? -j : j);
262+ uint16_t indexSet = realIndex + (IS_REVERSE ? -j : j);
265263 if (indexSet >= SEGMENT.start && indexSet < SEGMENT.stop ) {
266264 if (IS_MIRROR) { // set the corresponding mirrored pixel
267265 uint16_t indexMir = SEGMENT.stop - indexSet + SEGMENT.start - 1 ;
@@ -273,16 +271,15 @@ void WS2812FX::setPixelColor(uint16_t i, byte r, byte g, byte b, byte w)
273271 busses.setPixelColor (indexMir, col);
274272 }
275273 /* offset/phase */
276- indexSet += SEGMENT.offset ;
277- if (indexSet >= SEGMENT.stop ) indexSet -= len;
274+ indexSet += SEGMENT.offset ;
275+ if (indexSet >= SEGMENT.stop ) indexSet -= len;
278276
279277 if (indexSet < customMappingSize) indexSet = customMappingTable[indexSet];
280278 busses.setPixelColor (indexSet, col);
281279 }
282280 }
283281 } else { // live data, etc.
284282 if (i < customMappingSize) i = customMappingTable[i];
285-
286283 uint32_t col = ((w << 24 ) | (r << 16 ) | (g << 8 ) | (b));
287284 busses.setPixelColor (i, col);
288285 }
@@ -563,6 +560,7 @@ uint32_t WS2812FX::getPixelColor(uint16_t i)
563560 }
564561
565562 if (i < customMappingSize) i = customMappingTable[i];
563+ if (i >= _length) return 0 ;
566564
567565 return busses.getPixelColor (i);
568566}
@@ -584,15 +582,6 @@ uint32_t WS2812FX::getLastShow(void) {
584582 return _lastShow;
585583}
586584
587- // TODO these need to be on a per-strip basis
588- uint8_t WS2812FX::getColorOrder (void ) {
589- return COL_ORDER_GRB;
590- }
591-
592- void WS2812FX::setColorOrder (uint8_t co) {
593- // bus->SetColorOrder(co);
594- }
595-
596585void WS2812FX::setSegment (uint8_t n, uint16_t i1, uint16_t i2, uint8_t grouping, uint8_t spacing) {
597586 if (n >= MAX_NUM_SEGMENTS) return ;
598587 Segment& seg = _segments[n];
@@ -603,7 +592,11 @@ void WS2812FX::setSegment(uint8_t n, uint16_t i1, uint16_t i2, uint8_t grouping,
603592 if (seg.stop ) setRange (seg.start , seg.stop -1 , 0 ); // turn old segment range off
604593 if (i2 <= i1) // disable segment
605594 {
606- seg.stop = 0 ;
595+ seg.stop = 0 ;
596+ if (seg.name ) {
597+ delete[] seg.name ;
598+ seg.name = nullptr ;
599+ }
607600 if (n == mainSegment) // if main segment is deleted, set first active as main segment
608601 {
609602 for (uint8_t i = 0 ; i < MAX_NUM_SEGMENTS; i++)
@@ -628,6 +621,7 @@ void WS2812FX::setSegment(uint8_t n, uint16_t i1, uint16_t i2, uint8_t grouping,
628621}
629622
630623void WS2812FX::resetSegments () {
624+ for (uint8_t i = 0 ; i < MAX_NUM_SEGMENTS; i++) if (_segments[i].name ) delete _segments[i].name ;
631625 mainSegment = 0 ;
632626 memset (_segments, 0 , sizeof (_segments));
633627 // memset(_segment_runtimes, 0, sizeof(_segment_runtimes));
@@ -656,6 +650,25 @@ void WS2812FX::resetSegments() {
656650 _segment_runtimes[0 ].reset ();
657651}
658652
653+ void WS2812FX::populateDefaultSegments () {
654+ uint16_t length = 0 ;
655+ for (uint8_t i=0 ; i<busses.getNumBusses (); i++) {
656+ Bus *bus = busses.getBus (i);
657+ if (bus == nullptr ) continue ;
658+ _segments[i].start = bus->getStart ();
659+ length += bus->getLength ();
660+ _segments[i].stop = _segments[i].start + bus->getLength ();
661+ _segments[i].mode = DEFAULT_MODE;
662+ _segments[i].colors [0 ] = DEFAULT_COLOR;
663+ _segments[i].speed = DEFAULT_SPEED;
664+ _segments[i].intensity = DEFAULT_INTENSITY;
665+ _segments[i].grouping = 1 ;
666+ _segments[i].setOption (SEG_OPTION_SELECTED, 1 );
667+ _segments[i].setOption (SEG_OPTION_ON, 1 );
668+ _segments[i].opacity = 255 ;
669+ }
670+ }
671+
659672// After this function is called, setPixelColor() will use that segment (offsets, grouping, ... will apply)
660673void WS2812FX::setPixelSegment (uint8_t n)
661674{
@@ -1033,18 +1046,34 @@ uint32_t WS2812FX::color_from_palette(uint16_t i, bool mapping, bool wrap, uint8
10331046
10341047
10351048// load custom mapping table from JSON file
1036- void WS2812FX::deserializeMap (void ) {
1037- if (!WLED_FS.exists (" /ledmap.json" )) return ;
1038- DynamicJsonDocument doc (JSON_BUFFER_SIZE); // full sized buffer for larger maps
1049+ void WS2812FX::deserializeMap (uint8_t n) {
1050+ char fileName[32 ];
1051+ strcpy_P (fileName, PSTR (" /ledmap" ));
1052+ if (n) sprintf (fileName +7 , " %d" , n);
1053+ strcat (fileName, " .json" );
1054+ bool isFile = WLED_FS.exists (fileName);
1055+
1056+ if (!isFile) {
1057+ // erase custom mapping if selecting nonexistent ledmap.json (n==0)
1058+ if (!n && customMappingTable != nullptr ) {
1059+ customMappingSize = 0 ;
1060+ delete[] customMappingTable;
1061+ customMappingTable = nullptr ;
1062+ }
1063+ return ;
1064+ }
10391065
1040- DEBUG_PRINTLN (F (" Reading LED map from /ledmap.json..." ));
1066+ DynamicJsonDocument doc (JSON_BUFFER_SIZE); // full sized buffer for larger maps
1067+ DEBUG_PRINT (F (" Reading LED map from " ));
1068+ DEBUG_PRINTLN (fileName);
10411069
1042- if (!readObjectFromFile (" /ledmap.json " , nullptr , &doc)) return ; // if file does not exist just exit
1070+ if (!readObjectFromFile (fileName , nullptr , &doc)) return ; // if file does not exist just exit
10431071
1072+ // erase old custom ledmap
10441073 if (customMappingTable != nullptr ) {
1074+ customMappingSize = 0 ;
10451075 delete[] customMappingTable;
10461076 customMappingTable = nullptr ;
1047- customMappingSize = 0 ;
10481077 }
10491078
10501079 JsonArray map = doc[F (" map" )];
0 commit comments