This repository has been archived by the owner on Jun 11, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
led.c
366 lines (338 loc) · 9.2 KB
/
led.c
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/* Name: led.c
* Project: Olark Observer Hardware
* Author: Nathan Sollenberger
* Creation Date: 2016-01-03
* Copyright: (c) 2016 Nathan Sollenberger
* License: GPL
*/
#include "led.h"
/* These pins may be re-ordered, but if different pins are used,
enablePWM must be updated as well. Be sure to avoid conflict
with USB DATA- and DATA+ pins */
#define RED_PIN PB4
#define GREEN_PIN PB0
#define BLUE_PIN PB1
#define RED_OCP OCR1B
#define GREEN_OCP OCR0A
#define BLUE_OCP OCR0B
#define PULSE_MIN_BRIGHTNESS 140
#define PULSE_MAX_BRIGHTNESS 255
#define PULSE_PAUSE_DURATION 60
#define PULSE_INCREASE 1
#define PULSE_DECREASE -1
#define PULSE_PAUSE 0
#define FLASH_DURATION 30
#define RAINBOW_TRANSITION_DURATION 200
#define MOODLIGHT_FADE_DELAY 2
uint8_t fadePhase;
uint8_t fadeValue;
uint8_t colorMask[3];
uint8_t nextColor[3];
void initLED(void)
{
DDRB |= _BV(RED_PIN) | _BV(BLUE_PIN) | _BV(GREEN_PIN);
}
void toggleLED(void)
{
PORTB ^= _BV(RED_PIN) | _BV(BLUE_PIN) | _BV(GREEN_PIN);
}
void turnOffLED(void)
{
PORTB &= ~( _BV(RED_PIN) | _BV(BLUE_PIN) | _BV(GREEN_PIN) );
}
void initTimers(void)
{
TCCR0B |= _BV(CS02) | _BV(CS00); // clock/1024 ~60Hz
TCCR1 |= _BV(CS13) | _BV(CS11) | _BV(CS10); // clock/1024 ~60Hz
}
void enableIdleTimer(void)
{
disablePWM();
turnOffLED();
enableFade();
fadeValue = 0;
fadePhase = 0;
fadeFunction = &idleTimer;
}
void enablePulseEffect(uint8_t red, uint8_t green, uint8_t blue)
{
enablePWM();
enableFade();
fadePhase = PULSE_INCREASE;
fadeValue = PULSE_MIN_BRIGHTNESS;
colorMask[0] = red;
colorMask[1] = green;
colorMask[2] = blue;
fadeFunction = &pulseEffect;
}
void enableFlashEffect(uint8_t red, uint8_t green, uint8_t blue)
{
disablePWM();
turnOffLED();
enableFade();
// Turn on the LED the next time flashEffect is called
fadeValue = FLASH_DURATION - 1;
colorMask[0] = red;
colorMask[1] = green;
colorMask[2] = blue;
fadeFunction = &flashEffect;
}
void enableMoodlightEffect()
{
enablePWM();
enableFade();
fadePhase = 0;
fadeValue = 0;
nextColor[0] = colorMask[0];
nextColor[1] = colorMask[1];
nextColor[2] = colorMask[2];
fadeFunction = &moodLightEffect;
}
void setPWMDutyCycle(uint8_t redCycle, uint8_t greenCycle, uint8_t blueCycle) {
RED_OCP = redCycle;
GREEN_OCP = greenCycle;
BLUE_OCP = blueCycle;
}
void enablePWM(void)
{
cli();
// Fast PWM mode, clear on compare match
TCCR0A |= _BV(WGM01) | _BV(WGM00) | _BV(COM0A1) | _BV(COM0B1);
// PWM mode, clear on compare match with PB3 not connected
GTCCR |= _BV(PWM1B) | _BV(COM1B1);
setPWMDutyCycle(0, 0, 0);
sei();
}
void disablePWM(void)
{
TCCR0A = 0;
GTCCR = 0;
}
void enableFade(void)
{
cli();
// Enable overflow interrupt
TIMSK = _BV(TOIE1);
sei();
}
void disableFade(void)
{
TIMSK &= ~_BV(TOIE1);
}
ISR(TIMER1_OVF_vect)
{
fadeTick = 1;
}
void pulseEffect(void)
{
static uint8_t pauseValue;
if (fadePhase & (PULSE_INCREASE | PULSE_DECREASE)) {
fadeValue += fadePhase;
uint8_t redCycle = ((uint16_t)fadeValue * colorMask[0]) >> 8;
uint8_t greenCycle = ((uint16_t)fadeValue * colorMask[1]) >> 8;
uint8_t blueCycle = ((uint16_t)fadeValue * colorMask[2]) >> 8;
setPWMDutyCycle(redCycle, greenCycle, blueCycle);
// Detect min and max
if (fadeValue == PULSE_MIN_BRIGHTNESS) {
fadePhase = PULSE_PAUSE;
pauseValue = 0;
} else if (fadeValue == PULSE_MAX_BRIGHTNESS) {
fadePhase = PULSE_DECREASE;
}
} else {
pauseValue++;
if (pauseValue == PULSE_PAUSE_DURATION)
fadePhase = PULSE_INCREASE;
}
}
void flashEffect(void)
{
fadeValue++;
if (fadeValue == FLASH_DURATION) {
uint8_t redCycle = ((uint16_t)255 * colorMask[0]) >> 8;
uint8_t greenCycle = ((uint16_t)255 * colorMask[1]) >> 8;
uint8_t blueCycle = ((uint16_t)255 * colorMask[2]) >> 8;
enablePWM();
setPWMDutyCycle(redCycle, greenCycle, blueCycle);
} else if (fadeValue == (FLASH_DURATION * 2)) {
disablePWM();
turnOffLED();
fadeValue = 0;
}
}
void idleTimer(void)
{
fadeValue++;
if (fadeValue == 255) {
fadeValue = 0;
fadePhase++;
if (fadePhase == 1) {
enableMoodlightEffect();
}
}
}
void rainbowEffect(void)
{
if (fadeValue < 3) {
fadeValue++;
return;
}
fadeValue = 0;
switch (fadePhase) {
case 0:
if (RED_OCP == 0xFE) // ff0000
fadePhase++;
else
setPWMDutyCycle(RED_OCP+2, 0, 0);
break;
case 1:
if (GREEN_OCP == 0x80) // ff8000
fadePhase++;
else
setPWMDutyCycle(255, GREEN_OCP+1, 0);
break;
case 2:
if (GREEN_OCP == 0xFE) // c0ff00
fadePhase++;
else
setPWMDutyCycle(RED_OCP-1, GREEN_OCP+2, 0);
break;
case 3:
if (RED_OCP == 0x80) // 80ff00
fadePhase++;
else
setPWMDutyCycle(RED_OCP-1, 255, 0);
break;
case 4:
if (RED_OCP == 0x00) // 00FF40
fadePhase++;
else
setPWMDutyCycle(RED_OCP-2, 255, BLUE_OCP+1);
break;
case 5:
if (BLUE_OCP == 0xFD) // 00FFFF
fadePhase++;
else
setPWMDutyCycle(0, 255, BLUE_OCP+3);
break;
case 6:
if (GREEN_OCP == 0x00) // 0000FF
fadePhase++;
else
setPWMDutyCycle(0, GREEN_OCP-3, 255);
break;
case 7:
if (RED_OCP == 0xFE) // FF00FF
fadePhase++;
else
setPWMDutyCycle(RED_OCP+2, 0, 255);
break;
case 8:
if (BLUE_OCP == 0x2D) // 00002D
fadePhase++;
else
setPWMDutyCycle(255, 0, BLUE_OCP-3);
break;
case 9:
if (BLUE_OCP == 0x00) // 000000
fadePhase = 1;
else
setPWMDutyCycle(255, 0, BLUE_OCP-1);
break;
default:
fadePhase = 0;
break;
}
}
uint8_t randomColor(void)
{
// Take the upper byte
return (uint8_t)(rand() >> 8);
}
void increaseColorSaturation(uint8_t *color)
{
uint8_t i;
uint8_t *min = &color[0];
uint8_t *max = &color[0];
// Find the largest and smaller color values
for (i = 1; i < 3; i++) {
if (color[i] < *min) {
min = &color[i];
} else if (color[i] > *max) {
max = &color[i];
}
}
// If the difference is too small, the color is likely to be very washed out
// Fix this by zeroing the smaller value
if ((*max - *min) < 128) {
*min = 0;
}
// Increase the highest color value even more, as well
*max += (255 - *max) >> 1;
}
uint8_t calculateDistance(uint8_t start[3], uint8_t end[3])
{
uint16_t distance;
// Euclidean distance = sqrt(a2-a1^2 + b2-b1^2 + c2-c1^2)
// Divide by 4 to fit sum of squares in uint16_t
distance = (end[0] - start[0]) * (end[0] - start[0]) >> 2;
distance += (end[1] - start[1]) * (end[1] - start[1]) >> 2;
distance += (end[2] - start[2]) * (end[2] - start[2]) >> 2;
// Ranges determined by calculating the distance between two colors
// [0,0,0] and [i,i,i] where i is an easily interpolated value.
if (distance > 12096) {
return 255;
} else if (distance > 2976) {
return 127;
} else if (distance > 720) {
return 63;
} else {
return 31;
}
}
uint8_t interpolate(uint8_t start, uint8_t end, uint8_t distance, uint8_t step)
{
// Linear interpolation = (A * ((distance + 1) - x) + B * x) / distance + 1
// where 0 < x < distance
switch (distance) {
case 255:
return ((uint16_t)start * (256-step) + end * step) >> 8;
case 127:
return ((uint16_t)start * (128-step) + end * step) >> 7;
case 63:
return ((uint16_t)start * (64-step) + end * step) >> 6;
case 31:
return ((uint16_t)start * (32-step) + end * step) >> 5;
default:
return end;
}
}
void moodLightEffect(void)
{
static uint8_t i = 0;
if (i < MOODLIGHT_FADE_DELAY) {
i++;
return;
}
i = 0;
// If we've reached the end of the current phase, begin a new one
if (fadeValue == fadePhase) {
colorMask[0] = nextColor[0];
colorMask[1] = nextColor[1];
colorMask[2] = nextColor[2];
nextColor[0] = randomColor();
nextColor[1] = randomColor();
nextColor[2] = randomColor();
increaseColorSaturation(nextColor);
// Store distance in fadePhase
fadePhase = calculateDistance(colorMask, nextColor);
// Store step in fadeValue
fadeValue = 0;
}
fadeValue++;
setPWMDutyCycle(
interpolate(colorMask[0], nextColor[0], fadePhase, fadeValue),
interpolate(colorMask[1], nextColor[1], fadePhase, fadeValue),
interpolate(colorMask[2], nextColor[2], fadePhase, fadeValue)
);
}