2828#ifndef BASE_H
2929#define BASE_H
3030
31+ #include < vector>
32+
3133class Base
3234{
3335 // LED strip number
3436 int ledsNumber = 0 ;
35- // NeoPixelBusLibrary primary object
36- LED_DRIVER* ledStrip1 = nullptr ;
37- // NeoPixelBusLibrary second object
38- LED_DRIVER2* ledStrip2 = nullptr ;
37+
38+ // Should I use sk6812p instead?
39+ std::vector<LED_DRIVER*> ledStrips;
40+ std::vector<int > ledCounts = {240 , 129 , 150 };
41+
3942 // frame is set and ready to render
4043 bool readyToRender = false ;
4144
@@ -53,60 +56,50 @@ class Base
5356 // queue end position
5457 volatile int queueEnd = 0 ;
5558
56- inline int getLedsNumber ()
59+ inline int getLedCount ()
5760 {
58- return ledsNumber;
61+ int sum = 0 ;
62+ for (int ledCount : ledCounts) {
63+ sum += ledCount;
64+ }
65+ return sum;
5966 }
6067
61- inline LED_DRIVER* getLedStrip1 ()
68+ void initializeLedStrips ()
6269 {
63- return ledStrip1;
70+ for (LED_DRIVER* ledStrip : ledStrips) {
71+ delete ledStrip;
72+ }
73+ ledStrips.clear ();
74+
75+ for (int ledCount : ledCounts) {
76+ LED_DRIVER* ledStrip = new LED_DRIVER (ledCount, DATA_PIN);
77+ ledStrips.push_back (ledStrip);
78+ }
6479 }
6580
66- inline LED_DRIVER2* getLedStrip2 ()
81+ inline int getLedStripCount ()
6782 {
68- return ledStrip2 ;
83+ return ledStrips. size () ;
6984 }
7085
71- void initLedStrip (int count )
86+ inline LED_DRIVER* getLedStrip (int index )
7287 {
73- if (ledStrip1 != nullptr )
74- {
75- delete ledStrip1;
76- ledStrip1 = nullptr ;
77- }
78-
79- if (ledStrip2 != nullptr )
80- {
81- delete ledStrip2;
82- ledStrip2 = nullptr ;
88+ if (index < 0 || index >= ledStrips.size ()) {
89+ return nullptr ;
8390 }
91+ return ledStrips[index];
92+ }
8493
85- ledsNumber = count;
86-
87- #if defined(SECOND_SEGMENT_START_INDEX)
88- if (ledsNumber > SECOND_SEGMENT_START_INDEX)
89- {
90- #if defined(NEOPIXEL_RGBW) || defined(NEOPIXEL_RGB)
91- ledStrip1 = new LED_DRIVER (SECOND_SEGMENT_START_INDEX, DATA_PIN);
92- ledStrip2 = new LED_DRIVER2 (ledsNumber - SECOND_SEGMENT_START_INDEX, DATA_PIN);
93- #else
94- ledStrip1 = new LED_DRIVER (SECOND_SEGMENT_START_INDEX);
95- ledStrip1->Begin (CLOCK_PIN, 12 , DATA_PIN, 15 );
96- ledStrip2 = new LED_DRIVER2 (ledsNumber - SECOND_SEGMENT_START_INDEX);
97- ledStrip2->Begin (SECOND_SEGMENT_CLOCK_PIN, 12 , SECOND_SEGMENT_DATA_PIN, 15 );
98- #endif
94+ inline int getLedStripIndexByPixel (int pixelIndex) {
95+ int sum = 0 ;
96+ for (int i = 0 ; i < ledCounts.size (); i++) {
97+ sum += ledCounts[i];
98+ if (pixelIndex < sum) {
99+ return i;
99100 }
100- #endif
101-
102- if (ledStrip1 == nullptr )
103- {
104- #if defined(NEOPIXEL_RGBW) || defined(NEOPIXEL_RGB)
105- ledStrip1 = new LED_DRIVER (ledsNumber, DATA_PIN);
106- #else
107- ledStrip1 = new LED_DRIVER (ledsNumber, SPI_INTERFACE, DATA_PIN, CLOCK_PIN);
108- #endif
109101 }
102+ return -1 ;
110103 }
111104
112105 /* *
@@ -130,42 +123,50 @@ class Base
130123 if (newFrame)
131124 readyToRender = true ;
132125
126+ LED_DRIVER* firstLedStrip = getLedStrip (0 );
127+
133128 if (readyToRender &&
134- (ledStrip1 != nullptr && ledStrip1 ->isReadyBlocking ()))
129+ (firstLedStrip != nullptr && firstLedStrip ->isReadyBlocking ()))
135130 {
136131 statistics.increaseShow ();
137132 readyToRender = false ;
138133
139- // display segments
140- # if defined(SECOND_SEGMENT_START_INDEX)
141- ledStrip1-> renderAllLanes ();
142- # else
143- ledStrip1 ->renderSingleLane ();
144- # endif
134+ if ( getLedStripCount () > 1 ) {
135+ firstLedStrip-> renderAllLanes ();
136+ } else {
137+ // render only the first strip
138+ firstLedStrip ->renderSingleLane ();
139+ }
145140 }
146141 }
147142
148- inline bool setStripPixel (uint16_t pix , ColorDefinition &inputColor)
143+ inline bool setStripPixel (uint16_t pixelIndex , ColorDefinition &inputColor)
149144 {
150- if (pix < ledsNumber)
145+ // return true if there is another pixel after this one
146+
147+ if (pixelIndex < getLedCount ())
151148 {
152- #if defined(SECOND_SEGMENT_START_INDEX)
153- if (pix < SECOND_SEGMENT_START_INDEX)
154- ledStrip1->SetPixel (pix, inputColor);
155- else
156- {
157- #if defined(SECOND_SEGMENT_REVERSED)
158- ledStrip2->SetPixel (ledsNumber - pix - 1 , inputColor);
159- #else
160- ledStrip2->SetPixel (pix - SECOND_SEGMENT_START_INDEX, inputColor);
161- #endif
162- }
163- #else
164- ledStrip1->SetPixel (pix, inputColor);
165- #endif
149+ // figure out which strip it's in
150+ int stripIndex = getLedStripIndexByPixel (pixelIndex);
151+ if (stripIndex < 0 ) {
152+ return false ;
153+ }
154+
155+ LED_DRIVER* ledStrip = getLedStrip (stripIndex);
156+ if (ledStrip == nullptr ) {
157+ return false ;
158+ }
159+
160+ // set the pixel
161+ ledStrip->SetPixel (pixelIndex - ledCounts[stripIndex], inputColor);
162+
163+ // FIXME: Reverse
164+ // #if defined(SECOND_SEGMENT_REVERSED)
165+ // ledStrip2->SetPixel(ledsNumber - pixelIndex - 1, inputColor);
166166 }
167167
168- return (pix + 1 < ledsNumber);
168+ // return true if the pixel is not the last one
169+ return (pixelIndex + 1 < getLedCount ());
169170 }
170171} base;
171172
0 commit comments