Skip to content

Commit c9288c7

Browse files
author
Ethan Ransdell
committed
Ability to set a dynamic number of strips
1 parent be586ba commit c9288c7

File tree

4 files changed

+76
-71
lines changed

4 files changed

+76
-71
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: 69 additions & 68 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,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

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)