forked from osresearch/LEDscape
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix-test.c
201 lines (178 loc) · 4.73 KB
/
matrix-test.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
/** \file
* Test the matrix LCD PRU firmware with a multi-hue rainbow.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <time.h>
#include <inttypes.h>
#include <errno.h>
#include <unistd.h>
#include "ledscape.h"
// Borrowed by OctoWS2811 rainbow test
static unsigned int
h2rgb(
unsigned int v1,
unsigned int v2,
unsigned int hue
)
{
if (hue < 60)
return v1 * 60 + (v2 - v1) * hue;
if (hue < 180)
return v2 * 60;
if (hue < 240)
return v1 * 60 + (v2 - v1) * (240 - hue);
return v1 * 60;
}
// Convert HSL (Hue, Saturation, Lightness) to RGB (Red, Green, Blue)
//
// hue: 0 to 359 - position on the color wheel, 0=red, 60=orange,
// 120=yellow, 180=green, 240=blue, 300=violet
//
// saturation: 0 to 100 - how bright or dull the color, 100=full, 0=gray
//
// lightness: 0 to 100 - how light the color is, 100=white, 50=color, 0=black
//
static uint32_t
makeColor(
unsigned int hue,
unsigned int saturation,
unsigned int lightness
)
{
unsigned int red, green, blue;
unsigned int var1, var2;
if (hue > 359)
hue = hue % 360;
if (saturation > 100)
saturation = 100;
if (lightness > 100)
lightness = 100;
// algorithm from: http://www.easyrgb.com/index.php?X=MATH&H=19#text19
if (saturation == 0) {
red = green = blue = lightness * 255 / 100;
} else {
if (lightness < 50) {
var2 = lightness * (100 + saturation);
} else {
var2 = ((lightness + saturation) * 100) - (saturation * lightness);
}
var1 = lightness * 200 - var2;
red = h2rgb(var1, var2, (hue < 240) ? hue + 120 : hue - 240) * 255 / 600000;
green = h2rgb(var1, var2, hue) * 255 / 600000;
blue = h2rgb(var1, var2, (hue >= 120) ? hue - 120 : hue + 240) * 255 / 600000;
}
return (red << 16) | (green << 8) | blue;
}
static uint32_t rainbowColors[180];
// phaseShift is the shift between each row. phaseShift=0
// causes all rows to show the same colors moving together.
// phaseShift=180 causes each row to be the opposite colors
// as the previous.
//
// cycleTime is the number of milliseconds to shift through
// the entire 360 degrees of the color wheel:
// Red -> Orange -> Yellow -> Green -> Blue -> Violet -> Red
//
static void
rainbow(
uint32_t * const pixels,
unsigned width,
unsigned height,
unsigned phaseShift,
unsigned cycle
)
{
const unsigned color = cycle % 180;
const unsigned dim = 8;
for (unsigned x=0; x < width; x++) {
for (unsigned y=0; y < height; y++) {
const int index = (color + x + y*phaseShift/4) % 180;
const uint32_t in = rainbowColors[index];
uint8_t * const out = &pixels[x + y*width];
#if 1
out[0] = ((in >> 0) & 0xFF) * dim / 128; // * y / 16;
out[1] = ((in >> 8) & 0xFF) * dim / 128; // * y / 16;
out[2] = ((in >> 16) & 0xFF) * dim / 128; // * y / 16;
#else
//out[0] = ((in >> 0) & 0xFF);
//out[1] = ((in >> 8) & 0xFF);
//out[2] = ((in >> 16) & 0xFF);
out[0] = y + 3*x + cycle;
out[1] = y + 3*x + cycle;
out[2] = y + 3*x + cycle;
#endif
}
}
}
static void
gradient(
uint32_t * const pixels,
unsigned width,
unsigned height,
unsigned phaseShift,
unsigned cycle
)
{
cycle >>= 3;
for (unsigned x=0; x < width; x++) {
for (unsigned y=0; y < height; y++) {
uint8_t * const out = &pixels[x + y*width];
#if 0
//out[0] = ((x+cycle) % 32) * 8;
//out[1] = ((y+cycle) % 16) * 16;
uint8_t b = 0xFF;
out[1] = b * ((((x + y + cycle) >> 5) ) & 1);
#else
uint8_t b = ((x+y+cycle) % 32) * 8;
out[0] = b * ((((x + y + cycle) >> 5) % 3) & 1);
out[1] = b * ((((x + y + cycle) >> 5) % 5) & 1);
out[2] = b * ((((x + y + cycle) >> 5) % 7) & 1);
#endif
}
}
}
int
main(void)
{
const int width = 128;
const int height = 48;
ledscape_t * const leds = ledscape_init(width, height);
printf("init done\n");
time_t last_time = time(NULL);
unsigned last_i = 0;
// pre-compute the 180 rainbow colors
for (int i=0; i<180; i++)
{
int hue = i * 2;
int saturation = 100;
int lightness = 50;
rainbowColors[i] = makeColor(hue, saturation, lightness);
}
unsigned i = 0;
uint32_t * const p = calloc(width*height,4);
while (1)
{
if (1)
rainbow(p, width, height, 10, i++);
else
gradient(p, width, height, 10, i++);
ledscape_draw(leds, p);
usleep(20000);
// wait for the previous frame to finish;
//const uint32_t response = ledscape_wait(leds);
const uint32_t response = 0;
time_t now = time(NULL);
if (now != last_time)
{
printf("%d fps. starting %d previous %"PRIx32"\n",
i - last_i, i, response);
last_i = i;
last_time = now;
}
}
ledscape_close(leds);
return EXIT_SUCCESS;
}