Add SensorStateException
Throws an exception if trying to send a command while the sensor is sleeping
This commit is contained in:
@@ -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) { }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,6 +10,7 @@ using System.Threading.Tasks;
|
|||||||
using Unosquare.RaspberryIO;
|
using Unosquare.RaspberryIO;
|
||||||
using Unosquare.RaspberryIO.Abstractions;
|
using Unosquare.RaspberryIO.Abstractions;
|
||||||
using Unosquare.WiringPi;
|
using Unosquare.WiringPi;
|
||||||
|
using WaveshareUARTFingerprintSensor.Exceptions;
|
||||||
|
|
||||||
namespace WaveshareUARTFingerprintSensor
|
namespace WaveshareUARTFingerprintSensor
|
||||||
{
|
{
|
||||||
@@ -32,6 +33,7 @@ namespace WaveshareUARTFingerprintSensor
|
|||||||
private readonly int _rstPinNumber;
|
private readonly int _rstPinNumber;
|
||||||
private IGpioPin _wakePin;
|
private IGpioPin _wakePin;
|
||||||
private IGpioPin _rstPin;
|
private IGpioPin _rstPin;
|
||||||
|
private bool _sleeping = false;
|
||||||
private readonly object _lock = new object();
|
private readonly object _lock = new object();
|
||||||
|
|
||||||
public FingerprintSensor(string portName, int wakePin = 23, int rstPin = 24)
|
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)
|
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
|
// Command packet
|
||||||
byte[] buffer = { PacketSeparator, (byte)commandType, first, second, third, 0, 0, PacketSeparator };
|
byte[] buffer = { PacketSeparator, (byte)commandType, first, second, third, 0, 0, PacketSeparator };
|
||||||
|
|
||||||
@@ -170,11 +177,13 @@ namespace WaveshareUARTFingerprintSensor
|
|||||||
|
|
||||||
public void Sleep()
|
public void Sleep()
|
||||||
{
|
{
|
||||||
|
_sleeping = true;
|
||||||
_rstPin.Write(GpioPinValue.Low);
|
_rstPin.Write(GpioPinValue.Low);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Wake()
|
public void Wake()
|
||||||
{
|
{
|
||||||
|
_sleeping = false;
|
||||||
_rstPin.Write(GpioPinValue.High);
|
_rstPin.Write(GpioPinValue.High);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user