Add Settings

This commit is contained in:
2021-01-10 01:54:25 +01:00
parent 747db9d7dd
commit e602fb26c7
4 changed files with 112 additions and 15 deletions

View File

@@ -14,14 +14,8 @@ namespace WaveshareUARTFingerprintSensor.Sample
{ {
class Program class Program
{ {
public static FingerprintSensor FingerprintSensor { get; private set; }
static void Main(string[] args) static void Main(string[] args)
{ {
FingerprintSensor = new FingerprintSensor(FingerprintSensor.SecondarySerialPort);
FingerprintSensor.Start();
Application.Run<TUIManager>(); Application.Run<TUIManager>();
} }
} }

View File

@@ -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);
}
}
}

View File

@@ -12,8 +12,10 @@ namespace WaveshareUARTFingerprintSensor.Sample
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";
private readonly FingerprintSensor _fingerprintSensor; private FingerprintSensor _fingerprintSensor;
private Label _serialPortLabel;
private Label _comparisonLevelLabel; private Label _comparisonLevelLabel;
private Label _userCountLabel; private Label _userCountLabel;
@@ -21,8 +23,6 @@ namespace WaveshareUARTFingerprintSensor.Sample
public TUIManager() public TUIManager()
{ {
_fingerprintSensor = Program.FingerprintSensor;
Init(); Init();
} }
@@ -48,7 +48,7 @@ namespace WaveshareUARTFingerprintSensor.Sample
// 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 ("Reset Config", "", () => { ResetConfig(); }), new MenuItem ("_Change Config", "", () => { ChangeConfig(); }),
new MenuItem ("_Quit", "", () => { Application.RequestStop(); }) new MenuItem ("_Quit", "", () => { Application.RequestStop(); })
}) })
}); });
@@ -56,12 +56,18 @@ namespace WaveshareUARTFingerprintSensor.Sample
Add(menu); Add(menu);
// Window Content // Window Content
_comparisonLevelLabel = new Label("Comparison Level: 0") _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")
{
X = Pos.Left(_serialPortLabel),
Y = Pos.Bottom(_serialPortLabel),
Width = Dim.Fill()
};
_userCountLabel = new Label("Users: 0") _userCountLabel = new Label("Users: 0")
{ {
X = Pos.Left(_comparisonLevelLabel), X = Pos.Left(_comparisonLevelLabel),
@@ -140,6 +146,7 @@ namespace WaveshareUARTFingerprintSensor.Sample
quitButton.Clicked += Quit_Clicked; quitButton.Clicked += Quit_Clicked;
win.Add( win.Add(
_serialPortLabel,
_comparisonLevelLabel, _comparisonLevelLabel,
_userCountLabel, _userCountLabel,
userCountButton, userCountButton,
@@ -154,10 +161,30 @@ namespace WaveshareUARTFingerprintSensor.Sample
quitButton quitButton
); );
// Init Sensor
if (!File.Exists(SettingsFilePath))
{
Application.Run(new SettingsDisplay());
}
InitSensor();
// Update gui
UpdateSerialPort();
UpdateUserCount(); UpdateUserCount();
UpdateComparisonLevel(); 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() 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() private void Quit_Clicked()

View File

@@ -104,6 +104,7 @@
<Compile Include="FingerprintDialog.cs" /> <Compile Include="FingerprintDialog.cs" />
<Compile Include="Program.cs" /> <Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SettingsDisplay.cs" />
<Compile Include="SleepDisplay.cs" /> <Compile Include="SleepDisplay.cs" />
<Compile Include="TUIManager.cs" /> <Compile Include="TUIManager.cs" />
</ItemGroup> </ItemGroup>