diff --git a/WaveshareUARTFingerprintSensor.Sample/Views/DataDisplay.cs b/WaveshareUARTFingerprintSensor.Sample/Views/DataDisplay.cs index bfeec24..28f3902 100644 --- a/WaveshareUARTFingerprintSensor.Sample/Views/DataDisplay.cs +++ b/WaveshareUARTFingerprintSensor.Sample/Views/DataDisplay.cs @@ -1,65 +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); - } - } -} +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); + } + } +} diff --git a/WaveshareUARTFingerprintSensor.Sample/Views/EntryDialog.cs b/WaveshareUARTFingerprintSensor.Sample/Views/EntryDialog.cs index 521805a..95c8997 100644 --- a/WaveshareUARTFingerprintSensor.Sample/Views/EntryDialog.cs +++ b/WaveshareUARTFingerprintSensor.Sample/Views/EntryDialog.cs @@ -1,87 +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 _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"); - } - } - } -} +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 _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/Views/FingerprintDialog.cs b/WaveshareUARTFingerprintSensor.Sample/Views/FingerprintDialog.cs index 2c47d2e..3a7c224 100644 --- a/WaveshareUARTFingerprintSensor.Sample/Views/FingerprintDialog.cs +++ b/WaveshareUARTFingerprintSensor.Sample/Views/FingerprintDialog.cs @@ -1,53 +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(); - }); - } - } -} +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(); + }); + } + } +} diff --git a/WaveshareUARTFingerprintSensor.Sample/Views/SettingsDisplay.cs b/WaveshareUARTFingerprintSensor.Sample/Views/SettingsDisplay.cs index 8635363..80b9175 100644 --- a/WaveshareUARTFingerprintSensor.Sample/Views/SettingsDisplay.cs +++ b/WaveshareUARTFingerprintSensor.Sample/Views/SettingsDisplay.cs @@ -1,73 +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); - } - } -} +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); + } + } +} diff --git a/WaveshareUARTFingerprintSensor.Sample/Views/SleepDisplay.cs b/WaveshareUARTFingerprintSensor.Sample/Views/SleepDisplay.cs index 2de75ba..fb3ab4b 100644 --- a/WaveshareUARTFingerprintSensor.Sample/Views/SleepDisplay.cs +++ b/WaveshareUARTFingerprintSensor.Sample/Views/SleepDisplay.cs @@ -1,108 +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(); - } - } -} +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(); + } + } +} diff --git a/WaveshareUARTFingerprintSensor.Sample/Views/TUIManager.cs b/WaveshareUARTFingerprintSensor.Sample/Views/TUIManager.cs index 7a7058e..31f5ac1 100644 --- a/WaveshareUARTFingerprintSensor.Sample/Views/TUIManager.cs +++ b/WaveshareUARTFingerprintSensor.Sample/Views/TUIManager.cs @@ -1,416 +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(); - } - } -} +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(); + } + } +} diff --git a/WaveshareUARTFingerprintSensor.Sample/WaveshareUARTFingerprintSensor.Sample.csproj b/WaveshareUARTFingerprintSensor.Sample/WaveshareUARTFingerprintSensor.Sample.csproj index 8f32dac..6fe9565 100644 --- a/WaveshareUARTFingerprintSensor.Sample/WaveshareUARTFingerprintSensor.Sample.csproj +++ b/WaveshareUARTFingerprintSensor.Sample/WaveshareUARTFingerprintSensor.Sample.csproj @@ -1,141 +1,141 @@ - - - - - Debug - AnyCPU - {0A9C9910-45E4-428F-9BC4-054808794C66} - Exe - WaveshareUARTFingerprintSensor.Sample - WaveshareUARTFingerprintSensor.Sample - v4.7.2 - 512 - true - true - publish\ - true - Disk - false - Foreground - 7 - Days - false - false - true - 0 - 1.0.0.%2a - false - false - true - - - - - AnyCPU - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - AnyCPU - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - - - - ..\packages\NStack.Core.0.14.0\lib\netstandard2.0\NStack.dll - - - ..\packages\Unosquare.Swan.3.0.0\lib\net461\Swan.dll - - - ..\packages\Unosquare.Swan.Lite.3.0.0\lib\net461\Swan.Lite.dll - - - - ..\packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll - - - - - ..\packages\System.Memory.4.5.4\lib\net461\System.Memory.dll - - - - ..\packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll - - - ..\packages\System.Runtime.CompilerServices.Unsafe.4.5.3\lib\net461\System.Runtime.CompilerServices.Unsafe.dll - - - - ..\packages\System.ValueTuple.4.5.0\lib\net47\System.ValueTuple.dll - - - - - - - - - ..\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 - - - ..\packages\Unosquare.Raspberry.IO.0.27.1\lib\netstandard2.0\Unosquare.RaspberryIO.dll - - - ..\packages\Unosquare.WiringPi.0.5.1\lib\netstandard2.0\Unosquare.WiringPi.dll - - - - - - - - - - - - - - - - - - - False - Microsoft .NET Framework 4.7.2 %28x86 et x64%29 - true - - - False - .NET Framework 3.5 SP1 - false - - - - - {dc535997-1161-4a7d-8573-259b13595778} - WaveshareUARTFingerprintSensor - - - - - - - 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}. - - - + + + + + Debug + AnyCPU + {0A9C9910-45E4-428F-9BC4-054808794C66} + Exe + WaveshareUARTFingerprintSensor.Sample + WaveshareUARTFingerprintSensor.Sample + v4.7.2 + 512 + true + true + publish\ + true + Disk + false + Foreground + 7 + Days + false + false + true + 0 + 1.0.0.%2a + false + false + true + + + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\NStack.Core.0.14.0\lib\netstandard2.0\NStack.dll + + + ..\packages\Unosquare.Swan.3.0.0\lib\net461\Swan.dll + + + ..\packages\Unosquare.Swan.Lite.3.0.0\lib\net461\Swan.Lite.dll + + + + ..\packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll + + + + + ..\packages\System.Memory.4.5.4\lib\net461\System.Memory.dll + + + + ..\packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll + + + ..\packages\System.Runtime.CompilerServices.Unsafe.4.5.3\lib\net461\System.Runtime.CompilerServices.Unsafe.dll + + + + ..\packages\System.ValueTuple.4.5.0\lib\net47\System.ValueTuple.dll + + + + + + + + + ..\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 + + + ..\packages\Unosquare.Raspberry.IO.0.27.1\lib\netstandard2.0\Unosquare.RaspberryIO.dll + + + ..\packages\Unosquare.WiringPi.0.5.1\lib\netstandard2.0\Unosquare.WiringPi.dll + + + + + + + + + + + + + + + + + + + False + Microsoft .NET Framework 4.7.2 %28x86 et x64%29 + true + + + False + .NET Framework 3.5 SP1 + false + + + + + {dc535997-1161-4a7d-8573-259b13595778} + WaveshareUARTFingerprintSensor + + + + + + + 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