-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathColourTime.c
279 lines (218 loc) · 7.88 KB
/
ColourTime.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
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include "ColourTime.h"
// Taken from http://www.mikrocontroller.net/articles/LED-Fading
const uint16_t pwmtable_16[256] PROGMEM =
{
0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3,
3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 7,
7, 7, 8, 8, 8, 9, 9, 10, 10, 10, 11, 11, 12, 12, 13, 13, 14, 15,
15, 16, 17, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
31, 32, 33, 35, 36, 38, 40, 41, 43, 45, 47, 49, 52, 54, 56, 59,
61, 64, 67, 70, 73, 76, 79, 83, 87, 91, 95, 99, 103, 108, 112,
117, 123, 128, 134, 140, 146, 152, 159, 166, 173, 181, 189, 197,
206, 215, 225, 235, 245, 256, 267, 279, 292, 304, 318, 332, 347,
362, 378, 395, 412, 431, 450, 470, 490, 512, 535, 558, 583, 609,
636, 664, 693, 724, 756, 790, 825, 861, 899, 939, 981, 1024, 1069,
1117, 1166, 1218, 1272, 1328, 1387, 1448, 1512, 1579, 1649, 1722,
1798, 1878, 1961, 2048, 2139, 2233, 2332, 2435, 2543, 2656, 2773,
2896, 3025, 3158, 3298, 3444, 3597, 3756, 3922, 4096, 4277, 4467,
4664, 4871, 5087, 5312, 5547, 5793, 6049, 6317, 6596, 6889, 7194,
7512, 7845, 8192, 8555, 8933, 9329, 9742, 10173, 10624, 11094,
11585, 12098, 12634, 13193, 13777, 14387, 15024, 15689, 16384,
17109, 17867, 18658, 19484, 20346, 21247, 22188, 23170, 24196,
25267, 26386, 27554, 28774, 30048, 31378, 32768, 34218, 35733,
37315, 38967, 40693, 42494, 44376, 46340, 48392, 50534, 52772,
55108, 57548, 60096, 62757, 65535
};
// Currently used colour
struct rgb_colour rgb_current = {0,0,0};
struct hsv_colour hsv_current = {0,0,0};
// Fading parameters
struct rgb_colour rgb_fade_to, rgb_fade_from;
struct hsv_colour hsv_fade_to, hsv_fade_from = {0,0,0};
uint16_t duration, duration_done;
// State machine
enum colour_state state = OFF;
// Translate rgb values to logarithmic 16bit scale - and invert
void hardware_rgb(void) {
OCR1A = ~(pgm_read_word(&pwmtable_16[rgb_current.blue]));
OCR1B = ~(pgm_read_word(&pwmtable_16[rgb_current.green]));
OCR1C = ~(pgm_read_word(&pwmtable_16[rgb_current.red]));
}
// Inverted pwm - without logarithm
void hardware_rgb_linear(void) {
OCR1A = ~(rgb_current.blue << 8);
OCR1B = ~(rgb_current.green << 8);
OCR1C = ~(rgb_current.red << 8);
}
// Taken from http://www.mikrocontroller.net/topic/54561 - mostly
void hardware_hsv() {
struct rgb_colour res;
if(hsv_current.saturation == 0) {
res.red = res.green = res.blue = hsv_current.value;
} else {
uint16_t hi = (hsv_current.hue * 6) / 256,
f = (((hsv_current.hue * 6) % 256) / 6);
uint16_t p = (hsv_current.value * (255 - hsv_current.saturation)) / 256,
q = (hsv_current.value * (((42 * 255) - hsv_current.saturation * f) / 42)) / 256,
t = (hsv_current.value * (((42 * 255) - hsv_current.saturation * (42 - f)) / 42)) / 256;
switch(hi) {
case 0: res = (struct rgb_colour) {hsv_current.value, t, p}; break;
case 1: res = (struct rgb_colour) {q, hsv_current.value, p}; break;
case 2: res = (struct rgb_colour) {p, hsv_current.value, t}; break;
case 3: res = (struct rgb_colour) {p, q, hsv_current.value}; break;
case 4: res = (struct rgb_colour) {t, p, hsv_current.value}; break;
case 5: res = (struct rgb_colour) {hsv_current.value, p, q}; break;
}
}
rgb_current = res;
//hardware_rgb();
hardware_rgb_linear();
}
void colour_init() {
// Set LED pins to output
DDRB |= (1 << PB7);
DDRC |= (1 << PC6) | (1 << PC5);
// 16bit pwm - inverse mode so we get the brightness of zero to LED off
TCCR1A |= (1 << COM1A1) | (1 << COM1A0) |
(1 << COM1B1) | (1 << COM1B0) |
(1 << COM1C1) | (1 << COM1C0) |
(1 << WGM11) | (0 << WGM10);
// Overflow at 0xffff
ICR1 = 0xffff;
// clock/1 prescaler
TCCR1B |= (1 << WGM13) | (1 << WGM12) | (0 << CS12) | (0 << CS11) | (1 << CS10);
// clock/8 prescaler
//TCCR1B |= (0 << WGM12) | (0 << CS12) | (1 << CS11) | (0 << CS10);
// Overflow interrupt enabled for fading.
TIMSK1 |= (1 << TOIE1);
// Initial colour setting
hardware_rgb_linear();
if(state == OFF)
state = IDLE;
}
ISR (TIMER1_OVF_vect) {
tick();
}
void __attribute__ ((weak)) callback_colour_finished(void) {}
// Call once every 8 ms - circa
void tick() {
switch(state) {
case OFF:
case IDLE:
return;
case WAIT:
duration_done += 4;
if(duration_done > duration)
{
state = IDLE;
callback_colour_finished();
}
break;
case FADE_RGB:
duration_done += 4;
if(duration_done > duration)
{
// Get rid of rounding errors
rgb_current = rgb_fade_to;
state = IDLE;
callback_colour_finished();
} else {
rgb_current.red = (int32_t) rgb_fade_from.red +
((((int32_t) rgb_fade_to.red -
(int32_t) rgb_fade_from.red)
* (int32_t) duration_done)
/ (int32_t) duration);
rgb_current.green = (int32_t) rgb_fade_from.green +
((((int32_t) rgb_fade_to.green -
(int32_t) rgb_fade_from.green)
* (int32_t) duration_done)
/ (int32_t) duration);
rgb_current.blue = (int32_t) rgb_fade_from.blue +
((((int32_t) rgb_fade_to.blue -
(int32_t) rgb_fade_from.blue)
* (int32_t) duration_done)
/ (int32_t) duration);
}
hardware_rgb_linear();
break;
case FADE_HSV:
duration_done += 4;
if(duration_done > duration)
{
// Get rid of rounding errors
hsv_current = hsv_fade_to;
state = IDLE;
callback_colour_finished();
} else {
// TODO: Hue is a circle - have it wrap around properly?
// (Overflow is a good thing here)
hsv_current.hue = (int32_t) hsv_fade_from.hue +
((((int32_t) hsv_fade_to.hue -
(int32_t) hsv_fade_from.hue)
* (int32_t) duration_done)
/ (int32_t) duration);
hsv_current.saturation = (int32_t) hsv_fade_from.saturation +
((((int32_t) hsv_fade_to.saturation -
(int32_t) hsv_fade_from.saturation)
* (int32_t) duration_done)
/ (int32_t) duration);
hsv_current.value = (int32_t) hsv_fade_from.value +
((((int32_t) hsv_fade_to.value -
(int32_t) hsv_fade_from.value)
* (int32_t) duration_done)
/ (int32_t) duration);
}
hardware_hsv();
default:
break;
}
}
enum colour_state get_state() {
return state;
}
void wait(uint16_t duration_in) {
// Prevent duration overflow
if(duration_in > (0xffff - 8))
duration_in = 0xffff - 8;
duration = duration_in;
duration_done = 0;
state = WAIT;
}
void set_rgb(struct rgb_colour to) {
state = IDLE;
rgb_current = to;
hardware_rgb_linear();
}
void fade_rgb(struct rgb_colour to, uint16_t duration_in) {
state = FADE_RGB;
rgb_fade_from = rgb_current;
rgb_fade_to = to;
// Prevent duration overflow
if(duration_in > (0xffff - 8))
duration_in = 0xffff - 8;
duration = duration_in;
duration_done = 0;
}
//To be implemented
void set_hsv(struct hsv_colour to) {
state = IDLE;
hsv_current = to;
hardware_hsv();
}
void fade_hsv(struct hsv_colour to, uint16_t duration_in) {
state = FADE_HSV;
// TODO: This is NOT sync'ed with rgb
hsv_fade_from = hsv_current;
hsv_fade_to = to;
// Prevent duration overflow
if(duration_in > (0xffff - 8))
duration_in = 0xffff - 8;
duration = duration_in;
duration_done = 0;
}