Compare commits

..

5 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
15 changed files with 1154 additions and 950 deletions

View File

@@ -42,7 +42,7 @@ it may be different for you
| Pi 1 | Primary (/dev/ttyAMA0) | | Pi 1 | Primary (/dev/ttyAMA0) |
| Pi 2 | Primary (/dev/ttyAMA0) | | Pi 2 | Primary (/dev/ttyAMA0) |
| Pi 3 | Secondary (/dev/ttyS0) | | Pi 3 | Secondary (/dev/ttyS0) |
| Pi Zero 4 | Secondary (/dev/ttyS0) | | Pi 4 | Secondary (/dev/ttyS0) |
> The Secondary UART is **disabled by default**, you an activate it in `raspi-config` > The Secondary UART is **disabled by default**, you an activate it in `raspi-config`
> [**Source**](https://www.raspberrypi.org/documentation/configuration/uart.md) > [**Source**](https://www.raspberrypi.org/documentation/configuration/uart.md)

View File

@@ -9,7 +9,8 @@ using Terminal.Gui;
using Unosquare.RaspberryIO; using Unosquare.RaspberryIO;
using Unosquare.RaspberryIO.Abstractions; using Unosquare.RaspberryIO.Abstractions;
using Unosquare.WiringPi; using Unosquare.WiringPi;
using WaveshareUARTFingerprintSensor.Sample.Views;
namespace WaveshareUARTFingerprintSensor.Sample namespace WaveshareUARTFingerprintSensor.Sample
{ {
class Program class Program

View File

@@ -4,6 +4,8 @@ A sample app for the [Waveshare UART fingerprint sensor (C) library](../README.m
![Sample](../Sample.png) ![Sample](../Sample.png)
[![asciicast](https://asciinema.org/a/U1cBQzJ3ueFyN0urkh2NRcjB9.svg)](https://asciinema.org/a/U1cBQzJ3ueFyN0urkh2NRcjB9)
## Build ## Build
You can compile it using any **C# IDE** that supports **.Net Framework** or by running You can compile it using any **C# IDE** that supports **.Net Framework** or by running

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -1,141 +1,141 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup> <PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{0A9C9910-45E4-428F-9BC4-054808794C66}</ProjectGuid> <ProjectGuid>{0A9C9910-45E4-428F-9BC4-054808794C66}</ProjectGuid>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<RootNamespace>WaveshareUARTFingerprintSensor.Sample</RootNamespace> <RootNamespace>WaveshareUARTFingerprintSensor.Sample</RootNamespace>
<AssemblyName>WaveshareUARTFingerprintSensor.Sample</AssemblyName> <AssemblyName>WaveshareUARTFingerprintSensor.Sample</AssemblyName>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion> <TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment> <FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic> <Deterministic>true</Deterministic>
<PublishUrl>publish\</PublishUrl> <PublishUrl>publish\</PublishUrl>
<Install>true</Install> <Install>true</Install>
<InstallFrom>Disk</InstallFrom> <InstallFrom>Disk</InstallFrom>
<UpdateEnabled>false</UpdateEnabled> <UpdateEnabled>false</UpdateEnabled>
<UpdateMode>Foreground</UpdateMode> <UpdateMode>Foreground</UpdateMode>
<UpdateInterval>7</UpdateInterval> <UpdateInterval>7</UpdateInterval>
<UpdateIntervalUnits>Days</UpdateIntervalUnits> <UpdateIntervalUnits>Days</UpdateIntervalUnits>
<UpdatePeriodically>false</UpdatePeriodically> <UpdatePeriodically>false</UpdatePeriodically>
<UpdateRequired>false</UpdateRequired> <UpdateRequired>false</UpdateRequired>
<MapFileExtensions>true</MapFileExtensions> <MapFileExtensions>true</MapFileExtensions>
<ApplicationRevision>0</ApplicationRevision> <ApplicationRevision>0</ApplicationRevision>
<ApplicationVersion>1.0.0.%2a</ApplicationVersion> <ApplicationVersion>1.0.0.%2a</ApplicationVersion>
<IsWebBootstrapper>false</IsWebBootstrapper> <IsWebBootstrapper>false</IsWebBootstrapper>
<UseApplicationTrust>false</UseApplicationTrust> <UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled> <BootstrapperEnabled>true</BootstrapperEnabled>
<NuGetPackageImportStamp> <NuGetPackageImportStamp>
</NuGetPackageImportStamp> </NuGetPackageImportStamp>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget> <PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols> <DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType> <DebugType>full</DebugType>
<Optimize>false</Optimize> <Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath> <OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants> <DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport> <ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget> <PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType> <DebugType>pdbonly</DebugType>
<Optimize>true</Optimize> <Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath> <OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants> <DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport> <ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="NStack, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL"> <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> <HintPath>..\packages\NStack.Core.0.14.0\lib\netstandard2.0\NStack.dll</HintPath>
</Reference> </Reference>
<Reference Include="Swan, Version=3.0.0.0, Culture=neutral, processorArchitecture=MSIL"> <Reference Include="Swan, Version=3.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Unosquare.Swan.3.0.0\lib\net461\Swan.dll</HintPath> <HintPath>..\packages\Unosquare.Swan.3.0.0\lib\net461\Swan.dll</HintPath>
</Reference> </Reference>
<Reference Include="Swan.Lite, Version=3.0.0.0, Culture=neutral, PublicKeyToken=30c707c872729fff, processorArchitecture=MSIL"> <Reference Include="Swan.Lite, Version=3.0.0.0, Culture=neutral, PublicKeyToken=30c707c872729fff, processorArchitecture=MSIL">
<HintPath>..\packages\Unosquare.Swan.Lite.3.0.0\lib\net461\Swan.Lite.dll</HintPath> <HintPath>..\packages\Unosquare.Swan.Lite.3.0.0\lib\net461\Swan.Lite.dll</HintPath>
</Reference> </Reference>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Buffers, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL"> <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> <HintPath>..\packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll</HintPath>
</Reference> </Reference>
<Reference Include="System.Core" /> <Reference Include="System.Core" />
<Reference Include="System.Drawing" /> <Reference Include="System.Drawing" />
<Reference Include="System.Memory, Version=4.0.1.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL"> <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> <HintPath>..\packages\System.Memory.4.5.4\lib\net461\System.Memory.dll</HintPath>
</Reference> </Reference>
<Reference Include="System.Numerics" /> <Reference Include="System.Numerics" />
<Reference Include="System.Numerics.Vectors, Version=4.1.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> <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> <HintPath>..\packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll</HintPath>
</Reference> </Reference>
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> <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> <HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.4.5.3\lib\net461\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
</Reference> </Reference>
<Reference Include="System.ServiceProcess" /> <Reference Include="System.ServiceProcess" />
<Reference Include="System.ValueTuple, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL"> <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> <HintPath>..\packages\System.ValueTuple.4.5.0\lib\net47\System.ValueTuple.dll</HintPath>
</Reference> </Reference>
<Reference Include="System.Xml.Linq" /> <Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" /> <Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" /> <Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" /> <Reference Include="System.Data" />
<Reference Include="System.Net.Http" /> <Reference Include="System.Net.Http" />
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
<Reference Include="Terminal.Gui, Version=0.90.3.0, Culture=neutral, processorArchitecture=MSIL"> <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> <HintPath>..\packages\Terminal.Gui.0.90.3\lib\net472\Terminal.Gui.dll</HintPath>
</Reference> </Reference>
<Reference Include="Unosquare.Raspberry.Abstractions, Version=0.4.1.0, Culture=neutral, processorArchitecture=MSIL"> <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> <HintPath>..\packages\Unosquare.Raspberry.Abstractions.0.4.1\lib\netstandard2.0\Unosquare.Raspberry.Abstractions.dll</HintPath>
</Reference> </Reference>
<Reference Include="Unosquare.RaspberryIO, Version=0.27.1.0, Culture=neutral, processorArchitecture=MSIL"> <Reference Include="Unosquare.RaspberryIO, Version=0.27.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Unosquare.Raspberry.IO.0.27.1\lib\netstandard2.0\Unosquare.RaspberryIO.dll</HintPath> <HintPath>..\packages\Unosquare.Raspberry.IO.0.27.1\lib\netstandard2.0\Unosquare.RaspberryIO.dll</HintPath>
</Reference> </Reference>
<Reference Include="Unosquare.WiringPi, Version=0.5.1.0, Culture=neutral, processorArchitecture=MSIL"> <Reference Include="Unosquare.WiringPi, Version=0.5.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Unosquare.WiringPi.0.5.1\lib\netstandard2.0\Unosquare.WiringPi.dll</HintPath> <HintPath>..\packages\Unosquare.WiringPi.0.5.1\lib\netstandard2.0\Unosquare.WiringPi.dll</HintPath>
</Reference> </Reference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Views\DataDisplay.cs" /> <Compile Include="Views\DataDisplay.cs" />
<Compile Include="Views\EntryDialog.cs" /> <Compile Include="Views\EntryDialog.cs" />
<Compile Include="Views\FingerprintDialog.cs" /> <Compile Include="Views\FingerprintDialog.cs" />
<Compile Include="Program.cs" /> <Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Views\SettingsDisplay.cs" /> <Compile Include="Views\SettingsDisplay.cs" />
<Compile Include="Views\SleepDisplay.cs" /> <Compile Include="Views\SleepDisplay.cs" />
<Compile Include="Views\TUIManager.cs" /> <Compile Include="Views\TUIManager.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="App.config" /> <None Include="App.config" />
<None Include="packages.config" /> <None Include="packages.config" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<BootstrapperPackage Include=".NETFramework,Version=v4.7.2"> <BootstrapperPackage Include=".NETFramework,Version=v4.7.2">
<Visible>False</Visible> <Visible>False</Visible>
<ProductName>Microsoft .NET Framework 4.7.2 %28x86 et x64%29</ProductName> <ProductName>Microsoft .NET Framework 4.7.2 %28x86 et x64%29</ProductName>
<Install>true</Install> <Install>true</Install>
</BootstrapperPackage> </BootstrapperPackage>
<BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1"> <BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1">
<Visible>False</Visible> <Visible>False</Visible>
<ProductName>.NET Framework 3.5 SP1</ProductName> <ProductName>.NET Framework 3.5 SP1</ProductName>
<Install>false</Install> <Install>false</Install>
</BootstrapperPackage> </BootstrapperPackage>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\WaveshareUARTFingerprintSensor\WaveshareUARTFingerprintSensor.csproj"> <ProjectReference Include="..\WaveshareUARTFingerprintSensor\WaveshareUARTFingerprintSensor.csproj">
<Project>{dc535997-1161-4a7d-8573-259b13595778}</Project> <Project>{dc535997-1161-4a7d-8573-259b13595778}</Project>
<Name>WaveshareUARTFingerprintSensor</Name> <Name>WaveshareUARTFingerprintSensor</Name>
</ProjectReference> </ProjectReference>
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <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')" /> <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"> <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup> <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> <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> </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'))" /> <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> </Target>
</Project> </Project>

View File

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

View File

@@ -16,17 +16,45 @@ namespace WaveshareUARTFingerprintSensor
{ {
public class FingerprintSensor : IDisposable 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"; public const string SecondarySerialPort = "/dev/ttyS0";
/// <summary>
/// Timeout used for the <see cref="SerialPort"/> I/O operations
/// </summary>
public const int DefaultTimeout = 10_000; 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; public const int MaxUserID = 0xFFF;
/// <summary>
/// Internal buffer size
/// </summary>
public const int DataBufferSize = 4095; 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; public event WakedEventHandler Waked;
/// <summary>
/// Delegate for the <see cref="Waked"/> event
/// </summary>
/// <param name="sender"></param>
public delegate void WakedEventHandler(FingerprintSensor sender); public delegate void WakedEventHandler(FingerprintSensor sender);
/// <summary>
/// <see cref="SerialPort"/> name/path used
/// </summary>
public string PortName { get; } public string PortName { get; }
/// <summary>
/// Mark the end of a command packet, usually the 8th byte
/// </summary>
private const byte PacketSeparator = 0xF5; private const byte PacketSeparator = 0xF5;
private SerialPort _serialPort; private SerialPort _serialPort;
@@ -37,6 +65,12 @@ namespace WaveshareUARTFingerprintSensor
private bool _sleeping = false; private bool _sleeping = false;
private readonly object _lock = new object(); 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) public FingerprintSensor(string portName, int wakePin = 23, int rstPin = 24)
{ {
PortName = portName; PortName = portName;
@@ -44,6 +78,9 @@ namespace WaveshareUARTFingerprintSensor
_rstPinNumber = rstPin; _rstPinNumber = rstPin;
} }
/// <summary>
/// Start the sensor, initializing <see cref="BootstrapWiringPi"/>, the Gpio pins and the <see cref="SerialPort"/>
/// </summary>
public void Start() public void Start()
{ {
// Initialize Gpio // Initialize Gpio
@@ -64,6 +101,11 @@ namespace WaveshareUARTFingerprintSensor
_serialPort.Open(); _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) private byte ComputeChecksum(byte[] data)
{ {
byte checksum = 0; byte checksum = 0;
@@ -76,6 +118,12 @@ namespace WaveshareUARTFingerprintSensor
return checksum; 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) private byte ComputeChecksumData(byte[] data, int length)
{ {
byte checksum = 0; byte checksum = 0;
@@ -88,6 +136,15 @@ namespace WaveshareUARTFingerprintSensor
return checksum; 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) private (byte first, byte second, ResponseType responseType) SendAndReceive(CommandType commandType, byte first, byte second, byte third, int timeout = DefaultTimeout)
{ {
if (_sleeping) if (_sleeping)
@@ -132,6 +189,15 @@ namespace WaveshareUARTFingerprintSensor
return (buffer[2], buffer[3], (ResponseType)buffer[4]); 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) 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); (byte f, byte s, ResponseType response) = SendAndReceive(commandType, first, second, third, timeout);
@@ -139,6 +205,16 @@ namespace WaveshareUARTFingerprintSensor
return (f, s, (byte)response); 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) private bool TrySendAndReceive(CommandType commandType, byte first, byte second, byte third, out (byte first, byte second, ResponseType responseType) response, int timeout = DefaultTimeout)
{ {
try try
@@ -153,8 +229,18 @@ namespace WaveshareUARTFingerprintSensor
} }
return true; 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) private bool TrySendAndReceiveRaw(CommandType commandType, byte first, byte second, byte third, out (byte first, byte second, byte third) response, int timeout = DefaultTimeout)
{ {
try try
@@ -171,6 +257,12 @@ namespace WaveshareUARTFingerprintSensor
return true; 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) private byte[] ReadData(int length, bool skipChecksum = false)
{ {
byte first = (byte)_serialPort.ReadByte(); byte first = (byte)_serialPort.ReadByte();
@@ -200,6 +292,10 @@ namespace WaveshareUARTFingerprintSensor
return data; return data;
} }
/// <summary>
/// Query the sensor serial number
/// </summary>
/// <returns></returns>
public uint QuerySerialNumber() public uint QuerySerialNumber()
{ {
(byte first, byte second, byte third) = SendAndReceiveRaw(CommandType.QuerySerialNumber, 0, 0, 0); (byte first, byte second, byte third) = SendAndReceiveRaw(CommandType.QuerySerialNumber, 0, 0, 0);
@@ -207,6 +303,11 @@ namespace WaveshareUARTFingerprintSensor
return Utils.Merge(first, second, third); 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) public bool TryGetUserCount(out ushort count)
{ {
if (TrySendAndReceive(CommandType.QueryUserCount, 0, 0, 0, out var response, 1000)) if (TrySendAndReceive(CommandType.QueryUserCount, 0, 0, 0, out var response, 1000))
@@ -223,6 +324,12 @@ namespace WaveshareUARTFingerprintSensor
return false; 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) public ResponseType AddFingerprint(ushort userID, UserPermission userPermission)
{ {
if (userID > MaxUserID) if (userID > MaxUserID)
@@ -312,6 +419,11 @@ namespace WaveshareUARTFingerprintSensor
} }
} }
/// <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) public bool DeleteUser(ushort userID)
{ {
(byte high, byte low) = Utils.Split(userID); (byte high, byte low) = Utils.Split(userID);
@@ -324,6 +436,10 @@ namespace WaveshareUARTFingerprintSensor
return false; return false;
} }
/// <summary>
/// Delete all users (fingerprints) from the sensor
/// </summary>
/// <returns>true if successful, false otherwise</returns>
public bool DeleteAllUsers() public bool DeleteAllUsers()
{ {
if (TrySendAndReceive(CommandType.DeleteAllUsers, 0, 0, 0, out var response, 1000)) if (TrySendAndReceive(CommandType.DeleteAllUsers, 0, 0, 0, out var response, 1000))
@@ -334,6 +450,11 @@ namespace WaveshareUARTFingerprintSensor
return false; 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) public bool DeleteAllUsersWithPermission(UserPermission userPermission)
{ {
if (TrySendAndReceive(CommandType.DeleteAllUsers, 0, 0, (byte)userPermission, out var response, 1000)) if (TrySendAndReceive(CommandType.DeleteAllUsers, 0, 0, (byte)userPermission, out var response, 1000))
@@ -348,7 +469,7 @@ namespace WaveshareUARTFingerprintSensor
/// Read a fingerprint and check if it matches with the specified user /// Read a fingerprint and check if it matches with the specified user
/// </summary> /// </summary>
/// <param name="userID">A registered user ID</param> /// <param name="userID">A registered user ID</param>
/// <returns></returns> /// <returns>true if the fingerprint match</returns>
public bool Comparison11(ushort userID) public bool Comparison11(ushort userID)
{ {
(byte high, byte low) = Utils.Split(userID); (byte high, byte low) = Utils.Split(userID);
@@ -365,7 +486,7 @@ namespace WaveshareUARTFingerprintSensor
/// Read a fingerprint and check if it match with any registered user /// Read a fingerprint and check if it match with any registered user
/// </summary> /// </summary>
/// <param name="userInfo">The matched user info</param> /// <param name="userInfo">The matched user info</param>
/// <returns></returns> /// <returns>true if the fingerprint match</returns>
public bool TryComparison1N(out (ushort userID, UserPermission permission) userInfo) public bool TryComparison1N(out (ushort userID, UserPermission permission) userInfo)
{ {
if (TrySendAndReceive(CommandType.Comparison1N, 0, 0, 0, out var response)) if (TrySendAndReceive(CommandType.Comparison1N, 0, 0, 0, out var response))
@@ -383,6 +504,12 @@ namespace WaveshareUARTFingerprintSensor
return false; 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) public bool TryQueryPermission(ushort userID, out UserPermission userPermission)
{ {
(byte high, byte low) = Utils.Split(userID); (byte high, byte low) = Utils.Split(userID);
@@ -402,6 +529,11 @@ namespace WaveshareUARTFingerprintSensor
return false; 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) public bool TryGetComparisonLevel(out byte comparisonLevel)
{ {
if (TrySendAndReceive(CommandType.ManageComparisonLevel, 0, 0, 1, out var response, 1000)) if (TrySendAndReceive(CommandType.ManageComparisonLevel, 0, 0, 1, out var response, 1000))
@@ -420,7 +552,7 @@ namespace WaveshareUARTFingerprintSensor
} }
/// <summary> /// <summary>
/// Set comparison level used to compare fingerprints /// Set the comparison level used internally by the sensor to compare fingerprints
/// </summary> /// </summary>
/// <param name="comparisonLevel">A value in 0..9 range, 9 is the strictest, default is 5</param> /// <param name="comparisonLevel">A value in 0..9 range, 9 is the strictest, default is 5</param>
/// <returns></returns> /// <returns></returns>
@@ -439,6 +571,11 @@ namespace WaveshareUARTFingerprintSensor
return false; 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) public bool TryAcquireImage(out byte[] image)
{ {
if (TrySendAndReceive(CommandType.AcquireImage, 0, 0, 0, out var response)) if (TrySendAndReceive(CommandType.AcquireImage, 0, 0, 0, out var response))
@@ -458,6 +595,11 @@ namespace WaveshareUARTFingerprintSensor
return false; 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) public bool TryAcquireEigenvalues(out Span<byte> eigenvalues)
{ {
if (TrySendAndReceive(CommandType.AcquireEigenvalues, 0, 0, 0, out var response)) if (TrySendAndReceive(CommandType.AcquireEigenvalues, 0, 0, 0, out var response))
@@ -477,6 +619,13 @@ namespace WaveshareUARTFingerprintSensor
return false; 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) public bool TryAcquireUserEigenvalues(ushort userID, out Span<byte> eigenvalues, out UserPermission userPermission)
{ {
(byte high, byte low) = Utils.Split(userID); (byte high, byte low) = Utils.Split(userID);
@@ -501,12 +650,20 @@ namespace WaveshareUARTFingerprintSensor
return false; 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() public void Sleep()
{ {
_sleeping = true; _sleeping = true;
_rstPin.Write(GpioPinValue.Low); _rstPin.Write(GpioPinValue.Low);
} }
/// <summary>
/// Wake the sensor, do nothing if it was not sleeping
/// </summary>
public void Wake() public void Wake()
{ {
_sleeping = false; _sleeping = false;
@@ -525,6 +682,9 @@ namespace WaveshareUARTFingerprintSensor
} }
} }
/// <summary>
/// Dispose by disposing the <see cref="SerialPort"/> used
/// </summary>
public void Dispose() public void Dispose()
{ {
_serialPort.Close(); _serialPort.Close();

View File

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

View File

@@ -1,5 +1,8 @@
namespace WaveshareUARTFingerprintSensor namespace WaveshareUARTFingerprintSensor
{ {
/// <summary>
/// Permission level stored on the sensor for each user, it does not have any direct signification
/// </summary>
public enum UserPermission : byte public enum UserPermission : byte
{ {
Level1 = 1, Level1 = 1,

View File

@@ -8,12 +8,41 @@ namespace WaveshareUARTFingerprintSensor
{ {
public static class Utils 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); 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); 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)); 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)); 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)} ]"; public static string ArrayDisplay<T>(T[] arr) => $"[ {string.Join(", ", arr)} ]";
} }
} }