This repository has been archived by the owner on Mar 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cor.ino
374 lines (303 loc) · 8.4 KB
/
cor.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
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
370
371
/* vim: set ft=cpp: */
#include <math.h>
#include "button.h"
/* pins */
/* these leds are reversed: pin HIGH <=> LED off */
#define ACCEL_BUTTON0_PIN 12
#define ACCEL_LED0 13 /* also led on arduino board, though not reversed */
#define ACCEL_BUTTON1_PIN 2
#define ACCEL_LED1 3
#define HUE_BUTTON0_PIN 4
#define HUE_BUTTON1_PIN 7
#define PANEL 6
/* RGB leds are reversed: we send 255 - value to them */
#define LEDR 11
#define LEDG 10
#define LEDB 9
#define ACCELERATOR_TIMEOUT 2000
#define LONG_BOUNCE_PERIOD 1500
#define SHORT_BOUNCE_PERIOD 750
#define E 2.71828
int min_light = 20;
int max_light = 255;
int period = LONG_BOUNCE_PERIOD;
int acceleration_divider = 1;
#define SAMPLES 50
#define C (E * E - 1)
/* We keep a bit field state. Whenever that state changes, we need to stop the
* current bouncing and reinit the main loop. */
#define HUE_SELECTION_MODE (1 << 0)
#define ACCELERATOR0_ON (1 << 1)
#define ACCELERATOR1_ON (1 << 2)
int loop_state = 0;
#define ACCEL_BUTTON0 0
#define ACCEL_BUTTON1 1
#define HUE_BUTTON0 2
#define HUE_BUTTON1 3
#define BUTTONS_LENGTH 4
button_t buttons[BUTTONS_LENGTH] = {
{
ACCEL_BUTTON0_PIN, /* pin*/
LOW, /* state */
LOW, /* last_reading */
0, /* last_debounce_time */
},
{
ACCEL_BUTTON1_PIN, /* pin*/
LOW, /* state */
LOW, /* last_reading */
0, /* last_debounce_time */
},
{
HUE_BUTTON0_PIN, /* pin*/
LOW, /* state */
LOW, /* last_reading */
0, /* last_debounce_time */
},
{
HUE_BUTTON1_PIN, /* pin*/
LOW, /* state */
LOW, /* last_reading */
0, /* last_debounce_time */
}
};
#define ACCELERATORS_LENGTH 2
accelerator_t accelerators[ACCELERATORS_LENGTH] = {
{
false, /* forced */
false, /* timeout_wait */
0 /* end_time */
},
{
false, /* forced */
false, /* timeout_wait */
0 /* end_time */
}
};
int valAt(int t) {
return (E * E * min_light - max_light) / C +
(E * (max_light - min_light)) / C *
exp(sin((2 * PI / period) * (t + 0.75 * period)));
}
/* accelerator stuff */
bool accelerator_is_on(accelerator_t *accelerator) {
return accelerator->forced || accelerator->timeout_wait;
}
void accelerator_enable(accelerator_t *accelerator) {
accelerator->forced = true;
}
void accelerator_disable(accelerator_t *accelerator) {
accelerator->forced = false;
accelerator->timeout_wait = true;
accelerator->end_time = millis() + ACCELERATOR_TIMEOUT;
}
void accelerator_update(accelerator_t *accelerator) {
if (!accelerator->forced
&& accelerator->timeout_wait
&& millis() > accelerator->end_time) {
accelerator->timeout_wait = false;
/* accelerator->end_time becomes meaningless */
}
}
void update_accelerators() {
acceleration_divider = 1;
for (int i = 0; i < ACCELERATORS_LENGTH; i++) {
accelerator_update(&accelerators[i]);
if (accelerator_is_on(&accelerators[i])) {
acceleration_divider *= 2;
}
}
if (accelerator_is_on(&accelerators[0]))
digitalWrite(ACCEL_LED0, LOW); // led on
else
digitalWrite(ACCEL_LED0, HIGH);
if (accelerator_is_on(&accelerators[1]))
digitalWrite(ACCEL_LED1, LOW); // led on
else
digitalWrite(ACCEL_LED1, HIGH);
}
/* button stuff */
void on_press(button_t *button) {
switch(button->pin) {
case ACCEL_BUTTON0_PIN:
accelerator_enable(&accelerators[0]);
break;
case ACCEL_BUTTON1_PIN:
accelerator_enable(&accelerators[1]);
break;
default:
break;
}
}
void on_release(button_t *button) {
switch(button->pin) {
case ACCEL_BUTTON0_PIN:
accelerator_disable(&accelerators[0]);
break;
case ACCEL_BUTTON1_PIN:
accelerator_disable(&accelerators[1]);
break;
default:
break;
}
}
bool is_pressed(button_t *button) {
return button->state == HIGH;
}
void check_button(button_t *button) {
int reading = digitalRead(button->pin);
long now = millis();
if (reading != button->last_reading) {
// reset the debouncing timer
button->last_debounce_time = now;
}
if ((now - button->last_debounce_time) > DEBOUNCE_DELAY) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
// if the button state has changed:
if (reading != button->state) {
button->state = reading;
if (is_pressed(button))
on_press(button);
else
on_release(button);
}
}
// save the reading. Next time through the loop,
// it'll be the lastButtonState:
button->last_reading = reading;
}
void check_buttons() {
for (int i=0; i<BUTTONS_LENGTH; i++) {
check_button(&buttons[i]);
}
}
/* state stuff */
bool is_hue_selection_mode() {
return is_pressed(&buttons[HUE_BUTTON0])
&& is_pressed(&buttons[HUE_BUTTON1]);
}
int current_state() {
int state = 0;
if (is_hue_selection_mode())
state |= HUE_SELECTION_MODE;
if (accelerator_is_on(&accelerators[0]))
state |= ACCELERATOR0_ON;
if (accelerator_is_on(&accelerators[1]))
state |= ACCELERATOR1_ON;
return state;
}
bool need_reinit() {
int new_state;
bool result;
check_buttons();
update_accelerators();
new_state = current_state();
result = new_state != loop_state;
loop_state = new_state;
return result;
}
unsigned char r, g, b = 0;
unsigned char hue = 0;
void setup() {
pinMode(ACCEL_BUTTON0_PIN, INPUT);
pinMode(ACCEL_LED0, OUTPUT);
pinMode(ACCEL_BUTTON1_PIN, INPUT);
pinMode(ACCEL_LED1, OUTPUT);
pinMode(HUE_BUTTON0_PIN, INPUT);
pinMode(HUE_BUTTON1_PIN, INPUT);
pinMode(PANEL, OUTPUT);
pinMode(LEDR, OUTPUT);
pinMode(LEDG, OUTPUT);
pinMode(LEDB, OUTPUT);
}
/* HSV to RGB conversion function with only integer
* math. S and V go from 0 to 255, and H goes from 0 to 360
*/
void
hsvtorgb(unsigned char *r, unsigned char *g, unsigned char *b, unsigned char h, unsigned char s, unsigned char v)
{
unsigned char region, fpart, p, q, t;
if(s == 0) {
/* color is grayscale */
*r = *g = *b = v;
return;
}
/* make hue 0-5 */
region = h / 43;
/* find remainder part, make it from 0-255 */
fpart = (h - (region * 43)) * 6;
/* calculate temp vars, doing integer multiplication */
p = (v * (255 - s)) >> 8;
q = (v * (255 - ((s * fpart) >> 8))) >> 8;
t = (v * (255 - ((s * (255 - fpart)) >> 8))) >> 8;
/* assign temp vars based on color cone region */
switch(region) {
case 0:
*r = v; *g = t; *b = p; break;
case 1:
*r = q; *g = v; *b = p; break;
case 2:
*r = p; *g = v; *b = t; break;
case 3:
*r = p; *g = q; *b = v; break;
case 4:
*r = t; *g = p; *b = v; break;
default:
*r = v; *g = p; *b = q; break;
}
return;
}
void updateHue() {
hue = (hue == 360) ? 0 : hue + 1;
hsvtorgb(&r, &g, &b, hue, 255, 255);
analogWrite(LEDR, r);
analogWrite(LEDG, g);
analogWrite(LEDB, b);
}
void loop() {
init:
digitalWrite(ACCEL_LED0, HIGH);
digitalWrite(ACCEL_LED1, HIGH);
analogWrite(PANEL, min_light);
hsvtorgb (&r, &g, &b, hue, min_light, 255);
analogWrite(LEDR, r);
analogWrite(LEDG, g);
analogWrite(LEDB, b);
while (is_hue_selection_mode()) {
updateHue();
delay(20);
if (need_reinit())
goto init;
}
int t = 0;
period = LONG_BOUNCE_PERIOD / acceleration_divider;
max_light = 255;
for (int i = 0; i < SAMPLES; i++) {
t = (period / SAMPLES) * i;
int val = valAt(t);
analogWrite(PANEL, val);
delay(period / SAMPLES);
if (need_reinit())
goto init;
}
/* second bounce is on rgb, not on panel */
period = SHORT_BOUNCE_PERIOD / acceleration_divider;
max_light = 120;
for (int i = 0; i < SAMPLES; i++) {
t = (period / SAMPLES) * i;
int val = valAt(t);
hsvtorgb (&r, &g, &b, hue, val, 255);
analogWrite(LEDR, r);
analogWrite(LEDG, g);
analogWrite(LEDB, b);
delay(period / SAMPLES);
if (need_reinit())
goto init;
}
for (int i = 0; i<(100/acceleration_divider); i++) {
delay(10);
if (need_reinit())
goto init;
}
}