2021-02-24 09:05:11 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Windows.Controls;
|
|
|
|
|
using MassiveKnob.Plugin.SerialDevice.Settings;
|
|
|
|
|
using MassiveKnob.Plugin.SerialDevice.Worker;
|
2021-02-28 11:55:23 +01:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2021-02-24 09:05:11 +01:00
|
|
|
|
|
|
|
|
|
namespace MassiveKnob.Plugin.SerialDevice.Devices
|
|
|
|
|
{
|
|
|
|
|
public class SerialDevice : IMassiveKnobDevice
|
|
|
|
|
{
|
|
|
|
|
public Guid DeviceId { get; } = new Guid("65255f25-d8f6-426b-8f12-cf03c44a1bf5");
|
|
|
|
|
public string Name { get; } = "Serial device";
|
|
|
|
|
public string Description { get; } = "A Serial (USB) device which implements the Massive Knob Protocol.";
|
|
|
|
|
|
2021-02-28 11:55:23 +01:00
|
|
|
|
public IMassiveKnobDeviceInstance Create(ILogger logger)
|
2021-02-24 09:05:11 +01:00
|
|
|
|
{
|
2021-02-28 11:55:23 +01:00
|
|
|
|
return new Instance(logger);
|
2021-02-24 09:05:11 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private class Instance : IMassiveKnobDeviceInstance
|
|
|
|
|
{
|
2021-02-28 11:55:23 +01:00
|
|
|
|
private readonly ILogger logger;
|
2021-02-24 09:05:11 +01:00
|
|
|
|
private IMassiveKnobDeviceContext deviceContext;
|
|
|
|
|
private SerialDeviceSettings settings;
|
|
|
|
|
private SerialWorker worker;
|
2021-02-28 11:55:23 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Instance(ILogger logger)
|
|
|
|
|
{
|
|
|
|
|
this.logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-24 09:05:11 +01:00
|
|
|
|
|
|
|
|
|
public void Initialize(IMassiveKnobDeviceContext context)
|
|
|
|
|
{
|
|
|
|
|
deviceContext = context;
|
|
|
|
|
settings = deviceContext.GetSettings<SerialDeviceSettings>();
|
|
|
|
|
|
2021-02-28 11:55:23 +01:00
|
|
|
|
worker = new SerialWorker(context, logger);
|
2021-02-24 09:05:11 +01:00
|
|
|
|
ApplySettings();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
worker.Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void ApplySettings()
|
|
|
|
|
{
|
2021-02-28 11:55:23 +01:00
|
|
|
|
worker.Connect(settings.PortName, settings.BaudRate, settings.DtrEnable);
|
2021-02-24 09:05:11 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public UserControl CreateSettingsControl()
|
|
|
|
|
{
|
|
|
|
|
var viewModel = new SerialDeviceSettingsViewModel(settings);
|
|
|
|
|
viewModel.PropertyChanged += (sender, args) =>
|
|
|
|
|
{
|
|
|
|
|
if (!viewModel.IsSettingsProperty(args.PropertyName))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
deviceContext.SetSettings(settings);
|
|
|
|
|
ApplySettings();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return new SerialDeviceSettingsView(viewModel);
|
|
|
|
|
}
|
2021-02-24 19:35:01 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void SetAnalogOutput(int analogOutputIndex, byte value)
|
|
|
|
|
{
|
2021-02-28 13:01:43 +01:00
|
|
|
|
worker.SetAnalogOutput(analogOutputIndex, value);
|
2021-02-24 19:35:01 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-02-28 13:01:43 +01:00
|
|
|
|
public void SetDigitalOutput(int digitalOutputIndex, bool on)
|
2021-02-24 19:35:01 +01:00
|
|
|
|
{
|
2021-02-28 13:01:43 +01:00
|
|
|
|
worker.SetDigitalOutput(digitalOutputIndex, on);
|
2021-02-24 19:35:01 +01:00
|
|
|
|
}
|
2021-02-24 09:05:11 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|