-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
225 lines (206 loc) · 5.34 KB
/
main.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
/*
* main.c
*
* Created on: 12.11.2015
* Author: Leonard and Raphael
*/
#ifndef LENGTH
#define LENGTH 30
#endif
#ifndef F_CPU
#define F_CPU 16000000UL
#endif
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <stdlib.h>
#include "ws2812_config.h" // override config in submodule
#include "light_ws2812.h"
#include "uart.h"
// LED cRGB array ws2812 library reads periodically from
struct cRGB leds[LENGTH];
// gamma corrected warm white light (64 steps)
const uint8_t blind_r[64] PROGMEM = { 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44,
48, 52, 56, 60, 64, 68, 72, 76, 80, 84, 88, 92, 96, 100, 104, 108, 112,
116, 120, 124, 128, 131, 135, 139, 143, 147, 151, 155, 159, 163, 167,
171, 175, 179, 183, 187, 191, 195, 199, 203, 207, 211, 215, 219, 223,
227, 231, 235, 239, 243, 247, 251, 255 };
const uint8_t blind_g[64] PROGMEM = { 1, 3, 4, 6, 7, 8, 10, 11, 13, 14, 15, 17,
18, 20, 21, 23, 24, 25, 27, 28, 30, 31, 32, 34, 35, 37, 38, 39, 41, 42,
44, 45, 46, 48, 49, 51, 52, 53, 55, 56, 58, 59, 60, 62, 63, 65, 66, 68,
69, 70, 72, 73, 75, 76, 77, 79, 80, 82, 83, 84, 86, 87, 89, 90 };
const uint8_t blind_b[64] PROGMEM = { 0, 1, 1, 2, 2, 2, 3, 3, 4, 4, 4, 5, 5, 5,
6, 6, 7, 7, 7, 8, 8, 9, 9, 9, 10, 10, 11, 11, 11, 12, 12, 13, 13, 13,
14, 14, 14, 15, 15, 16, 16, 16, 17, 17, 18, 18, 18, 19, 19, 20, 20, 20,
21, 21, 21, 22, 22, 23, 23, 23, 24, 24, 25, 25 };
// variable delay
void delay_ms(int milliseconds) {
while(--milliseconds){
_delay_us(1000);
}
}
void doSingleColor(uint8_t r, uint8_t g, uint8_t b) {
for (uint8_t i = 0; i < LENGTH; i++) {
leds[i].r = r;
leds[i].g = g;
leds[i].b = b;
}
}
// blink adjustable duty cycle
void doBlink(uint16_t counter, uint16_t periode, uint8_t r, uint8_t g,
uint8_t b, float dutycycle) {
if ((counter % periode) == 0) {
doSingleColor(0, 0, 0);
} else if ((counter % periode) == (uint16_t) (dutycycle * periode)) {
doSingleColor(r, g, b);
}
}
void doColorRotation(uint16_t rotation) {
// Convert HSV (h = rotation, s = 255, v = 255; saturation and lightness not regarded)
uint8_t r, g, b;
uint8_t section, section_rotation;
uint16_t q, t;
section = (rotation % 360) / 43;
section_rotation = (rotation % 360) % 43;
// p = 0;
q = (255 * ((10710 - (255 * section_rotation)) / 42)) / 256;
t = (255 * ((10710 - (255 * (42 - section_rotation))) / 42)) / 256;
switch (section) {
case 0:
r = 255;
g = t;
b = 0;
break;
case 1:
r = q;
g = 255;
b = 0;
break;
case 2:
r = 0;
g = 255;
b = t;
break;
case 3:
r = 0;
g = q;
b = 255;
break;
case 4:
r = t;
g = 0;
b = 255;
break;
case 5:
r = 255;
g = 0;
b = q;
break;
default:
r = 0;
g = 0;
b = 0;
break;
}
for (uint8_t i = 0; i < LENGTH; i++) {
leds[i].r = r;
leds[i].g = g;
leds[i].b = b;
}
}
void doSlowFade(uint16_t stepsPerSecond, uint8_t r, uint8_t g, uint8_t b,
uint8_t hardness) {
doSingleColor(r / 100 * hardness, g / 100 * hardness, b / 100 * hardness);
delay_ms((int) 1000 / stepsPerSecond);
}
/*Strobe with maximal and minimal period time in ms
and predefined or random (0, 0, 0) color.
For periodical strobe set min_time = max_time.
Returns time in ms until doStrobe wants to be called next time. */
int doStrobe(int min_time, int max_time, int r, int g, int b) {
// time until next function call
int next_call;
// 0 = off, 1 = on
static int on_off = 0;
if (on_off == 0) {
// random rgb, if not defined
if ((r == 0) && (g == 0) && (b == 0)) {
r = (rand() % 255);
g = (rand() % 255);
b = (rand() % 255);
}
doSingleColor(r, g, b);
on_off = 1;
} else {
doSingleColor(0, 0, 0);
on_off = 0;
}
// if time should not be periodic -> random time on / off
if (min_time != max_time) {
next_call = (rand() % (max_time - min_time)) + min_time;
} else {
next_call = min_time;
}
return next_call;
}
int main(void) {
// initialize UART
uart_init();
uint16_t doStrobe_next_call = 0;
_delay_ms(1000);
DDRD = (1 << PD7); // PD7 (ws2812 data out) as output
// led (arduino pin 13 / PB5) for debugging
DDRB = (1 << PB5); // PB5 as output
PORTB = (1 << PB5); // PB5 on
uint16_t counter = 0;
// 1 second R - G - B as 'start signal'
doSingleColor(255, 0, 0);
ws2812_setleds(leds, LENGTH);
_delay_ms(1000);
doSingleColor(0, 255, 0);
ws2812_setleds(leds, LENGTH);
_delay_ms(1000);
doSingleColor(0, 0, 255);
ws2812_setleds(leds, LENGTH);
_delay_ms(1000);
// uint8_t hardness = 0;
while (1) {
// handle UART command
if (uart_rcv_complete == 1) {
switch(command[0]) {
case 'r':
doSingleColor(255, 0, 0);
break;
case 'g':
doSingleColor(0, 255, 0);
break;
case 'b':
doSingleColor(0, 0, 255);
break;
}
}
// delay 800 µs -> loop needs ~1ms per iteration
_delay_us(800);
//////////////////////
//doColorRotation(counter);
//////////////////////
//doBlink(counter, 900, 0, 0, 150, 0.5);
/////////////////////
//doSlowFade(5, 255, 255, 255, hardness);
//if (hardness < 100) {
// hardness++;
//}
/////////////////////////
// only call doStrobe, if counter is equal to the time,
// the function wants to be called again
//if (counter == doStrobe_next_call) {
// doStrobe_next_call = doStrobe(100, 300, 0, 0, 0) + counter;
//}
// Refresh LED strip every loop
ws2812_setleds(leds, LENGTH);
// toggle led for debugging
PORTB ^= (1 << PB5);
counter++;
}
}