-
-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy pathSps30Sensor.cs
157 lines (140 loc) · 6.12 KB
/
Sps30Sensor.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
// Copyright (c) 2017 The nanoFramework project contributors
// See LICENSE file in the project root for full license information.
using System.Buffers.Binary;
using System.Text;
using Iot.Device.Sps30.Entities;
using Iot.Device.Sps30.Shdlc;
namespace Iot.Device.Sps30
{
/// <summary>
/// Allows for interaction with the SPS30 particulate matter sensor. Uses the SHDLC protocol as specified by Sensirion.
/// </summary>
/// <remarks>
/// Datasheet can be found at https://sensirion.com/media/documents/8600FF88/616542B5/Sensirion_PM_Sensors_Datasheet_SPS30.pdf.
/// </remarks>
public class Sps30Sensor
{
private readonly ShdlcProtocol _shdlc;
/// <summary>
/// Initializes a new instance of the <see cref="Sps30Sensor" /> class.
/// </summary>
/// <param name="shdlc">An initialized <see cref="ShdlcProtocol"/> instance.</param>
public Sps30Sensor(ShdlcProtocol shdlc)
{
_shdlc = shdlc;
}
/// <summary>
/// Starts the measurement. After power up, the module is in Idle-Mode. Before any measurement values can be read, the Measurement-Mode needs to be started using this command.
/// </summary>
/// <param name="format">The format for the measurement, see <see cref="MeasurementOutputFormat"/>.</param>
public void StartMeasurement(MeasurementOutputFormat format)
{
_shdlc.Execute(0, 0x00, new byte[] { 0x01, (byte)format }, 20);
}
/// <summary>
/// Stops the measurement. Use this command to return to the initial state (Idle-Mode).
/// </summary>
public void StopMeasurement()
{
_shdlc.Execute(0, 0x01, new byte[0], 20);
}
/// <summary>
/// Reads the measured values from the module. This command can be used to poll for new measurement values. The measurement interval is 1 second.
/// </summary>
/// <returns>The parsed measurement, either Float or UInt16, depending on <see cref="StartMeasurement(MeasurementOutputFormat)"/>.</returns>
public Measurement ReadMeasuredValues()
{
var data = _shdlc.Execute(0, 0x03, new byte[0], 20);
if (data.Length == 0)
{
return null;
}
return new Measurement(data);
}
/// <summary>
/// Enters the Sleep-Mode with minimum power consumption. This will also deactivate the UART interface, note the wake-up sequence described at the Wake-up command.
/// </summary>
public void Sleep()
{
_shdlc.Execute(0, 0x10, new byte[0], 5);
}
/// <summary>
/// Use this command to switch from Sleep-Mode to Idle-Mode.
/// </summary>
public void WakeUp()
{
_shdlc.Serial.Write(new byte[] { 0xFF }, 0, 1); // Generate a low pulse to wake-up the interface
_shdlc.Execute(0, 0x11, new byte[0], 5); // Send the wake-up command within 100ms to fully wake the device
}
/// <summary>
/// Starts the fan-cleaning manually.
/// </summary>
public void StartFanCleaning()
{
_shdlc.Execute(0, 0x56, new byte[0], 20);
}
/// <summary>
/// Reads the interval [s] of the periodic fan-cleaning.
/// </summary>
/// <returns>The auto cleaning interval in seconds.</returns>
public uint GetAutoCleaningInterval()
{
var data = _shdlc.Execute(0, 0x80, new byte[] { 0x00 }, 20);
return BinaryPrimitives.ReadUInt32BigEndian(data);
}
/// <summary>
/// Writes the interval [s] of the periodic fan-cleaning.
/// </summary>
/// <param name="intervalInSeconds">The new interval in seconds.</param>
public void SetAutoCleaningInterval(uint intervalInSeconds)
{
var newvalue = new byte[4];
BinaryPrimitives.WriteUInt32BigEndian(newvalue, intervalInSeconds);
_shdlc.Execute(0, 0x80, new byte[] { 0x00, newvalue[0], newvalue[1], newvalue[2], newvalue[3] }, 20);
}
/// <summary>
/// This command returns product type with a maximum of 32 characters.
/// </summary>
/// <returns>The device product type as a string.</returns>
public string GetDeviceInfoProductType()
{
var data = _shdlc.Execute(0, 0xD0, new byte[] { 0x00 }, 20);
return Encoding.UTF8.GetString(data, 0, data.Length);
}
/// <summary>
/// This command returns serial number with a maximum of 32 characters.
/// </summary>
/// <returns>The device serial number as a string.</returns>
public string GetDeviceInfoSerialNumber()
{
var data = _shdlc.Execute(0, 0xD0, new byte[] { 0x03 }, 20);
return Encoding.UTF8.GetString(data, 0, data.Length);
}
/// <summary>
/// Gets version information about the firmware, hardware, and SHDLC protocol.
/// </summary>
/// <returns>The parsed version information.</returns>
public VersionInformation ReadVersion()
{
var data = _shdlc.Execute(0, 0xD1, new byte[0], 20);
return new VersionInformation(data);
}
/// <summary>
/// Use this command to read the Device Status Register.
/// </summary>
/// <param name="clearBitsAfterRead">True to clear any persistent error bits after reading the status.</param>
/// <returns>The parsed device status.</returns>
public DeviceStatus ReadDeviceStatusRegister(bool clearBitsAfterRead = false)
{
var data = _shdlc.Execute(0, 0xD2, new byte[] { (byte)(clearBitsAfterRead ? 0x01 : 0x0) }, 20);
return new DeviceStatus(data);
}
/// <summary>
/// Soft reset command. After calling this command, the module is in the same state as after a Power-Reset. The reset is executed after sending the MISO response frame.
/// </summary>
public void DeviceReset()
{
_shdlc.Execute(0, 0xD3, new byte[0], 20);
}
}
}