forked from dotnet/iot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.Matrix.cs
191 lines (161 loc) · 3.62 KB
/
Program.Matrix.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
// 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;
// Supports Matrix 8x8 and 16x8
// - https://www.adafruit.com/product/1632
// - https://www.adafruit.com/product/2042
// Initialize display
// Use Matrix8x8 type for 8x8 matrix
// Use Matrix16x8 type for 16x8 matrix
using Matrix8x8 matrix = new(I2cDevice.Create(new I2cConnectionSettings(busId: 1, Ht16k33.DefaultI2cAddress)))
{
// Set max brightness
Brightness = Ht16k33.MaxBrightness,
BufferingEnabled = true
};
// Dimensions
int width = matrix.Width - 1;
int halfWidth = matrix.Width / 2;
// Clear matrix
matrix.Clear();
// Set pixel in the origin 0, 0 position.
matrix[0, 0] = 1;
// Set pixels in the middle
matrix[halfWidth - 1, 3] = 1;
matrix[halfWidth - 1, 4] = 1;
matrix[halfWidth, 3] = 1;
matrix[halfWidth, 4] = 1;
// Set pixel on the opposite edge
matrix[width - 1, 6] = 1;
matrix[width - 1, 7] = 1;
matrix[width, 6] = 1;
matrix[width, 7] = 1;
Thread.Sleep(500);
matrix.Clear();
// Draw line in first row
for (int i = 0; i < matrix.Width; i++)
{
if (i % 2 is 1)
{
continue;
}
matrix[i, 0] = 1;
Thread.Sleep(50);
}
// Draw line in last row
for (int i = 0; i < matrix.Width; i++)
{
if (i % 2 is 0)
{
continue;
}
matrix[i, 7] = 1;
Thread.Sleep(50);
}
Thread.Sleep(500);
matrix.Clear();
// Draw diagonal lines
for (int i = 0; i < 8; i++)
{
matrix[i, i] = 1;
matrix[8 - i, i] = 1;
// uncomment if matrix.Width == 16
// matrix[i + 8, i] = 1;
// matrix[15 - i, i] = 1;
Thread.Sleep(50);
}
for (int i = 0; i < 8; i++)
{
matrix[i, i] = 0;
matrix[8 - i, i] = 0;
// uncomment if matrix.Width == 16
// matrix[i + 8, i] = 0;
// matrix[15 - i, i] = 0;
Thread.Sleep(50);
}
// Draw a bounding box
for (int i = 0; i < matrix.Width; i++)
{
matrix[i, 0] = 1;
Thread.Sleep(10);
}
for (int i = 0; i < 8; i++)
{
matrix[width, i] = 1;
Thread.Sleep(10);
}
for (int i = width; i >= 0; i--)
{
matrix[i, 7] = 1;
Thread.Sleep(10);
}
for (int i = 7; i >= 0; i--)
{
matrix[0, i] = 1;
Thread.Sleep(50);
}
Thread.Sleep(500);
matrix.Clear();
void WriteRowPixels(int row, IEnumerable<int> pixels, int value)
{
foreach (int pixel in pixels)
{
matrix[pixel, row] = value;
Thread.Sleep(15);
}
}
void WriteColumnPixels(int column, IEnumerable<int> pixels, int value)
{
foreach (int pixel in pixels)
{
matrix[column, pixel] = value;
Thread.Sleep(15);
}
}
// Draw a spiral bounding box
for (int j = 0; j < 4; j++)
{
int rangeW = matrix.Width - j * 2;
int rangeH = 8 - j * 2;
// top
WriteRowPixels(j, Enumerable.Range(j, rangeW), 1);
// right
WriteColumnPixels(width - j, Enumerable.Range(j + 1, rangeH - 2), 1);
// bottom
WriteRowPixels(7 - j, Enumerable.Range(j, rangeW).Reverse(), 1);
// left
WriteColumnPixels(j, Enumerable.Range(j + 1, rangeH - 2).Reverse(), 1);
}
Thread.Sleep(500);
matrix.Clear();
matrix[0, 0] = 1;
matrix[0, 7] = 1;
matrix[width, 0] = 1;
matrix[width, 7] = 1;
Thread.Sleep(500);
matrix.Clear();
// Fill matrix
matrix.Fill();
Thread.Sleep(500);
matrix.Clear();
var smiley = new byte[]
{
0b00111100,
0b01000010,
0b10100101,
0b10000001,
0b10100101,
0b10011001,
0b01000010,
0b00111100
};
matrix.Write(smiley);
// uncomment if matrix.Width == 16
// matrix.Write(smiley, 1);
Thread.Sleep(2000);
matrix.Clear();