// ReSharper disable MemberCanBePrivate.Global using System; using System.Collections.Generic; using System.Threading; using RGB.NET.Core; namespace RGB.NET.Devices.WS281X; /// /// /// Represents a device provider responsible for WS2812B- and WS2811-Led-devices. /// // ReSharper disable once InconsistentNaming // ReSharper disable once UnusedType.Global public sealed class WS281XDeviceProvider : AbstractRGBDeviceProvider { #region Properties & Fields // ReSharper disable once InconsistentNaming private static readonly Lock _lock = new(); private static WS281XDeviceProvider? _instance; /// /// Gets the singleton instance. /// public static WS281XDeviceProvider Instance { get { lock (_lock) return _instance ?? new WS281XDeviceProvider(); } } /// /// Gets a list of all defined device-definitions. /// // ReSharper disable once CollectionNeverUpdated.Global // ReSharper disable once ReturnTypeCanBeEnumerable.Global public List DeviceDefinitions { get; } = []; #endregion #region Constructors /// /// Initializes a new instance of the class. /// /// Thrown if this constructor is called even if there is already an instance of this class. public WS281XDeviceProvider() { lock (_lock) { if (_instance != null) throw new InvalidOperationException($"There can be only one instance of type {nameof(WS281XDeviceProvider)}"); _instance = this; } } #endregion #region Methods /// /// Adds the specified to this device-provider. /// /// The to add. // ReSharper disable once UnusedMember.Global public void AddDeviceDefinition(IWS281XDeviceDefinition deviceDefinition) => DeviceDefinitions.Add(deviceDefinition); /// protected override void InitializeSDK() { } /// protected override IEnumerable LoadDevices() { int i = 0; foreach (IWS281XDeviceDefinition deviceDefinition in DeviceDefinitions) { IDeviceUpdateTrigger updateTrigger = GetUpdateTrigger(i++); foreach (IRGBDevice device in deviceDefinition.CreateDevices(updateTrigger)) yield return device; } } /// protected override void Dispose(bool disposing) { lock (_lock) { base.Dispose(disposing); DeviceDefinitions.Clear(); _instance = null; } } #endregion }