Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 201 lines (177 sloc) 4.782 kB
1fa2383 started on LED matrix code; this breaks the ws2821x code for now
root authored
1 /** \file
10fc4d2 @osresearch 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 @osresearch 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 @osresearch improve matrix test to do fancy hue shifting
authored
92 rainbow(
93 uint32_t * const pixels,
71c60bc @osresearch send pre-formatted matrix to pru, rearrange based on config values
authored
94 unsigned width,
95 unsigned height,
10fc4d2 @osresearch 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 @osresearch improve matrix test to do fancy hue shifting
authored
100 const unsigned color = cycle % 180;
101
71c60bc @osresearch send pre-formatted matrix to pru, rearrange based on config values
authored
102 for (unsigned x=0; x < width; x++) {
103 for (unsigned y=0; y < height; y++) {
b546d79 @osresearch more test to demonstate various ghosting bugs
authored
104 const int index = (color + x + y*phaseShift/4) % 180;
80cf09e @osresearch tweak the pwm params
authored
105 const uint32_t in = rainbowColors[index];
71c60bc @osresearch send pre-formatted matrix to pru, rearrange based on config values
authored
106 uint8_t * const out = &pixels[x + y*width];
f6b7fd9 @osresearch matrix test with a few other color options
authored
107 #if 1
108 out[0] = ((in >> 0) & 0xFF); // * y / 16;
109 out[1] = ((in >> 8) & 0xFF); // * y / 16;
110 out[2] = ((in >> 16) & 0xFF); // * y / 16;
80cf09e @osresearch tweak the pwm params
authored
111 #else
f6b7fd9 @osresearch matrix test with a few other color options
authored
112 //out[0] = ((in >> 0) & 0xFF);
113 //out[1] = ((in >> 8) & 0xFF);
114 //out[2] = ((in >> 16) & 0xFF);
115 out[0] = y + 3*x + cycle;
116 out[1] = y + 3*x + cycle;
117 out[2] = y + 3*x + cycle;
80cf09e @osresearch tweak the pwm params
authored
118 #endif
10fc4d2 @osresearch improve matrix test to do fancy hue shifting
authored
119 }
120 }
1fa2383 started on LED matrix code; this breaks the ws2821x code for now
root authored
121 }
122
123
8a36317 @osresearch disable output during low-brightness stages
authored
124 static void
125 gradient(
126 uint32_t * const pixels,
127 unsigned width,
128 unsigned height,
129 unsigned phaseShift,
130 unsigned cycle
131 )
132 {
133 cycle >>= 3;
134 for (unsigned x=0; x < width; x++) {
135 for (unsigned y=0; y < height; y++) {
136 uint8_t * const out = &pixels[x + y*width];
b546d79 @osresearch more test to demonstate various ghosting bugs
authored
137 #if 0
c67741f @osresearch fix bright rows 7 and 15. adjust brightness scaling
authored
138 //out[0] = ((x+cycle) % 32) * 8;
139 //out[1] = ((y+cycle) % 16) * 16;
b546d79 @osresearch more test to demonstate various ghosting bugs
authored
140 uint8_t b = 0xFF;
141 out[1] = b * ((((x + y + cycle) >> 5) ) & 1);
142 #else
143 uint8_t b = ((x+y+cycle) % 32) * 8;
144 out[0] = b * ((((x + y + cycle) >> 5) % 3) & 1);
145 out[1] = b * ((((x + y + cycle) >> 5) % 5) & 1);
146 out[2] = b * ((((x + y + cycle) >> 5) % 7) & 1);
147 #endif
8a36317 @osresearch disable output during low-brightness stages
authored
148 }
149 }
150 }
151
10fc4d2 @osresearch improve matrix test to do fancy hue shifting
authored
152 int
153 main(void)
1fa2383 started on LED matrix code; this breaks the ws2821x code for now
root authored
154 {
71c60bc @osresearch send pre-formatted matrix to pru, rearrange based on config values
authored
155 const int width = 256;
156 const int height = 64;
157 ledscape_t * const leds = ledscape_init(width, height);
1fa2383 started on LED matrix code; this breaks the ws2821x code for now
root authored
158 printf("init done\n");
159 time_t last_time = time(NULL);
160 unsigned last_i = 0;
161
10fc4d2 @osresearch improve matrix test to do fancy hue shifting
authored
162 // pre-compute the 180 rainbow colors
163 for (int i=0; i<180; i++)
164 {
165 int hue = i * 2;
166 int saturation = 100;
167 int lightness = 50;
168 rainbowColors[i] = makeColor(hue, saturation, lightness);
169 }
170
1fa2383 started on LED matrix code; this breaks the ws2821x code for now
root authored
171 unsigned i = 0;
71c60bc @osresearch send pre-formatted matrix to pru, rearrange based on config values
authored
172 uint32_t * const p = calloc(width*height,4);
173
1fa2383 started on LED matrix code; this breaks the ws2821x code for now
root authored
174 while (1)
175 {
c67741f @osresearch fix bright rows 7 and 15. adjust brightness scaling
authored
176 if (1)
177 rainbow(p, width, height, 10, i++);
178 else
179 gradient(p, width, height, 10, i++);
71c60bc @osresearch send pre-formatted matrix to pru, rearrange based on config values
authored
180 ledscape_draw(leds, p);
a131a9f @osresearch Run bitmap faster, hues slower
authored
181 usleep(20000);
1fa2383 started on LED matrix code; this breaks the ws2821x code for now
root authored
182
183 // wait for the previous frame to finish;
184 //const uint32_t response = ledscape_wait(leds);
185 const uint32_t response = 0;
186 time_t now = time(NULL);
187 if (now != last_time)
188 {
189 printf("%d fps. starting %d previous %"PRIx32"\n",
190 i - last_i, i, response);
191 last_i = i;
192 last_time = now;
193 }
194
195 }
196
197 ledscape_close(leds);
198
199 return EXIT_SUCCESS;
200 }
Something went wrong with that request. Please try again.