-
Notifications
You must be signed in to change notification settings - Fork 4
/
water_torture.cpp
233 lines (198 loc) · 4.99 KB
/
water_torture.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
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
/**
* Water_Torture.cpp
*
* Adapted for FastLED from Danny Havenith's code:
* https://github.com/DannyHavenith/ws2811
*
* @author Dougal Campbell <dougal@gunters.org>
* @link https://github.com/dougalcampbell
* @license BSL 1.0
*/
#include "Water_Torture.h"
/**
* Constructor
*
* Sets a few sane defaults, but gives you complete freedom to
* override them, of course. Dont forget to call setFastled()
* and animate().
*/
Water_Torture::Water_Torture()
{
state = WT_SWELLING;
color = CRGB::Blue;
position = 0;
speed = 0;
droplet_pause = 10;
}
/**
* Deconstructor
*/
Water_Torture::~Water_Torture()
{
// Go dark on destruction
color = CRGB::Black;
fastled->clearLedData();
fastled->showLeds();
}
/**
* State machine. Wait for the nextEvent time, and change states.
*
* WT_WAITING -> WT_SWELLING
* ^ |
* | v
* WT_BOUNCING <- WT_FALLING
*
*/
void Water_Torture::step() {
if ( state == WT_FALLING || state == WT_BOUNCING ) {
position += speed;
speed += gravity;
// If we hit the bottom...
const uint16_t maxpos16 = maxpos << 8;
if (position > maxpos16) {
if ( state == WT_BOUNCING ) {
// This is the second collision, deactivate.
state = WT_WAITING;
} else {
// reverse direction and dampen the speed
position = maxpos16 - (position - maxpos16);
speed = -speed/3;
color = scale(color, collision_scaling);
state = WT_BOUNCING;
}
}
} else if ( state == WT_SWELLING ) {
++position;
if (color.getLuma() <= 10 || color.getLuma() - position/2 <= 10) {
state = WT_FALLING;
position = 0;
}
}
}
// perform one step and draw
void Water_Torture::step(CRGB *arr) {
step();
draw( arr );
}
void Water_Torture::draw(CRGB *arr) {
uint8_t position8 = position >> 8;
uint8_t remainder = position; // get lower bits
// Allow direction to be reversed:
uint16_t startpos = (reverse_dir ? maxpos : 0);
uint16_t endpos = (reverse_dir ? 0 : maxpos);
uint16_t where;
if (reverse_dir) {
if (position8 > maxpos) {
where = 0;
} else {
where = maxpos - position8;
}
} else {
where = position8;
}
if (state == WT_FALLING || state == WT_BOUNCING) {
add_clipped_to(&leds[where], scale(color, 256 - remainder));
if (remainder) {
// Also light the adjacent pixel, to add to the motion illusion:
add_clipped_to( &leds[where + ( (reverse_dir && where) ? -1 : 1 )], scale(color, where));
}
if (state == WT_BOUNCING) {
add_clipped_to(&leds[endpos], color);
}
} else if (state == WT_SWELLING) {
add_clipped_to( &leds[startpos], scale(color, where));
}
}
bool Water_Torture::is_active() const {
return (state != WT_WAITING);
}
static uint8_t Water_Torture::add_clipped(uint16_t left, uint16_t right) {
uint16_t result = left + right;
if (result > 255) result = 255;
return result;
}
static void Water_Torture::add_clipped_to(CRGB *left, CRGB right) {
left->red = add_clipped(left->red, right.red);
left->green = add_clipped(left->green, right.green);
left->blue = add_clipped(left->blue, right.blue);
}
static uint8_t Water_Torture::mult( uint8_t value, uint16_t multiplier) {
return (static_cast<uint16_t>( value) * multiplier) >> 8;
}
static CRGB Water_Torture::scale(CRGB value, uint16_t amplitude) {
CRGB newcolor;
newcolor.red = mult(value.red, amplitude);
newcolor.green = mult(value.green, amplitude);
newcolor.blue = mult(value.blue, amplitude);
return newcolor;
}
void Water_Torture::animate() {
if (droplet_pause) {
--droplet_pause;
} else {
if (!is_active()) {
// Create a new droplet
// Reset to original base color.
color = base_color;
droplet_pause = 200 + random16() % 500;
state = (random8() < 128) ? WT_SWELLING : WT_FALLING;
// reset positon & speed for new droplet
position = 0;
speed = 0;
}
}
fastled->clearLedData();
step(leds);
fastled->showLeds();
FastLED.delay(1);
}
/**
* Getters
*/
uint32_t Water_Torture::getColor() {
return color;
}
uint8_t Water_Torture::getState() {
return state;
}
int16_t Water_Torture::getSpeed() {
return speed;
}
int16_t Water_Torture::getGravity() {
return gravity;
}
bool Water_Torture::getReverse() {
return reverse_dir;
}
/**
* Setters
*/
void Water_Torture::setColor(CRGB newColor) {
base_color = newColor;
color = base_color;
}
void Water_Torture::setState(States newState) {
state = newState;
}
void Water_Torture::setSpeed(int16_t newspeed) {
speed = newspeed;
}
void Water_Torture::setGravity(int16_t newgravity) {
gravity = newgravity;
}
void Water_Torture::setReverse(bool rev) {
reverse_dir = rev;
}
void Water_Torture::setFastled(CLEDController* fled) {
fastled = fled;
leds = fastled->leds();
NumLeds = fastled->size();
maxpos = NumLeds - 1;
droplet_pause = 1;
position = 0;
speed = 0;
}
void Water_Torture::setLeds(CRGB* arr) {
leds = arr;
}
// - fin -