// ReSharper disable MemberCanBePrivate.Global using System; using System.Collections.Generic; using System.Collections.ObjectModel; using RGB.NET.Core; using RGB.NET.Devices.WS281X.NodeMCU; namespace RGB.NET.Devices.WS281X { /// /// /// Represents a device provider responsible for WS2812B- and WS2811-Led-devices. /// // ReSharper disable once InconsistentNaming public class WS281XDeviceProvider : IRGBDeviceProvider { #region Properties & Fields private static WS281XDeviceProvider _instance; /// /// Gets the singleton instance. /// public static WS281XDeviceProvider Instance => _instance ?? new WS281XDeviceProvider(); /// public bool IsInitialized { get; private set; } /// public IEnumerable Devices { get; private set; } /// public bool HasExclusiveAccess => false; /// /// Gets a list of all defined device-definitions. /// // ReSharper disable once CollectionNeverUpdated.Global // ReSharper disable once ReturnTypeCanBeEnumerable.Global public List DeviceDefinitions { get; } = new List(); /// /// The used to trigger the updates for corsair devices. /// public DeviceUpdateTrigger UpdateTrigger { 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() { if (_instance != null) throw new InvalidOperationException($"There can be only one instance of type {nameof(WS281XDeviceProvider)}"); _instance = this; UpdateTrigger = new DeviceUpdateTrigger(); } #endregion #region Methods /// /// Adds the given to this device-provider. /// /// The to add. // ReSharper disable once UnusedMember.Global public void AddDeviceDefinition(IWS281XDeviceDefinition deviceDefinition) => DeviceDefinitions.Add(deviceDefinition); /// public bool Initialize(RGBDeviceType loadFilter = RGBDeviceType.Unknown, bool exclusiveAccessIfPossible = false, bool throwExceptions = false) { IsInitialized = false; try { UpdateTrigger?.Stop(); List devices = new List(); foreach (IWS281XDeviceDefinition deviceDefinition in DeviceDefinitions) { try { devices.AddRange(deviceDefinition.CreateDevices(UpdateTrigger)); } catch { if (throwExceptions) throw; } } UpdateTrigger?.Start(); Devices = new ReadOnlyCollection(devices); IsInitialized = true; } catch { if (throwExceptions) throw; return false; } return true; } /// public void ResetDevices() { foreach (IRGBDevice device in Devices) if (device is NodeMCUWS2812USBDevice nodemcuDevice) nodemcuDevice.UpdateQueue.ResetDevice(); } /// public void Dispose() { try { UpdateTrigger?.Dispose(); } catch { /* at least we tried */} DeviceDefinitions.Clear(); } #endregion } }