Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 237 lines (211 sloc) 5.25 kb
1fa2383 started on LED matrix code; this breaks the ws2821x code for now
root authored
1 /** \file
10fc4d2 Trammell Hudson improve matrix test to do fancy hue shifting
authored
2 * Test the matrix LCD PRU firmware with a multi-hue rainbow.
1fa2383 started on LED matrix code; this breaks the ws2821x code for now
root authored
3 */
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <stdint.h>
7 #include <string.h>
8 #include <time.h>
9 #include <inttypes.h>
10 #include <errno.h>
11 #include <unistd.h>
12 #include "ledscape.h"
13
10fc4d2 Trammell Hudson improve matrix test to do fancy hue shifting
authored
14
15 // Borrowed by OctoWS2811 rainbow test
16 static unsigned int
17 h2rgb(
18 unsigned int v1,
19 unsigned int v2,
20 unsigned int hue
21 )
22 {
23 if (hue < 60)
24 return v1 * 60 + (v2 - v1) * hue;
25 if (hue < 180)
26 return v2 * 60;
27 if (hue < 240)
28 return v1 * 60 + (v2 - v1) * (240 - hue);
29
30 return v1 * 60;
31 }
32
33
34 // Convert HSL (Hue, Saturation, Lightness) to RGB (Red, Green, Blue)
35 //
36 // hue: 0 to 359 - position on the color wheel, 0=red, 60=orange,
37 // 120=yellow, 180=green, 240=blue, 300=violet
38 //
39 // saturation: 0 to 100 - how bright or dull the color, 100=full, 0=gray
40 //
41 // lightness: 0 to 100 - how light the color is, 100=white, 50=color, 0=black
42 //
43 static uint32_t
44 makeColor(
45 unsigned int hue,
46 unsigned int saturation,
47 unsigned int lightness
48 )
49 {
50 unsigned int red, green, blue;
51 unsigned int var1, var2;
52
53 if (hue > 359)
54 hue = hue % 360;
55 if (saturation > 100)
56 saturation = 100;
57 if (lightness > 100)
58 lightness = 100;
59
60 // algorithm from: http://www.easyrgb.com/index.php?X=MATH&H=19#text19
61 if (saturation == 0) {
62 red = green = blue = lightness * 255 / 100;
63 } else {
64 if (lightness < 50) {
65 var2 = lightness * (100 + saturation);
66 } else {
67 var2 = ((lightness + saturation) * 100) - (saturation * lightness);
68 }
69 var1 = lightness * 200 - var2;
70 red = h2rgb(var1, var2, (hue < 240) ? hue + 120 : hue - 240) * 255 / 600000;
71 green = h2rgb(var1, var2, hue) * 255 / 600000;
72 blue = h2rgb(var1, var2, (hue >= 120) ? hue - 120 : hue + 240) * 255 / 600000;
73 }
74 return (red << 16) | (green << 8) | blue;
75 }
76
77
78
79 static uint32_t rainbowColors[180];
80
81
82 // phaseShift is the shift between each row. phaseShift=0
83 // causes all rows to show the same colors moving together.
84 // phaseShift=180 causes each row to be the opposite colors
85 // as the previous.
86 //
87 // cycleTime is the number of milliseconds to shift through
88 // the entire 360 degrees of the color wheel:
89 // Red -> Orange -> Yellow -> Green -> Blue -> Violet -> Red
90 //
1fa2383 started on LED matrix code; this breaks the ws2821x code for now
root authored
91 static void
10fc4d2 Trammell Hudson improve matrix test to do fancy hue shifting
authored
92 rainbow(
93 uint32_t * const pixels,
71c60bc Trammell Hudson send pre-formatted matrix to pru, rearrange based on config values
authored
94 unsigned width,
95 unsigned height,
10fc4d2 Trammell Hudson improve matrix test to do fancy hue shifting
authored
96 unsigned phaseShift,
97 unsigned cycle
1fa2383 started on LED matrix code; this breaks the ws2821x code for now
root authored
98 )
99 {
10fc4d2 Trammell Hudson improve matrix test to do fancy hue shifting
authored
100 const unsigned color = cycle % 180;
7d4147a Trammell Hudson steeper steps to show more gradation
authored
101 const unsigned dim = 128;
10fc4d2 Trammell Hudson improve matrix test to do fancy hue shifting
authored
102
71c60bc Trammell Hudson send pre-formatted matrix to pru, rearrange based on config values
authored
103 for (unsigned x=0; x < width; x++) {
104 for (unsigned y=0; y < height; y++) {
b546d79 Trammell Hudson more test to demonstate various ghosting bugs
authored
105 const int index = (color + x + y*phaseShift/4) % 180;
80cf09e Trammell Hudson tweak the pwm params
authored
106 const uint32_t in = rainbowColors[index];
71c60bc Trammell Hudson send pre-formatted matrix to pru, rearrange based on config values
authored
107 uint8_t * const out = &pixels[x + y*width];
f6b7fd9 Trammell Hudson matrix test with a few other color options
authored
108 #if 1
a57a09c Trammell Hudson dim mode
authored
109 out[0] = ((in >> 0) & 0xFF) * dim / 128; // * y / 16;
110 out[1] = ((in >> 8) & 0xFF) * dim / 128; // * y / 16;
111 out[2] = ((in >> 16) & 0xFF) * dim / 128; // * y / 16;
80cf09e Trammell Hudson tweak the pwm params
authored
112 #else
f6b7fd9 Trammell Hudson matrix test with a few other color options
authored
113 //out[0] = ((in >> 0) & 0xFF);
114 //out[1] = ((in >> 8) & 0xFF);
115 //out[2] = ((in >> 16) & 0xFF);
116 out[0] = y + 3*x + cycle;
117 out[1] = y + 3*x + cycle;
118 out[2] = y + 3*x + cycle;
80cf09e Trammell Hudson tweak the pwm params
authored
119 #endif
10fc4d2 Trammell Hudson improve matrix test to do fancy hue shifting
authored
120 }
121 }
1fa2383 started on LED matrix code; this breaks the ws2821x code for now
root authored
122 }
123
124
8a36317 Trammell Hudson disable output during low-brightness stages
authored
125 static void
126 gradient(
127 uint32_t * const pixels,
128 unsigned width,
129 unsigned height,
130 unsigned phaseShift,
131 unsigned cycle
132 )
133 {
134 cycle >>= 3;
135 for (unsigned x=0; x < width; x++) {
136 for (unsigned y=0; y < height; y++) {
137 uint8_t * const out = &pixels[x + y*width];
b546d79 Trammell Hudson more test to demonstate various ghosting bugs
authored
138 #if 0
c67741f Trammell Hudson fix bright rows 7 and 15. adjust brightness scaling
authored
139 //out[0] = ((x+cycle) % 32) * 8;
140 //out[1] = ((y+cycle) % 16) * 16;
b546d79 Trammell Hudson more test to demonstate various ghosting bugs
authored
141 uint8_t b = 0xFF;
142 out[1] = b * ((((x + y + cycle) >> 5) ) & 1);
143 #else
a87f2db fix for cube
root authored
144 uint32_t b = 0;
145
49d633c tweak identify routine
root authored
146 if (x % 32 < (x/32 + 4) && y % 32 < (y/32+1))
a87f2db fix for cube
root authored
147 {
148 b = 0xFFFFFF;
149 } else
150 if (x < 32)
151 {
152 if (y < 32)
153 b = 0xFF0000;
154 else
155 if (y < 64)
156 b = 0x0000FF;
157 else
158 if (y < 96)
159 b = 0x00FF00;
160 else
161 b = 0x411111;
162 } else
163 if (x < 64)
164 {
165 if (y < 32)
166 b = 0xFF00FF;
167 else
168 if (y < 64)
169 b = 0x00FFFF;
170 else
171 if (y < 96)
172 b = 0xFFFF00;
173 else
174 b = 0x114111;
175 } else {
176 b = 0x111141;
177 }
178
179 out[0] = (b >> 16) & 0xFF;
180 out[1] = (b >> 8) & 0xFF;
181 out[2] = (b >> 0) & 0xFF;
49d633c tweak identify routine
root authored
182 //*out = b;
b546d79 Trammell Hudson more test to demonstate various ghosting bugs
authored
183 #endif
8a36317 Trammell Hudson disable output during low-brightness stages
authored
184 }
185 }
186 }
187
10fc4d2 Trammell Hudson improve matrix test to do fancy hue shifting
authored
188 int
189 main(void)
1fa2383 started on LED matrix code; this breaks the ws2821x code for now
root authored
190 {
77cf177 something is not right with the width; glitches at 128?
root authored
191 const int width = 128;
a87f2db fix for cube
root authored
192 const int height = 128;
71c60bc Trammell Hudson send pre-formatted matrix to pru, rearrange based on config values
authored
193 ledscape_t * const leds = ledscape_init(width, height);
1fa2383 started on LED matrix code; this breaks the ws2821x code for now
root authored
194 printf("init done\n");
195 time_t last_time = time(NULL);
196 unsigned last_i = 0;
197
10fc4d2 Trammell Hudson improve matrix test to do fancy hue shifting
authored
198 // pre-compute the 180 rainbow colors
199 for (int i=0; i<180; i++)
200 {
201 int hue = i * 2;
202 int saturation = 100;
203 int lightness = 50;
204 rainbowColors[i] = makeColor(hue, saturation, lightness);
205 }
206
1fa2383 started on LED matrix code; this breaks the ws2821x code for now
root authored
207 unsigned i = 0;
71c60bc Trammell Hudson send pre-formatted matrix to pru, rearrange based on config values
authored
208 uint32_t * const p = calloc(width*height,4);
209
1fa2383 started on LED matrix code; this breaks the ws2821x code for now
root authored
210 while (1)
211 {
77cf177 something is not right with the width; glitches at 128?
root authored
212 if (1)
c67741f Trammell Hudson fix bright rows 7 and 15. adjust brightness scaling
authored
213 rainbow(p, width, height, 10, i++);
214 else
215 gradient(p, width, height, 10, i++);
71c60bc Trammell Hudson send pre-formatted matrix to pru, rearrange based on config values
authored
216 ledscape_draw(leds, p);
a131a9f Trammell Hudson Run bitmap faster, hues slower
authored
217 usleep(20000);
1fa2383 started on LED matrix code; this breaks the ws2821x code for now
root authored
218
219 // wait for the previous frame to finish;
220 //const uint32_t response = ledscape_wait(leds);
221 const uint32_t response = 0;
222 time_t now = time(NULL);
223 if (now != last_time)
224 {
225 printf("%d fps. starting %d previous %"PRIx32"\n",
226 i - last_i, i, response);
227 last_i = i;
228 last_time = now;
229 }
230
231 }
232
233 ledscape_close(leds);
234
235 return EXIT_SUCCESS;
236 }
Something went wrong with that request. Please try again.