-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathanim.cpp
168 lines (143 loc) · 4.29 KB
/
anim.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
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
#include <Adafruit_NeoPixel.h>
#include "color.h"
#include "palette.h"
#include "anim.h"
#include "brightness.h"
//Adafruit's class to operate strip
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(LEDS, PIN, NEO_GRB + NEO_KHZ800);
Anim::Anim()
{
nextms = millis();
}
void Anim::setPeriod(byte period) {
this->period = period;
}
void Anim::setPalette(Palette * pal) {
this->palette = pal;
if (setUpOnPalChange) {
setUp();
}
pinMode(LED_BUILTIN, OUTPUT);
}
bool Anim::run()
{
if ( millis()<=nextms) {
digitalWrite(LED_BUILTIN, LOW);
return false;
}
digitalWrite(LED_BUILTIN, HIGH);
nextms=millis() + period;
if (runImpl != NULL)
{
(this->*runImpl)();
}
//transition coef, if within 0..1 - transition is active
//changes from 1 to 0 during transition, so we interpolate from current color to previous
float transc = (float)((long)transms - (long)millis()) / TRANSITION_MS;
Color * leds_prev = (leds == leds1) ? leds2 : leds1;
if (transc > 0) {
for(int i=0; i<LEDS; i++) {
//transition is in progress
Color c = leds[i].interpolate(leds_prev[i], transc);
//pixels.setPixelColor(i, pixels.Color(c.r, c.g, c.b));
byte r = (int)pgm_read_byte_near(BRI + c.r) * BRIGHTNESS / 256;
byte g = (int)pgm_read_byte_near(BRI + c.g) * BRIGHTNESS / 256;
byte b = (int)pgm_read_byte_near(BRI + c.b) * BRIGHTNESS / 256;
pixels.setPixelColor(i, pixels.Color(r, g, b));
}
} else {
for(int i=0; i<LEDS; i++) {
//regular operation
//pixels.setPixelColor(i, pixels.Color(leds[i].r, leds[i].g, leds[i].b));
byte r = (int)pgm_read_byte_near(BRI + leds[i].r) * BRIGHTNESS / 256;
byte g = (int)pgm_read_byte_near(BRI + leds[i].g) * BRIGHTNESS / 256;
byte b = (int)pgm_read_byte_near(BRI + leds[i].b) * BRIGHTNESS / 256;
pixels.setPixelColor(i, pixels.Color(r, g, b));
}
}
pixels.show();
return true;
}
void Anim::setUp()
{
//pinMode(LED_BUILTIN, OUTPUT);
transms = millis() + TRANSITION_MS;
//switch operation buffers (for transition to operate)
if (leds == leds1) {
leds = leds2;
} else {
leds = leds1;
}
if (setUpImpl != NULL) {
(this->*setUpImpl)();
}
}
void Anim::doSetUp()
{
if (!setUpOnPalChange) {
setUp();
}
}
void Anim::setAnim(byte animInd)
{
switch (animInd) {
case 0:
setUpImpl = &Anim::animRun_SetUp;
runImpl = &Anim::animRun_Run;
setUpOnPalChange = true;
break;
case 1:
setUpImpl = &Anim::animPixieDust_SetUp;
runImpl = &Anim::animPixieDust_Run;
setUpOnPalChange = true;
break;
case 2:
setUpImpl = &Anim::animSparkr_SetUp;
runImpl = &Anim::animSparkr_Run;
setUpOnPalChange = true;
break;
case 3:
setUpImpl = &Anim::animRandCyc_SetUp;
runImpl = &Anim::animRandCyc_Run;
setUpOnPalChange = true;
break;
case 4:
setUpImpl = &Anim::animStars_SetUp;
runImpl = &Anim::animStars_Run;
setUpOnPalChange = false;
break;
case 5:
setUpImpl = &Anim::animSpread_SetUp;
runImpl = &Anim::animSpread_Run;
setUpOnPalChange = false;
break;
case 6:
setUpImpl = &Anim::animFly_SetUp;
runImpl = &Anim::animFly_Run;
setUpOnPalChange = false;
break;
case 7: //special
setUpImpl = &Anim::animBT_SetUp;
runImpl = &Anim::animBT_Run;
setUpOnPalChange = false;
break;
default:
setUpImpl = &Anim::animStart_SetUp;
runImpl = &Anim::animStart_Run;
setUpOnPalChange = true;
break;
}
}
unsigned int rng() {
static unsigned int y = 0;
y += micros(); // seeded with changing number
y ^= y << 2; y ^= y >> 7; y ^= y << 7;
return (y);
}
byte rngb() {
return (byte)rng();
}
Color Anim::leds1[LEDS];
Color Anim::leds2[LEDS];
Color Anim::ledstmp[LEDS];
byte Anim::seq[LEDS];