Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 195 lines (171 sloc) 4.606 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;
101
71c60bc Trammell Hudson 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++) {
10fc4d2 Trammell Hudson improve matrix test to do fancy hue shifting
authored
104 const int index = (color + x + y*phaseShift/2) % 180;
80cf09e Trammell Hudson tweak the pwm params
authored
105 const uint32_t in = rainbowColors[index];
71c60bc Trammell Hudson send pre-formatted matrix to pru, rearrange based on config values
authored
106 uint8_t * const out = &pixels[x + y*width];
f6b7fd9 Trammell Hudson 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 Trammell Hudson tweak the pwm params
authored
111 #else
f6b7fd9 Trammell Hudson 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 Trammell Hudson tweak the pwm params
authored
118 #endif
10fc4d2 Trammell Hudson 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 Trammell Hudson 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];
c67741f Trammell Hudson fix bright rows 7 and 15. adjust brightness scaling
authored
137 //out[0] = ((x+cycle) % 32) * 8;
138 //out[1] = ((y+cycle) % 16) * 16;
139 out[0] = ((x+y+cycle) % 32) * 8;
140 out[1] = ((x+y+cycle) % 32) * 8;
141 out[2] = ((x+y+cycle) % 32) * 8;
8a36317 Trammell Hudson disable output during low-brightness stages
authored
142 }
143 }
144 }
145
10fc4d2 Trammell Hudson improve matrix test to do fancy hue shifting
authored
146 int
147 main(void)
1fa2383 started on LED matrix code; this breaks the ws2821x code for now
root authored
148 {
71c60bc Trammell Hudson send pre-formatted matrix to pru, rearrange based on config values
authored
149 const int width = 256;
150 const int height = 64;
151 ledscape_t * const leds = ledscape_init(width, height);
1fa2383 started on LED matrix code; this breaks the ws2821x code for now
root authored
152 printf("init done\n");
153 time_t last_time = time(NULL);
154 unsigned last_i = 0;
155
10fc4d2 Trammell Hudson improve matrix test to do fancy hue shifting
authored
156 // pre-compute the 180 rainbow colors
157 for (int i=0; i<180; i++)
158 {
159 int hue = i * 2;
160 int saturation = 100;
161 int lightness = 50;
162 rainbowColors[i] = makeColor(hue, saturation, lightness);
163 }
164
1fa2383 started on LED matrix code; this breaks the ws2821x code for now
root authored
165 unsigned i = 0;
71c60bc Trammell Hudson send pre-formatted matrix to pru, rearrange based on config values
authored
166 uint32_t * const p = calloc(width*height,4);
167
1fa2383 started on LED matrix code; this breaks the ws2821x code for now
root authored
168 while (1)
169 {
c67741f Trammell Hudson fix bright rows 7 and 15. adjust brightness scaling
authored
170 if (1)
171 rainbow(p, width, height, 10, i++);
172 else
173 gradient(p, width, height, 10, i++);
71c60bc Trammell Hudson send pre-formatted matrix to pru, rearrange based on config values
authored
174 ledscape_draw(leds, p);
a131a9f Trammell Hudson Run bitmap faster, hues slower
authored
175 usleep(20000);
1fa2383 started on LED matrix code; this breaks the ws2821x code for now
root authored
176
177 // wait for the previous frame to finish;
178 //const uint32_t response = ledscape_wait(leds);
179 const uint32_t response = 0;
180 time_t now = time(NULL);
181 if (now != last_time)
182 {
183 printf("%d fps. starting %d previous %"PRIx32"\n",
184 i - last_i, i, response);
185 last_i = i;
186 last_time = now;
187 }
188
189 }
190
191 ledscape_close(leds);
192
193 return EXIT_SUCCESS;
194 }
Something went wrong with that request. Please try again.