This repository has been archived by the owner on May 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLimitSwitch.cs
73 lines (65 loc) · 2.72 KB
/
LimitSwitch.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
using System;
using Scarlet.Utilities;
using Scarlet.IO;
namespace Scarlet.Components.Sensors
{
/// <summary> Quickly releases incoming interrupt events to prevent I/O system from getting bogged down, then will pass them on when UpdateState is called next. </summary>
public class LimitSwitch : ISensor
{
private IDigitalIn Input;
private readonly bool Invert;
private volatile bool HadEvent, NewState;
public event EventHandler<LimitSwitchToggle> SwitchToggle;
public bool State { get => (this.Invert ? !Input.GetInput() : Input.GetInput()); }
public string System { get; set; }
public bool TraceLogging { get; set; }
/// <param name="Input"> Must also implement IInterruptSource. </param>
public LimitSwitch(IDigitalIn Input, bool Invert = false)
{
if (!(Input is IInterruptSource)) { throw new Exception("LimitSwitch IDigitalIn must also be IInterruptSource."); }
this.Input = Input;
this.Invert = Invert;
((IInterruptSource)Input).RegisterInterruptHandler(EventTriggered, InterruptType.ANY_EDGE);
}
public bool Test()
{
return true; // TODO: Determine method for, and implement, limit switch testing.
}
/// <summary>
/// Checks if the switch has been pressed since the last UpdateState, and fires off relevant events if so.
/// Call this frequently enough to catch all presses, as only one event is registered per cycle.
/// I.e. if a switch is pressed 3 times between UpdateState() calls, only one event is sent.
/// </summary>
public void UpdateState()
{
if(this.HadEvent)
{
LimitSwitchToggle Event = new LimitSwitchToggle() { CurrentState = this.Invert ? !this.NewState : this.NewState };
OnSwitchToggle(Event);
this.HadEvent = false;
}
}
protected virtual void OnSwitchToggle(LimitSwitchToggle Event) { SwitchToggle?.Invoke(this, Event); }
/// <summary> Receives incoming InputInterrupt events to process. </summary>
public void EventTriggered(object Sender, EventArgs Event)
{
if(Event is InputInterrupt)
{
this.HadEvent = true;
this.NewState = ((InputInterrupt)Event).NewState;
}
}
public DataUnit GetData()
{
return new DataUnit("LimitSwitch")
{
{ "Triggered", this.State }
}
.SetSystem(this.System);
}
}
public class LimitSwitchToggle : EventArgs
{
public bool CurrentState { get; set; }
}
}