using System; using System.Collections.Generic; using System.IO; using System.Linq; using RGB.NET.Core; using RGB.NET.Core.Layout; using Sanford.Multimedia.Midi; namespace RGB.NET.Devices.Novation { /// /// /// Represents a generic Novation-device. (launchpad). /// public abstract class NovationRGBDevice : AbstractRGBDevice { #region Properties & Fields private readonly OutputDevice _outputDevice; private readonly NovationRGBDeviceInfo _deviceInfo; /// /// /// Gets information about the . /// public override IRGBDeviceInfo DeviceInfo => _deviceInfo; #endregion #region Constructors /// /// Initializes a new instance of the class. /// /// The generic information provided by Novation for the device. protected NovationRGBDevice(NovationRGBDeviceInfo info) { _deviceInfo = info; _outputDevice = new OutputDevice(info.DeviceId); } #endregion #region Methods /// /// Initializes the device. /// internal void Initialize() { InitializeLayout(); if (InternalSize == null) { Rectangle ledRectangle = new Rectangle(this.Select(x => x.LedRectangle)); InternalSize = ledRectangle.Size + new Size(ledRectangle.Location.X, ledRectangle.Location.Y); } } /// /// Initializes the and of the device. /// protected abstract void InitializeLayout(); /// /// Applies the given layout. /// /// The file containing the layout. /// The name of the layout used to get the images of the leds. /// The path images for this device are collected in. protected void ApplyLayoutFromFile(string layoutPath, string imageLayout, string imageBasePath) { DeviceLayout layout = DeviceLayout.Load(layoutPath); if (layout != null) { LedImageLayout ledImageLayout = layout.LedImageLayouts.FirstOrDefault(x => string.Equals(x.Layout, imageLayout, StringComparison.OrdinalIgnoreCase)); InternalSize = new Size(layout.Width, layout.Height); if (layout.Leds != null) foreach (LedLayout layoutLed in layout.Leds) { if (Enum.TryParse(layoutLed.Id, true, out NovationLedIds ledId)) { if (LedMapping.TryGetValue(new NovationLedId(this, ledId), out Led led)) { led.LedRectangle.Location.X = layoutLed.X; led.LedRectangle.Location.Y = layoutLed.Y; led.LedRectangle.Size.Width = layoutLed.Width; led.LedRectangle.Size.Height = layoutLed.Height; led.Shape = layoutLed.Shape; led.ShapeData = layoutLed.ShapeData; LedImage image = ledImageLayout?.LedImages.FirstOrDefault(x => x.Id == layoutLed.Id); led.Image = (!string.IsNullOrEmpty(image?.Image)) ? new Uri(Path.Combine(imageBasePath, image.Image), UriKind.Absolute) : new Uri(Path.Combine(imageBasePath, "Missing.png"), UriKind.Absolute); } } } } } /// 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 = led.Id as NovationLedId; if (ledId == null) continue; int color = ConvertColor(led.Color); SendMessage(ledId.LedId.GetStatus(), ledId.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 } }