-
Notifications
You must be signed in to change notification settings - Fork 37
/
arduino-unicorn-hat-hd.ino
executable file
·186 lines (166 loc) · 4.32 KB
/
arduino-unicorn-hat-hd.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
#include <SPI.h>
const int slaveSelectPin = D8;
uint8_t buffer[16][16][3] = { 0 };
unsigned long step = 0;
//------------------demos------------------------
void gradient(uint8_t x, uint8_t y, unsigned long step) {
uint8_t g = x * 16;
uint8_t b = y * 16;
uint8_t r = 255 - (x * 16);
setPixelU(x, y, r, g, b);
}
void run(uint8_t x, uint8_t y, unsigned long step) {
uint8_t g = x * 16;
uint8_t b = y * 16;
uint8_t r = 255 - (x * 16);
uint8_t v = step / 16 % 16 == y && step % 16 == x ? 1 : 0;
setPixelU(x, y, r * v, g * v, b * v);
}
void swirl(uint8_t x, uint8_t y, unsigned long step) {
float fx = x - 7.5;
float fy = y - 7.5;
float dist = sqrt(fx * fx + fy * fy) * 0.5;
//float dist = (fx + fy) * 0.5;
//float dist = 5;
float angle = (step * 0.1) + (dist * 1.5);
float s = sin(angle);
float c = cos(angle);
float xs = x * c - y * s;
float ys = x * s + y * c;
float r = abs(xs + ys) * 12.0 - 20;
float g = r + (s * 130);
float b = r + (c * 130);
setPixel(x, y,
r,
g,
b
);
}
void rainbowSearch(uint8_t x, uint8_t y, unsigned long step) {
float xs = sin((step) * 0.01) * 20.0;
float ys = cos((step) * 0.01) * 20.0;
float scale = ((sin(step / 60.0) + 1.0) * 0.2) + 0.2;
float r = sin((x + xs) * scale) + cos((y + xs) * scale);
float g = sin((x + xs) * scale) + cos((y + ys) * scale);
float b = sin((x + ys) * scale) + cos((y + ys) * scale);
setPixel(x, y,
r * 255,
g * 255,
b * 255
);
}
void checker(uint8_t _x, uint8_t _y, unsigned long step) {
float x = _x - 8;
float y = _y - 8;
float angle = step / 5.0;
float s = sin(angle);
float c = cos(angle);
float xs = x * c - y * s;
float ys = x * s + y * c;
xs -= sin(step / 200.0) * 40.0;
ys -= cos(step / 200.0) * 40.0;
float scale = step % 20;
scale /= 20.0;
scale = (sin(step / 50.0) / 8.0) + 0.25;
xs *= scale;
ys *= scale;
float xo = abs(xs) - int(abs(xs));
float yo = abs(ys) - int(abs(ys));
// l = 0 if @ else 1 if xo > .1 and else .5
float l = int(floor(xs) + floor(ys)) % 2 ? 0 : (xo > 0.1 && yo > .1 ? 1 : 0.5);
float r, g, b;
HSVtoRGB(r, g, b, (step % 255) / 255.0, 1, 1);
setPixel(_x, _y,
r * (l * 255),
g * (l * 255),
b * (l * 255)
);
}
void (*demoFuncs[])(uint8_t x, uint8_t y, unsigned long step) = {
run,
gradient,
swirl,
rainbowSearch,
checker
};
//-----------------------------------------------
void setPixelU(uint8_t x, uint8_t y, uint8_t r, uint8_t g, uint8_t b) {
buffer[y][x][0] = r;
buffer[y][x][1] = g;
buffer[y][x][2] = b;
}
void setPixel(uint8_t x, uint8_t y, float r, float g, float b) {
buffer[y][x][0] = uint8_t(max(0, min(255, r)));
buffer[y][x][1] = uint8_t(max(0, min(255, g)));
buffer[y][x][2] = uint8_t(max(0, min(255, b)));
}
void sendBuffer() {
SPI.beginTransaction(SPISettings(9000000, MSBFIRST, SPI_MODE0));
digitalWrite(slaveSelectPin, LOW);
SPI.transfer(0x72);
for (int x = 0; x < 16; x++) {
for (int y = 0; y < 16; y++) {
for (int c = 0; c < 3; c++) {
SPI.transfer(buffer[x][y][c]);
}
}
}
digitalWrite(slaveSelectPin, HIGH);
SPI.endTransaction();
}
void HSVtoRGB(float& r, float& g, float& b, float h, float s, float v) {
if (s == 0.0) {
r = v;
g = v;
b = v;
}
int i = int(h * 6.0); // XXX assume int() truncates!
float f = (h * 6.0) - i;
float p = v * (1.0 - s);
float q = v * (1.0 - s * f);
float t = v * (1.0 - s * (1.0 - f));
i = i % 6;
if (i == 0) {
r = v; g = t; b = p; return; // v, t, p
}
if (i == 1) {
r = q; g = v; b = p; return; // q, v, p
}
if (i == 2) {
r = p; g = v; b = t; return; // p, v, t
}
if (i == 3) {
r = p; g = q; b = v; return; // p, q, v
}
if (i == 4) {
r = t; g = p; b = v; return; // t, p, v
}
if (i == 5) {
r = v; g = p; b = q; return; // v, p, q
}
}
void setup() {
Serial.begin(9600);
pinMode(slaveSelectPin, OUTPUT);
SPI.begin();
}
void (*curFunc)(uint8_t x, uint8_t y, unsigned long step) = demoFuncs[0];
unsigned long now = 0;
unsigned long diff = 0;
void loop() {
now = millis();
uint8_t i = (now / 5000) % 5;
curFunc = demoFuncs[i];
for (uint8_t x = 0; x < 16; x++) {
for (uint8_t y = 0; y < 16; y++) {
curFunc(x, y, step);
}
}
diff = millis() - now;
//Serial.println(diff);
sendBuffer();
step++;
if (diff < 8) {
delay(8 - diff);
}
}