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 pathPotentiometer.cs
71 lines (62 loc) · 2.42 KB
/
Potentiometer.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
using System;
using Scarlet.Utilities;
using Scarlet.IO;
namespace Scarlet.Components.Sensors
{
/// <summary>
/// A class for converting an IAnalogueInput with a linear potentiometer connected into a rotation degrees value.
/// Can send events when the potentiometer has been turned.
/// Will only update the current angle when UpdateState is called, and therefore will only send events then as well.
/// </summary>
public class Potentiometer : ISensor
{
private IAnalogueIn Input;
public int Angle { get; private set; }
private readonly int Range;
private readonly bool Invert;
public event EventHandler<PotentiometerTurn> Turned;
public string System { get; set; }
public bool TraceLogging { get; set; }
public Potentiometer(IAnalogueIn Input, int Degrees, bool Invert = false)
{
this.Input = Input;
this.Range = Degrees;
this.Invert = Invert;
}
public bool Test()
{
return true; // TODO: What could we check for?
}
/// <summary>
/// Checks the IAnalogueInput for the new angle, and sends events if applicable.
/// Cannot determine the total amount turned since the last UpdateState call, only the net change.
/// As such, if the potentiometer is turned one direction, then back to the same place, we have no way of determining any movement occured.
/// </summary>
public void UpdateState()
{
int NewAngle = this.Range - (int)((((float)this.Input.GetRawInput() / (float)this.Input.GetRawRange()) * this.Range));
if (this.Invert) { NewAngle = this.Range - NewAngle; }
int AngleChange = this.Angle - NewAngle;
this.Angle = NewAngle;
if (AngleChange != 0)
{
PotentiometerTurn Event = new PotentiometerTurn() { TurnAmount = AngleChange, Angle = this.Angle };
OnTurn(Event);
}
}
protected virtual void OnTurn(PotentiometerTurn Event) { Turned?.Invoke(this, Event); }
public DataUnit GetData()
{
return new DataUnit("Potentiometer")
{
{ "Angle", this.Angle }
}
.SetSystem(this.System);
}
}
public class PotentiometerTurn : EventArgs
{
public int TurnAmount;
public int Angle;
}
}