Skip to content

Commit 9d3372d

Browse files
committed
Add multiple strips
1 parent be586ba commit 9d3372d

File tree

4 files changed

+88
-54
lines changed

4 files changed

+88
-54
lines changed
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
name: HyperSerialPico CI Build
22

3-
on: [push]
3+
on:
4+
push:
5+
branches:
6+
- "**" # Runs on push to any branch
47

58
jobs:
69

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ bld/
3838

3939
# Visual Studio 2015/2017 cache/options directory
4040
.vs/
41+
.vscode/
4142
# Uncomment if you have tasks that create the project's static files in wwwroot
4243
#wwwroot/
4344

include/base.h

Lines changed: 81 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,17 @@
2828
#ifndef BASE_H
2929
#define BASE_H
3030

31+
#include <vector>
32+
3133
class 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,36 +56,31 @@ class Base
5356
// queue end position
5457
volatile int queueEnd = 0;
5558

56-
inline int getLedsNumber()
57-
{
58-
return ledsNumber;
59-
}
60-
61-
inline LED_DRIVER* getLedStrip1()
59+
inline int getLedCount()
6260
{
63-
return ledStrip1;
64-
}
65-
66-
inline LED_DRIVER2* getLedStrip2()
67-
{
68-
return ledStrip2;
61+
int sum = 0;
62+
for (int ledCount : ledCounts) {
63+
sum += ledCount;
64+
}
65+
return sum;
6966
}
7067

71-
void initLedStrip(int count)
68+
void initializeLedStrips()
7269
{
73-
if (ledStrip1 != nullptr)
74-
{
75-
delete ledStrip1;
76-
ledStrip1 = nullptr;
70+
for (LED_DRIVER* ledStrip : ledStrips) {
71+
delete ledStrip;
7772
}
73+
ledStrips.clear();
7874

79-
if (ledStrip2 != nullptr)
80-
{
81-
delete ledStrip2;
82-
ledStrip2 = nullptr;
83-
}
75+
#if defined(NEOPIXEL_RGBW) || defined(NEOPIXEL_RGB)
76+
for (int ledCount : ledCounts) {
77+
LED_DRIVER* ledStrip = new LED_DRIVER(ledCount, DATA_PIN);
78+
ledStrips.push_back(ledStrip);
79+
}
80+
#else
81+
82+
#endif
8483

85-
ledsNumber = count;
8684

8785
#if defined(SECOND_SEGMENT_START_INDEX)
8886
if (ledsNumber > SECOND_SEGMENT_START_INDEX)
@@ -109,6 +107,30 @@ class Base
109107
}
110108
}
111109

110+
inline int getLedStripCount()
111+
{
112+
return ledStrips.size();
113+
}
114+
115+
inline LED_DRIVER* getLedStrip(int index)
116+
{
117+
if (index < 0 || index >= ledStrips.size()) {
118+
return nullptr;
119+
}
120+
return ledStrips[index];
121+
}
122+
123+
inline int getLedStripIndexByPixel(int pixelIndex) {
124+
int sum = 0;
125+
for (int i = 0; i < ledCounts.size(); i++) {
126+
sum += ledCounts[i];
127+
if (pixelIndex < sum) {
128+
return i;
129+
}
130+
}
131+
return -1;
132+
}
133+
112134
/**
113135
* @brief Check if there is already prepared frame to display
114136
*
@@ -130,42 +152,50 @@ class Base
130152
if (newFrame)
131153
readyToRender = true;
132154

155+
LED_DRIVER* firstLedStrip = getLedStrip(0);
156+
133157
if (readyToRender &&
134-
(ledStrip1 != nullptr && ledStrip1->isReadyBlocking()))
158+
(firstLedStrip != nullptr && firstLedStrip->isReadyBlocking()))
135159
{
136160
statistics.increaseShow();
137161
readyToRender = false;
138162

139-
// display segments
140-
#if defined(SECOND_SEGMENT_START_INDEX)
141-
ledStrip1->renderAllLanes();
142-
#else
143-
ledStrip1->renderSingleLane();
144-
#endif
163+
if (getLedStripCount() > 1) {
164+
firstLedStrip->renderAllLanes();
165+
} else {
166+
// render only the first strip
167+
firstLedStrip->renderSingleLane();
168+
}
145169
}
146170
}
147171

148-
inline bool setStripPixel(uint16_t pix, ColorDefinition &inputColor)
172+
inline bool setStripPixel(uint16_t pixelIndex, ColorDefinition &inputColor)
149173
{
150-
if (pix < ledsNumber)
174+
// return true if there is another pixel after this one
175+
176+
if (pixelIndex < getLedCount())
151177
{
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
178+
// figure out which strip it's in
179+
int stripIndex = getLedStripIndexByPixel(pixelIndex);
180+
if (stripIndex < 0) {
181+
return false;
182+
}
183+
184+
LED_DRIVER* ledStrip = getLedStrip(stripIndex);
185+
if (ledStrip == nullptr) {
186+
return false;
187+
}
188+
189+
// set the pixel
190+
ledStrip->SetPixel(pixelIndex - ledCounts[stripIndex], inputColor);
191+
192+
// FIXME: Reverse
193+
// #if defined(SECOND_SEGMENT_REVERSED)
194+
// ledStrip2->SetPixel(ledsNumber - pixelIndex - 1, inputColor);
166195
}
167196

168-
return (pix + 1 < ledsNumber);
197+
// return true if the pixel is not the last one
198+
return (pixelIndex + 1 < getLedCount());
169199
}
170200
} base;
171201

include/main.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ void processData()
128128
frameState.setState(AwaProtocol::HEADER_A);
129129
else
130130
{
131-
if (ledSize != base.getLedsNumber())
132-
base.initLedStrip(ledSize);
131+
if (ledSize != base.getLedCount())
132+
base.initializeLedStrips();
133133

134134
frameState.setState(AwaProtocol::RED);
135135
}

0 commit comments

Comments
 (0)