forked from nanoframework/nanoFramework.IoT.Device
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
109 lines (89 loc) · 2.43 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
// 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.Diagnostics;
using System.Threading;
using Iot.Device.Multiplexing;
int[] pins = new int[] { 4, 17, 27, 22 };
int charlieSegmentLength = 8;
bool controlCRequested = false;
int twoSeconds = 2000;
// calling this method helps with determing the correct pin circuit to use
CharlieplexSegmentNode[] nodes = CharlieplexSegment.GetNodes(pins, charlieSegmentLength);
for (int i = 0; i < charlieSegmentLength; i++)
{
CharlieplexSegmentNode node = nodes[i];
Debug.WriteLine($"Node {i} -- Anode: {node.Anode}; Cathode: {node.Cathode}");
}
using CharlieplexSegment segment = new(pins, charlieSegmentLength);
CancellationTokenSource cts = new();
Debug.WriteLine("Light all LEDs");
for (int i = 0; i < charlieSegmentLength; i++)
{
segment.Write(i, 1);
}
DisplayShouldCancel();
Debug.WriteLine("Dim all LEDs");
for (int i = 0; i < charlieSegmentLength; i++)
{
segment.Write(i, 0);
}
Debug.WriteLine("Light odd values");
for (int i = 0; i < charlieSegmentLength; i++)
{
if (i % 2 == 1)
{
segment.Write(i, 1);
}
}
DisplayShouldCancel();
for (int i = 0; i < charlieSegmentLength; i++)
{
segment.Write(i, 0);
}
var delayLengths = new int[] { 1, 5, 10, 25, 50, 100, 250, 500, 1000 };
foreach (var delay in delayLengths)
{
Debug.WriteLine($"Light one LED at a time -- Delay {delay}");
for (int i = 0; i < charlieSegmentLength; i++)
{
segment.Write(i, 1);
DisplayShouldCancel(delay);
segment.Write(i, 0);
DisplayShouldCancel(delay / 2);
}
}
Reverse(delayLengths);
foreach (var delay in delayLengths)
{
Debug.WriteLine($"Light and then dim all LEDs, in sequence. Delay: {delay}");
for (int i = 0; i < charlieSegmentLength; i++)
{
segment.Write(i, 1);
DisplayShouldCancel(delay);
}
for (int i = 0; i < charlieSegmentLength; i++)
{
segment.Write(i, 0);
DisplayShouldCancel(delay / 2);
}
}
void DisplayShouldCancel(int delay = -1)
{
if (delay == -1)
{
delay = twoSeconds;
}
cts.CancelAfter(delay);
segment.Display(cts.Token);
}
segment.Dispose();
void Reverse(int[] array)
{
int[] reversed = new int[array.Length];
for (int i = 0; i < array.Length; i++)
{
reversed[reversed.Length - 1 - i] = array[i];
}
array = reversed;
}