/
Pcx857x.DigitalInterruptPort.cs
115 lines (102 loc) · 4.11 KB
/
Pcx857x.DigitalInterruptPort.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
using Meadow.Hardware;
using System;
namespace Meadow.Foundation.ICs.IOExpanders
{
public partial class Pcx857x
{
/// <summary>
/// A Pcx857x-specific implementation of the IDigitalInterruptPort
/// </summary>
public class DigitalInterruptPort : DigitalInterruptPortBase
{
/// <inheritdoc/>
public override ResistorMode Resistor
{
get => resistorMode;
set
{
switch (value)
{
case ResistorMode.InternalPullUp:
case ResistorMode.InternalPullDown:
throw new NotSupportedException($"This device does not support internal resistors");
}
resistorMode = value;
}
}
/// <inheritdoc/>
public override bool State => state;
private bool state = false;
private DateTime lastUpdate;
private ResistorMode resistorMode = ResistorMode.Disabled;
internal event EventHandler Disposed = default!;
/// <summary>
/// Debouce duration
/// </summary>
public override TimeSpan DebounceDuration { get; set; } = TimeSpan.Zero;
/// <summary>
/// Glitch duration
/// </summary>
public override TimeSpan GlitchDuration
{
get => TimeSpan.FromMilliseconds(0.00015);
set => _ = value; //fail silently
}
/// <summary>
/// Create a new DigitalInterruptPort object
/// </summary>
/// <param name="pin">The interrupt pin</param>
/// <param name="interruptMode">The interrupt mode used for the interrupt pin</param>
/// <param name="resistorMode">The resistor mode used by the interrupt pin</param>
public DigitalInterruptPort(IPin pin, InterruptMode interruptMode = InterruptMode.None, ResistorMode resistorMode = ResistorMode.Disabled)
: base(pin, (IDigitalChannelInfo)pin.SupportedChannels![0], interruptMode)
{
this.resistorMode = resistorMode;
}
/// <summary>
/// Update the port value
/// </summary>
/// <param name="newState">The new port state</param>
internal void Update(bool newState)
{
if (DateTime.UtcNow - lastUpdate < DebounceDuration)
{
return;
}
var now = DateTime.UtcNow;
if (newState != state)
{
switch (InterruptMode)
{
case InterruptMode.EdgeFalling:
if (newState)
{
RaiseChangedAndNotify(new DigitalPortResult(new DigitalState(false, now), new DigitalState(true, lastUpdate)));
}
break;
case InterruptMode.EdgeRising:
if (newState)
{
RaiseChangedAndNotify(new DigitalPortResult(new DigitalState(true, now), new DigitalState(false, lastUpdate)));
}
break;
case InterruptMode.EdgeBoth:
RaiseChangedAndNotify(new DigitalPortResult(new DigitalState(newState, now), new DigitalState(!newState, lastUpdate)));
break;
case InterruptMode.None:
default:
break;
}
}
state = newState;
lastUpdate = now;
}
///<inheritdoc/>
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
Disposed?.Invoke(this, EventArgs.Empty);
}
}
}
}