Compare commits

...

47 Commits

Author SHA1 Message Date
89edff1624 Update README.md
Fix typo
2021-05-30 17:31:23 +02:00
5fabec9061 Update Sample README.md
Add asciinema
2021-01-27 22:35:04 +01:00
0f2f35bfae Add xml doc 2021-01-27 21:59:49 +01:00
cec004e095 Add missing using 2021-01-23 11:42:36 +01:00
3e1932f598 Revert "Add Views namespace"
This reverts commit 5446ec3739.
2021-01-23 11:41:48 +01:00
5446ec3739 Add Views namespace 2021-01-23 11:39:21 +01:00
2843fe93a7 Fix event propagation
Add Modal to avoid upward event propagation
2021-01-23 11:37:28 +01:00
7b7e83a7d2 Add Views namespace
Move viewst to this namespace
2021-01-23 11:37:27 +01:00
c42abc1537 Update 'WaveshareUARTFingerprintSensor.Sample/README.md' 2021-01-12 18:35:59 +01:00
48f7f59594 Update 'README.md' 2021-01-12 18:26:57 +01:00
c416358def Update README.md 2021-01-12 18:21:53 +01:00
b9cc6e0aac Add README.md 2021-01-12 18:17:41 +01:00
2a413fb9fb Prepare for nuget 2021-01-12 16:27:48 +01:00
e602fb26c7 Add Settings 2021-01-10 01:54:25 +01:00
747db9d7dd [Sample] Add missing titles 2021-01-10 01:21:29 +01:00
56a0eecdc9 [Sample] Add Sleep command 2021-01-10 00:51:55 +01:00
7c2a102bee Fix Wake 2021-01-10 00:50:59 +01:00
15e3e5a82a Fix TryComparison11 and TryComparison1N 2021-01-10 00:50:37 +01:00
a50a7ec97e [Sample] Implement commands 2021-01-06 19:24:44 +01:00
c5a990d1cb Use gui.cs 2021-01-05 21:17:47 +01:00
19b6225b25 Check amount of received data
Fix a bug
2020-12-14 22:00:38 +01:00
7455000aae Update Test 2020-12-13 11:40:13 +01:00
e05296ed4c Add TryAcquireUserEigenvalues command 2020-12-07 19:10:55 +01:00
7f34c2af57 Remove checksum skip 2020-12-07 19:07:48 +01:00
6c2a5e3bab Optimize using Span 2020-12-07 18:57:10 +01:00
93e7af380a Add TryAcquireEigenvalues command 2020-12-07 18:30:43 +01:00
97f3ac4513 Add TryAcquireImage command 2020-12-07 18:23:02 +01:00
70e96a5055 Update ReadData
Now use a buffer and check how much bytes has been read
Don't use a sleep anymore
2020-12-01 17:01:08 +01:00
4889aecc47 Add more doc comments 2020-12-01 16:19:40 +01:00
23c6036924 Add doc comments 2020-12-01 16:17:25 +01:00
5a061d7600 Fix TryQueryPermission 2020-12-01 16:13:16 +01:00
e295afd899 Fix TryComparison1N
Should sleep more
2020-12-01 16:07:21 +01:00
6f5ff3ce00 Fix ReadData
Stupid serial
2020-12-01 15:53:13 +01:00
80e67f3afc Add ArrayDisplay Utils method 2020-12-01 15:33:43 +01:00
7b5716b8f3 Fix ComputeChecksumData 2020-12-01 15:33:08 +01:00
f93a821a2b Add TryGetComparisonLevel and TrySetComparisonLevel commands 2020-11-30 09:45:30 +01:00
76163715bb Add TryQueryPermission command 2020-11-30 09:39:50 +01:00
713a3a2188 Clean code 2020-11-30 09:36:46 +01:00
7fa2159d05 Add Comparison11 and TryComparison1N commands 2020-11-30 09:31:17 +01:00
bf8ce006e4 Add DeleteAllUsers command 2020-11-30 09:05:16 +01:00
da763d5912 Add DeleteUser command 2020-11-30 09:01:38 +01:00
4287e00665 Add AddFingerprintAndAcquireEigenvalues command 2020-11-30 09:01:33 +01:00
ca04700291 Add ReadData 2020-11-30 08:45:05 +01:00
6ea11cb368 Add Include in .csproj 2020-11-30 08:20:51 +01:00
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
22 changed files with 1562 additions and 24 deletions

View File

@@ -0,0 +1,69 @@
# Waveshare UART fingerprint sensor (C)
A C# library for the [**Waveshare UART fingerprint sensor (C)**][Sensor], running on
.Net Framework 4.7 (**Mono**) on a **Raspberry Pi**
This library is tested using a Raspberry Pi Zero (hence the use of Mono)
but should work on any Raspberry.
It should also be easily portable to any device that supports Mono
or an equivalent (*.Net Core*).
## Usage
- First install it from [**nuget**](https://www.nuget.org/packages/WaveshareUARTFingerprintSensor/)
Then you only need to start the sensor
```csharp
// PrimarySerialPort refers to /dev/ttyAMA0
// SecondarySerialPort refers to /dev/ttyS0
// You need to choose it according to your Raspberry
// Check the table below
var sensor = new FingerprintSensor(FingerprintSensor.PrimarySerialPort);
sensor.Start();
// Do any command
// Example: get the user count
if (sensor.TryGetUserCount(out ushort count))
{
Console.WriteLine($"User count: {count}");
}
```
Here is a table of which serial port to use on which Raspberry Pi,
it may be different for you
| Model | Port |
| --------- | ---------------------- |
| Pi Zero | Primary (/dev/ttyAMA0) |
| Pi Zero W | Secondary (/dev/ttyS0) |
| Pi 1 | Primary (/dev/ttyAMA0) |
| Pi 2 | Primary (/dev/ttyAMA0) |
| Pi 3 | Secondary (/dev/ttyS0) |
| Pi 4 | Secondary (/dev/ttyS0) |
> The Secondary UART is **disabled by default**, you an activate it in `raspi-config`
> [**Source**](https://www.raspberrypi.org/documentation/configuration/uart.md)
## Sample App
You can find a [**sample app**](WaveshareUARTFingerprintSensor.Sample) which shows basic usages of
this library and may help you to understand how to use it
## Contributing
If you have a feature idea or want to report a bug, don't hesitate to create a new
[**Issue**](https://github.com/Eveldee/WaveshareUARTFingerprintSensor/issues) or do a
[**Pull Request**](https://github.com/Eveldee/WaveshareUARTFingerprintSensor/pulls)
## Copyright and license
*[**WaveshareUARTFingerprintSensor**](README.md)* library is licensed under the [MIT License](LICENSE).
*[**Unosquare.Raspberry.IO**](https://github.com/migueldeicaza/gui.cs/)* library is under the [MIT License](https://github.com/unosquare/raspberryio/blob/master/LICENSE).
<!-- Links -->
[Sensor]: https://www.waveshare.com/wiki/UART_Fingerprint_Sensor_(C)

BIN
Sample.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 43 KiB

View File

@@ -5,27 +5,19 @@ using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Terminal.Gui;
using Unosquare.RaspberryIO;
using Unosquare.RaspberryIO.Abstractions;
using Unosquare.WiringPi;
using WaveshareUARTFingerprintSensor.Sample.Views;
namespace WaveshareUARTFingerprintSensor.Sample
{
class Program
{
static void Main(string[] args)
{
var sensor = new FingerprintSensor(FingerprintSensor.PrimarySerialPort);
sensor.Start();
sensor.Waked += s => sensor.Wake();
sensor.Sleep();
Console.WriteLine("End");
Thread.Sleep(-1);
Application.Run<TUIManager>();
}
}
}

View File

@@ -0,0 +1,24 @@
# Sample App
A sample app for the [Waveshare UART fingerprint sensor (C) library](../README.md)
![Sample](../Sample.png)
[![asciicast](https://asciinema.org/a/U1cBQzJ3ueFyN0urkh2NRcjB9.svg)](https://asciinema.org/a/U1cBQzJ3ueFyN0urkh2NRcjB9)
## Build
You can compile it using any **C# IDE** that supports **.Net Framework** or by running
`dotnet build` in a **terminal** using [**.Net CLI**](https://docs.microsoft.com/en-us/dotnet/core/tools/)
## Usage
- Install [**Mono**](https://www.mono-project.com/) on your Raspberry Pi
- [**Download**](https://github.com/Eveldee/WaveshareUARTFingerprintSensor/releases) or [**Build**](#build) the application
- Run it with `mono WaveshareUARTFingerprintSensor.Sample.exe`
## Copyright and license
[**WaveshareUARTFingerprintSensor**](../README.md) library is licensed under the [MIT License](../LICENSE).
[**gui.cs**](https://github.com/migueldeicaza/gui.cs/) library is under the [MIT License](https://github.com/migueldeicaza/gui.cs/blob/master/LICENSE).

View File

@@ -0,0 +1,65 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Terminal.Gui;
namespace WaveshareUARTFingerprintSensor.Sample.Views
{
public class DataDisplay : Toplevel
{
private string _title;
private byte[] _data;
public DataDisplay(string title, byte[] data)
{
_title = title;
_data = data;
Init();
}
private void Init()
{
Modal = true;
ColorScheme = Colors.TopLevel;
// Creates the top-level window to show
var win = new Window(_title)
{
X = 0,
Y = 0,
// By using Dim.Fill(), it will automatically resize without manual intervention
Width = Dim.Fill(),
Height = Dim.Fill()
};
win.ColorScheme = Colors.ColorSchemes["Dialog"];
Add(win);
var quitButton = new Button("_Ok")
{
X = Pos.Right(this) - 9,
Y = Pos.Bottom(this) - 2
};
quitButton.Clicked += () => Application.RequestStop();
var stream = new MemoryStream(_data);
var text = new HexView(stream)
{
X = Pos.Center(),
Y = Pos.Center(),
Height = Dim.Fill() - 5,
Width = Dim.Fill() - 2,
AllowEdits = false
};
Add(text, quitButton);
}
}
}

View File

@@ -0,0 +1,87 @@
using NStack;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Terminal.Gui;
namespace WaveshareUARTFingerprintSensor.Sample.Views
{
public class EntryDialog : Dialog
{
private Func<ustring, bool> _validator;
private bool _success;
private string _title;
private string _errorMessage;
public EntryDialog(string title, Func<ustring, bool> validator = null, string errorMessage = "") : base(title, 60, 7)
{
_title = title;
_errorMessage = errorMessage;
_validator = validator;
ColorScheme = Colors.ColorSchemes["Menu"];
}
public bool TryShow(out ustring input)
{
_success = false;
var levelEntry = new TextField("")
{
X = 1,
Y = 2,
Width = Dim.Fill(),
Height = 1
};
var cancelButton = new Button("_Cancel")
{
X = Pos.Percent(82),
Y = Pos.Percent(95)
};
cancelButton.Clicked += () => Application.RequestStop();
var okButton = new Button("_Ok")
{
X = Pos.Percent(70),
Y = Pos.Percent(95)
};
okButton.Clicked += () => CheckInput(levelEntry.Text);
Add(levelEntry, okButton, cancelButton);
levelEntry.SetFocus();
Application.Run(this);
if (_success)
{
input = levelEntry.Text;
return true;
}
input = default;
return false;
}
private void CheckInput(ustring input)
{
if (_validator?.Invoke(input) ?? true)
{
_success = true;
Application.RequestStop();
}
else
{
MessageBox.ErrorQuery(_title, _errorMessage, "Ok");
}
}
}
}

View File

@@ -0,0 +1,53 @@
using NStack;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Terminal.Gui;
namespace WaveshareUARTFingerprintSensor.Sample.Views
{
public class FingerprintDialog : Dialog
{
public string ErrorTitle { get; set; }
public string ErrorMessage { get; set; }
public FingerprintDialog(string errorTitle, string errorMessage) : base(errorTitle, 60, 7)
{
ErrorTitle = errorTitle;
ErrorMessage = errorMessage;
ColorScheme = Colors.ColorSchemes["Menu"];
}
public void Show()
{
var label = new Label("Please place your finger flat on the sensor")
{
X = Pos.Center(),
Y = Pos.Center(),
Height = 1
};
Add(label);
Application.Run(this);
}
public void Cancel()
{
Application.MainLoop.Invoke(() => Application.RequestStop());
}
public void CancelAndShowError()
{
Application.MainLoop.Invoke(() =>
{
MessageBox.ErrorQuery(ErrorTitle, ErrorMessage, "Ok");
Application.RequestStop();
});
}
}
}

View File

@@ -0,0 +1,73 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Terminal.Gui;
namespace WaveshareUARTFingerprintSensor.Sample.Views
{
public class SettingsDisplay : Toplevel
{
private string _port;
private RadioGroup _radioPort;
public SettingsDisplay()
{
Init();
}
private void Init()
{
Modal = true;
ColorScheme = Colors.Error;
// Creates the top-level window to show
var win = new Window("Settings")
{
X = 0,
Y = 0,
// By using Dim.Fill(), it will automatically resize without manual intervention
Width = Dim.Fill(),
Height = Dim.Fill()
};
win.ColorScheme = Colors.ColorSchemes["Dialog"];
Add(win);
// Window Content
var portLabel = new Label("Serial Port:")
{
X = 4,
Y = 3
};
_radioPort = new RadioGroup(new NStack.ustring[] { FingerprintSensor.PrimarySerialPort, FingerprintSensor.SecondarySerialPort })
{
X = Pos.Right(portLabel) + 2,
Y = Pos.Top(portLabel),
Width = Dim.Fill()
};
var saveButton = new Button("_Save")
{
X = Pos.Right(this) - 14,
Y = Pos.Bottom(this) - 4
};
saveButton.Clicked += () => { Save(); Application.RequestStop(); };
win.Add(portLabel, _radioPort, saveButton);
}
private void Save()
{
_port = _radioPort.RadioLabels[_radioPort.SelectedItem].ToString();
File.WriteAllText(TUIManager.SettingsFilePath, _port);
}
}
}

View File

@@ -0,0 +1,108 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Terminal.Gui;
namespace WaveshareUARTFingerprintSensor.Sample.Views
{
public class SleepDisplay : Toplevel
{
private FingerprintSensor _fingerprintSensor;
private int _count;
private int _lastID;
private Label _sleepModeLabel;
private Label _readCountLabel;
private Label _lastReadLabel;
public SleepDisplay(FingerprintSensor fingerprintSensor)
{
_fingerprintSensor = fingerprintSensor;
_count = 0;
_lastID = -1;
Init();
}
private void Init()
{
Modal = true;
ColorScheme = Colors.Error;
// Creates the top-level window to show
var win = new Window("Sleep")
{
X = 0,
Y = 0,
// By using Dim.Fill(), it will automatically resize without manual intervention
Width = Dim.Fill(),
Height = Dim.Fill()
};
win.ColorScheme = Colors.ColorSchemes["Dialog"];
Add(win);
// Window Content
_sleepModeLabel = new Label("Sleep mode is on, waiting for fingerprints...")
{
X = 2,
Y = 1,
Width = Dim.Fill()
};
_readCountLabel = new Label("Read: 0")
{
X = Pos.Left(_sleepModeLabel),
Y = Pos.Bottom(_sleepModeLabel) + 1,
Width = Dim.Fill()
};
_lastReadLabel = new Label("Last User: - 1")
{
X = Pos.Left(_readCountLabel),
Y = Pos.Bottom(_readCountLabel),
Width = Dim.Fill()
};
var stopButton = new Button("_Stop")
{
X = Pos.Right(this) - 11,
Y = Pos.Bottom(this) - 2
};
stopButton.Clicked += () => { _fingerprintSensor.Waked -= FingerprintSensor_Waked; Application.RequestStop(); };
win.Add(_sleepModeLabel, _readCountLabel, _lastReadLabel);
Add(stopButton);
_fingerprintSensor.Waked += FingerprintSensor_Waked;
_fingerprintSensor.Sleep();
}
private void UpdateInfo()
{
_readCountLabel.Text = $"Read: {_count}";
_lastReadLabel.Text = $"Last User: {_lastID}";
}
private void FingerprintSensor_Waked(FingerprintSensor sender)
{
_fingerprintSensor.Wake();
if (_fingerprintSensor.TryComparison1N(out var userInfo))
{
_count += 1;
_lastID = userInfo.userID;
Application.MainLoop.Invoke(UpdateInfo);
}
_fingerprintSensor.Sleep();
}
}
}

View File

@@ -0,0 +1,416 @@
using NStack;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Terminal.Gui;
namespace WaveshareUARTFingerprintSensor.Sample.Views
{
public class TUIManager : Toplevel
{
public const string OutputFilePath = "out.txt";
public const string SettingsFilePath = "settings.txt";
private FingerprintSensor _fingerprintSensor;
private Label _serialPortLabel;
private Label _comparisonLevelLabel;
private Label _userCountLabel;
private object _outputFileLock = new object();
public TUIManager()
{
Init();
}
private void Init()
{
ColorScheme = Colors.Error;
// Creates the top-level window to show
var win = new Window("TUIManager")
{
X = 0,
Y = 1, // Leave one row for the toplevel menu
// By using Dim.Fill(), it will automatically resize without manual intervention
Width = Dim.Fill(),
Height = Dim.Fill()
};
win.ColorScheme = Colors.ColorSchemes["Dialog"];
Add(win);
// Creates a menubar, the item "New" has a help menu.
var menu = new MenuBar(new MenuBarItem[] {
new MenuBarItem ("_Options", new MenuItem [] {
new MenuItem ("_Change Config", "", () => { ChangeConfig(); }),
new MenuItem ("_Quit", "", () => { Application.RequestStop(); })
})
});
Add(menu);
// Window Content
_serialPortLabel = new Label("Serial Port:")
{
X = 2,
Y = 1,
Width = Dim.Fill()
};
_comparisonLevelLabel = new Label("Comparison Level: 0")
{
X = Pos.Left(_serialPortLabel),
Y = Pos.Bottom(_serialPortLabel),
Width = Dim.Fill()
};
_userCountLabel = new Label("Users: 0")
{
X = Pos.Left(_comparisonLevelLabel),
Y = Pos.Bottom(_comparisonLevelLabel),
Width = Dim.Fill()
};
var userCountButton = new Button("Query _User Count")
{
X = Pos.Center(),
Y = 7
};
userCountButton.Clicked += UserCountButton_Clicked;
var readFingerprintButton = new Button("_Read Fingerprint")
{
X = Pos.Center(),
Y = Pos.Bottom(userCountButton) + 1
};
readFingerprintButton.Clicked += ReadFingerprintButton_Clicked;
var readEigenvaluesButton = new Button("Read _Eigenvalues")
{
X = Pos.Center(),
Y = Pos.Bottom(readFingerprintButton)
};
readEigenvaluesButton.Clicked += ReadEigenvaluesButton_Clicked;
var readImageButton = new Button("Read _Image")
{
X = Pos.Center(),
Y = Pos.Bottom(readEigenvaluesButton)
};
readImageButton.Clicked += ReadImageButton_Clicked;
var addFingerprintButton = new Button("_Add Fingerprint")
{
X = Pos.Center(),
Y = Pos.Bottom(readImageButton) + 1
};
addFingerprintButton.Clicked += AddFingerprintButton_Clicked;
var deleteAFingerprint = new Button("_Delete a Fingerprint")
{
X = Pos.Center(),
Y = Pos.Bottom(addFingerprintButton)
};
deleteAFingerprint.Clicked += DeleteAFingerprint_Clicked;
var clearFingerprintsButton = new Button("_Clear Fingerprints")
{
X = Pos.Center(),
Y = Pos.Bottom(deleteAFingerprint)
};
clearFingerprintsButton.Clicked += ClearFingerprintsButton_Clicked;
var setComparisonLevelButton = new Button("Set Comparison _Level")
{
X = Pos.Center(),
Y = Pos.Bottom(clearFingerprintsButton) + 1
};
setComparisonLevelButton.Clicked += SetComparisonLevelButton_Clicked;
var sleepButton = new Button("_Sleep Mode")
{
X = Pos.Center(),
Y = Pos.Bottom(setComparisonLevelButton) + 1
};
sleepButton.Clicked += SleepButton_Clicked;
var quitButton = new Button("_Quit")
{
X = Pos.Center(),
Y = Pos.Percent(95)
};
quitButton.Clicked += Quit_Clicked;
win.Add(
_serialPortLabel,
_comparisonLevelLabel,
_userCountLabel,
userCountButton,
readFingerprintButton,
readEigenvaluesButton,
readImageButton,
addFingerprintButton,
deleteAFingerprint,
clearFingerprintsButton,
setComparisonLevelButton,
sleepButton,
quitButton
);
// Init Sensor
if (!File.Exists(SettingsFilePath))
{
Application.Run(new SettingsDisplay());
}
InitSensor();
// Update gui
UpdateSerialPort();
UpdateUserCount();
UpdateComparisonLevel();
}
private void UpdateSerialPort()
{
_serialPortLabel.Text = $"Serial Port: {_fingerprintSensor.PortName}";
}
private void InitSensor()
{
_fingerprintSensor = new FingerprintSensor(File.ReadAllText(SettingsFilePath));
_fingerprintSensor.Start();
}
private void UpdateUserCount()
{
if (_fingerprintSensor.TryGetUserCount(out ushort count))
{
_userCountLabel.Text = $"Users: {count}";
}
}
private void UpdateComparisonLevel()
{
if (_fingerprintSensor.TryGetComparisonLevel(out byte comparisonLevel))
{
_comparisonLevelLabel.Text = $"Comparison Level: {comparisonLevel}";
}
}
private void ReadImageButton_Clicked()
{
var dialog = new FingerprintDialog("Acquire Image", "Can't acquire image, try to place your finger flat on the sensor");
byte[] image = null;
Task.Run(() =>
{
if (_fingerprintSensor.TryAcquireImage(out image))
{
dialog.Cancel();
}
else
{
dialog.CancelAndShowError();
}
});
dialog.Show();
if (image != null)
{
WriteOut($"Image:\n{Utils.ArrayDisplay(image)}\n\n\n");
var window = new DataDisplay("Image", image.ToArray());
Application.Run(window);
}
}
private void ReadEigenvaluesButton_Clicked()
{
var dialog = new FingerprintDialog("Acquire Eigenvalues", "Can't acquire eigenvalues, try to place your finger flat on the sensor");
byte[] eigenvalues = null;
Task.Run(() =>
{
if (_fingerprintSensor.TryAcquireEigenvalues(out var values))
{
eigenvalues = values.ToArray();
dialog.Cancel();
}
else
{
dialog.CancelAndShowError();
}
});
dialog.Show();
if (eigenvalues != null)
{
var window = new DataDisplay("Eigenvalues", eigenvalues);
WriteOut($"Eigenvalues:\n{Utils.ArrayDisplay(eigenvalues)}\n\n\n");
Application.Run(window);
}
}
private void SetComparisonLevelButton_Clicked()
{
if (new EntryDialog("Comparison Level", i => int.TryParse(i.ToString(), out var n) && n > 0 && n < 10, "Need to be a valid number in 0-9 range").TryShow(out var input))
{
if (!_fingerprintSensor.TrySetComparisonLevel(byte.Parse(input.ToString())))
{
MessageBox.ErrorQuery("Comparison Level", "Can't set comparison level", "Ok");
}
}
UpdateComparisonLevel();
}
private void DeleteAFingerprint_Clicked()
{
if (new EntryDialog("User id", i => int.TryParse(i.ToString(), out var n), "Need to be a valid user id").TryShow(out var input))
{
if (!_fingerprintSensor.DeleteUser(ushort.Parse(input.ToString())))
{
MessageBox.ErrorQuery("Delete User", "Can't delete user, check user id", "Ok");
}
}
UpdateUserCount();
}
private void SleepButton_Clicked()
{
var window = new SleepDisplay(_fingerprintSensor);
Application.Run(window);
}
private void ClearFingerprintsButton_Clicked()
{
if (!_fingerprintSensor.DeleteAllUsers())
{
MessageBox.ErrorQuery("Delete All Users", "Can't delete all user", "Ok");
}
}
private void AddFingerprintButton_Clicked()
{
if (new EntryDialog("User id", i => ushort.TryParse(i.ToString(), out var n), "Need to be a valid user id").TryShow(out var input))
{
var dialog = new FingerprintDialog("Add Fingerprint", "Can't add fingerprint, try to place your finger flat on the sensor");
Task.Run(() =>
{
switch (_fingerprintSensor.AddFingerprint(ushort.Parse(input.ToString()), UserPermission.Level1))
{
case ResponseType.Success:
dialog.Cancel();
Application.MainLoop.Invoke(() => MessageBox.Query("Add Fingerprint", "Successfully added fingerprint", "Ok"));
break;
case ResponseType.Full:
dialog.ErrorMessage = "Sensor full, can't add more users";
dialog.CancelAndShowError();
break;
case ResponseType.NoUser:
dialog.ErrorMessage = "Can't add fingerprint, invalid id";
dialog.CancelAndShowError();
break;
case ResponseType.FingerOccupied:
dialog.ErrorMessage = "Can't add fingerprint, finger already registered";
dialog.CancelAndShowError();
break;
case ResponseType.UserOccupied:
dialog.ErrorMessage = "Can't add fingerprint, id already used";
dialog.CancelAndShowError();
break;
default:
dialog.CancelAndShowError();
break;
}
});
dialog.Show();
UpdateUserCount();
}
}
private void ReadFingerprintButton_Clicked()
{
var dialog = new FingerprintDialog("Read Fingerprint", "Can't read fingerprint, try to place your finger flat on the sensor");
(ushort userID, UserPermission permission) userInfo = default;
Task.Run(() =>
{
if (_fingerprintSensor.TryComparison1N(out userInfo))
{
dialog.Cancel();
}
else
{
dialog.CancelAndShowError();
}
});
dialog.Show();
if (userInfo != default)
{
MessageBox.Query("Read Fingerprint", $"Successfully read fingerprint\n\nUser ID: {userInfo.userID}\nPermissions: {userInfo.permission}\n ", "Ok");
}
}
private void UserCountButton_Clicked()
{
UpdateUserCount();
}
private void WriteOut(string text)
{
Task.Run(() =>
{
lock (_outputFileLock)
{
File.AppendAllText(OutputFilePath, text);
}
});
}
private void ChangeConfig()
{
File.Delete(SettingsFilePath);
Application.Run(new SettingsDisplay());
InitSensor();
UpdateSerialPort();
}
private void Quit_Clicked()
{
Application.RequestStop();
}
}
}

View File

@@ -27,6 +27,8 @@
<IsWebBootstrapper>false</IsWebBootstrapper>
<UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
@@ -48,6 +50,9 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="NStack, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\NStack.Core.0.14.0\lib\netstandard2.0\NStack.dll</HintPath>
</Reference>
<Reference Include="Swan, Version=3.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Unosquare.Swan.3.0.0\lib\net461\Swan.dll</HintPath>
</Reference>
@@ -55,8 +60,21 @@
<HintPath>..\packages\Unosquare.Swan.Lite.3.0.0\lib\net461\Swan.Lite.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Buffers, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll</HintPath>
</Reference>
<Reference Include="System.Core" />
<Reference Include="System.Drawing" />
<Reference Include="System.Memory, Version=4.0.1.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Memory.4.5.4\lib\net461\System.Memory.dll</HintPath>
</Reference>
<Reference Include="System.Numerics" />
<Reference Include="System.Numerics.Vectors, Version=4.1.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll</HintPath>
</Reference>
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.4.5.3\lib\net461\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
</Reference>
<Reference Include="System.ServiceProcess" />
<Reference Include="System.ValueTuple, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.ValueTuple.4.5.0\lib\net47\System.ValueTuple.dll</HintPath>
@@ -67,6 +85,9 @@
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
<Reference Include="Terminal.Gui, Version=0.90.3.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Terminal.Gui.0.90.3\lib\net472\Terminal.Gui.dll</HintPath>
</Reference>
<Reference Include="Unosquare.Raspberry.Abstractions, Version=0.4.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Unosquare.Raspberry.Abstractions.0.4.1\lib\netstandard2.0\Unosquare.Raspberry.Abstractions.dll</HintPath>
</Reference>
@@ -78,8 +99,14 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Views\DataDisplay.cs" />
<Compile Include="Views\EntryDialog.cs" />
<Compile Include="Views\FingerprintDialog.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Views\SettingsDisplay.cs" />
<Compile Include="Views\SleepDisplay.cs" />
<Compile Include="Views\TUIManager.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
@@ -104,4 +131,11 @@
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\packages\Microsoft.NETFramework.ReferenceAssemblies.net472.1.0.0\build\Microsoft.NETFramework.ReferenceAssemblies.net472.targets" Condition="Exists('..\packages\Microsoft.NETFramework.ReferenceAssemblies.net472.1.0.0\build\Microsoft.NETFramework.ReferenceAssemblies.net472.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>Ce projet fait référence à des packages NuGet qui sont manquants sur cet ordinateur. Utilisez l'option de restauration des packages NuGet pour les télécharger. Pour plus d'informations, consultez http://go.microsoft.com/fwlink/?LinkID=322105. Le fichier manquant est : {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\Microsoft.NETFramework.ReferenceAssemblies.net472.1.0.0\build\Microsoft.NETFramework.ReferenceAssemblies.net472.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.NETFramework.ReferenceAssemblies.net472.1.0.0\build\Microsoft.NETFramework.ReferenceAssemblies.net472.targets'))" />
</Target>
</Project>

View File

@@ -1,6 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.NETFramework.ReferenceAssemblies" version="1.0.0" targetFramework="net472" developmentDependency="true" />
<package id="Microsoft.NETFramework.ReferenceAssemblies.net472" version="1.0.0" targetFramework="net472" developmentDependency="true" />
<package id="NStack.Core" version="0.14.0" targetFramework="net472" />
<package id="System.Buffers" version="4.5.1" targetFramework="net472" />
<package id="System.Memory" version="4.5.4" targetFramework="net472" />
<package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net472" />
<package id="System.Runtime.CompilerServices.Unsafe" version="4.5.3" targetFramework="net472" />
<package id="System.ValueTuple" version="4.5.0" targetFramework="net472" />
<package id="Terminal.Gui" version="0.90.3" targetFramework="net472" />
<package id="Unosquare.Raspberry.Abstractions" version="0.4.1" targetFramework="net472" />
<package id="Unosquare.Raspberry.IO" version="0.27.1" targetFramework="net472" />
<package id="Unosquare.Swan" version="3.0.0" targetFramework="net472" />

View File

@@ -6,6 +6,9 @@ using System.Threading.Tasks;
namespace WaveshareUARTFingerprintSensor
{
/// <summary>
/// Command flags recognized by the sensor
/// </summary>
public enum CommandType : byte
{
ModifySerialNumber = 0x08,

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,29 +10,67 @@ using System.Threading.Tasks;
using Unosquare.RaspberryIO;
using Unosquare.RaspberryIO.Abstractions;
using Unosquare.WiringPi;
using WaveshareUARTFingerprintSensor.Exceptions;
namespace WaveshareUARTFingerprintSensor
{
public class FingerprintSensor : IDisposable
{
public const string PrimarySerialPort = "/dev/ttyAMA0";
/// <summary>
/// Primary serial port on the RPI. <see href="https://www.raspberrypi.org/documentation/configuration/uart.md">Source</see>
/// </summary>
public const string PrimarySerialPort = "/dev/ttyAMA0";
/// <summary>
/// Secondary serial port on the RPI. <see href="https://www.raspberrypi.org/documentation/configuration/uart.md">Source</see>
/// </summary>
public const string SecondarySerialPort = "/dev/ttyS0";
/// <summary>
/// Timeout used for the <see cref="SerialPort"/> I/O operations
/// </summary>
public const int DefaultTimeout = 10_000;
/// <summary>
/// Maximum number of user that the sensor can store before throwing <see cref="ResponseType.Full"/>. It is also the ID of the last user
/// </summary>
public const int MaxUserID = 0xFFF;
/// <summary>
/// Internal buffer size
/// </summary>
public const int DataBufferSize = 4095;
/// <summary>
/// Thrown when "WAKE" pin is high, works like a press button. Can be used while the sensor is asleep
/// </summary>
public event WakedEventHandler Waked;
/// <summary>
/// Delegate for the <see cref="Waked"/> event
/// </summary>
/// <param name="sender"></param>
public delegate void WakedEventHandler(FingerprintSensor sender);
/// <summary>
/// <see cref="SerialPort"/> name/path used
/// </summary>
public string PortName { get; }
/// <summary>
/// Mark the end of a command packet, usually the 8th byte
/// </summary>
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();
/// <summary>
/// Create a new instance of <see cref="FingerprintSensor"/>, don't forget to call <see cref="Start"/> before using any command
/// </summary>
/// <param name="portName">A valid name/path to a serial port, see <see cref="PrimarySerialPort"/> and <see cref="SecondarySerialPort"/> for the RPI</param>
/// <param name="wakePin">WAKE <see cref="GpioPin"/> number, default is according to the sensor documentation wiring</param>
/// <param name="rstPin">RST <see cref="GpioPin"/> number, default is according to the sensor documentation wiring</param>
public FingerprintSensor(string portName, int wakePin = 23, int rstPin = 24)
{
PortName = portName;
@@ -40,6 +78,9 @@ namespace WaveshareUARTFingerprintSensor
_rstPinNumber = rstPin;
}
/// <summary>
/// Start the sensor, initializing <see cref="BootstrapWiringPi"/>, the Gpio pins and the <see cref="SerialPort"/>
/// </summary>
public void Start()
{
// Initialize Gpio
@@ -60,6 +101,11 @@ namespace WaveshareUARTFingerprintSensor
_serialPort.Open();
}
/// <summary>
/// Compute the checksum for a command packet: XOR of <see cref="byte"/> 1 to 5 (counting from 0)
/// </summary>
/// <param name="data">A command packet, usually 8 bytes</param>
/// <returns>The computed checksum, usually stored in the 6th <see cref="byte"/></returns>
private byte ComputeChecksum(byte[] data)
{
byte checksum = 0;
@@ -72,8 +118,40 @@ namespace WaveshareUARTFingerprintSensor
return checksum;
}
/// <summary>
/// Compute the checksum for a data packet: XOR of <see cref="byte"/> 1 to (last - 2) (counting from 0)
/// </summary>
/// <param name="data">A data, not to be confused with a command packet</param>
/// <param name="length">Length of the data packet, should be 2 bytes less than data.Length</param>
/// <returns>The computed checksum, usually stored in the (last - 2)th byte</returns>
private byte ComputeChecksumData(byte[] data, int length)
{
byte checksum = 0;
for (int i = 1; i < length; i++)
{
checksum ^= data[i];
}
return checksum;
}
/// <summary>
/// Send an 8 <see cref="byte"/> command and read the response, can throw an <see cref="Exception"/>
/// </summary>
/// <param name="commandType">Command flag, see <see cref="CommandType"/></param>
/// <param name="first">First data <see cref="byte"/>, the 2th <see cref="byte"/> (counting from 0)</param>
/// <param name="second">Second data <see cref="byte"/>, the 3th <see cref="byte"/> (counting from 0)</param>
/// <param name="third">Third data <see cref="byte"/>, the 4th <see cref="byte"/> (counting from 0)</param>
/// <param name="timeout">Timeout used for the <see cref="SerialPort"/>, default to <see cref="DefaultTimeout"/></param>
/// <returns>The 3 data <see cref="byte"/> from the response (2, 3 and 4), the third one is parsed as a <see cref="ResponseType"/></returns>
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 };
@@ -89,7 +167,13 @@ namespace WaveshareUARTFingerprintSensor
_serialPort.Write(buffer, 0, buffer.Length);
// Response
_serialPort.Read(buffer, 0, buffer.Length);
int length = buffer.Length;
int offset = 0;
do
{
int toRead = length - offset;
offset += _serialPort.Read(buffer, offset, toRead);
} while (offset < length);
if (buffer[0] != PacketSeparator || buffer[7] != PacketSeparator || buffer[1] != (byte)commandType)
{
@@ -105,6 +189,15 @@ namespace WaveshareUARTFingerprintSensor
return (buffer[2], buffer[3], (ResponseType)buffer[4]);
}
/// <summary>
/// Send an 8 <see cref="byte"/> command and read the response, can throw an <see cref="Exception"/>
/// </summary>
/// <param name="commandType">Command flag, see <see cref="CommandType"/></param>
/// <param name="first">First data <see cref="byte"/>, the 2th <see cref="byte"/> (counting from 0)</param>
/// <param name="second">Second data <see cref="byte"/>, the 3th <see cref="byte"/> (counting from 0)</param>
/// <param name="third">Third data <see cref="byte"/>, the 4th <see cref="byte"/> (counting from 0)</param>
/// <param name="timeout">Timeout used for the <see cref="SerialPort"/>, default to <see cref="DefaultTimeout"/></param>
/// <returns>The 3 data <see cref="byte"/> from the response (2, 3 and 4)</returns>
private (byte first, byte second, byte third) SendAndReceiveRaw(CommandType commandType, byte first, byte second, byte third, int timeout = DefaultTimeout)
{
(byte f, byte s, ResponseType response) = SendAndReceive(commandType, first, second, third, timeout);
@@ -112,6 +205,16 @@ namespace WaveshareUARTFingerprintSensor
return (f, s, (byte)response);
}
/// <summary>
/// Send an 8 <see cref="byte"/> command and read the response without throwing an <see cref="Exception"/>
/// </summary>
/// <param name="commandType">Command flag, see <see cref="CommandType"/></param>
/// <param name="first">First data <see cref="byte"/>, the 2th <see cref="byte"/> (counting from 0)</param>
/// <param name="second">Second data <see cref="byte"/>, the 3th <see cref="byte"/> (counting from 0)</param>
/// <param name="third">Third data <see cref="byte"/>, the 4th <see cref="byte"/> (counting from 0)</param>
/// <param name="response">The response as returned by <see cref="SendAndReceive(CommandType, byte, byte, byte, int)"/></param>
/// <param name="timeout">Timeout used for the <see cref="SerialPort"/>, default to <see cref="DefaultTimeout"/></param>
/// <returns>true if successful, false otherwise</returns>
private bool TrySendAndReceive(CommandType commandType, byte first, byte second, byte third, out (byte first, byte second, ResponseType responseType) response, int timeout = DefaultTimeout)
{
try
@@ -126,8 +229,18 @@ namespace WaveshareUARTFingerprintSensor
}
return true;
}
}
/// <summary>
/// Send an 8 <see cref="byte"/> command and read the response without throwing an <see cref="Exception"/>
/// </summary>
/// <param name="commandType">Command flag, see <see cref="CommandType"/></param>
/// <param name="first">First data <see cref="byte"/>, the 2th <see cref="byte"/> (counting from 0)</param>
/// <param name="second">Second data <see cref="byte"/>, the 3th <see cref="byte"/> (counting from 0)</param>
/// <param name="third">Third data <see cref="byte"/>, the 4th <see cref="byte"/> (counting from 0)</param>
/// <param name="response">The response as returned by <see cref="SendAndReceive(CommandType, byte, byte, byte, int)"/></param>
/// <param name="timeout">Timeout used for the <see cref="SerialPort"/>, default to <see cref="DefaultTimeout"/></param>
/// <returns>true if successful, false otherwise</returns>
private bool TrySendAndReceiveRaw(CommandType commandType, byte first, byte second, byte third, out (byte first, byte second, byte third) response, int timeout = DefaultTimeout)
{
try
@@ -144,6 +257,45 @@ namespace WaveshareUARTFingerprintSensor
return true;
}
/// <summary>
/// Read a data packet
/// </summary>
/// <param name="length">The length of the data packet</param>
/// <param name="skipChecksum">An invalid checksum will throw an <see cref="Exception"/>, use this to ignore the checksum</param>
/// <returns></returns>
private byte[] ReadData(int length, bool skipChecksum = false)
{
byte first = (byte)_serialPort.ReadByte();
if (first != PacketSeparator)
{
throw new InvalidDataException("Invalid response from the sensor");
}
byte[] data = new byte[length];
int offset = 0;
do
{
int toRead = length - offset > DataBufferSize ? DataBufferSize : length - offset;
offset += _serialPort.Read(data, offset, toRead);
} while (offset < length);
byte checksum = (byte)_serialPort.ReadByte();
byte separator = (byte)_serialPort.ReadByte();
if (separator != PacketSeparator || (!skipChecksum && checksum != ComputeChecksumData(data, length)))
{
throw new InvalidDataException("Invalid checksum");
}
return data;
}
/// <summary>
/// Query the sensor serial number
/// </summary>
/// <returns></returns>
public uint QuerySerialNumber()
{
(byte first, byte second, byte third) = SendAndReceiveRaw(CommandType.QuerySerialNumber, 0, 0, 0);
@@ -151,6 +303,11 @@ namespace WaveshareUARTFingerprintSensor
return Utils.Merge(first, second, third);
}
/// <summary>
/// Get the number of registered users (fingerprints)
/// </summary>
/// <param name="count">User count if the command is successful</param>
/// <returns></returns>
public bool TryGetUserCount(out ushort count)
{
if (TrySendAndReceive(CommandType.QueryUserCount, 0, 0, 0, out var response, 1000))
@@ -167,14 +324,354 @@ namespace WaveshareUARTFingerprintSensor
return false;
}
/// <summary>
/// Add a fingerprint by sending three commands, with a delay between each to ensure the fingerprint is perfectly read
/// </summary>
/// <param name="userID">The id where to store the user (fingerprint)</param>
/// <param name="userPermission">The <see cref="UserPermission"/> to store</param>
/// <returns></returns>
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;
}
/// <summary>
/// Although there is the word 'Add' in the command (in the official doc), it does not currently add the fingerprint
/// in the captor storage for whatever reason
/// </summary>
/// <param name="userID"></param>
/// <param name="userPermission"></param>
/// <returns></returns>
public ResponseType AddFingerprintAndAcquireEigenvalues(ushort userID, UserPermission userPermission, out Span<byte> eigenvalues)
{
if (userID > MaxUserID)
{
eigenvalues = Span<byte>.Empty;
return ResponseType.Full;
}
CommandType[] commands = { CommandType.AddFingerprint1, CommandType.AddFingerprint2 };
(byte idHigh, byte idLow) = Utils.Split(userID);
foreach (var command in commands)
{
if (TrySendAndReceive(command, idHigh, idLow, (byte)userPermission, out var loopResponse))
{
if (loopResponse.responseType != ResponseType.Success)
{
eigenvalues = Span<byte>.Empty;
return loopResponse.responseType;
}
}
else
{
eigenvalues = Span<byte>.Empty;
return ResponseType.Timeout;
}
Thread.Sleep(50);
}
if (TrySendAndReceive(CommandType.AddAndAcquireFingerprint, 0, 0, 0, out var response))
{
if (response.responseType != ResponseType.Success)
{
eigenvalues = Span<byte>.Empty;
return response.responseType;
}
ushort length = Utils.Merge(response.first, response.second);
var data = ReadData(length);
eigenvalues = data.AsSpan(3);
return ResponseType.Success;
}
else
{
eigenvalues = Span<byte>.Empty;
return ResponseType.Timeout;
}
}
/// <summary>
/// Delete a user (fingerprint) from the sensor
/// </summary>
/// <param name="userID">A valid user (fingerprint) id</param>
/// <returns>true if successful, false otherwise</returns>
public bool DeleteUser(ushort userID)
{
(byte high, byte low) = Utils.Split(userID);
if (TrySendAndReceive(CommandType.DeleteUser, high, low, 0, out var response, 1000))
{
return response.responseType == ResponseType.Success;
}
return false;
}
/// <summary>
/// Delete all users (fingerprints) from the sensor
/// </summary>
/// <returns>true if successful, false otherwise</returns>
public bool DeleteAllUsers()
{
if (TrySendAndReceive(CommandType.DeleteAllUsers, 0, 0, 0, out var response, 1000))
{
return response.responseType == ResponseType.Success;
}
return false;
}
/// <summary>
/// Delete all users that match a specified <see cref="UserPermission"/>
/// </summary>
/// <param name="userPermission"></param>
/// <returns>true if successful, false otherwise</returns>
public bool DeleteAllUsersWithPermission(UserPermission userPermission)
{
if (TrySendAndReceive(CommandType.DeleteAllUsers, 0, 0, (byte)userPermission, out var response, 1000))
{
return response.responseType == ResponseType.Success;
}
return false;
}
/// <summary>
/// Read a fingerprint and check if it matches with the specified user
/// </summary>
/// <param name="userID">A registered user ID</param>
/// <returns>true if the fingerprint match</returns>
public bool Comparison11(ushort userID)
{
(byte high, byte low) = Utils.Split(userID);
if (TrySendAndReceive(CommandType.Comparison11, high, low, 0, out var response))
{
return response.responseType == ResponseType.Success;
}
return false;
}
/// <summary>
/// Read a fingerprint and check if it match with any registered user
/// </summary>
/// <param name="userInfo">The matched user info</param>
/// <returns>true if the fingerprint match</returns>
public bool TryComparison1N(out (ushort userID, UserPermission permission) userInfo)
{
if (TrySendAndReceive(CommandType.Comparison1N, 0, 0, 0, out var response))
{
if (response.responseType != ResponseType.NoUser && response.responseType != ResponseType.Timeout)
{
userInfo = (Utils.Merge(response.first, response.second), (UserPermission)response.responseType);
return true;
}
}
userInfo = default;
return false;
}
/// <summary>
/// Get the stored <see cref="UserPermission"/> for a user
/// </summary>
/// <param name="userID">A registered user</param>
/// <param name="userPermission"></param>
/// <returns>true if the user exist, false otherwise</returns>
public bool TryQueryPermission(ushort userID, out UserPermission userPermission)
{
(byte high, byte low) = Utils.Split(userID);
if (TrySendAndReceive(CommandType.QueryPermission, high, low, 0, out var reponse, 1000))
{
if (reponse.responseType != ResponseType.NoUser)
{
userPermission = (UserPermission)reponse.responseType;
return userPermission != 0;
}
}
userPermission = default;
return false;
}
/// <summary>
/// Retrieve the comparison level used internally by the sensor to compare fingerprints
/// </summary>
/// <param name="comparisonLevel"></param>
/// <returns></returns>
public bool TryGetComparisonLevel(out byte comparisonLevel)
{
if (TrySendAndReceive(CommandType.ManageComparisonLevel, 0, 0, 1, out var response, 1000))
{
if (response.responseType == ResponseType.Success)
{
comparisonLevel = response.second;
return true;
}
}
comparisonLevel = default;
return false;
}
/// <summary>
/// Set the comparison level used internally by the sensor to compare fingerprints
/// </summary>
/// <param name="comparisonLevel">A value in 0..9 range, 9 is the strictest, default is 5</param>
/// <returns></returns>
public bool TrySetComparisonLevel(byte comparisonLevel)
{
if (comparisonLevel < 0 || comparisonLevel > 9)
{
return false;
}
if (TrySendAndReceive(CommandType.ManageComparisonLevel, 0, comparisonLevel, 0, out var response, 1000))
{
return response.responseType == ResponseType.Success;
}
return false;
}
/// <summary>
/// Read a fingerprint and retrieve it's raw image
/// </summary>
/// <param name="image"></param>
/// <returns>true if a valid fingerprint has been read</returns>
public bool TryAcquireImage(out byte[] image)
{
if (TrySendAndReceive(CommandType.AcquireImage, 0, 0, 0, out var response))
{
if (response.responseType == ResponseType.Success)
{
var length = Utils.Merge(response.first, response.second);
image = ReadData(length, skipChecksum: true);
return true;
}
}
image = default;
return false;
}
/// <summary>
/// Read a fingerprint and retrieve the sensor computed eigenvalues
/// </summary>
/// <param name="eigenvalues"></param>
/// <returns>true if a valid fingerprint has been read</returns>
public bool TryAcquireEigenvalues(out Span<byte> eigenvalues)
{
if (TrySendAndReceive(CommandType.AcquireEigenvalues, 0, 0, 0, out var response))
{
if (response.responseType == ResponseType.Success)
{
var length = Utils.Merge(response.first, response.second);
eigenvalues = ReadData(length).AsSpan(3);
return true;
}
}
eigenvalues = Span<byte>.Empty;
return false;
}
/// <summary>
/// Retrieve the sensor computed eigenvalues from a registered user
/// </summary>
/// <param name="userID"></param>
/// <param name="eigenvalues"></param>
/// <param name="userPermission"></param>
/// <returns></returns>
public bool TryAcquireUserEigenvalues(ushort userID, out Span<byte> eigenvalues, out UserPermission userPermission)
{
(byte high, byte low) = Utils.Split(userID);
if (TrySendAndReceive(CommandType.AcquireEigenvaluesDSP, high, low, 0, out var response))
{
if (response.responseType == ResponseType.Success)
{
var length = Utils.Merge(response.first, response.second);
var data = ReadData(length);
eigenvalues = data.AsSpan(3);
userPermission = (UserPermission)data[2];
return true;
}
}
userPermission = default;
eigenvalues = Span<byte>.Empty;
return false;
}
/// <summary>
/// Make the sensor sleep, in this mode the sensor use less power (&lt;16 µA) but won't answer commands until it is waked using <see cref="Wake"/>.
/// <para/>
/// You can know when to wake the sensor using the <see cref="Waked"/> event that is still triggered while asleep
/// </summary>
public void Sleep()
{
_sleeping = true;
_rstPin.Write(GpioPinValue.Low);
}
/// <summary>
/// Wake the sensor, do nothing if it was not sleeping
/// </summary>
public void Wake()
{
_rstPin.Write(GpioPinValue.High);
_sleeping = false;
_rstPin.Write(GpioPinValue.High);
// Needed after wake to really wake the sensor
// Because the first command always fail for whatever reason
TryGetUserCount(out var _);
}
private void OnWake()
@@ -185,6 +682,9 @@ namespace WaveshareUARTFingerprintSensor
}
}
/// <summary>
/// Dispose by disposing the <see cref="SerialPort"/> used
/// </summary>
public void Dispose()
{
_serialPort.Close();

View File

@@ -5,12 +5,12 @@ using System.Runtime.InteropServices;
// Les informations générales relatives à un assembly dépendent de
// l'ensemble d'attributs suivant. Changez les valeurs de ces attributs pour modifier les informations
// associées à un assembly.
[assembly: AssemblyTitle("WaveshareUARTFingerprintSensor")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyTitle("Waveshare UART Fingerprint Sensor (C)")]
[assembly: AssemblyDescription("C# library for the Waveshare UART fingerprint sensor (C)")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyCompany("Ilyx")]
[assembly: AssemblyProduct("WaveshareUARTFingerprintSensor")]
[assembly: AssemblyCopyright("Copyright © 2020")]
[assembly: AssemblyCopyright("")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

View File

@@ -6,10 +6,16 @@ using System.Threading.Tasks;
namespace WaveshareUARTFingerprintSensor
{
/// <summary>
/// If available, it is the third byte from a response packet
/// </summary>
public enum ResponseType : byte
{
Success = 0x00,
Fail = 0x01,
/// <summary>
/// Sensor has exceeded maximum number of users (0xFFF)
/// </summary>
Full = 0x04,
NoUser = 0x05,
UserOccupied = 0x06,

View File

@@ -0,0 +1,12 @@
namespace WaveshareUARTFingerprintSensor
{
/// <summary>
/// Permission level stored on the sensor for each user, it does not have any direct signification
/// </summary>
public enum UserPermission : byte
{
Level1 = 1,
Level2 = 2,
Level3 = 3
}
}

View File

@@ -8,10 +8,41 @@ namespace WaveshareUARTFingerprintSensor
{
public static class Utils
{
/// <summary>
/// Parse two <see cref="byte"/> to an <see cref="ushort"/> according to the sensor documentation
/// </summary>
/// <param name="high">Usually the first data <see cref="byte"/> from a response</param>
/// <param name="low">Usually the second data <see cref="byte"/> from a response</param>
/// <returns></returns>
public static ushort Merge(byte high, byte low) => (ushort)(high << 8 | low);
/// <summary>
/// Parse threee <see cref="byte"/> to an <see cref="uint"/> according to the sensor documentation
/// </summary>
/// <param name="first">Usually the first data <see cref="byte"/> from a response</param>
/// <param name="second">Usually the second data <see cref="byte"/> from a response</param>
/// <param name="third">Usually the third data <see cref="byte"/> from a response</param>
/// <returns></returns>
public static uint Merge(byte first, byte second, byte third) => (uint)(first << 16 | second << 8 | third);
/// <summary>
/// Split an <see cref="ushort"/> to two bytes according to the sensor documentation
/// </summary>
/// <param name="value"></param>
/// <returns>The first <see cref="byte"/> (high) and the second <see cref="byte"/> (low)</returns>
public static (byte high, byte low) Split(ushort value) => ((byte)(value >> 8), (byte)(value & 0xFF));
/// <summary>
/// Split an <see cref="uint"/> to three bytes according to the sensor documentation
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static (byte first, byte second, byte third) Split(uint value) => ((byte)(value >> 16 & 0xFF), (byte)(value >> 8 & 0xFF), (byte)(value & 0xFF));
/// <summary>
/// Display an array in a standard format
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="arr"></param>
/// <returns>A <see cref="string"/> in the form: [ element1, element2, element3, ... ]</returns>
public static string ArrayDisplay<T>(T[] arr) => $"[ {string.Join(", ", arr)} ]";
}
}

View File

@@ -38,8 +38,21 @@
<HintPath>..\packages\Unosquare.Swan.Lite.3.0.0\lib\net461\Swan.Lite.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Buffers, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll</HintPath>
</Reference>
<Reference Include="System.Core" />
<Reference Include="System.Drawing" />
<Reference Include="System.Memory, Version=4.0.1.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Memory.4.5.4\lib\net461\System.Memory.dll</HintPath>
</Reference>
<Reference Include="System.Numerics" />
<Reference Include="System.Numerics.Vectors, Version=4.1.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll</HintPath>
</Reference>
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.4.5.3\lib\net461\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
</Reference>
<Reference Include="System.ServiceProcess" />
<Reference Include="System.ValueTuple, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.ValueTuple.4.5.0\lib\net47\System.ValueTuple.dll</HintPath>
@@ -62,9 +75,11 @@
</ItemGroup>
<ItemGroup>
<Compile Include="CommandType.cs" />
<Compile Include="Exceptions\SensorStateException.cs" />
<Compile Include="FingerprintSensor.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ResponseType.cs" />
<Compile Include="UserPermission.cs" />
<Compile Include="Utils.cs" />
</ItemGroup>
<ItemGroup>

View File

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<package >
<metadata>
<id>$id$</id>
<version>$version$</version>
<title>Waveshare UART Fingerprint Sensor</title>
<authors>Eveldee</authors>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<license type="expression">MIT</license>
<projectUrl>https://github.com/Eveldee/WaveshareUARTFingerprintSensor</projectUrl>
<!-- <iconUrl>http://icon_url_here_or_delete_this_line/</iconUrl> -->
<description>C# library for the Waveshare UART fingerprint sensor (C)</description>
<releaseNotes>Initial version.</releaseNotes>
<copyright>$copyright$</copyright>
<tags>Fingerprint Sensor Waveshare UART</tags>
</metadata>
</package>

View File

@@ -1,5 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="System.Buffers" version="4.5.1" targetFramework="net472" />
<package id="System.Memory" version="4.5.4" targetFramework="net472" />
<package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net472" />
<package id="System.Runtime.CompilerServices.Unsafe" version="4.5.3" targetFramework="net472" />
<package id="System.ValueTuple" version="4.5.0" targetFramework="net472" />
<package id="Unosquare.Raspberry.Abstractions" version="0.4.1" targetFramework="net472" />
<package id="Unosquare.Raspberry.IO" version="0.27.1" targetFramework="net472" />