Compare commits

...

3 Commits

Author SHA1 Message Date
933aac042f Add SensorStateException
Throws an exception if trying to send
a command while the sensor is sleeping
2020-11-30 08:14:43 +01:00
5af87ed327 Add readonly to fields 2020-11-30 08:13:11 +01:00
b2b275d638 Add AddFingerprint command 2020-11-30 07:54:16 +01:00
5 changed files with 76 additions and 5 deletions

View File

@@ -19,9 +19,9 @@ namespace WaveshareUARTFingerprintSensor.Sample
sensor.Start();
sensor.Waked += s => sensor.Wake();
Console.WriteLine("Adding fingerprint");
sensor.Sleep();
var response = sensor.AddFingerprint(40, UserPermission.Level3);
Console.WriteLine("End");

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
{
@@ -18,6 +19,7 @@ namespace WaveshareUARTFingerprintSensor
public const string PrimarySerialPort = "/dev/ttyAMA0";
public const string SecondarySerialPort = "/dev/ttyS0";
public const int DefaultTimeout = 10_000;
public const int MaxUserID = 0xFFF;
public event WakedEventHandler Waked;
public delegate void WakedEventHandler(FingerprintSensor sender);
@@ -27,11 +29,12 @@ namespace WaveshareUARTFingerprintSensor
private const byte PacketSeparator = 0xF5;
private SerialPort _serialPort;
private int _wakePinNumber;
private int _rstPinNumber;
private readonly int _wakePinNumber;
private readonly int _rstPinNumber;
private IGpioPin _wakePin;
private IGpioPin _rstPin;
private object _lock = new object();
private bool _sleeping = false;
private readonly object _lock = new object();
public FingerprintSensor(string portName, int wakePin = 23, int rstPin = 24)
{
@@ -74,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 };
@@ -169,14 +177,46 @@ namespace WaveshareUARTFingerprintSensor
public void Sleep()
{
_sleeping = true;
_rstPin.Write(GpioPinValue.Low);
}
public void Wake()
{
_sleeping = false;
_rstPin.Write(GpioPinValue.High);
}
public ResponseType AddFingerprint(ushort userID, UserPermission userPermission)
{
if (userID > MaxUserID)
{
return ResponseType.Full;
}
CommandType[] commands = { CommandType.AddFingerprint1, CommandType.AddFingerprint2, CommandType.AddFingerprint3 };
(byte idHigh, byte idLow) = Utils.Split(userID);
foreach (var command in commands)
{
if (TrySendAndReceive(command, idHigh, idLow, (byte)userPermission, out var response))
{
if (response.responseType != ResponseType.Success)
{
return response.responseType;
}
}
else
{
return ResponseType.Timeout;
}
Thread.Sleep(50);
}
return ResponseType.Success;
}
private void OnWake()
{
if (_wakePin.Read())

View File

@@ -0,0 +1,9 @@
namespace WaveshareUARTFingerprintSensor
{
public enum UserPermission
{
Level1 = 1,
Level2 = 2,
Level3 = 3
}
}

View File

@@ -65,6 +65,7 @@
<Compile Include="FingerprintSensor.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ResponseType.cs" />
<Compile Include="UserPermission.cs" />
<Compile Include="Utils.cs" />
</ItemGroup>
<ItemGroup>