forked from dotnet/iot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
137 lines (103 loc) · 3.02 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
// 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 = "0123456789aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ-=+/\\*?%_|°[] ";
// Initialize display
using Large4Digit14SegmentDisplay 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.WriteChar('°', 1);
// Wait 2 seconds
Thread.Sleep(2000);
// 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("Hmm").PadLeft(4));
// 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("-42.C");
// 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),
("PPPP", Alignment.Left),
};
// Iterate WriteString branches
foreach (var (sample, alignment) in stringSamples)
{
display.Write(sample, alignment);
// Wait .5 second
Thread.Sleep(500);
}
var hexDigits = new byte[4];
new Random().NextBytes(hexDigits);
// Display random hex number
display.WriteHex(hexDigits);
// Wait 3 seconds
Thread.Sleep(3000);
// Display border
// ---
// | |
// ---
display.Write(new[]
{
Segment14.Top | Segment14.TopLeft | Segment14.BottomLeft | Segment14.Bottom,
Segment14.Top | Segment14.Bottom,
Segment14.Top | Segment14.Bottom,
Segment14.Top | Segment14.TopRight | Segment14.BottomRight | Segment14.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);
}
// Wait 3 seconds
Thread.Sleep(3000);
// Write OFF to right side of display
display.Write("Off", Alignment.Right);
// Wait 2 seconds
Thread.Sleep(2000);
// clear display for next run
display.Clear();
display.Flush();
// Turn off display
display.DisplayOn = false;