/
Alspt19315C.cs
104 lines (96 loc) · 3.67 KB
/
Alspt19315C.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
using Meadow.Hardware;
using Meadow.Units;
using System;
using System.Threading.Tasks;
namespace Meadow.Foundation.Sensors.Light
{
/// <summary>
/// Represents an Alspt19315C analog light sensor
/// </summary>
public class Alspt19315C : SamplingSensorBase<Voltage>
{
/// <summary>
/// Analog port connected to the sensor
/// </summary>
private readonly IAnalogInputPort AnalogInputPort;
/// <summary>
/// The current voltage reading of the sensor
/// </summary>
public Voltage Voltage { get; protected set; }
/// <summary>
/// Create a new light sensor object using a static reference voltage
/// </summary>
/// <param name="pin">The analog pin</param>
/// <param name="sampleCount">The sample count</param>
/// <param name="sampleInterval">The sample interval</param>
/// <param name="voltage">The peak voltage</param>
public Alspt19315C(IPin pin, int sampleCount = 5, TimeSpan? sampleInterval = null, Voltage? voltage = null)
: this(pin.CreateAnalogInputPort(sampleCount, sampleInterval ?? TimeSpan.FromMilliseconds(40), voltage ?? new Voltage(3.3)))
{ }
/// <summary>
/// Create a new light sensor object using a static reference voltage
/// </summary>
/// <param name="port"></param>
public Alspt19315C(IAnalogInputPort port)
{
AnalogInputPort = port;
AnalogInputPort.Subscribe
(
IAnalogInputPort.CreateObserver(
result =>
{
ChangeResult<Voltage> changeResult = new ChangeResult<Voltage>()
{
New = result.New,
Old = Voltage
};
Voltage = changeResult.New;
RaiseEventsAndNotify(changeResult);
}
)
);
}
/// <summary>
/// Starts continuously sampling the sensor
///
/// This method also starts raising `Changed` events and IObservable
/// subscribers getting notified. Use the `readIntervalDuration` parameter
/// to specify how often events and notifications are raised/sent.
/// </summary>
/// <param name="updateInterval">A `TimeSpan` that specifies how long to
/// wait between readings. This value influences how often `*Updated`
/// events are raised and `IObservable` consumers are notified.
/// </param>
public override void StartUpdating(TimeSpan? updateInterval)
{
lock (samplingLock)
{
if (IsSampling) return;
IsSampling = true;
AnalogInputPort.StartUpdating(updateInterval);
}
}
/// <summary>
/// Stops sampling the sensor
/// </summary>
public override void StopUpdating()
{
lock (samplingLock)
{
if (!IsSampling) return;
base.IsSampling = false;
AnalogInputPort.StopUpdating();
}
}
/// <summary>
/// Convenience method to get the current temperature. For frequent reads, use
/// StartSampling() and StopSampling() in conjunction with the SampleBuffer.
/// </summary>
/// <returns>A float value that's an average value of all the samples taken.</returns>
protected override async Task<Voltage> ReadSensor()
{
Voltage = await AnalogInputPort.Read();
return Voltage;
}
}
}