-
-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy pathButtonBase.cs
224 lines (186 loc) · 7.15 KB
/
ButtonBase.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// 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.Threading;
namespace Iot.Device.Button
{
/// <summary>
/// Base implementation of Button logic.
/// Hardware independent. Inherit for specific hardware handling.
/// </summary>
public class ButtonBase : IDisposable
{
internal const long DefaultDoublePressTicks = 15000000;
internal const long DefaultHoldingMilliseconds = 2000;
private readonly long _doublePressTicks;
private readonly long _holdingMs;
private readonly TimeSpan _debounceTime;
private bool _disposed = false;
private long _debounceStartTicks;
private ButtonHoldingState _holdingState = ButtonHoldingState.Completed;
private long _lastPress = DateTime.MinValue.Ticks;
private Timer _holdingTimer;
private bool ShouldDebounce => DateTime.UtcNow.Ticks - _debounceStartTicks < _debounceTime.Ticks;
/// <summary>
/// Delegate for button pressed.
/// </summary>
/// <param name="sender">Caller object.</param>
/// <param name="e">Arguments for invoked delegate.</param>
public delegate void ButtonPressedDelegate(object sender, EventArgs e);
/// <summary>
/// Delegate for button holding.
/// </summary>
/// <param name="sender">Caller object.</param>
/// <param name="e">Arguments for invoked delegate.</param>
public delegate void ButtonHoldingDelegate(object sender, ButtonHoldingEventArgs e);
/// <summary>
/// Delegate for button up event.
/// </summary>
public event ButtonPressedDelegate ButtonUp;
/// <summary>
/// Delegate for button down event.
/// </summary>
public event ButtonPressedDelegate ButtonDown;
/// <summary>
/// Delegate for button pressed event.
/// </summary>
public event ButtonPressedDelegate Press;
/// <summary>
/// Delegate for button double pressed event.
/// </summary>
public event ButtonPressedDelegate DoublePress;
/// <summary>
/// Delegate for button holding event.
/// </summary>
public event ButtonHoldingDelegate Holding;
/// <summary>
/// Gets or sets a value indicating whether holding event is enabled or disabled on the button.
/// </summary>
public bool IsHoldingEnabled { get; set; } = false;
/// <summary>
/// Gets or sets a value indicating whether double press event is enabled or disabled on the button.
/// </summary>
public bool IsDoublePressEnabled { get; set; } = false;
/// <summary>
/// Gets or sets a value indicating whether single press event is enabled or disabled on the button.
/// </summary>
public bool IsPressed { get; set; } = false;
/// <summary>
/// Initializes a new instance of the <see cref="ButtonBase" /> class.
/// </summary>
public ButtonBase() : this(TimeSpan.FromTicks(DefaultDoublePressTicks), TimeSpan.FromMilliseconds(DefaultHoldingMilliseconds))
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ButtonBase" /> class.
/// </summary>
/// <param name="doublePress">Max ticks between button presses to count as doublepress.</param>
/// <param name="holding">Min ms a button is pressed to count as holding.</param>
/// <param name="debounceTime">The amount of time during which the transitions are ignored, or zero.</param>
public ButtonBase(TimeSpan doublePress, TimeSpan holding, TimeSpan debounceTime = default(TimeSpan))
{
if (debounceTime.TotalMilliseconds * 3 > doublePress.TotalMilliseconds)
{
throw new ArgumentException($"The parameter {nameof(doublePress)} should be at least three times {nameof(debounceTime)}");
}
_doublePressTicks = doublePress.Ticks;
_holdingMs = (long)holding.TotalMilliseconds;
_debounceTime = debounceTime;
}
/// <summary>
/// Handler for pressing the button.
/// </summary>
protected void HandleButtonPressed()
{
if (IsPressed || ShouldDebounce)
{
return;
}
IsPressed = true;
UpdateDebounce();
ButtonDown?.Invoke(this, new EventArgs());
if (IsHoldingEnabled)
{
StartHoldingTimer();
}
}
/// <summary>
/// Handler for releasing the button.
/// </summary>
protected void HandleButtonReleased()
{
ClearHoldingTimer();
if (!IsPressed)
{
return;
}
IsPressed = false;
UpdateDebounce();
ButtonUp?.Invoke(this, new EventArgs());
Press?.Invoke(this, new EventArgs());
if (IsHoldingEnabled && _holdingState == ButtonHoldingState.Started)
{
SetHoldingState(ButtonHoldingState.Completed);
}
if (IsDoublePressEnabled)
{
if (_lastPress == DateTime.MinValue.Ticks)
{
_lastPress = DateTime.UtcNow.Ticks;
}
else
{
if (DateTime.UtcNow.Ticks - _lastPress <= _doublePressTicks)
{
DoublePress.Invoke(this, new EventArgs());
}
_lastPress = DateTime.MinValue.Ticks;
}
}
}
/// <summary>
/// Handler for holding the button.
/// </summary>
/// <param name="state">What's that.</param>
private void StartHoldingHandler(object state)
{
ClearHoldingTimer();
SetHoldingState(ButtonHoldingState.Started);
}
private void UpdateDebounce() => _debounceStartTicks = DateTime.UtcNow.Ticks;
private void StartHoldingTimer() => _holdingTimer = new Timer(StartHoldingHandler, null, (int)_holdingMs, Timeout.Infinite);
private void ClearHoldingTimer()
{
_holdingTimer?.Dispose();
_holdingTimer = null;
}
private void SetHoldingState(ButtonHoldingState state)
{
_holdingState = state;
Holding?.Invoke(this, new ButtonHoldingEventArgs { HoldingState = state });
}
/// <summary>
/// Cleanup resources.
/// </summary>
/// <param name="disposing">Should dispose managed resources.</param>
protected virtual void Dispose(bool disposing)
{
if (_disposed)
{
return;
}
if (disposing)
{
ClearHoldingTimer();
}
_disposed = true;
}
/// <summary>
/// <inheritdoc/>
/// </summary>
public void Dispose()
{
Dispose(true);
}
}
}