-
Notifications
You must be signed in to change notification settings - Fork 598
/
Copy pathAnimateLeds.cs
177 lines (144 loc) · 4.59 KB
/
AnimateLeds.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
// 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.Gpio;
using System.Threading;
using System.Linq;
using System.Collections.Generic;
using Iot.Device.Multiplexing;
public class AnimateLeds : IDisposable
{
private readonly int[] _leds;
private readonly int[] _ledsRev;
private readonly int _litTimeDefault = 200;
private readonly int _dimTimeDefault = 50;
private IOutputSegment _segment;
public AnimateLeds(IOutputSegment segment)
{
_segment = segment;
_leds = Enumerable.Range(0,_segment.Length).ToArray();
_ledsRev = _leds.Reverse().ToArray();
LitTime = _litTimeDefault;
DimTime = _dimTimeDefault;
}
public int LitTime {get; set; }
public int DimTime {get; set; }
public void ResetTime()
{
LitTime = _litTimeDefault;
DimTime = _dimTimeDefault;
}
private void CycleLeds(CancellationToken token, params int[] outputs)
{
if (token.IsCancellationRequested) return;
// light time
foreach (int output in outputs)
{
_segment.Write(output, PinValue.High);
}
if (DisplayShouldCancel(token, LitTime)) return;
// dim time
foreach (int output in outputs)
{
_segment.Write(output, PinValue.Low);
}
if (DisplayShouldCancel(token, DimTime)) return;
}
public void Sequence(CancellationToken token, IEnumerable<int> outputs)
{
Console.WriteLine(nameof(Sequence));
foreach (int output in outputs)
{
if (token.IsCancellationRequested) return;
CycleLeds(token, output);
}
}
public void FrontToBack(CancellationToken token, bool skipLast = false)
{
Console.WriteLine(nameof(FrontToBack));
if (token.IsCancellationRequested) return;
int iterations = skipLast ? _segment.Length : _segment.Length - 2;
Sequence(token, _leds.AsSpan(0,iterations).ToArray());
}
public void BacktoFront(CancellationToken token)
{
Console.WriteLine(nameof(BacktoFront));
if (token.IsCancellationRequested) return;
Sequence(token,_ledsRev);
}
public void MidToEnd(CancellationToken token)
{
Console.WriteLine(nameof(MidToEnd));
var half = _leds.Length / 2;
if (half % 2 == 1)
{
if (token.IsCancellationRequested) return;
CycleLeds(token, half);
}
for (var i = 1; i < half+1; i ++)
{
var ledA= half - i;
var ledB = half - 1 + i;
if (token.IsCancellationRequested) return;
CycleLeds(token, ledA, ledB);
}
}
public void EndToMid(CancellationToken token)
{
Console.WriteLine(nameof(EndToMid));
var half = _leds.Length / 2;
for (var i = 0; i < half ; i++)
{
var ledA = i;
var ledB = _segment.Length - 1 - i;
if (token.IsCancellationRequested) return;
CycleLeds(token, ledA, ledB);
}
if (half % 2 == 1)
{
if (token.IsCancellationRequested) return;
CycleLeds(token, half);
}
}
public void LightAll(CancellationToken token)
{
Console.WriteLine(nameof(LightAll));
for(int i = 0; i < _leds.Length; i++)
{
if (token.IsCancellationRequested) return;
_segment.Write(i, PinValue.High);
}
if (DisplayShouldCancel(token, LitTime)) return;
}
public void DimAllAtRandom(CancellationToken token)
{
Console.WriteLine(nameof(DimAllAtRandom));
var random = new Random();
var ledList = _leds.ToList();
while (ledList.Count > 0)
{
if (DisplayShouldCancel(token, DimTime)) return;
var led = random.Next(_leds.Length);
if (ledList.Remove(led))
{
_segment.Write(led, PinValue.Low);
}
}
}
public void Dispose()
{
_segment?.Dispose();
_segment = null!;
}
bool DisplayShouldCancel(CancellationToken token, int delay)
{
if (token.IsCancellationRequested)
{
return token.IsCancellationRequested;
}
using CancellationTokenSource delaySource = CancellationTokenSource.CreateLinkedTokenSource(token);
delaySource.CancelAfter(delay);
_segment.Display(delaySource.Token);
return token.IsCancellationRequested;
}
}