-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsegment_models.cpp
129 lines (82 loc) · 2.44 KB
/
segment_models.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
#include "segment_models.h"
/*** Spot Methods *********************************************/
// Base Spot Constructor
Spot::Spot (double n_position, uint8_t n_width, uint32_t n_color):
position (n_position),
width (n_width),
color (n_color), /* Initializer list */
speed (1.0),
amplitude (1.0),
offset (0.0)
{
}
// Virtual update to be implemented by subclasses
void Spot::update () { }
// Return a 0.0 to 1.0 scaled over {speed} + {offset} seconds
double Spot::percent ()
{
return (int((millis() * speed) + (offset*1000)) % 1000) * 0.001;
}
/*** Circler Methods ******************************************/
// Circler Constructor
Circler::Circler (double n_position, uint8_t n_width, uint32_t n_color):
Spot(n_position, n_width, n_color) /* Base Class Constructor */
{
start_position = position;
}
// Scale from 0 to 1 once a second
void Circler::update ()
{
position = percent () + start_position;
}
/*** Wobbler Methods ******************************************/
// Wobbler Constructor
Wobbler::Wobbler (double n_position, uint8_t n_width, uint32_t n_color):
Spot(n_position, n_width, n_color) /* Base Class Constructor */
{
start_position = position;
}
void Wobbler::update ()
{
// Scale from 0 to 6.28 every second
position = percent () * M_PI * 2;
// Scale -1/1 to 0/1
position = (sin(position) + 1) * 0.5;
position *= amplitude;
position += start_position;
}
/*** Pulsar Methods ******************************************/
// Pulsar Constructor
Pulsar::Pulsar (double n_position, uint8_t n_width, uint32_t n_color):
Spot(n_position, n_width, n_color) /* Base Class Constructor */
{
start_color = color;
}
void Pulsar::update ()
{
double value = percent () * M_PI * 2;
// Scale -1/1 to 0/1
value = (sin(value) + 1) * 0.5;
// Extract original r g b
uint8_t r,g,b;
r = (uint8_t)(start_color >> 16),
g = (uint8_t)(start_color >> 8),
b = (uint8_t)(start_color >> 0);
// Dim brightness
color = led_strip.Color (r * value, g * value, b * value);
}
/*** Grower Methods ******************************************/
// Grower Constructor
Grower::Grower (double n_position, uint8_t n_width, uint32_t n_color):
Spot(n_position, n_width, n_color) /* Base Class Constructor */
{
start_width = width;
}
void Grower::update ()
{
double value = percent () * M_PI * 2;
// Scale -1/1 to 0/1
value = (sin(value) + 1) * 0.5;
// Width 0 to 10
width = value * start_width;
}