-
Notifications
You must be signed in to change notification settings - Fork 0
/
ambilight.cpp
370 lines (308 loc) · 11.8 KB
/
ambilight.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
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
367
368
369
#include <Arduino.h>
#include <EEPROM.h>
#include <Tlc5940.h>
#include <stdio.h>
#include "buffer.h"
#define NANO
#if defined NANO
#include <SoftwareSerial.h>
SoftwareSerial Software_serial(2, 4); // RX, TX
#define bt_serial Software_serial
#else
#define bt_serial Serial1
#endif
// functions
void strip_write_color_linear(int strip, int max_level, int red, int green, int blue);
void strip_write_color_with_correction(int strip, int max_level, int red, int green, int blue);
int get_fade_out_value(int current_color, int new_color, long last_modified, long current_time);
void fluorescent_lamp_animation();
void init_gamma_correction_table();
void save_settings(struct configuration config);
void (*strip_write_color)(int strip, int max_level, int red, int green, int blue);
struct configuration {
int mode;
int light_level; // = 1023; // max intensity of LED stripes
byte light_level_percent;
byte red_color;
byte green_color;
byte blue_color;
};
struct rgb {
int red;
int green;
int blue;
};
struct channel_data {
struct rgb value;
struct rgb direction;
long last_modified;
};
// global variables
struct configuration config;
struct channel_data channels_data[CHANNELS];
int gamma_correction[256];
read_buffer buffer;
// FUNCTIONS
void save_settings(struct configuration config) {
EEPROM.write(MODE_ADDR, config.mode);
EEPROM.write(LIGHT_LEVEL_ADDR, config.light_level_percent);
EEPROM.write(RED_COLOR_ADDR, config.red_color);
EEPROM.write(GREEN_COLOR_ADDR, config.green_color);
EEPROM.write(BLUE_COLOR_ADDR, config.blue_color);
}
void upload_settings(struct configuration config) {
bt_serial.write(MODE);
bt_serial.write(config.mode);
bt_serial.write(LIGHT_LEVEL);
bt_serial.write(config.light_level_percent);
bt_serial.write(RED_COLOR);
bt_serial.write(config.red_color);
bt_serial.write(GREEN_COLOR);
bt_serial.write(config.green_color);
bt_serial.write(BLUE_COLOR);
bt_serial.write(config.blue_color);
}
void strip_write_color_linear(int strip, int max_level, int red, int green, int blue) {
Tlc.set(strip * 3, map(green, 0, 255, 0, max_level));
Tlc.set(strip * 3 + 1, map(blue, 0, 255, 0, max_level));
Tlc.set(strip * 3 + 2, map(red, 0, 255, 0, max_level));
}
void strip_write_color_with_correction(int strip, int max_level, int red, int green, int blue) {
Tlc.set(strip * 3, map(gamma_correction[green], 0, 4096, 0, max_level));
Tlc.set(strip * 3 + 1, map(gamma_correction[blue], 0, 4096, 0, max_level));
Tlc.set(strip * 3 + 2, map(gamma_correction[red], 0, 4096, 0, max_level));
}
int get_fade_out_value(int current_color, int new_color, long last_modified, long current_time) {
int tmp_color;
if(new_color < current_color) {
tmp_color = ((current_color - new_color) * (FADE_OUT_DELAY + last_modified - current_time)) / FADE_OUT_DELAY;
new_color = (tmp_color < new_color) ? new_color : tmp_color;
}
return new_color;
}
void fluorescent_lamp_animation() {
// black
for(int i = 0; i < CHANNELS; i++) {
strip_write_color(i, config.light_level, 0, 0, 0);
}
Tlc.update();
// top
for(int j = 0; j < 3; j++) {
for(int i = LEFT; i < LEFT + TOP; i++) {
strip_write_color(i, 2095, config.red_color, config.green_color, config.blue_color);
}
Tlc.update();
delay(20);
for(int i = LEFT; i < LEFT + TOP; i++) {
strip_write_color(i, 4095, 0, 0, 0);
}
Tlc.update();
delay(80);
}
strip_write_color(LEFT, config.light_level, config.red_color, config.green_color, config.blue_color);
strip_write_color(LEFT + TOP - 1, config.light_level, config.red_color, config.green_color, config.blue_color);
Tlc.update();
delay(800);
for(int i = LEFT; i < LEFT + TOP; i ++) {
strip_write_color(i, config.light_level, config.red_color, config.green_color, config.blue_color);
strip_write_color(LEFT + TOP + RIGHT - i, config.light_level, config.red_color, config.green_color, config.blue_color);
Tlc.update();
delay(10);
}
Tlc.update();
//left + right
for(int j = 0; j < 3; j++) {
for(int i = 0; i < LEFT; i++) {
strip_write_color(i, 2095, config.red_color, config.green_color, config.blue_color);
strip_write_color(i + LEFT + TOP, 2095, config.red_color, config.green_color, config.blue_color);
}
Tlc.update();
delay(20);
for(int i = 0; i < LEFT; i++) {
strip_write_color(i, 4095, 0, 0, 0);
strip_write_color(i + LEFT + TOP, 4095, 0, 0, 0);
}
Tlc.update();
delay(80);
}
strip_write_color(0, config.light_level, config.red_color, config.green_color, config.blue_color);
strip_write_color(LEFT + TOP, config.light_level, config.red_color, config.green_color, config.blue_color);
Tlc.update();
delay(500);
for(int i = 0; i < LEFT; i ++) {
strip_write_color(i, config.light_level, config.red_color, config.green_color, config.blue_color);
Tlc.update();
delay(10);
}
delay(400);
for(int i = 0; i < LEFT; i ++) {
strip_write_color(i + LEFT + TOP, config.light_level, config.red_color, config.green_color, config.blue_color);
Tlc.update();
delay(10);
}
}
void init_gamma_correction_table() {
float base;
for(int i = 0; i < 256; i++) {
base = ((float)4095 / 256 * (i + 1)) / 4095;
gamma_correction[i] = int(4096 * pow(base, GAMMA));
}
}
unsigned long boblight(channel_data channels_data[]) {
int red, green, blue;
long current_time;
static unsigned long last_received = millis();
if(buffer.available() >= 3 * CHANNELS + 1) {
if(buffer.read() == 0xFF) {
current_time = millis();
for(int i = 0; i < CHANNELS; i++) {
red = buffer.read();
green = buffer.read();
blue = buffer.read();
/* if(red < FADE_OUT_THRESHOLD && green < FADE_OUT_THRESHOLD && blue < FADE_OUT_THRESHOLD) {
red = get_fade_out_value(channels_data[i].value.red, red, channels_data[i].last_modified, current_time);
green = get_fade_out_value(channels_data[i].value.green, green, channels_data[i].last_modified, current_time);
blue = get_fade_out_value(channels_data[i].value.blue, blue, channels_data[i].last_modified, current_time);
}
channels_data[i].last_modified = current_time;
channels_data[i].value.red = red;
channels_data[i].value.green = green;
channels_data[i].value.blue = blue; */
strip_write_color(i, config.light_level, red, green, blue);
}
Tlc.update();
last_received = millis();
}
}
return last_received;
}
void one_color(int light_level, byte red_color, byte green_color, byte blue_color) {
for(int i = 0; i < CHANNELS; i++) {
strip_write_color(i, light_level, red_color, green_color, blue_color);
}
Tlc.update();
delay(40);
}
void demo(channel_data channels_data[]) {
for(int i = 0; i < CHANNELS; i++) {
channels_data[i].value.red += channels_data[i].direction.red;
channels_data[i].value.green += channels_data[i].direction.green;
channels_data[i].value.blue += channels_data[i].direction.blue;
channels_data[i].direction.red = random(0,20) == 0?random(-1,2):channels_data[i].direction.red;
channels_data[i].direction.green = random(0,20) == 0?random(-1,2):channels_data[i].direction.green;
channels_data[i].direction.blue = random(0,20) == 0?random(-1,2):channels_data[i].direction.blue;
if(channels_data[i].value.red + channels_data[i].direction.red > 255 ||
channels_data[i].value.red + channels_data[i].direction.red < 0) {
channels_data[i].direction.red = 0;
}
if(channels_data[i].value.green + channels_data[i].direction.green > 255 ||
channels_data[i].value.green + channels_data[i].direction.green < 0) {
channels_data[i].direction.green = 0;
}
if(channels_data[i].value.blue + channels_data[i].direction.blue > 255 ||
channels_data[i].value.blue + channels_data[i].direction.blue < 0) {
channels_data[i].direction.blue = 0;
}
strip_write_color(i, config.light_level, channels_data[0].value.red, channels_data[0].value.green, channels_data[0].value.blue);
}
Tlc.update();
delay(40);
}
void setup() {
Serial.begin(115200);
//bt_serial.begin(14400);
bt_serial.begin(9600);
Tlc.init(0);
// load settings from EEPROM
config.mode = EEPROM.read(MODE_ADDR);
config.light_level_percent = EEPROM.read(LIGHT_LEVEL_ADDR);
config.light_level = map(config.light_level_percent, 0, 100, 0, 4095);
config.red_color = EEPROM.read(RED_COLOR_ADDR);
config.green_color = EEPROM.read(GREEN_COLOR_ADDR);
config.blue_color = EEPROM.read(BLUE_COLOR_ADDR);
// init auxiliary array
for(int i = 0; i < CHANNELS; i++) {
channels_data[i].value.red = 0;
channels_data[i].value.green = 0;
channels_data[i].value.blue = 0;
channels_data[i].direction.red = 1;
channels_data[i].direction.green = 1;
channels_data[i].direction.blue = 1;
channels_data[i].last_modified = millis();
}
init_gamma_correction_table();
strip_write_color = strip_write_color_with_correction;
// starting animation
// fluorescent_lamp_animation();
}
void loop() {
int command;
unsigned long last_received;
while(bt_serial.available() >= 2) {
command = bt_serial.read();
switch(command) {
case MODE:
config.mode = bt_serial.read();
break;
case SAVE_SETTINGS:
bt_serial.read(); // ignore value
save_settings(config);
break;
case LIGHT_LEVEL:
config.light_level_percent = bt_serial.read();
config.light_level = map(config.light_level_percent, 0, 100, 0, 4095);
break;
case RED_COLOR:
config.red_color = bt_serial.read();
break;
case GREEN_COLOR:
config.green_color = bt_serial.read();
break;
case BLUE_COLOR:
config.blue_color = bt_serial.read();
break;
case UPLOAD_SETTINGS:
bt_serial.read(); // ignore value
upload_settings(config);
break;
}
}
while(Serial.available() > 0) {
buffer.store(Serial.read());
}
switch(config.mode) {
case BOBLIGHT:
last_received = boblight(channels_data);
if(last_received < millis() - 5000) { // 5 seconds without boblight data? switch to constant color
config.mode = CONSTANT_COLOR;
}
break;
case CONSTANT_COLOR:
one_color(config.light_level, config.red_color, config.green_color, config.blue_color);
if(buffer.available() >= 3 * CHANNELS + 1) { // data from boblight available? switch to BOBLIGHT mode
config.mode = BOBLIGHT;
}
break;
case MODE_OFF:
one_color(config.light_level, 0, 0, 0);
break;
case DEMO:
demo(channels_data);
break;
default: // unknown mode, probably not yet written in EEPROM - switch do BOBLIGHT
config.mode = BOBLIGHT;
break;
} // case mode
}
int main(void) {
init();
#if defined(USBCON)
USB.attach();
#endif
setup();
for (;;) {
loop();
if (serialEventRun) serialEventRun();
}
return 0;
}