-
-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathProgram.cs
239 lines (209 loc) · 5.48 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Iot.Device.Ws28xx.Esp32;
using nanoFramework.Hardware.Esp32;
using System;
using System.Diagnostics;
using System.Drawing;
// Configure the count of pixels
const int Count = 10;
// Adjust the pin number
const int Pin = 15;
// Uncomment for WS2008
// Ws28xx neo = new Ws2808(Pin, Count);
// Uncomment for WS2812B
// Ws28xx neo = new Ws2812b(Pin, Count);
// Uncomment for WS2812C
// Ws28xx neo = new Ws2812c(Pin, Count);
// Comment if you are using one of the previous one
Ws28xx neo = new Sk6812(Pin, Count);
// BenchmarkClearPixel(); uncomment to benchmark
while (true)
{
// Uncomment to run this test as well:
// ColorFade(neo, Count);
ColorWipe(neo, Color.White, Count);
ColorWipe(neo, Color.Red, Count);
ColorWipe(neo, Color.Green, Count);
ColorWipe(neo, Color.Blue, Count);
TheatreChase(neo, Color.White, Count);
TheatreChase(neo, Color.Red, Count);
TheatreChase(neo, Color.Green, Count);
TheatreChase(neo, Color.Blue, Count);
Rainbow(neo, Count);
RainbowCycle(neo, Count);
TheaterChaseRainbow(neo, Count);
}
void ColorWipe(Ws28xx neo, Color color, int count)
{
BitmapImage img = neo.Image;
for (var i = 0; i < count; i++)
{
img.SetPixel(i, 0, color);
neo.Update();
}
}
void ColorFade(Ws28xx neo, int count)
{
BitmapImage img = neo.Image;
// White
for (byte iteration = 0; iteration < 255; iteration++)
{
for (var pixel = 0; pixel < count; pixel++)
{
img.SetPixel(pixel, 0, iteration, iteration, iteration);
}
neo.Update();
System.Threading.Thread.Sleep(10);
}
// Red
for (byte iteration = 0; iteration < 255; iteration++)
{
for (var pixel = 0; pixel < count; pixel++)
{
img.SetPixel(pixel, 0, iteration, 0, 0);
}
neo.Update();
System.Threading.Thread.Sleep(10);
}
// Green
for (byte iteration = 0; iteration < 255; iteration++)
{
for (var pixel = 0; pixel < count; pixel++)
{
img.SetPixel(pixel, 0, 0, iteration, 0);
}
neo.Update();
System.Threading.Thread.Sleep(10);
}
// Blue
for (byte iteration = 0; iteration < 255; iteration++)
{
for (var pixel = 0; pixel < count; pixel++)
{
img.SetPixel(pixel, 0, 0, 0, iteration);
}
neo.Update();
System.Threading.Thread.Sleep(10);
}
}
void TheatreChase(Ws28xx neo, Color color, int count, int iterations = 10)
{
BitmapImage img = neo.Image;
for (var i = 0; i < iterations; i++)
{
for (var j = 0; j < 3; j++)
{
for (var k = 0; k < count; k += 3)
{
if (j + k < count)
{
img.SetPixel(j + k, 0, color);
}
}
neo.Update();
System.Threading.Thread.Sleep(100);
for (var k = 0; k < count; k += 3)
{
if (j + k < count)
{
img.SetPixel(j + k, 0, Color.Black);
}
}
}
}
}
Color Wheel(int position)
{
if (position < 85)
{
return Color.FromArgb(position * 3, 255 - position * 3, 0);
}
else if (position < 170)
{
position -= 85;
return Color.FromArgb(255 - position * 3, 0, position * 3);
}
else
{
position -= 170;
return Color.FromArgb(0, position * 3, 255 - position * 3);
}
}
void Rainbow(Ws28xx neo, int count, int iterations = 1)
{
BitmapImage img = neo.Image;
for (var i = 0; i < 255 * iterations; i++)
{
for (var j = 0; j < count; j++)
{
img.SetPixel(j, 0, Wheel((i + j) & 255));
}
neo.Update();
}
}
void RainbowCycle(Ws28xx neo, int count, int iterations = 1)
{
BitmapImage img = neo.Image;
for (var i = 0; i < 255 * iterations; i++)
{
for (var j = 0; j < count; j++)
{
img.SetPixel(j, 0, Wheel(((j * 255 / count) + i) & 255));
}
neo.Update();
}
}
void TheaterChaseRainbow(Ws28xx neo, int count)
{
BitmapImage img = neo.Image;
for (var i = 0; i < 255; i++)
{
for (var j = 0; j < 3; j++)
{
for (var k = 0; k < count; k += 3)
{
if (k + j < count)
{
img.SetPixel(k + j, 0, Wheel((k + i) % 255));
}
}
neo.Update();
System.Threading.Thread.Sleep(100);
for (var k = 0; k < count; k += 3)
{
if (k + j < count)
{
img.SetPixel(k + j, 0, Color.Black);
}
}
}
}
}
void BenchmarkClearPixel()
{
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < 1000; i++)
{
neo.Image.Clear();
neo.Update();
}
sw.Stop();
Debug.WriteLine("Clear all" + sw.Elapsed.ToString());
Stopwatch sw2 = new Stopwatch();
sw2.Start();
for (int i = 0; i < 1000; i++)
{
for (int y = 0; y < neo.Image.Height; y++)
{
for (int x = 0; x < neo.Image.Width; x++)
{
neo.Image.Clear(x, y);
}
}
neo.Update();
}
sw2.Stop();
Debug.WriteLine("Clear pixel by pixel " + sw2.Elapsed.ToString());
}