1
0
mirror of https://github.com/DarthAffe/RGB.NET.git synced 2025-12-12 17:48:31 +00:00

Allowed to use custom serial-connection implementations for WS281X-devices.

This commit is contained in:
Darth Affe 2020-09-07 00:18:52 +02:00
parent 33f896e17d
commit cb83135c62
7 changed files with 193 additions and 38 deletions

View File

@ -9,7 +9,7 @@ namespace RGB.NET.Devices.WS281X.Arduino
/// <summary>
/// Represents the update-queue performing updates for arduino WS2812 devices.
/// </summary>
public class ArduinoWS2812USBUpdateQueue : SerialPortUpdateQueue<byte[]>
public class ArduinoWS2812USBUpdateQueue : SerialConnectionUpdateQueue<byte[]>
{
#region Constants
@ -33,8 +33,8 @@ namespace RGB.NET.Devices.WS281X.Arduino
/// <param name="updateTrigger">The update trigger used by this queue.</param>
/// <param name="portName">The name of the serial-port to connect to.</param>
/// <param name="baudRate">The baud-rate used by the serial-connection.</param>
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
}
/// <inheritdoc />
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);
}

View File

@ -16,15 +16,20 @@ namespace RGB.NET.Devices.WS281X.Arduino
{
#region Properties & Fields
/// <summary>
/// Gets the serial-connection used for the device.
/// </summary>
public ISerialConnection SerialConnection { get; }
/// <summary>
/// Gets the name of the serial-port to connect to.
/// </summary>
public string Port { get; }
public string Port => SerialConnection?.Port;
/// <summary>
/// Gets the baud-rate used by the serial-connection.
/// </summary>
public int BaudRate { get; set; } = 115200;
public int BaudRate => SerialConnection?.BaudRate ?? 0;
/// <summary>
/// Gets or sets the name used by this device.
@ -39,10 +44,20 @@ namespace RGB.NET.Devices.WS281X.Arduino
/// <summary>
/// Initializes a new instance of the <see cref="ArduinoWS281XDeviceDefinition"/> class.
/// </summary>
/// <param name="port">The name of the serial-port to connect to.</param>
public ArduinoWS281XDeviceDefinition(string port)
/// <param name="serialConnection">The serial connection used for the device.</param>
public ArduinoWS281XDeviceDefinition(ISerialConnection serialConnection)
{
this.Port = port;
this.SerialConnection = serialConnection;
}
/// <summary>
/// Initializes a new instance of the <see cref="ArduinoWS281XDeviceDefinition"/> class.
/// </summary>
/// <param name="port">The name of the serial-port to connect to.</param>
/// <param name="baudRate">The baud-rate of the serial-connection.</param>
public ArduinoWS281XDeviceDefinition(string port, int baudRate = 115200)
{
SerialConnection = new SerialPortConnection(port, baudRate);
}
#endregion
@ -52,7 +67,7 @@ namespace RGB.NET.Devices.WS281X.Arduino
/// <inheritdoc />
public IEnumerable<IRGBDevice> 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)

View File

@ -8,7 +8,7 @@ namespace RGB.NET.Devices.WS281X.Bitwizard
/// <summary>
/// Represents the update-queue performing updates for a bitwizard WS2812 device.
/// </summary>
public class BitwizardWS2812USBUpdateQueue : SerialPortUpdateQueue<string>
public class BitwizardWS2812USBUpdateQueue : SerialConnectionUpdateQueue<string>
{
#region Constructors
@ -19,8 +19,8 @@ namespace RGB.NET.Devices.WS281X.Bitwizard
/// <param name="updateTrigger">The update trigger used by this queue.</param>
/// <param name="portName">The name of the serial-port to connect to.</param>
/// <param name="baudRate">The baud-rate used by the serial-connection.</param>
public BitwizardWS2812USBUpdateQueue(IDeviceUpdateTrigger updateTrigger, string portName, int baudRate = 115200)
: base(updateTrigger, portName, baudRate)
public BitwizardWS2812USBUpdateQueue(IDeviceUpdateTrigger updateTrigger, ISerialConnection serialConnection)
: base(updateTrigger, serialConnection)
{ }
#endregion

View File

@ -16,15 +16,20 @@ namespace RGB.NET.Devices.WS281X.Bitwizard
{
#region Properties & Fields
/// <summary>
/// Gets the serial-connection used for the device.
/// </summary>
public ISerialConnection SerialConnection { get; }
/// <summary>
/// Gets the name of the serial-port to connect to.
/// </summary>
public string Port { get; }
public string Port => SerialConnection?.Port;
/// <summary>
/// Gets the baud-rate used by the serial-connection.
/// </summary>
public int BaudRate { get; set; } = 115200;
public int BaudRate => SerialConnection?.BaudRate ?? 0;
/// <summary>
/// Gets or sets the name used by this device.
@ -43,10 +48,20 @@ namespace RGB.NET.Devices.WS281X.Bitwizard
/// <summary>
/// Initializes a new instance of the <see cref="BitwizardWS281XDeviceDefinition"/> class.
/// </summary>
/// <param name="port">The name of the serial-port to connect to.</param>
public BitwizardWS281XDeviceDefinition(string port)
/// <param name="serialConnection">The serial connection used for the device.</param>
public BitwizardWS281XDeviceDefinition(ISerialConnection serialConnection)
{
this.Port = port;
this.SerialConnection = serialConnection;
}
/// <summary>
/// Initializes a new instance of the <see cref="BitwizardWS281XDeviceDefinition"/> class.
/// </summary>
/// <param name="port">The name of the serial-port to connect to.</param>
/// <param name="baudRate">The baud-rate of the serial-connection.</param>
public BitwizardWS281XDeviceDefinition(string port, int baudRate = 115200)
{
SerialConnection = new SerialPortConnection(port, baudRate);
}
#endregion
@ -56,7 +71,7 @@ namespace RGB.NET.Devices.WS281X.Bitwizard
/// <inheritdoc />
public IEnumerable<IRGBDevice> 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);

View File

@ -0,0 +1,62 @@
using System;
namespace RGB.NET.Devices.WS281X
{
/// <summary>
/// Represents a generic serial connection.
/// </summary>
public interface ISerialConnection : IDisposable
{
/// <summary>
/// Gets the COM-port used by the serial connection.
/// </summary>
string Port { get; }
/// <summary>
/// Gets the baud-rate used by the serial connection.
/// </summary>
int BaudRate { get; }
/// <summary>
/// Gets the connection-status of the serial connection.
/// <c>true</c> if connected; otherwise <c>false</c>.
/// </summary>
bool IsOpen { get; }
/// <summary>
/// Opens the serial connection.
/// </summary>
void Open();
/// <summary>
/// Discards the in-buffer of the serial connection.
/// </summary>
void DiscardInBuffer();
/// <summary>
/// Reads a single byte from the serial connection
/// </summary>
/// <returns>The byte read.</returns>
byte ReadByte();
/// <summary>
/// Blocks till the provided char is received from the serial connection.
/// </summary>
/// <param name="target">The target-character to read to.</param>
void ReadTo(char target);
/// <summary>
/// Writes the provided data to the serial connection.
/// </summary>
/// <param name="buffer">The buffer containing the data to write.</param>
/// <param name="offset">The offset of the data in the buffer.</param>
/// <param name="length">The amount of data to write.</param>
void Write(byte[] buffer, int offset, int length);
/// <summary>
/// Write the provided text to the serial connection followed by a line break.
/// </summary>
/// <param name="line">The text to write.</param>
void WriteLine(string line);
}
}

View File

@ -0,0 +1,63 @@
using System.IO.Ports;
namespace RGB.NET.Devices.WS281X
{
/// <inheritdoc />
/// <summary>
/// Represents a serial-connection using the default microsoft serial-port implementation.
/// </summary>
public class SerialPortConnection : ISerialConnection
{
#region Properties & Fields
/// <summary>
/// The <see cref="System.IO.Ports.SerialPort"/> used for the connection.
/// </summary>
public SerialPort SerialPort { get; }
/// <inheritdoc />
public string Port => SerialPort.PortName;
/// <inheritdoc />
public int BaudRate => SerialPort.BaudRate;
/// <inheritdoc />
public bool IsOpen => SerialPort.IsOpen;
#endregion
#region Constructors
public SerialPortConnection(string port, int baudRate)
{
SerialPort = new SerialPort(port, baudRate);
}
#endregion
#region Methods
/// <inheritdoc />
public void Open() => SerialPort.Open();
/// <inheritdoc />
public void DiscardInBuffer() => SerialPort.DiscardInBuffer();
/// <inheritdoc />
public byte ReadByte() => (byte)SerialPort.ReadByte();
/// <inheritdoc />
public void ReadTo(char target) => SerialPort.ReadTo(target.ToString());
/// <inheritdoc />
public void Write(byte[] buffer, int offset, int length) => SerialPort.Write(buffer, offset, length);
/// <inheritdoc />
public void WriteLine(string line) => SerialPort.WriteLine(line);
/// <inheritdoc />
public void Dispose() => SerialPort.Dispose();
#endregion
}
}

View File

@ -9,7 +9,7 @@ namespace RGB.NET.Devices.WS281X
/// Represents a update queue for serial devices.
/// </summary>
/// <typeparam name="TData">The type of data sent through the serial connection.</typeparam>
public abstract class SerialPortUpdateQueue<TData> : UpdateQueue
public abstract class SerialConnectionUpdateQueue<TData> : UpdateQueue
{
#region Properties & Fields
@ -17,12 +17,12 @@ namespace RGB.NET.Devices.WS281X
/// Gets or sets the prompt to wait for between sending commands.
/// </summary>
// ReSharper disable once AutoPropertyCanBeMadeGetOnly.Global
protected string Prompt { get; set; } = ">";
protected char Prompt { get; set; } = '>';
/// <summary>
/// Gets the serial port used by this queue.
/// </summary>
protected SerialPort SerialPort { get; }
protected ISerialConnection SerialConnection { get; }
#endregion
@ -30,15 +30,15 @@ namespace RGB.NET.Devices.WS281X
/// <inheritdoc />
/// <summary>
/// Initializes a new instance of the <see cref="SerialPortUpdateQueue{TData}"/> class.
/// Initializes a new instance of the <see cref="SerialConnectionUpdateQueue{TData}"/> class.
/// </summary>
/// <param name="updateTrigger">The update trigger used by this queue.</param>
/// <param name="portName">The name of the serial-port to connect to.</param>
/// <param name="baudRate">The baud-rate used by the serial-connection.</param>
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();
}
/// <inheritdoc />
@ -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.
/// </summary>
/// <param name="command">The command to be sent.</param>
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
}