iocommander input and virtual input

This commit is contained in:
meelstorm
2025-07-21 12:07:33 +02:00
parent cd70088d9a
commit e761b24c95
21 changed files with 418 additions and 21 deletions

View File

@@ -0,0 +1,19 @@
using VisionBuilder.UI.IOCommander.Interfaces;
using VisionBuilder.UI.IOCommander.Settings;
namespace VisionBuilder.UI.IOCommander;
public class DefaultIOCommanderFactory: IIOCommanderFactory
{
private readonly IOCommanderSettings _settings;
public DefaultIOCommanderFactory(IOCommanderSettings settings)
{
_settings = settings;
}
public IIOCommander Create()
{
return new IOCommander(_settings.Port);
}
}

View File

@@ -1,9 +1,10 @@
using System.Collections;
using System.IO.Ports;
using VisionBuilder.UI.IOCommander.Interfaces;
namespace VisionBuilder.UI.IOCommander;
public class IOCommander
public class IOCommander : IIOCommander
{
SerialPort _port;

View File

@@ -0,0 +1,10 @@
using System.Collections;
namespace VisionBuilder.UI.IOCommander.Interfaces;
public interface IIOCommander
{
BitArray PinState { get; }
event Action<int, bool> OnPinChanged;
void SetPins(int pin, bool state);
}

View File

@@ -0,0 +1,6 @@
namespace VisionBuilder.UI.IOCommander.Interfaces;
public interface IIOCommanderFactory
{
public IIOCommander Create();
}

View File

@@ -22,6 +22,7 @@ public static class ModuleExtensions
self.Bind<IOCommanderModule,IVisionBuilderModule>().To<IOCommanderModule>().InSingletonScope();
self.Bind<IOCommanderProgramModule, IVisionBuilderModule>().To<IOCommanderProgramModule>().InSingletonScope();
self.Bind<IIOCommanderDebugViewService>().To<NoDebugView>().InSingletonScope();
self.Bind<IIOCommanderFactory>().To<DefaultIOCommanderFactory>().InSingletonScope();
return self;
}

View File

@@ -14,22 +14,54 @@ public class IOCommanderCameraModule: IVisionBuilderModule
private readonly IOCommanderCameraSettings _cameraSettings;
private readonly IRecognitionControl _recognitionControl;
private readonly MainWindowVM _mainWindowVm;
private readonly SingleCameraVM _cameraVm;
public IOCommanderCameraModule(IOCommanderModule mainModule, IOCommanderSettings settings, IOCommanderCameraSettings cameraSettings, IRecognitionControl recognitionControl, MainWindowVM mainWindowVm)
public IOCommanderCameraModule(IOCommanderModule mainModule, IOCommanderSettings settings, IOCommanderCameraSettings cameraSettings, IRecognitionControl recognitionControl, MainWindowVM mainWindowVm, SingleCameraVM cameraVm)
{
_mainModule = mainModule;
_settings = settings;
_cameraSettings = cameraSettings;
_recognitionControl = recognitionControl;
_mainWindowVm = mainWindowVm;
_cameraVm = cameraVm;
_recognitionControl.ImageProcessed += _recognitionControl_ImageProcessed;
_recognitionControl.SessionStarted += _recognitionControl_SessionStarted;
_recognitionControl.SessionEnded += _recognitionControl_SessionEnded;
mainModule.OnPinChangedEvent += MainModule_OnPinChangedEvent;
}
private void ProcessCommand(EGPIOCommand command)
{
switch (command)
{
case EGPIOCommand.Start:
if(_cameraVm.StartCommand.CanExecute(null))
{
_cameraVm.StartCommand.Execute(null);
}
break;
case EGPIOCommand.Stop:
if(_cameraVm.StopCommand.CanExecute(null))
{
_cameraVm.StopCommand.Execute(null);
}
break;
default:
throw new ArgumentOutOfRangeException(nameof(command), command, null);
}
}
private void MainModule_OnPinChangedEvent(int arg1, bool arg2)
{
var @event = _cameraSettings.GPIOCommands.FirstOrDefault(x => x.Pin == arg1 && x.OnHigh == arg2);
if (@event != null)
{
ProcessCommand(@event.Command);
}
}
private void EmitEvent(EProcessingEvent @event)
{

View File

@@ -12,28 +12,32 @@ namespace VisionBuilder.UI.IOCommander.Modules;
public class IOCommanderModule:IVisionBuilderModule
{
private readonly IOCommanderSettings _settings;
private readonly IIOCommanderFactory _factory;
private readonly IIOCommanderDebugViewService _debugViewService;
private IOCommander? _ioCommander;
public IOCommanderModule(IOCommanderSettings settings, IIOCommanderDebugViewService debugViewService)
private IIOCommander? _ioCommander;
public IOCommanderModule(IOCommanderSettings settings, IIOCommanderFactory factory, IIOCommanderDebugViewService debugViewService)
{
_settings = settings;
_factory = factory;
_debugViewService = debugViewService;
}
public void InitializeModule()
{
if (!_settings.EmulationMode)
{
_ioCommander = new IOCommander(_settings.Port);
_ioCommander.OnPinChanged += OnPinChanged;
}
_ioCommander = _factory.Create();
_ioCommander.OnPinChanged += OnPinChanged;
if(_settings.ShowDebugView)_debugViewService.ShowDebugView();
if (_settings.ShowDebugView)_debugViewService.ShowDebugView();
}
ConcurrentDictionary<int, bool> _pinStates = new ConcurrentDictionary<int, bool>();
public event Action<int, bool>? OnPinChangedEvent
{
add => _ioCommander!.OnPinChanged += value;
remove => _ioCommander!.OnPinChanged -= value;
}
private void OnPinChanged(int arg1, bool arg2)
{
_pinStates.TryAdd(arg1, arg2);

View File

@@ -8,7 +8,6 @@ public class IOCommanderSettings: ISettings
{
public IOCommanderCameraSettings[] Cameras { get; }
public int PulseLength { get; set; } = 100;
public bool EmulationMode { get; set; } = true;
public bool ShowDebugView { get; set; } = false;
public string Port { get; set; }="COM3";
@@ -27,7 +26,6 @@ public class IOCommanderSettings: ISettings
{
settings.RegisterSimple(this, () => Port, "IOCommander", nameof(Port));
settings.RegisterSimple(this, () => PulseLength, "IOCommander", nameof(PulseLength));
settings.RegisterSimple(this, () => EmulationMode, "IOCommander", nameof(EmulationMode));
settings.RegisterSimple(this, () => ShowDebugView, "IOCommander", nameof(ShowDebugView));
foreach (IOCommanderProcessingEvent @event in GlobalEvents)