io commander
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
using System.Runtime;
|
||||
using VisionBuilder.UI.Common;
|
||||
using VisionBuilder.UI.Common.Commands;
|
||||
using VisionBuilder.UI.Common.RecipeProcessing;
|
||||
using VisionBuilder.UI.Common.ViewModel;
|
||||
using VisionBuilder.UI.IOCommander.Settings;
|
||||
|
||||
namespace VisionBuilder.UI.IOCommander.Modules;
|
||||
|
||||
public class IOCommanderCameraModule: IVisionBuilderModule
|
||||
{
|
||||
private readonly IOCommanderModule _mainModule;
|
||||
private readonly IOCommanderSettings _settings;
|
||||
private readonly IOCommanderCameraSettings _cameraSettings;
|
||||
private readonly IRecognitionControl _recognitionControl;
|
||||
private readonly MainWindowVM _mainWindowVm;
|
||||
|
||||
public IOCommanderCameraModule(IOCommanderModule mainModule, IOCommanderSettings settings, IOCommanderCameraSettings cameraSettings, IRecognitionControl recognitionControl, MainWindowVM mainWindowVm)
|
||||
{
|
||||
_mainModule = mainModule;
|
||||
_settings = settings;
|
||||
_cameraSettings = cameraSettings;
|
||||
_recognitionControl = recognitionControl;
|
||||
_mainWindowVm = mainWindowVm;
|
||||
|
||||
_recognitionControl.ImageProcessed += _recognitionControl_ImageProcessed;
|
||||
_recognitionControl.SessionStarted += _recognitionControl_SessionStarted;
|
||||
_recognitionControl.SessionEnded += _recognitionControl_SessionEnded;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
private void EmitEvent(EProcessingEvent @event)
|
||||
{
|
||||
|
||||
var @eventActions = _cameraSettings.ProcessingEvents.First(x => x.Event == @event);
|
||||
var actions = @eventActions.Actions;
|
||||
if (_mainWindowVm.TestMode)
|
||||
{
|
||||
var testModeActions = _cameraSettings.TestModeOverride;
|
||||
var testModePins = testModeActions.Select(x => x.Pin).ToList();
|
||||
actions = actions.Where(x => !testModePins.Contains(x.Pin)).ToList();
|
||||
actions.AddRange(testModeActions);
|
||||
}
|
||||
|
||||
|
||||
foreach (var action in actions)
|
||||
{
|
||||
_mainModule.SetOutput(action.Pin, !action.Low);
|
||||
if (action.Pulse)
|
||||
{
|
||||
Task.Run(async () =>
|
||||
{
|
||||
await Task.Delay(_settings.PulseLength);
|
||||
_mainModule.SetOutput(action.Pin, action.Low);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void InitializeModule()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private void _recognitionControl_SessionEnded(SessionEndedEvent obj)
|
||||
{
|
||||
EmitEvent(EProcessingEvent.SessionEnded);
|
||||
|
||||
}
|
||||
|
||||
private void _recognitionControl_SessionStarted(SessionStartedEvent obj)
|
||||
{
|
||||
EmitEvent(EProcessingEvent.SessionStarted);
|
||||
}
|
||||
|
||||
private void _recognitionControl_ImageProcessed(Common.Commands.ImageProcessedEvent obj)
|
||||
{
|
||||
|
||||
if (obj.HasError)
|
||||
{
|
||||
EmitEvent(EProcessingEvent.ErrorOccurred);
|
||||
}
|
||||
else
|
||||
{
|
||||
EmitEvent(EProcessingEvent.GoodOccured);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
62
VisionBuilder.UI.IOCommander/Modules/IOCommanderModule.cs
Normal file
62
VisionBuilder.UI.IOCommander/Modules/IOCommanderModule.cs
Normal file
@@ -0,0 +1,62 @@
|
||||
using System.Collections.Concurrent;
|
||||
using VisionBuilder.UI.Common;
|
||||
using VisionBuilder.UI.Common.Commands;
|
||||
using VisionBuilder.UI.Common.RecipeProcessing;
|
||||
using VisionBuilder.UI.Common.ViewModel;
|
||||
using VisionBuilder.UI.IOCommander.Interfaces;
|
||||
using VisionBuilder.UI.IOCommander.Settings;
|
||||
|
||||
namespace VisionBuilder.UI.IOCommander.Modules;
|
||||
|
||||
|
||||
public class IOCommanderModule:IVisionBuilderModule
|
||||
{
|
||||
private readonly IOCommanderSettings _settings;
|
||||
private readonly IIOCommanderDebugViewService _debugViewService;
|
||||
|
||||
private IOCommander? _ioCommander;
|
||||
public IOCommanderModule(IOCommanderSettings settings, IIOCommanderDebugViewService debugViewService)
|
||||
{
|
||||
_settings = settings;
|
||||
_debugViewService = debugViewService;
|
||||
}
|
||||
|
||||
public void InitializeModule()
|
||||
{
|
||||
if (!_settings.EmulationMode)
|
||||
{
|
||||
_ioCommander = new IOCommander(_settings.Port);
|
||||
_ioCommander.OnPinChanged += OnPinChanged;
|
||||
}
|
||||
|
||||
if(_settings.ShowDebugView)_debugViewService.ShowDebugView();
|
||||
|
||||
|
||||
}
|
||||
ConcurrentDictionary<int, bool> _pinStates = new ConcurrentDictionary<int, bool>();
|
||||
private void OnPinChanged(int arg1, bool arg2)
|
||||
{
|
||||
_pinStates.TryAdd(arg1, arg2);
|
||||
_debugViewService.SetInputPinStatus(arg1, arg2);
|
||||
}
|
||||
|
||||
public void SetOutput(int pin, bool value)
|
||||
{
|
||||
if (_ioCommander != null)
|
||||
{
|
||||
_ioCommander.SetPins(pin, value);
|
||||
}
|
||||
_debugViewService.SetOutputPinStatus(pin,value);
|
||||
}
|
||||
|
||||
public bool GetInput(int pin)
|
||||
{
|
||||
if (_ioCommander == null) return false;
|
||||
var found=_pinStates.TryGetValue(pin, out var value);
|
||||
if (found)
|
||||
{
|
||||
return value;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
using VisionBuilder.UI.Common;
|
||||
using VisionBuilder.UI.Common.ViewModel;
|
||||
using VisionBuilder.UI.IOCommander.Settings;
|
||||
|
||||
namespace VisionBuilder.UI.IOCommander.Modules;
|
||||
|
||||
public class IOCommanderProgramModule: IVisionBuilderModule
|
||||
{
|
||||
private readonly IOCommanderSettings _settings;
|
||||
private readonly IOCommanderModule _mainModule;
|
||||
private readonly MainWindowVM _mainWindowVm;
|
||||
|
||||
public IOCommanderProgramModule(IOCommanderSettings settings, IOCommanderModule mainModule, MainWindowVM mainWindowVm)
|
||||
{
|
||||
_settings = settings;
|
||||
_mainModule = mainModule;
|
||||
_mainWindowVm = mainWindowVm;
|
||||
_mainWindowVm.ProgramStarted += _mainWindowVm_ProgramStarted;
|
||||
_mainWindowVm.ProgramClosed += _mainWindowVm_ProgramClosed;
|
||||
}
|
||||
|
||||
private void EmitEvent(EProcessingEvent @event)
|
||||
{
|
||||
var actions = _settings.GlobalEvents.First(x => x.Event == @event);
|
||||
foreach (var action in actions.Actions)
|
||||
{
|
||||
_mainModule.SetOutput(action.Pin,!action.Low);
|
||||
if (action.Pulse)
|
||||
{
|
||||
Task.Run(async () =>
|
||||
{
|
||||
await Task.Delay(_settings.PulseLength);
|
||||
_mainModule.SetOutput(action.Pin, action.Low);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void _mainWindowVm_ProgramClosed()
|
||||
{
|
||||
EmitEvent(EProcessingEvent.ProgramEnded);
|
||||
}
|
||||
|
||||
|
||||
private void _mainWindowVm_ProgramStarted()
|
||||
{
|
||||
EmitEvent(EProcessingEvent.ProgramStarted);
|
||||
}
|
||||
|
||||
public void InitializeModule()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user