diff --git a/WaveshareUARTFingerprintSensor.Sample/DataDisplay.cs b/WaveshareUARTFingerprintSensor.Sample/DataDisplay.cs new file mode 100644 index 0000000..7dde60c --- /dev/null +++ b/WaveshareUARTFingerprintSensor.Sample/DataDisplay.cs @@ -0,0 +1,63 @@ +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 +{ + public class DataDisplay : Toplevel + { + private string _title; + private byte[] _data; + + public DataDisplay(string title, byte[] data) + { + _title = title; + _data = data; + + Init(); + } + + private void Init() + { + ColorScheme = Colors.TopLevel; + + // Creates the top-level window to show + var win = new Window("TUIManager") + { + 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); + } + } +} diff --git a/WaveshareUARTFingerprintSensor.Sample/EntryDialog.cs b/WaveshareUARTFingerprintSensor.Sample/EntryDialog.cs new file mode 100644 index 0000000..cc68970 --- /dev/null +++ b/WaveshareUARTFingerprintSensor.Sample/EntryDialog.cs @@ -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 +{ + public class EntryDialog : Dialog + { + private Func _validator; + private bool _success; + private string _title; + private string _errorMessage; + + public EntryDialog(string title, Func 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"); + } + } + } +} diff --git a/WaveshareUARTFingerprintSensor.Sample/Program.cs b/WaveshareUARTFingerprintSensor.Sample/Program.cs index cd143f8..6d24254 100644 --- a/WaveshareUARTFingerprintSensor.Sample/Program.cs +++ b/WaveshareUARTFingerprintSensor.Sample/Program.cs @@ -5,6 +5,7 @@ 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; @@ -13,36 +14,15 @@ namespace WaveshareUARTFingerprintSensor.Sample { class Program { + public static FingerprintSensor FingerprintSensor { get; private set; } + static void Main(string[] args) { - var sensor = new FingerprintSensor(FingerprintSensor.SecondarySerialPort); + FingerprintSensor = new FingerprintSensor(FingerprintSensor.SecondarySerialPort); - sensor.Start(); + FingerprintSensor.Start(); - Console.WriteLine("Here"); - - //if (sensor.TryAcquireUserEigenvalues(2, out var image, out var permission)) - //{ - // Console.WriteLine(Utils.ArrayDisplay(image.ToArray())); - //} - if (sensor.TryGetUserCount(out var count)) - { - Console.WriteLine(count); - } - //while (true) - //{ - // var resp = sensor.AddFingerprintAndAcquireEigenvalues(67, UserPermission.Level3); - - // Console.WriteLine(resp.responseType); - // if (resp.responseType == ResponseType.Success) - // { - // Console.WriteLine(Utils.ArrayDisplay(resp.eigenvalues)); - // } - //} - - Console.WriteLine("End"); - - Thread.Sleep(-1); + Application.Run(); } } } diff --git a/WaveshareUARTFingerprintSensor.Sample/TUIManager.cs b/WaveshareUARTFingerprintSensor.Sample/TUIManager.cs new file mode 100644 index 0000000..4555887 --- /dev/null +++ b/WaveshareUARTFingerprintSensor.Sample/TUIManager.cs @@ -0,0 +1,274 @@ +using NStack; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Terminal.Gui; + +namespace WaveshareUARTFingerprintSensor.Sample +{ + public class TUIManager : Toplevel + { + private readonly FingerprintSensor _fingerprintSensor; + private Label _comparisonLevelLabel; + private Label _userCountLabel; + + public TUIManager() + { + _fingerprintSensor = Program.FingerprintSensor; + + 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 ("Reset Config", "", () => { ResetConfig(); }), + new MenuItem ("_Quit", "", () => { Application.RequestStop(); }) + }) + }); + + Add(menu); + + // Window Content + _comparisonLevelLabel = new Label("Comparison Level: 0") + { + X = 2, + Y = 1, + 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( + _comparisonLevelLabel, + _userCountLabel, + userCountButton, + readFingerprintButton, + readEigenvaluesButton, + readImageButton, + addFingerprintButton, + deleteAFingerprint, + clearFingerprintsButton, + setComparisonLevelButton, + sleepButton, + quitButton + ); + + UpdateUserCount(); + UpdateComparisonLevel(); + + // TODO Config at first 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() + { + if (_fingerprintSensor.TryAcquireImage(out var image)) + { + var window = new DataDisplay("Image", image.ToArray()); + + Application.Run(window); + } + else + { + MessageBox.ErrorQuery("Acquire Image", "Can't acquire image, try to place your finger flat on the sensor", "Ok"); + } + } + + private void ReadEigenvaluesButton_Clicked() + { + // TODO Fingerprint popup to know it is waiting for a fingerprint + + if (_fingerprintSensor.TryAcquireEigenvalues(out var eigenvalues)) + { + var window = new DataDisplay("Eigenvalues", eigenvalues.ToArray()); + + Application.Run(window); + } + else + { + MessageBox.ErrorQuery("Acquire Eigenvalues", "Can't acquire eigenvalues, try to place your finger flat on the sensor", "Ok"); + } + } + + 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() + { + //TODO + } + + 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 => int.TryParse(i.ToString(), out var n), "Need to be a valid user id").TryShow(out var input)) + { + Console.WriteLine(input); + // TODO + } + } + + private void ReadFingerprintButton_Clicked() + { + //TODO + } + + private void UserCountButton_Clicked() + { + UpdateUserCount(); + } + + private void ResetConfig() + { + MessageBox.Query("Reset Config", "Config reset", "OK"); + + //TODO + } + + private void Quit_Clicked() + { + Application.RequestStop(); + } + } +} diff --git a/WaveshareUARTFingerprintSensor.Sample/WaveshareUARTFingerprintSensor.Sample.csproj b/WaveshareUARTFingerprintSensor.Sample/WaveshareUARTFingerprintSensor.Sample.csproj index 14d92e7..a149b3d 100644 --- a/WaveshareUARTFingerprintSensor.Sample/WaveshareUARTFingerprintSensor.Sample.csproj +++ b/WaveshareUARTFingerprintSensor.Sample/WaveshareUARTFingerprintSensor.Sample.csproj @@ -27,6 +27,8 @@ false false true + + AnyCPU @@ -48,6 +50,9 @@ 4 + + ..\packages\NStack.Core.0.14.0\lib\netstandard2.0\NStack.dll + ..\packages\Unosquare.Swan.3.0.0\lib\net461\Swan.dll @@ -80,6 +85,9 @@ + + ..\packages\Terminal.Gui.0.90.3\lib\net472\Terminal.Gui.dll + ..\packages\Unosquare.Raspberry.Abstractions.0.4.1\lib\netstandard2.0\Unosquare.Raspberry.Abstractions.dll @@ -91,8 +99,11 @@ + + + @@ -117,4 +128,11 @@ + + + + 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}. + + + \ No newline at end of file diff --git a/WaveshareUARTFingerprintSensor.Sample/packages.config b/WaveshareUARTFingerprintSensor.Sample/packages.config index e075a78..201038e 100644 --- a/WaveshareUARTFingerprintSensor.Sample/packages.config +++ b/WaveshareUARTFingerprintSensor.Sample/packages.config @@ -1,10 +1,14 @@  + + + +