未验证 提交 74be13e0 编写于 作者: J James Coliz 提交者: GitHub

Refactor status handling in SHTC3 binding (#2038)

* Refactor status handling in SHTC3 binding

* Reduce to a single Status property
* Only check current status in WakeUp/Sleep methods, where it's needed
* Add an "Unknown" status enum value to describe the moments before we have explictly set the status.

* Update SHTC3 refactoring to use field, not property
上级 58c8b143
......@@ -35,17 +35,13 @@ namespace Iot.Device.Shtc3
{
_i2cDevice = i2cDevice ?? throw new ArgumentNullException(nameof(i2cDevice));
Wakeup();
Reset();
}
private Status _status;
/// <summary>
/// Set Shtc3 state
/// Current state of Shtc3 sensor
/// </summary>
internal Status Status => _status;
private Status _status = Status.Unknown;
private static Register GetMeasurementCmd(bool lowPower, bool clockStretching)
{
......@@ -122,10 +118,7 @@ namespace Iot.Device.Shtc3
/// <returns>True if operation was successful</returns>
public bool TryGetTemperatureAndHumidity(out Temperature temperature, out RelativeHumidity relativeHumidity, bool lowPower = false, bool clockStretching = false)
{
if (Status == Status.Sleep)
{
Wakeup();
}
Wakeup();
Register cmd = GetMeasurementCmd(lowPower, clockStretching);
......@@ -174,14 +167,12 @@ namespace Iot.Device.Shtc3
/// </summary>
public void Sleep()
{
if (Status == Status.Sleep)
if (_status != Status.Sleep)
{
return;
}
Write(Register.SHTC3_SLEEP);
Write(Register.SHTC3_SLEEP);
_status = Status.Sleep;
_status = Status.Sleep;
}
}
/// <summary>
......@@ -189,9 +180,12 @@ namespace Iot.Device.Shtc3
/// </summary>
private void Wakeup()
{
Write(Register.SHTC3_WAKEUP);
if (_status != Status.Idle)
{
Write(Register.SHTC3_WAKEUP);
_status = Status.Idle;
_status = Status.Idle;
}
}
/// <summary>
......@@ -199,10 +193,7 @@ namespace Iot.Device.Shtc3
/// </summary>
public void Reset()
{
if (Status == Status.Sleep)
{
Wakeup();
}
Wakeup();
Write(Register.SHTC3_RESET);
}
......@@ -217,10 +208,7 @@ namespace Iot.Device.Shtc3
/// </summary>
private int? ReadId()
{
if (Status == Status.Sleep)
{
Wakeup();
}
Wakeup();
Write(Register.SHTC3_ID);
......
......@@ -8,6 +8,11 @@ namespace Iot.Device.Shtc3
/// </summary>
internal enum Status
{
/// <summary>
/// Sensor in an unknown state
/// </summary>
Unknown = 0,
/// <summary>
/// Sensor ready to use
/// </summary>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册