forked from dotnet/iot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
164 lines (126 loc) · 3.73 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
// 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.Device.I2c;
using System.Threading;
using Iot.Device.Display;
const string SupportedCharacters = "0123456789aAbBcCdDeEfFgGhHiIjJlLnNoOpPrRsStuUyYzZ-=_|°[] ";
// Initialize display
using Large4Digit7SegmentDisplay display = new(I2cDevice.Create(new I2cConnectionSettings(busId: 1, Ht16k33.DefaultI2cAddress)))
{
// Set max brightness
Brightness = Ht16k33.MaxBrightness
};
// Write "Iot" on the display
display.Write("Iot");
// Wait 2 seconds
Thread.Sleep(2000);
// Change 'o' to '°' in "IoT"
display[1] = (Segment)FontHelper.GetCharacter('°');
// Wait .5 seconds
Thread.Sleep(500);
// Write 42 to the left side of the display
display.Write(42, Alignment.Left);
// Set blinkrate to once per second
display.BlinkRate = BlinkRate.Blink1Hz;
// Wait 5 seconds
Thread.Sleep(5000);
// Set blinkrate to twice per second
display.BlinkRate = BlinkRate.Blink2Hz;
// Write 42 to the right side of the display
display.Write(42, Alignment.Right);
// Set brightness to half max
display.Brightness = Ht16k33.MaxBrightness / 2;
// Wait 2 seconds
Thread.Sleep(2000);
// Turn off blinking
display.BlinkRate = BlinkRate.Off;
// Write time to the display
display.Write(DateTime.Now.ToString("H:mm").PadLeft(5));
// Wait 3 seconds
Thread.Sleep(3000);
// Turn on buffering
display.BufferingEnabled = true;
// Write -42°C to display using "decimal point" between 3rd and 4th digit as the ° character
display.Write("-42C");
display.Dots = Dot.DecimalPoint;
// Turn off buffering
display.BufferingEnabled = false;
// Wait 3 seconds
Thread.Sleep(3000);
var stringSamples = new[]
{
("P", Alignment.Left),
("P", Alignment.Right),
("PP", Alignment.Left),
("PP", Alignment.Right),
("PPP", Alignment.Left),
("PPP", Alignment.Right),
("PP:", Alignment.Left),
(":PP", Alignment.Right),
("PP:P", Alignment.Left),
("P:PP", Alignment.Right),
("PPPP", Alignment.Left),
("PP:PP", Alignment.Left),
(":PP:PP", Alignment.Left),
(":PP:P", Alignment.Left),
(":PP:", Alignment.Left),
(":PP", Alignment.Left),
(":P", Alignment.Left),
(":PPP", Alignment.Left),
(":PPPP", Alignment.Left),
(":", Alignment.Left),
};
// Iterate WriteString branches
foreach (var (sample, alignment) in stringSamples)
{
display.Write(sample, alignment);
// Wait .5 second
Thread.Sleep(500);
}
// Turn off all dots
display.Dots = Dot.Off;
var hexDigits = new byte[4];
new Random().NextBytes(hexDigits);
// Display random hex number
display.Write(FontHelper.GetHexDigits(hexDigits));
// Wait 3 seconds
Thread.Sleep(3000);
// Display border
// ---
// | |
// ---
display.Write(new[]
{
Segment.Top | Segment.TopLeft | Segment.BottomLeft | Segment.Bottom,
Segment.Top | Segment.Bottom,
Segment.Top | Segment.Bottom,
Segment.Top | Segment.TopRight | Segment.BottomRight | Segment.Bottom
});
// Wait 3 seconds
Thread.Sleep(3000);
// Iterate supported characters
for (int i = 0, l = SupportedCharacters.Length - 3; i < l; i++)
{
display.Write(SupportedCharacters.Substring(i, 4));
// Wait 0.5 seconds
Thread.Sleep(500);
}
var dots = new[] { Dot.CenterColon, Dot.DecimalPoint, Dot.LeftLower, Dot.LeftUpper };
// Iterate dots
foreach (var dot in dots)
{
display.Dots = dot;
// Wait 0.5 seconds
Thread.Sleep(500);
}
// Set all supported dots
display.Dots = Dot.CenterColon | Dot.DecimalPoint | Dot.LeftLower | Dot.LeftUpper;
// Wait 3 seconds
Thread.Sleep(3000);
// Write OFF to right side of display
display.Write("Off", Alignment.Right);
// Wait 2 seconds
Thread.Sleep(2000);
// Turn off display
display.DisplayOn = false;