diff --git a/RGB.NET.Devices.WS281X/Arduino/ArduinoWS2812USBUpdateQueue.cs b/RGB.NET.Devices.WS281X/Arduino/ArduinoWS2812USBUpdateQueue.cs
index e9fea46..6b2c1cb 100644
--- a/RGB.NET.Devices.WS281X/Arduino/ArduinoWS2812USBUpdateQueue.cs
+++ b/RGB.NET.Devices.WS281X/Arduino/ArduinoWS2812USBUpdateQueue.cs
@@ -9,7 +9,7 @@ namespace RGB.NET.Devices.WS281X.Arduino
///
/// Represents the update-queue performing updates for arduino WS2812 devices.
///
- public class ArduinoWS2812USBUpdateQueue : SerialPortUpdateQueue
+ public class ArduinoWS2812USBUpdateQueue : SerialConnectionUpdateQueue
{
#region Constants
@@ -33,8 +33,8 @@ namespace RGB.NET.Devices.WS281X.Arduino
/// The update trigger used by this queue.
/// The name of the serial-port to connect to.
/// The baud-rate used by the serial-connection.
- public ArduinoWS2812USBUpdateQueue(IDeviceUpdateTrigger updateTrigger, string portName, int baudRate = 115200)
- : base(updateTrigger, portName, baudRate)
+ public ArduinoWS2812USBUpdateQueue(IDeviceUpdateTrigger updateTrigger, ISerialConnection serialConnection)
+ : base(updateTrigger, serialConnection)
{ }
#endregion
@@ -45,7 +45,7 @@ namespace RGB.NET.Devices.WS281X.Arduino
protected override void OnStartup(object sender, CustomUpdateData customData)
{
base.OnStartup(sender, customData);
-
+
SendCommand(ASK_PROMPT_COMMAND); // Get initial prompt
}
@@ -75,26 +75,26 @@ namespace RGB.NET.Devices.WS281X.Arduino
}
///
- protected override void SendCommand(byte[] command) => SerialPort.Write(command, 0, command.Length);
+ protected override void SendCommand(byte[] command) => SerialConnection.Write(command, 0, command.Length);
internal IEnumerable<(int channel, int ledCount)> GetChannels()
{
- if (!SerialPort.IsOpen)
- SerialPort.Open();
+ if (!SerialConnection.IsOpen)
+ SerialConnection.Open();
- SerialPort.DiscardInBuffer();
+ SerialConnection.DiscardInBuffer();
SendCommand(ASK_PROMPT_COMMAND);
- SerialPort.ReadTo(Prompt);
+ SerialConnection.ReadTo(Prompt);
SendCommand(COUNT_COMMAND);
- int channelCount = SerialPort.ReadByte();
+ int channelCount = SerialConnection.ReadByte();
for (int i = 1; i <= channelCount; i++)
{
- SerialPort.ReadTo(Prompt);
+ SerialConnection.ReadTo(Prompt);
byte[] channelLedCountCommand = { (byte)((i << 4) | COUNT_COMMAND[0]) };
SendCommand(channelLedCountCommand);
- int ledCount = SerialPort.ReadByte();
+ int ledCount = SerialConnection.ReadByte();
if (ledCount > 0)
yield return (i, ledCount);
}
diff --git a/RGB.NET.Devices.WS281X/Arduino/ArduinoWS281XDeviceDefinition.cs b/RGB.NET.Devices.WS281X/Arduino/ArduinoWS281XDeviceDefinition.cs
index 0690926..ff1b88f 100644
--- a/RGB.NET.Devices.WS281X/Arduino/ArduinoWS281XDeviceDefinition.cs
+++ b/RGB.NET.Devices.WS281X/Arduino/ArduinoWS281XDeviceDefinition.cs
@@ -16,15 +16,20 @@ namespace RGB.NET.Devices.WS281X.Arduino
{
#region Properties & Fields
+ ///
+ /// Gets the serial-connection used for the device.
+ ///
+ public ISerialConnection SerialConnection { get; }
+
///
/// Gets the name of the serial-port to connect to.
///
- public string Port { get; }
+ public string Port => SerialConnection?.Port;
///
/// Gets the baud-rate used by the serial-connection.
///
- public int BaudRate { get; set; } = 115200;
+ public int BaudRate => SerialConnection?.BaudRate ?? 0;
///
/// Gets or sets the name used by this device.
@@ -39,10 +44,20 @@ namespace RGB.NET.Devices.WS281X.Arduino
///
/// Initializes a new instance of the class.
///
- /// The name of the serial-port to connect to.
- public ArduinoWS281XDeviceDefinition(string port)
+ /// The serial connection used for the device.
+ public ArduinoWS281XDeviceDefinition(ISerialConnection serialConnection)
{
- this.Port = port;
+ this.SerialConnection = serialConnection;
+ }
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ /// The name of the serial-port to connect to.
+ /// The baud-rate of the serial-connection.
+ public ArduinoWS281XDeviceDefinition(string port, int baudRate = 115200)
+ {
+ SerialConnection = new SerialPortConnection(port, baudRate);
}
#endregion
@@ -52,7 +67,7 @@ namespace RGB.NET.Devices.WS281X.Arduino
///
public IEnumerable CreateDevices(IDeviceUpdateTrigger updateTrigger)
{
- ArduinoWS2812USBUpdateQueue queue = new ArduinoWS2812USBUpdateQueue(updateTrigger, Port, BaudRate);
+ ArduinoWS2812USBUpdateQueue queue = new ArduinoWS2812USBUpdateQueue(updateTrigger, SerialConnection);
IEnumerable<(int channel, int ledCount)> channels = queue.GetChannels();
int counter = 0;
foreach ((int channel, int ledCount) in channels)
diff --git a/RGB.NET.Devices.WS281X/Bitwizard/BitwizardWS2812USBUpdateQueue.cs b/RGB.NET.Devices.WS281X/Bitwizard/BitwizardWS2812USBUpdateQueue.cs
index 5b70008..dd6acc0 100644
--- a/RGB.NET.Devices.WS281X/Bitwizard/BitwizardWS2812USBUpdateQueue.cs
+++ b/RGB.NET.Devices.WS281X/Bitwizard/BitwizardWS2812USBUpdateQueue.cs
@@ -8,7 +8,7 @@ namespace RGB.NET.Devices.WS281X.Bitwizard
///
/// Represents the update-queue performing updates for a bitwizard WS2812 device.
///
- public class BitwizardWS2812USBUpdateQueue : SerialPortUpdateQueue
+ public class BitwizardWS2812USBUpdateQueue : SerialConnectionUpdateQueue
{
#region Constructors
@@ -19,8 +19,8 @@ namespace RGB.NET.Devices.WS281X.Bitwizard
/// The update trigger used by this queue.
/// The name of the serial-port to connect to.
/// The baud-rate used by the serial-connection.
- public BitwizardWS2812USBUpdateQueue(IDeviceUpdateTrigger updateTrigger, string portName, int baudRate = 115200)
- : base(updateTrigger, portName, baudRate)
+ public BitwizardWS2812USBUpdateQueue(IDeviceUpdateTrigger updateTrigger, ISerialConnection serialConnection)
+ : base(updateTrigger, serialConnection)
{ }
#endregion
diff --git a/RGB.NET.Devices.WS281X/Bitwizard/BitwizardWS281XDeviceDefinition.cs b/RGB.NET.Devices.WS281X/Bitwizard/BitwizardWS281XDeviceDefinition.cs
index b769519..b7f61b3 100644
--- a/RGB.NET.Devices.WS281X/Bitwizard/BitwizardWS281XDeviceDefinition.cs
+++ b/RGB.NET.Devices.WS281X/Bitwizard/BitwizardWS281XDeviceDefinition.cs
@@ -16,15 +16,20 @@ namespace RGB.NET.Devices.WS281X.Bitwizard
{
#region Properties & Fields
+ ///
+ /// Gets the serial-connection used for the device.
+ ///
+ public ISerialConnection SerialConnection { get; }
+
///
/// Gets the name of the serial-port to connect to.
///
- public string Port { get; }
+ public string Port => SerialConnection?.Port;
///
/// Gets the baud-rate used by the serial-connection.
///
- public int BaudRate { get; set; } = 115200;
+ public int BaudRate => SerialConnection?.BaudRate ?? 0;
///
/// Gets or sets the name used by this device.
@@ -43,10 +48,20 @@ namespace RGB.NET.Devices.WS281X.Bitwizard
///
/// Initializes a new instance of the class.
///
- /// The name of the serial-port to connect to.
- public BitwizardWS281XDeviceDefinition(string port)
+ /// The serial connection used for the device.
+ public BitwizardWS281XDeviceDefinition(ISerialConnection serialConnection)
{
- this.Port = port;
+ this.SerialConnection = serialConnection;
+ }
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ /// The name of the serial-port to connect to.
+ /// The baud-rate of the serial-connection.
+ public BitwizardWS281XDeviceDefinition(string port, int baudRate = 115200)
+ {
+ SerialConnection = new SerialPortConnection(port, baudRate);
}
#endregion
@@ -56,7 +71,7 @@ namespace RGB.NET.Devices.WS281X.Bitwizard
///
public IEnumerable CreateDevices(IDeviceUpdateTrigger updateTrigger)
{
- BitwizardWS2812USBUpdateQueue queue = new BitwizardWS2812USBUpdateQueue(updateTrigger, Port, BaudRate);
+ BitwizardWS2812USBUpdateQueue queue = new BitwizardWS2812USBUpdateQueue(updateTrigger, SerialConnection);
string name = Name ?? $"Bitwizard WS2812 USB ({Port})";
BitwizardWS2812USBDevice device = new BitwizardWS2812USBDevice(new BitwizardWS2812USBDeviceInfo(name), queue);
device.Initialize(StripLength);
diff --git a/RGB.NET.Devices.WS281X/Generic/ISerialConnection.cs b/RGB.NET.Devices.WS281X/Generic/ISerialConnection.cs
new file mode 100644
index 0000000..5851e7a
--- /dev/null
+++ b/RGB.NET.Devices.WS281X/Generic/ISerialConnection.cs
@@ -0,0 +1,62 @@
+using System;
+
+namespace RGB.NET.Devices.WS281X
+{
+ ///
+ /// Represents a generic serial connection.
+ ///
+ public interface ISerialConnection : IDisposable
+ {
+ ///
+ /// Gets the COM-port used by the serial connection.
+ ///
+ string Port { get; }
+
+ ///
+ /// Gets the baud-rate used by the serial connection.
+ ///
+ int BaudRate { get; }
+
+ ///
+ /// Gets the connection-status of the serial connection.
+ /// true if connected; otherwise false.
+ ///
+ bool IsOpen { get; }
+
+ ///
+ /// Opens the serial connection.
+ ///
+ void Open();
+
+ ///
+ /// Discards the in-buffer of the serial connection.
+ ///
+ void DiscardInBuffer();
+
+ ///
+ /// Reads a single byte from the serial connection
+ ///
+ /// The byte read.
+ byte ReadByte();
+
+ ///
+ /// Blocks till the provided char is received from the serial connection.
+ ///
+ /// The target-character to read to.
+ void ReadTo(char target);
+
+ ///
+ /// Writes the provided data to the serial connection.
+ ///
+ /// The buffer containing the data to write.
+ /// The offset of the data in the buffer.
+ /// The amount of data to write.
+ void Write(byte[] buffer, int offset, int length);
+
+ ///
+ /// Write the provided text to the serial connection followed by a line break.
+ ///
+ /// The text to write.
+ void WriteLine(string line);
+ }
+}
diff --git a/RGB.NET.Devices.WS281X/Generic/SerialPortConnection.cs b/RGB.NET.Devices.WS281X/Generic/SerialPortConnection.cs
new file mode 100644
index 0000000..fded9dc
--- /dev/null
+++ b/RGB.NET.Devices.WS281X/Generic/SerialPortConnection.cs
@@ -0,0 +1,63 @@
+using System.IO.Ports;
+
+namespace RGB.NET.Devices.WS281X
+{
+ ///
+ ///
+ /// Represents a serial-connection using the default microsoft serial-port implementation.
+ ///
+ public class SerialPortConnection : ISerialConnection
+ {
+ #region Properties & Fields
+
+ ///
+ /// The used for the connection.
+ ///
+ public SerialPort SerialPort { get; }
+
+ ///
+ public string Port => SerialPort.PortName;
+
+ ///
+ public int BaudRate => SerialPort.BaudRate;
+
+ ///
+ public bool IsOpen => SerialPort.IsOpen;
+
+ #endregion
+
+ #region Constructors
+
+ public SerialPortConnection(string port, int baudRate)
+ {
+ SerialPort = new SerialPort(port, baudRate);
+ }
+
+ #endregion
+
+ #region Methods
+
+ ///
+ public void Open() => SerialPort.Open();
+
+ ///
+ public void DiscardInBuffer() => SerialPort.DiscardInBuffer();
+
+ ///
+ public byte ReadByte() => (byte)SerialPort.ReadByte();
+
+ ///
+ public void ReadTo(char target) => SerialPort.ReadTo(target.ToString());
+
+ ///
+ public void Write(byte[] buffer, int offset, int length) => SerialPort.Write(buffer, offset, length);
+
+ ///
+ public void WriteLine(string line) => SerialPort.WriteLine(line);
+
+ ///
+ public void Dispose() => SerialPort.Dispose();
+
+ #endregion
+ }
+}
diff --git a/RGB.NET.Devices.WS281X/Generic/SerialPortUpdateQueue.cs b/RGB.NET.Devices.WS281X/Generic/SerialPortUpdateQueue.cs
index 21504cf..0f343da 100644
--- a/RGB.NET.Devices.WS281X/Generic/SerialPortUpdateQueue.cs
+++ b/RGB.NET.Devices.WS281X/Generic/SerialPortUpdateQueue.cs
@@ -9,7 +9,7 @@ namespace RGB.NET.Devices.WS281X
/// Represents a update queue for serial devices.
///
/// The type of data sent through the serial connection.
- public abstract class SerialPortUpdateQueue : UpdateQueue
+ public abstract class SerialConnectionUpdateQueue : UpdateQueue
{
#region Properties & Fields
@@ -17,12 +17,12 @@ namespace RGB.NET.Devices.WS281X
/// Gets or sets the prompt to wait for between sending commands.
///
// ReSharper disable once AutoPropertyCanBeMadeGetOnly.Global
- protected string Prompt { get; set; } = ">";
+ protected char Prompt { get; set; } = '>';
///
/// Gets the serial port used by this queue.
///
- protected SerialPort SerialPort { get; }
+ protected ISerialConnection SerialConnection { get; }
#endregion
@@ -30,15 +30,15 @@ namespace RGB.NET.Devices.WS281X
///
///
- /// Initializes a new instance of the class.
+ /// Initializes a new instance of the class.
///
/// The update trigger used by this queue.
/// The name of the serial-port to connect to.
/// The baud-rate used by the serial-connection.
- internal SerialPortUpdateQueue(IDeviceUpdateTrigger updateTrigger, string portName, int baudRate = 115200)
+ internal SerialConnectionUpdateQueue(IDeviceUpdateTrigger updateTrigger, ISerialConnection serialConnection)
: base(updateTrigger)
{
- SerialPort = new SerialPort(portName, baudRate);
+ SerialConnection = serialConnection;
}
#endregion
@@ -50,10 +50,10 @@ namespace RGB.NET.Devices.WS281X
{
base.OnStartup(sender, customData);
- if (!SerialPort.IsOpen)
- SerialPort.Open();
+ if (!SerialConnection.IsOpen)
+ SerialConnection.Open();
- SerialPort.DiscardInBuffer();
+ SerialConnection.DiscardInBuffer();
}
///
@@ -61,7 +61,7 @@ namespace RGB.NET.Devices.WS281X
{
foreach (TData command in GetCommands(dataSet))
{
- SerialPort.ReadTo(Prompt);
+ SerialConnection.ReadTo(Prompt);
SendCommand(command);
}
}
@@ -78,7 +78,7 @@ namespace RGB.NET.Devices.WS281X
/// This most likely needs to be overwritten if the data-type isn't string.
///
/// The command to be sent.
- protected virtual void SendCommand(TData command) => SerialPort.WriteLine((command as string) ?? string.Empty);
+ protected virtual void SendCommand(TData command) => SerialConnection.WriteLine((command as string) ?? string.Empty);
#endregion
}