Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 160 lines (136 sloc) 3.613 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,
94 unsigned num_leds,
95 unsigned phaseShift,
96 unsigned cycle
1fa2383 started on LED matrix code; this breaks the ws2821x code for now
root authored
97 )
98 {
10fc4d2 @osresearch improve matrix test to do fancy hue shifting
authored
99 const unsigned color = cycle % 180;
100
101 for (unsigned x=0; x < num_leds; x++) {
102 for (unsigned y=0; y < 16; y++) {
103 const int index = (color + x + y*phaseShift/2) % 180;
104 pixels[x + y*num_leds] = rainbowColors[index];
105 }
106 }
1fa2383 started on LED matrix code; this breaks the ws2821x code for now
root authored
107 }
108
109
10fc4d2 @osresearch improve matrix test to do fancy hue shifting
authored
110 int
111 main(void)
1fa2383 started on LED matrix code; this breaks the ws2821x code for now
root authored
112 {
1200674 @osresearch Off by one error in row assignment fixed; brightness is not so great
authored
113 const int num_pixels = 256;
1fa2383 started on LED matrix code; this breaks the ws2821x code for now
root authored
114 ledscape_t * const leds = ledscape_init(num_pixels);
115 printf("init done\n");
116 time_t last_time = time(NULL);
117 unsigned last_i = 0;
118
10fc4d2 @osresearch improve matrix test to do fancy hue shifting
authored
119 // pre-compute the 180 rainbow colors
120 for (int i=0; i<180; i++)
121 {
122 int hue = i * 2;
123 int saturation = 100;
124 int lightness = 50;
125 rainbowColors[i] = makeColor(hue, saturation, lightness);
126 }
127
1fa2383 started on LED matrix code; this breaks the ws2821x code for now
root authored
128 unsigned i = 0;
129 while (1)
130 {
131 // Alternate frame buffers on each draw command
132 const unsigned frame_num = i++ % 2;
133 ledscape_frame_t * const frame
134 = ledscape_frame(leds, frame_num);
135
136 uint32_t * const p = (void*) frame;
137
10fc4d2 @osresearch improve matrix test to do fancy hue shifting
authored
138 rainbow(p, num_pixels, 10, i);
1200674 @osresearch Off by one error in row assignment fixed; brightness is not so great
authored
139 ledscape_draw(leds, frame_num);
10fc4d2 @osresearch improve matrix test to do fancy hue shifting
authored
140 usleep(10000);
1fa2383 started on LED matrix code; this breaks the ws2821x code for now
root authored
141
142 // wait for the previous frame to finish;
143 //const uint32_t response = ledscape_wait(leds);
144 const uint32_t response = 0;
145 time_t now = time(NULL);
146 if (now != last_time)
147 {
148 printf("%d fps. starting %d previous %"PRIx32"\n",
149 i - last_i, i, response);
150 last_i = i;
151 last_time = now;
152 }
153
154 }
155
156 ledscape_close(leds);
157
158 return EXIT_SUCCESS;
159 }
Something went wrong with that request. Please try again.