Use gui.cs
This commit is contained in:
63
WaveshareUARTFingerprintSensor.Sample/DataDisplay.cs
Normal file
63
WaveshareUARTFingerprintSensor.Sample/DataDisplay.cs
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
87
WaveshareUARTFingerprintSensor.Sample/EntryDialog.cs
Normal file
87
WaveshareUARTFingerprintSensor.Sample/EntryDialog.cs
Normal file
@@ -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<ustring, bool> _validator;
|
||||
private bool _success;
|
||||
private string _title;
|
||||
private string _errorMessage;
|
||||
|
||||
public EntryDialog(string title, Func<ustring, bool> 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");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<TUIManager>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
274
WaveshareUARTFingerprintSensor.Sample/TUIManager.cs
Normal file
274
WaveshareUARTFingerprintSensor.Sample/TUIManager.cs
Normal file
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -27,6 +27,8 @@
|
||||
<IsWebBootstrapper>false</IsWebBootstrapper>
|
||||
<UseApplicationTrust>false</UseApplicationTrust>
|
||||
<BootstrapperEnabled>true</BootstrapperEnabled>
|
||||
<NuGetPackageImportStamp>
|
||||
</NuGetPackageImportStamp>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
@@ -48,6 +50,9 @@
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<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>
|
||||
</Reference>
|
||||
<Reference Include="Swan, Version=3.0.0.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Unosquare.Swan.3.0.0\lib\net461\Swan.dll</HintPath>
|
||||
</Reference>
|
||||
@@ -80,6 +85,9 @@
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Net.Http" />
|
||||
<Reference Include="System.Xml" />
|
||||
<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>
|
||||
</Reference>
|
||||
<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>
|
||||
</Reference>
|
||||
@@ -91,8 +99,11 @@
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="DataDisplay.cs" />
|
||||
<Compile Include="EntryDialog.cs" />
|
||||
<Compile Include="Program.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="TUIManager.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="App.config" />
|
||||
@@ -117,4 +128,11 @@
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<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')" />
|
||||
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
|
||||
<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>
|
||||
</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'))" />
|
||||
</Target>
|
||||
</Project>
|
||||
@@ -1,10 +1,14 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="Microsoft.NETFramework.ReferenceAssemblies" version="1.0.0" targetFramework="net472" developmentDependency="true" />
|
||||
<package id="Microsoft.NETFramework.ReferenceAssemblies.net472" version="1.0.0" targetFramework="net472" developmentDependency="true" />
|
||||
<package id="NStack.Core" version="0.14.0" targetFramework="net472" />
|
||||
<package id="System.Buffers" version="4.5.1" targetFramework="net472" />
|
||||
<package id="System.Memory" version="4.5.4" targetFramework="net472" />
|
||||
<package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net472" />
|
||||
<package id="System.Runtime.CompilerServices.Unsafe" version="4.5.3" targetFramework="net472" />
|
||||
<package id="System.ValueTuple" version="4.5.0" targetFramework="net472" />
|
||||
<package id="Terminal.Gui" version="0.90.3" targetFramework="net472" />
|
||||
<package id="Unosquare.Raspberry.Abstractions" version="0.4.1" targetFramework="net472" />
|
||||
<package id="Unosquare.Raspberry.IO" version="0.27.1" targetFramework="net472" />
|
||||
<package id="Unosquare.Swan" version="3.0.0" targetFramework="net472" />
|
||||
|
||||
Reference in New Issue
Block a user