This repository has been archived by the owner on Mar 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RainbowStyle.cpp
64 lines (55 loc) · 1.57 KB
/
RainbowStyle.cpp
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
#include <Adafruit_NeoPixel.h>
#include "Arduino.h"
#include "RainbowStyle.h"
#include "PixelBuffer.h"
RainbowStyle::RainbowStyle(String name, PixelBuffer* pixelBuffer) : LightStyle(name, pixelBuffer) {
m_nextUpdate = 0;
m_currentHue = 0;
}
void RainbowStyle::update() {
if (millis() < m_nextUpdate) {
return;
}
uint32_t newColor = Adafruit_NeoPixel::ColorHSV(m_currentHue);
shiftColorUsingPattern(newColor);
incrementHue();
m_nextUpdate = millis() + getIterationDelay();
}
void RainbowStyle::reset()
{
int numBlocks = getNumberOfBlocksForPattern();
if (numBlocks > 100) {
// The only patterns with this many blocks are the line patterns.
// Instead of shifting tons of times, just set the pixels directly.
for (int i = numBlocks - 1; i >= 0; i--) {
m_pixelBuffer->setPixel(i, Adafruit_NeoPixel::ColorHSV(m_currentHue));
incrementHue();
}
return;
}
for (int i = 0; i < numBlocks - 1; i++) {
shiftColorUsingPattern(Adafruit_NeoPixel::ColorHSV(m_currentHue));
incrementHue();
}
}
int RainbowStyle::getIterationDelay() {
// Convert "speed" to a delay.
// Speed ranges from 1 to 100.
int minDelay = 5;
int maxDelay = 500;
double m = (maxDelay - minDelay)/-99.0;
double b = maxDelay - m;
int delay = m_speed*m + b;
return delay;
}
void RainbowStyle::incrementHue() {
// Convert "step" to an increment.
int maxInc = 1000;
int minInc = 5;
int maxStep = 100;
int minStep = 1;
double m = (double)(maxInc - minInc)/(maxStep - minStep);
double b = minInc - m;
int inc = m_step*m + b;
m_currentHue += inc;
}