Add SensorStateException

Throws an exception if trying to send
a command while the sensor is sleeping
This commit is contained in:
2020-11-30 08:14:43 +01:00
parent 5af87ed327
commit 933aac042f
2 changed files with 30 additions and 0 deletions

View File

@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WaveshareUARTFingerprintSensor.Exceptions
{
[Serializable]
public class SensorStateException : Exception
{
public const string SensorSleepingMessage = "Sensor is sleeping, can't send commands";
public SensorStateException() { }
public SensorStateException(string message) : base(message) { }
public SensorStateException(string message, Exception inner) : base(message, inner) { }
protected SensorStateException(
System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext context) : base(info, context) { }
}
}

View File

@@ -10,6 +10,7 @@ using System.Threading.Tasks;
using Unosquare.RaspberryIO;
using Unosquare.RaspberryIO.Abstractions;
using Unosquare.WiringPi;
using WaveshareUARTFingerprintSensor.Exceptions;
namespace WaveshareUARTFingerprintSensor
{
@@ -32,6 +33,7 @@ namespace WaveshareUARTFingerprintSensor
private readonly int _rstPinNumber;
private IGpioPin _wakePin;
private IGpioPin _rstPin;
private bool _sleeping = false;
private readonly object _lock = new object();
public FingerprintSensor(string portName, int wakePin = 23, int rstPin = 24)
@@ -75,6 +77,11 @@ namespace WaveshareUARTFingerprintSensor
private (byte first, byte second, ResponseType responseType) SendAndReceive(CommandType commandType, byte first, byte second, byte third, int timeout = DefaultTimeout)
{
if (_sleeping)
{
throw new SensorStateException(SensorStateException.SensorSleepingMessage);
}
// Command packet
byte[] buffer = { PacketSeparator, (byte)commandType, first, second, third, 0, 0, PacketSeparator };
@@ -170,11 +177,13 @@ namespace WaveshareUARTFingerprintSensor
public void Sleep()
{
_sleeping = true;
_rstPin.Write(GpioPinValue.Low);
}
public void Wake()
{
_sleeping = false;
_rstPin.Write(GpioPinValue.High);
}