forked from dotnet/iot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
221 lines (184 loc) · 5.18 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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.Device.I2c;
using System.Linq;
using System.Threading;
using Iot.Device.Display;
// For 16x9 matrix
// https://www.adafruit.com/product/2974
using I2cDevice i2cDevice = I2cDevice.Create(new I2cConnectionSettings(busId: 1, Backpack16x9.DefaultI2cAddress));
Backpack16x9 matrix = new(i2cDevice);
// For 16x8 matrix Charlieplex bonet
// https://www.adafruit.com/product/4122
// using I2cDevice i2cDevice = I2cDevice.Create(new I2cConnectionSettings(busId: 1, Bonnet16x8.DefaultI2cAddress));
// Bonnet16x8 matrix = new(i2cDevice);
// For Scroll Phat HD 17x7 and Scroll Hat Mini 17x7
// https://shop.pimoroni.com/products/scroll-phat-hd
// https://shop.pimoroni.com/products/scroll-hat-mini
// using I2cDevice i2cDevice = I2cDevice.Create(new I2cConnectionSettings(busId: 1, ScrollPhat17x7.DefaultI2cAddress));
// ScrollPhat17x7 matrix = new(i2cDevice);
// For LED Matrix Breakout 11x7
// https://shop.pimoroni.com/products/11x7-led-matrix-breakout
// using I2cDevice i2cDevice = I2cDevice.Create(new I2cConnectionSettings(busId: 1, Breakout11x7.DefaultI2cAddress));
// Breakout11x7 matrix = new(i2cDevice);
// blink range: 270-2159; smaller is faster blink
// brightness range: 0-255; higher is brighter
byte fullLit = 255;
byte halfLit = 128;
byte quarterLit = 64;
matrix.Initialize();
matrix.SetBlinkingRate(0);
matrix.Fill(0);
// Dimensions
int width = matrix.Width - 1;
int height = matrix.Height - 1;
int halfWidth = matrix.Width / 2;
int halfHeight = matrix.Height / 2;
matrix[0, 0] = fullLit;
matrix[0, height] = fullLit;
matrix[width, 0] = fullLit;
matrix[width, height] = fullLit;
Thread.Sleep(1000);
matrix.Fill(0);
matrix.SetBlinkingRate(270);
matrix.WritePixel(halfWidth - 1, halfHeight - 1, halfLit, true, true);
matrix.WritePixel(halfWidth - 1, halfHeight, halfLit, true, true);
matrix.WritePixel(halfWidth, halfHeight - 1, halfLit, true, true);
matrix.WritePixel(halfWidth, halfHeight, halfLit, true, true);
Thread.Sleep(1500);
matrix.SetBlinkingRate(0);
matrix.Fill(0);
FillSlow(1);
FillSlow(2);
FillSlow(4);
FillSlow(8);
FillSlow(16);
for (int i = 0; i < matrix.Width; i++)
{
matrix[i, 0] = 0;
Thread.Sleep(50);
matrix[i, 0] = quarterLit;
Thread.Sleep(50);
}
Thread.Sleep(100);
// Clear matrixgit
matrix.Fill(0);
// Set pixel in the origin 0, 0 position.
matrix[0, 0] = halfLit;
// Set pixels in the middle
matrix[halfWidth - 1, halfHeight - 1] = halfLit;
matrix[halfWidth - 1, halfHeight] = halfLit;
matrix[halfWidth, halfHeight - 1] = halfLit;
matrix[halfWidth, halfHeight] = halfLit;
// Set pixel on the opposite edge
matrix[width - 1, height - 1] = halfLit;
matrix[width - 1, height] = halfLit;
matrix[width, height - 1] = halfLit;
matrix[width, height] = halfLit;
Thread.Sleep(500);
matrix.Fill(0);
// Draw line in first row
for (int i = 0; i < matrix.Width; i++)
{
if (i % 2 is 1)
{
continue;
}
matrix[i, 0] = quarterLit;
Thread.Sleep(50);
}
// Draw line in last row
for (int i = 0; i < matrix.Width; i++)
{
if (i % 2 is 0)
{
continue;
}
matrix[i, height] = quarterLit;
Thread.Sleep(50);
}
Thread.Sleep(500);
matrix.Fill(0);
int diagonal = Math.Min(height, width);
// Draw diagonal lines
for (int i = 0; i <= diagonal; i++)
{
matrix[i, i] = fullLit;
matrix[diagonal - i, i] = fullLit;
Thread.Sleep(50);
}
for (int i = 0; i <= diagonal; i++)
{
matrix[i, i] = 0;
matrix[diagonal - i, i] = 0;
Thread.Sleep(50);
}
// Draw a bounding box
for (int i = 0; i < matrix.Width; i++)
{
matrix[i, 0] = fullLit;
Thread.Sleep(10);
}
for (int i = 0; i < matrix.Height; i++)
{
matrix[width, i] = fullLit;
Thread.Sleep(10);
}
for (int i = width; i >= 0; i--)
{
matrix[i, height] = fullLit;
Thread.Sleep(10);
}
for (int i = height; i >= 0; i--)
{
matrix[0, i] = fullLit;
Thread.Sleep(50);
}
Thread.Sleep(500);
matrix.Fill(0);
// Draw a spiral bounding box
int iterations = (int)Math.Ceiling(matrix.Height / 2.0);
for (int j = 0; j < iterations; j++)
{
int rangeW = matrix.Width - j * 2;
int rangeH = matrix.Height - j * 2;
// top
WriteRowPixels(j, Enumerable.Range(j, rangeW), quarterLit);
// right
WriteColumnPixels(width - j, Enumerable.Range(j + 1, rangeH - 1), quarterLit);
// bottom
WriteRowPixels(height - j, Enumerable.Range(j, rangeW).Reverse(), quarterLit);
// left
WriteColumnPixels(j, Enumerable.Range(j + 1, rangeH - 1).Reverse(), quarterLit);
}
Thread.Sleep(1000);
matrix.Fill(0);
void WriteRowPixels(int row, IEnumerable<int> pixels, int value)
{
foreach (int pixel in pixels)
{
matrix[pixel, row] = (byte)value;
Thread.Sleep(15);
}
}
void WriteColumnPixels(int column, IEnumerable<int> pixels, int value)
{
foreach (int pixel in pixels)
{
matrix[column, pixel] = (byte)value;
Thread.Sleep(15);
}
}
void FillSlow(byte brightness)
{
for (int h = 0; h < matrix.Height; h++)
{
for (int w = 0; w < matrix.Width; w++)
{
matrix[w, h] = brightness;
Thread.Sleep(2);
}
}
}