forked from dotnet/iot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
175 lines (144 loc) · 3.79 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
// 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.Gpio;
using System.Device.I2c;
using System.Linq;
using System.Threading;
using Iot.Device.Display;
// For LED Dot Matrix Breakouts
// using I2cDevice i2cDevice = I2cDevice.Create(new I2cConnectionSettings(busId: 1, DotMatrix10x7.DefaultI2cAddress));
// DotMatrix10x7 matrix = new(i2cDevice);
// For Micro Dot pHat
using I2cDevice first = I2cDevice.Create(new I2cConnectionSettings(busId: 1, MicroDotPhat30x7.I2cAddresses[0]));
using I2cDevice second = I2cDevice.Create(new I2cConnectionSettings(busId: 1, MicroDotPhat30x7.I2cAddresses[1]));
using I2cDevice third = I2cDevice.Create(new I2cConnectionSettings(busId: 1, MicroDotPhat30x7.I2cAddresses[2]));
MicroDotPhat30x7 matrix = new(first, second, third);
// Dimensions
int width = matrix.Width - 1;
int height = matrix.Height - 1;
int halfWidth = matrix.Width / 2;
int halfHeight = matrix.Height / 2;
matrix.Fill(0);
matrix[0, 0] = 1;
matrix[0, height] = 1;
matrix[width, 0] = 1;
matrix[width, height] = 1;
Thread.Sleep(500);
matrix.Fill(1);
Thread.Sleep(1000);
matrix.Fill(0);
// Set pixel in the origin 0, 0 position.
matrix[0, 0] = 1;
// Set pixels in the middle
matrix[halfWidth - 1, halfHeight - 1] = 1;
matrix[halfWidth - 1, halfHeight] = 1;
matrix[halfWidth, halfHeight - 1] = 1;
matrix[halfWidth, halfHeight] = 1;
// Set pixel on the opposite edge
matrix[width - 1, height - 1] = 1;
matrix[width - 1, height] = 1;
matrix[width, height - 1] = 1;
matrix[width, height] = 1;
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] = 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, height] = 1;
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] = 1;
matrix[diagonal - i, i] = 1;
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] = 1;
Thread.Sleep(10);
}
for (int i = 0; i < matrix.Height; i++)
{
matrix[width, i] = 1;
Thread.Sleep(10);
}
for (int i = width; i >= 0; i--)
{
matrix[i, height] = 1;
Thread.Sleep(10);
}
for (int i = height; i >= 0; i--)
{
matrix[0, i] = 1;
Thread.Sleep(50);
}
Thread.Sleep(500);
matrix.Fill(0);
void WriteRowPixels(int row, IEnumerable<int> pixels, PinValue value)
{
foreach (int pixel in pixels)
{
matrix[pixel, row] = value;
Thread.Sleep(15);
}
}
void WriteColumnPixels(int column, IEnumerable<int> pixels, PinValue value)
{
foreach (int pixel in pixels)
{
matrix[column, pixel] = value;
Thread.Sleep(15);
}
}
// Draw a spiral bounding box
int iterations = (int)(Math.Ceiling(diagonal / 2.0) + 1);
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), 1);
// right
WriteColumnPixels(width - j, Enumerable.Range(j + 1, rangeH - 1), 1);
// bottom
WriteRowPixels(height - j, Enumerable.Range(j, rangeW).Reverse(), 1);
// left
WriteColumnPixels(j, Enumerable.Range(j + 1, rangeH - 1).Reverse(), 1);
}
Thread.Sleep(1000);
matrix.Fill(0);
foreach (var index in Enumerable.Range(0, matrix.Length))
{
matrix[index].WriteDecimalPoint(1);
Thread.Sleep(500);
}
Thread.Sleep(250);
matrix.Fill(0);