From e602fb26c7893c61279b6352e090fca01b4d577c Mon Sep 17 00:00:00 2001 From: Eveldee Date: Sun, 10 Jan 2021 01:54:25 +0100 Subject: [PATCH] Add Settings --- .../Program.cs | 6 -- .../SettingsDisplay.cs | 71 +++++++++++++++++++ .../TUIManager.cs | 49 ++++++++++--- ...veshareUARTFingerprintSensor.Sample.csproj | 1 + 4 files changed, 112 insertions(+), 15 deletions(-) create mode 100644 WaveshareUARTFingerprintSensor.Sample/SettingsDisplay.cs diff --git a/WaveshareUARTFingerprintSensor.Sample/Program.cs b/WaveshareUARTFingerprintSensor.Sample/Program.cs index 6d24254..647e9ab 100644 --- a/WaveshareUARTFingerprintSensor.Sample/Program.cs +++ b/WaveshareUARTFingerprintSensor.Sample/Program.cs @@ -14,14 +14,8 @@ namespace WaveshareUARTFingerprintSensor.Sample { class Program { - public static FingerprintSensor FingerprintSensor { get; private set; } - static void Main(string[] args) { - FingerprintSensor = new FingerprintSensor(FingerprintSensor.SecondarySerialPort); - - FingerprintSensor.Start(); - Application.Run(); } } diff --git a/WaveshareUARTFingerprintSensor.Sample/SettingsDisplay.cs b/WaveshareUARTFingerprintSensor.Sample/SettingsDisplay.cs new file mode 100644 index 0000000..7dae4ae --- /dev/null +++ b/WaveshareUARTFingerprintSensor.Sample/SettingsDisplay.cs @@ -0,0 +1,71 @@ +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 +{ + public class SettingsDisplay : Toplevel + { + private string _port; + private RadioGroup _radioPort; + + public SettingsDisplay() + { + Init(); + } + + private void Init() + { + 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/TUIManager.cs b/WaveshareUARTFingerprintSensor.Sample/TUIManager.cs index 9fe84b3..2703576 100644 --- a/WaveshareUARTFingerprintSensor.Sample/TUIManager.cs +++ b/WaveshareUARTFingerprintSensor.Sample/TUIManager.cs @@ -12,8 +12,10 @@ namespace WaveshareUARTFingerprintSensor.Sample public class TUIManager : Toplevel { public const string OutputFilePath = "out.txt"; + public const string SettingsFilePath = "settings.txt"; - private readonly FingerprintSensor _fingerprintSensor; + private FingerprintSensor _fingerprintSensor; + private Label _serialPortLabel; private Label _comparisonLevelLabel; private Label _userCountLabel; @@ -21,8 +23,6 @@ namespace WaveshareUARTFingerprintSensor.Sample public TUIManager() { - _fingerprintSensor = Program.FingerprintSensor; - Init(); } @@ -48,7 +48,7 @@ namespace WaveshareUARTFingerprintSensor.Sample // 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 ("_Change Config", "", () => { ChangeConfig(); }), new MenuItem ("_Quit", "", () => { Application.RequestStop(); }) }) }); @@ -56,12 +56,18 @@ namespace WaveshareUARTFingerprintSensor.Sample Add(menu); // Window Content - _comparisonLevelLabel = new Label("Comparison Level: 0") + _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), @@ -140,6 +146,7 @@ namespace WaveshareUARTFingerprintSensor.Sample quitButton.Clicked += Quit_Clicked; win.Add( + _serialPortLabel, _comparisonLevelLabel, _userCountLabel, userCountButton, @@ -154,10 +161,30 @@ namespace WaveshareUARTFingerprintSensor.Sample quitButton ); + // Init Sensor + if (!File.Exists(SettingsFilePath)) + { + Application.Run(new SettingsDisplay()); + } + + InitSensor(); + + // Update gui + UpdateSerialPort(); UpdateUserCount(); UpdateComparisonLevel(); + } - // TODO Config at first start + private void UpdateSerialPort() + { + _serialPortLabel.Text = $"Serial Port: {_fingerprintSensor.PortName}"; + } + + private void InitSensor() + { + _fingerprintSensor = new FingerprintSensor(File.ReadAllText(SettingsFilePath)); + + _fingerprintSensor.Start(); } private void UpdateUserCount() @@ -370,11 +397,15 @@ namespace WaveshareUARTFingerprintSensor.Sample }); } - private void ResetConfig() + private void ChangeConfig() { - MessageBox.Query("Reset Config", "Config reset", "OK"); + File.Delete(SettingsFilePath); - //TODO + Application.Run(new SettingsDisplay()); + + InitSensor(); + + UpdateSerialPort(); } private void Quit_Clicked() diff --git a/WaveshareUARTFingerprintSensor.Sample/WaveshareUARTFingerprintSensor.Sample.csproj b/WaveshareUARTFingerprintSensor.Sample/WaveshareUARTFingerprintSensor.Sample.csproj index ac2b348..cbb60d9 100644 --- a/WaveshareUARTFingerprintSensor.Sample/WaveshareUARTFingerprintSensor.Sample.csproj +++ b/WaveshareUARTFingerprintSensor.Sample/WaveshareUARTFingerprintSensor.Sample.csproj @@ -104,6 +104,7 @@ +