-
Notifications
You must be signed in to change notification settings - Fork 9
/
time_loop.ino
225 lines (166 loc) · 5.81 KB
/
time_loop.ino
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
#include <Adafruit_NeoPixel.h>
#define PIN 4
// From adaFruit NEOPIXEL goggles example: Gamma correction improves appearance of midrange colors
const uint8_t gamma[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3,
3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 7,
7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10, 11, 11, 11, 12, 12,
13, 13, 13, 14, 14, 15, 15, 16, 16, 17, 17, 18, 18, 19, 19, 20,
20, 21, 21, 22, 22, 23, 24, 24, 25, 25, 26, 27, 27, 28, 29, 29,
30, 31, 31, 32, 33, 34, 34, 35, 36, 37, 38, 38, 39, 40, 41, 42,
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, 68, 69, 70, 71, 72, 73, 75,
76, 77, 78, 80, 81, 82, 84, 85, 86, 88, 89, 90, 92, 93, 94, 96,
97, 99,100,102,103,105,106,108,109,111,112,114,115,117,119,120,
122,124,125,127,129,130,132,134,136,137,139,141,143,145,146,148,
150,152,154,156,158,160,162,164,166,168,170,172,174,176,178,180,
182,184,186,188,191,193,195,197,199,202,204,206,209,211,213,215,
218,220,223,225,227,230,232,235,237,240,242,245,247,250,252,255
};
// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_RGB Pixels are wired for RGB bitstream
// NEO_GRB Pixels are wired for GRB bitstream
// NEO_KHZ400 400 KHz bitstream (e.g. FLORA pixels)
// NEO_KHZ800 800 KHz bitstream (e.g. High Density LED strip)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(16, PIN, NEO_GRB + NEO_KHZ800);
uint32_t milli_color = strip.Color ( 20, 0, 0);
uint32_t second_color = strip.Color ( 0, 0, 20);
uint32_t hour_color = strip.Color ( 0, 20, 0);
uint32_t minute_color = strip.Color ( 15, 10, 10);
uint32_t off_color = strip.Color ( 0, 0, 0);
/* TODO 0.0 to 1.0 percent between current and next value. for color fading */
/* or event based lerping? */
/* TODO gamut values from goggles */
/* TODO smooth sub pixel rendering of bar/pixel ends (careful to avoid 50% average brightness in middle for a pixel */
/* CLOCK */
class ClockPositions
{
public:
uint8_t milli;
uint8_t second;
uint8_t minute;
uint8_t hour;
ClockPositions ();
void update ();
};
ClockPositions::ClockPositions()
{
milli = second = minute = hour = 0;
//DateTime(__DATE__, __TIME__);
}
void ClockPositions::update()
{
second = map ((millis() % 60000), 0, 60000, 0, 15);
milli = map ((millis() % 1000), 0, 1000, 0, 16);
hour = map (10 % 12, 0, 12, 0, 16);
minute = map (31 % 60, 0, 60, 0, 16);
}
/* CLOCK VIEW */
class ClockSegments
{
public:
ClockPositions &positions;
Adafruit_NeoPixel &strip;
ClockSegments (Adafruit_NeoPixel&, ClockPositions&);
void draw ();
void clear ();
void add_color (uint8_t position, uint32_t color);
uint32_t blend (uint32_t color1, uint32_t color2);
};
ClockSegments::ClockSegments (Adafruit_NeoPixel& n_strip, ClockPositions& n_positions): strip (n_strip), positions (n_positions)
{
}
void ClockSegments::draw()
{
clear();
add_color (positions.minute % 16, minute_color);
add_color (positions.hour % 16, hour_color );
add_color ((positions.hour+1) % 16, hour_color );
add_color (positions.second % 16, second_color);
add_color ((positions.second+1) % 16, second_color);
add_color ((positions.second+2) % 16, second_color);
add_color (positions.milli % 16, milli_color);
add_color ((positions.milli+1) % 16, milli_color);
add_color ((positions.milli+2) % 16, milli_color);
strip.show ();
}
void ClockSegments::add_color (uint8_t position, uint32_t color)
{
uint32_t blended_color = blend (strip.getPixelColor (position), color);
/* Gamma mapping */
uint8_t r,b,g;
r = (uint8_t)(blended_color >> 16),
g = (uint8_t)(blended_color >> 8),
b = (uint8_t)(blended_color >> 0);
strip.setPixelColor (position, blended_color);
}
uint32_t ClockSegments::blend (uint32_t color1, uint32_t color2)
{
uint8_t r1,g1,b1;
uint8_t r2,g2,b2;
uint8_t r3,g3,b3;
r1 = (uint8_t)(color1 >> 16),
g1 = (uint8_t)(color1 >> 8),
b1 = (uint8_t)(color1 >> 0);
r2 = (uint8_t)(color2 >> 16),
g2 = (uint8_t)(color2 >> 8),
b2 = (uint8_t)(color2 >> 0);
return strip.Color (constrain (r1+r2, 0, 255), constrain (g1+g2, 0, 255), constrain (b1+b2, 0, 255));
}
void ClockSegments::clear ()
{
for(uint16_t i=0; i<strip.numPixels (); i++) {
strip.setPixelColor (i, off_color);
}
}
/* SIMPLE MIXER */
// add rgb and clamp
/* APP */
ClockPositions positions;
ClockSegments segments(strip, positions);
void setup ()
{
strip.begin ();
strip.show (); // Initialize all pixels to 'off'
}
void loop ()
{
positions.update ();
segments.draw ();
}
// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint32_t wait) {
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}
// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
for(i=0; i< strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
}
strip.show();
delay(wait);
}
}
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
if(WheelPos < 85) {
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
} else if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
} else {
WheelPos -= 170;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
}