using System; using System.Collections.Generic; using System.Linq; using RGB.NET.Core; using Sanford.Multimedia.Midi; namespace RGB.NET.Devices.Novation { /// /// /// /// Represents a generic Novation-device. (launchpad). /// public abstract class NovationRGBDevice : AbstractRGBDevice, INovationRGBDevice where TDeviceInfo : NovationRGBDeviceInfo { #region Properties & Fields private readonly OutputDevice _outputDevice; private readonly TDeviceInfo _deviceInfo; /// /// /// Gets information about the . /// public override TDeviceInfo DeviceInfo => _deviceInfo; #endregion #region Constructors /// /// Initializes a new instance of the class. /// /// The generic information provided by Novation for the device. protected NovationRGBDevice(TDeviceInfo info) { this._deviceInfo = info; _outputDevice = new OutputDevice(info.DeviceId); } #endregion #region Methods /// /// Initializes the device. /// public void Initialize() { InitializeLayout(); if (Size == Size.Invalid) { Rectangle ledRectangle = new Rectangle(this.Select(x => x.LedRectangle)); Size = ledRectangle.Size + new Size(ledRectangle.Location.X, ledRectangle.Location.Y); } } /// /// Initializes the and of the device. /// protected abstract void InitializeLayout(); /// protected override void UpdateLeds(IEnumerable ledsToUpdate) { List leds = ledsToUpdate.Where(x => x.Color.A > 0).ToList(); if (leds.Count > 0) { foreach (Led led in leds) { NovationLedId ledId = (NovationLedId)led.CustomData; int color = ConvertColor(led.Color); SendMessage(ledId.GetStatus(), ledId.GetId(), color); } } } /// /// Resets the back top default. /// public virtual void Reset() => SendMessage(0xB0, 0, 0); /// /// Convert a to its novation-representation depending on the of the . /// /// The to convert. /// The novation-representation of the . protected virtual int ConvertColor(Color color) { switch (_deviceInfo.ColorCapabilities) { case NovationColorCapabilities.RGB: return ConvertColorFull(color); case NovationColorCapabilities.LimitedRG: return ConvertColorLimited(color); default: return 0; } } /// /// Convert a to its novation-representation depending on the of the . /// The conversion uses the full rgb-range. /// /// The to convert. /// The novation-representation of the . protected virtual int ConvertColorFull(Color color) { //TODO DarthAffe 16.08.2017: How are colors for full rgb devices encoded? return 0; } /// /// Convert a to its novation-representation depending on the of the . /// The conversion uses only a limited amount of colors (3 red, 3 yellow, 3 green). /// /// The to convert. /// The novation-representation of the . protected virtual int ConvertColorLimited(Color color) { if ((color.Hue >= 330) || (color.Hue < 30)) return (int)Math.Ceiling(color.Value * 3); // red with brightness 1, 2 or 3 if ((color.Hue >= 30) && (color.Hue < 90)) // yellow with brightness 17, 34 or 51 return (int)Math.Ceiling(color.Value * 3) * 17; if ((color.Hue >= 90) && (color.Hue < 150)) // green with brightness 16, 32 or 48 return (int)Math.Ceiling(color.Value * 3) * 16; return 0; } /// /// Sends a message to the . /// /// The status-code of the message. /// The first data-package of the message. /// The second data-package of the message. protected virtual void SendMessage(int status, int data1, int data2) { ShortMessage shortMessage = new ShortMessage(Convert.ToByte(status), Convert.ToByte(data1), Convert.ToByte(data2)); _outputDevice.SendShort(shortMessage.Message); } /// /// public override void Dispose() { Reset(); _outputDevice.Dispose(); base.Dispose(); } #endregion } }