-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFlow.cpp
More file actions
242 lines (192 loc) · 8.01 KB
/
Flow.cpp
File metadata and controls
242 lines (192 loc) · 8.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
Copyright 2014 Mark Briggs
This file is part of ColorRing.
ColorRing is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
ColorRing is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
There is a copy of the GNU General Public License in the
accompanying COPYING file
*/
#include "Flow.h"
#include "Shift.h"
#include "SetSeqPixels.h"
Flow::Flow() {
}
Flow::Flow(Adafruit_NeoPixel* strip,
byte startPixelNum,
byte endPixelNum,
byte numSections,
byte numPixelsEachColor,
byte colorSeriesNumIter,
byte numPixelsToSkip,
word animDelay,
word pauseAfter,
// boolBits
bool direction,
bool clearStripBefore,
bool gradiate,
bool gradiateLastPixelFirstColor,
byte numColorsInSeries,
PixelColor *colorSeriesArr)
{
init(strip, startPixelNum, endPixelNum, numSections, numPixelsEachColor, colorSeriesNumIter, numPixelsToSkip, animDelay, pauseAfter, direction, clearStripBefore, gradiate, gradiateLastPixelFirstColor, numColorsInSeries, colorSeriesArr);
}
Flow::Flow(Adafruit_NeoPixel* strip, byte* stripCmdArray) {
byte startPixelNum = stripCmdArray[1];
byte endPixelNum = stripCmdArray[2];
byte numSections = stripCmdArray[3];
byte numPixelsEachColor = stripCmdArray[4];
byte colorSeriesNumIter = stripCmdArray[5];
byte numPixelsToSkip = stripCmdArray[6];
word animDelay = (stripCmdArray[7] << 8) + stripCmdArray[8];
word pauseAfter = (stripCmdArray[9] << 8) + stripCmdArray[10];
// boolBits
byte boolBits = stripCmdArray[11];
/*
//bool destructive = bitChecker(BOOLBIT_DESTRUCTIVE, boolBits);
bool direction = bitChecker(BOOLBIT_DIRECTION, boolBits);
//bool wrap = bitChecker(BOOLBIT_WRAP, boolBits);
//bool isAnim = bitChecker(BOOLBIT_ISANIM, boolBits);
bool clearStripBefore = bitChecker(BOOLBIT_CLEARSTRIP, boolBits);
bool gradiate = bitChecker(BOOLBIT_GRADIATE, boolBits);
bool gradiateLastPixelFirstColor = bitChecker(BOOLBIT_GRADIATE_LASTPIXEL_FIRSTCOLOR, boolBits);
*/
setBoolBitVars(boolBits, destructive, direction, wrap, isAnim, clearStripBefore, gradiate, gradiateLastPixelFirstColor);
// colorSeriesArr
byte numColorsInSeries = stripCmdArray[12];
byte csaFirstBytePos = 13;
//byte maxNumColors = (MAX_STRIPCMD_SIZE - csaFirstBytePos) / 3; // stripCmd's are 32-bytes total (MAX_STRIPCMD_SIZE).
//PixelColor colorSeriesArr[maxNumColors];
for (int i = 0; i < numColorsInSeries; i++) {
byte bOffset = csaFirstBytePos + (3*i);
colorSeriesArr[i] = PixelColor(stripCmdArray[bOffset], stripCmdArray[bOffset+1], stripCmdArray[bOffset+2]);
}
init(strip, startPixelNum, endPixelNum, numSections, numPixelsEachColor, colorSeriesNumIter, numPixelsToSkip, animDelay, pauseAfter, direction, clearStripBefore, gradiate, gradiateLastPixelFirstColor, numColorsInSeries, colorSeriesArr);
}
void Flow::init(Adafruit_NeoPixel* strip,
byte startPixelNum,
byte endPixelNum,
byte numSections,
byte numPixelsEachColor,
byte colorSeriesNumIter,
byte numPixelsToSkip,
word animDelay,
word pauseAfter,
// boolBits
bool direction,
bool clearStripBefore,
bool gradiate,
bool gradiateLastPixelFirstColor,
byte numColorsInSeries,
PixelColor *colorSeriesArr)
{
//Note 'animation' is assumed.
this->cmdType = 3;
this->strip = strip;
this->startPixelNum = startPixelNum;
this->endPixelNum = endPixelNum;
this->numSections = numSections;
this->numPixelsEachColor = numPixelsEachColor;
this->colorSeriesNumIter = colorSeriesNumIter;
this->numPixelsToSkip = numPixelsToSkip;
this->numIter = numColorsInSeries * numPixelsEachColor * colorSeriesNumIter;
this->animDelay = animDelay;
this->pauseAfter = pauseAfter;
// boolBits
this->destructive = true; // Hard-coded.
this->direction = direction;
this->wrap = NULL; // N/A
this->isAnim = true; // hard-code this, becuase it HAS to be animated.
this->clearStripBefore = clearStripBefore;
this->gradiate = gradiate;
this->gradiateLastPixelFirstColor = gradiateLastPixelFirstColor;
this->numColorsInSeries = numColorsInSeries;
for (int i = 0; i < numColorsInSeries; i++) {
this->colorSeriesArr[i] = colorSeriesArr[i];
}
if (gradiate && numColorsInSeries > 1 && numPixelsEachColor > 0) {
multiGradient = MultiGradient(colorSeriesArr, numColorsInSeries, numPixelsEachColor, gradiateLastPixelFirstColor); // Makes it "valid"
}
// Init other variables
//this->inOutStr = strip->getPin() == OUT_STRIP_PIN ? "OUTSIDE" : "INSIDE";
}
void Flow::step(boolean isShowStrip) {
//cout << "Flow::step(" << (int)isShowStrip << ")" << endl;
if (!isPauseMode) {
//cout << "Flow::step(" << (int)isShowStrip << ") [not paused]" << endl;
if (numSections == 0 || (endPixelNum - startPixelNum) == 0 || numPixelsEachColor < 1 || numColorsInSeries < 1) {
// Invalid scenarios
return;
}
lscStepPreCommon();
if (gradiate && multiGradient.getIsValid() && numColorsInSeries > 1) {
currPixelColor = multiGradient.getTweenPixelColor(currIteration);
} else {
// Don't worry about gradients
//if (numPixelsEachColor > 0 && numColorsInSeries > 0) {
currPixelColor = colorSeriesArr[(currIteration / numPixelsEachColor) % numColorsInSeries];
//} else {
//currPixelColor = BLACK;
//}
}
//cout << F("currPixelColor: "); currPixelColor.println();
vector<byte> startPixelNums(numSections);
vector<byte> endPixelNums(numSections);
vector<boolean> directions(numSections);
// Do all the math
byte numPixelsPerSection = (endPixelNum - startPixelNum + 1) / numSections;
for (int i = 0; i < numSections; i++) {
startPixelNums[i] = startPixelNum + (i * numPixelsPerSection);
//cout << "startPixelNums[" << i << "]: " << (int)startPixelNums[i] << endl;
endPixelNums[i] = startPixelNums[i] + numPixelsPerSection - 1;
//cout << "endPixelNums[" << i << "]: " << (int)endPixelNums[i] << endl;
//directions[i] = (i % 2 == 0) ? CW : CCW;
directions[i] = (i % 2 == 0) ? direction : !direction;
//cout << "directions[" << i << "]: " << (int)directions[i] << endl;
}
//Serial.print("Free RAM Flow: "); Serial.println(getFreeRam(), DEC);
// Execute
for (int i = 0; i < numSections; i++) {
singleFlow(strip, startPixelNums[i], endPixelNums[i], currPixelColor, directions[i], numPixelsToSkip);
}
lscStepPostCommon(isShowStrip);
}
}
void Flow::singleFlow(Adafruit_NeoPixel* strip, byte startPixelNum, byte endPixelNum, PixelColor pixelColor, boolean direction, byte numPixelsToSkip) {
// First Shift
//Shift shift(strip, startPixelNum, endPixelNum, 0, 1, 0, 0, direction, NOWRAP, NONANIMATED);
//shift.exec(NOSHOWSTRIP);
Shift *shift = new Shift(strip, startPixelNum, endPixelNum, numPixelsToSkip, 1, 0, 0, direction, NOWRAP, NONANIMATED);
shift->exec(NOSHOWSTRIP);
delete(shift);
// Then set "Source" pixel
byte sourcePixelNum = direction ? startPixelNum : endPixelNum;
/*
byte numColorsInSeries = 1;
PixelColor colorSeriesArr[numColorsInSeries]; colorSeriesArr[0] = pixelColor;
SetSeqPixels ssp(strip, sourcePixelNum, 1, 1, 0, 0, 0, DESTRUCTIVE, direction, NONANIMATED, NOCLEAR, NOGRADIATE, GRADIATE_LASTPIXEL_LASTCOLOR, numColorsInSeries, colorSeriesArr);
ssp.exec(NOSHOWSTRIP); // or ssp.step()
*/
strip->setPixelColor(sourcePixelNum, pixelColor.getLongVal()); // Don't ->show() yet
}
void Flow::reset() {
//cout << "Flow::reset()" << endl;
lscResetCommon();
// Nothing else needed, I think
}
/*
void printStrip(Adafruit_NeoPixel* strip, int numPixels) {
//uint8_t pixels[60*3] = strip->getPixels();
for (int i = 0; i < numPixels; i++) {
uint32_t color = strip->getPixelColor(i);
PixelColor pc = PixelColor(color);
//Serial.print(pixels[i]); Serial.print(",");
Serial.print(i); Serial.print(": "); Serial.print(pc.R); Serial.print(","); Serial.print(pc.G); Serial.print(","); Serial.print(pc.B); Serial.println();
}
}
*/