Skip to content
Newer
Older
100644 236 lines (210 sloc) 5.1 KB
1fa2383 started on LED matrix code; this breaks the ws2821x code for now
root authored Jan 1, 2000
1 /** \file
10fc4d2 @osresearch improve matrix test to do fancy hue shifting
authored Sep 8, 2013
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 Jan 1, 2000
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 @osresearch improve matrix test to do fancy hue shifting
authored Sep 8, 2013
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 Jan 1, 2000
91 static void
10fc4d2 @osresearch improve matrix test to do fancy hue shifting
authored Sep 8, 2013
92 rainbow(
93 uint32_t * const pixels,
71c60bc @osresearch send pre-formatted matrix to pru, rearrange based on config values
authored Dec 31, 1999
94 unsigned width,
95 unsigned height,
10fc4d2 @osresearch improve matrix test to do fancy hue shifting
authored Sep 8, 2013
96 unsigned phaseShift,
97 unsigned cycle
1fa2383 started on LED matrix code; this breaks the ws2821x code for now
root authored Jan 1, 2000
98 )
99 {
10fc4d2 @osresearch improve matrix test to do fancy hue shifting
authored Sep 8, 2013
100 const unsigned color = cycle % 180;
7d4147a @osresearch steeper steps to show more gradation
authored Dec 18, 2013
101 const unsigned dim = 128;
10fc4d2 @osresearch improve matrix test to do fancy hue shifting
authored Sep 8, 2013
102
71c60bc @osresearch send pre-formatted matrix to pru, rearrange based on config values
authored Jan 1, 2000
103 for (unsigned x=0; x < width; x++) {
104 for (unsigned y=0; y < height; y++) {
b546d79 @osresearch more test to demonstate various ghosting bugs
authored Dec 31, 1999
105 const int index = (color + x + y*phaseShift/4) % 180;
80cf09e @osresearch tweak the pwm params
authored Sep 11, 2013
106 const uint32_t in = rainbowColors[index];
71c60bc @osresearch send pre-formatted matrix to pru, rearrange based on config values
authored Jan 1, 2000
107 uint8_t * const out = &pixels[x + y*width];
f6b7fd9 @osresearch matrix test with a few other color options
authored Sep 11, 2013
108 #if 1
a57a09c @osresearch dim mode
authored Jan 1, 2000
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 @osresearch tweak the pwm params
authored Sep 11, 2013
112 #else
f6b7fd9 @osresearch matrix test with a few other color options
authored Sep 11, 2013
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 @osresearch tweak the pwm params
authored Sep 11, 2013
119 #endif
10fc4d2 @osresearch improve matrix test to do fancy hue shifting
authored Sep 8, 2013
120 }
121 }
1fa2383 started on LED matrix code; this breaks the ws2821x code for now
root authored Jan 1, 2000
122 }
123
124
8a36317 @osresearch disable output during low-brightness stages
authored Dec 31, 1999
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 @osresearch more test to demonstate various ghosting bugs
authored Jan 1, 2000
138 #if 0
c67741f @osresearch fix bright rows 7 and 15. adjust brightness scaling
authored Dec 31, 1999
139 //out[0] = ((x+cycle) % 32) * 8;
140 //out[1] = ((y+cycle) % 16) * 16;
b546d79 @osresearch more test to demonstate various ghosting bugs
authored Jan 1, 2000
141 uint8_t b = 0xFF;
142 out[1] = b * ((((x + y + cycle) >> 5) ) & 1);
143 #else
a87f2db fix for cube
root authored Feb 26, 2014
144 uint32_t b = 0;
145
146 if (x % 32 == 0 && y % 32 == 0)
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;
b546d79 @osresearch more test to demonstate various ghosting bugs
authored Jan 1, 2000
182 #endif
8a36317 @osresearch disable output during low-brightness stages
authored Jan 1, 2000
183 }
184 }
185 }
186
10fc4d2 @osresearch improve matrix test to do fancy hue shifting
authored Sep 8, 2013
187 int
188 main(void)
1fa2383 started on LED matrix code; this breaks the ws2821x code for now
root authored Jan 1, 2000
189 {
77cf177 something is not right with the width; glitches at 128?
root authored Feb 26, 2014
190 const int width = 128;
a87f2db fix for cube
root authored Feb 26, 2014
191 const int height = 128;
71c60bc @osresearch send pre-formatted matrix to pru, rearrange based on config values
authored Jan 1, 2000
192 ledscape_t * const leds = ledscape_init(width, height);
1fa2383 started on LED matrix code; this breaks the ws2821x code for now
root authored Jan 1, 2000
193 printf("init done\n");
194 time_t last_time = time(NULL);
195 unsigned last_i = 0;
196
10fc4d2 @osresearch improve matrix test to do fancy hue shifting
authored Sep 8, 2013
197 // pre-compute the 180 rainbow colors
198 for (int i=0; i<180; i++)
199 {
200 int hue = i * 2;
201 int saturation = 100;
202 int lightness = 50;
203 rainbowColors[i] = makeColor(hue, saturation, lightness);
204 }
205
1fa2383 started on LED matrix code; this breaks the ws2821x code for now
root authored Jan 1, 2000
206 unsigned i = 0;
71c60bc @osresearch send pre-formatted matrix to pru, rearrange based on config values
authored Jan 1, 2000
207 uint32_t * const p = calloc(width*height,4);
208
1fa2383 started on LED matrix code; this breaks the ws2821x code for now
root authored Jan 1, 2000
209 while (1)
210 {
77cf177 something is not right with the width; glitches at 128?
root authored Feb 26, 2014
211 if (1)
c67741f @osresearch fix bright rows 7 and 15. adjust brightness scaling
authored Jan 1, 2000
212 rainbow(p, width, height, 10, i++);
213 else
214 gradient(p, width, height, 10, i++);
71c60bc @osresearch send pre-formatted matrix to pru, rearrange based on config values
authored Jan 1, 2000
215 ledscape_draw(leds, p);
a131a9f @osresearch Run bitmap faster, hues slower
authored Sep 9, 2013
216 usleep(20000);
1fa2383 started on LED matrix code; this breaks the ws2821x code for now
root authored Jan 1, 2000
217
218 // wait for the previous frame to finish;
219 //const uint32_t response = ledscape_wait(leds);
220 const uint32_t response = 0;
221 time_t now = time(NULL);
222 if (now != last_time)
223 {
224 printf("%d fps. starting %d previous %"PRIx32"\n",
225 i - last_i, i, response);
226 last_i = i;
227 last_time = now;
228 }
229
230 }
231
232 ledscape_close(leds);
233
234 return EXIT_SUCCESS;
235 }
Something went wrong with that request. Please try again.