示例#1
0
        public override void Apply(ref List <LedColor> colors, PortIdentifier port, ICacheProvider cache)
        {
            var ledCount = cache.GetDeviceConfig(port).LedCount;

            if (ledCount == colors.Count)
            {
                return;
            }

            if (Config.LerpType == LerpType.Smooth)
            {
                var newColors = new List <LedColor>();
                var gradient  = new LedColorGradient(colors, ledCount - 1);

                for (var i = 0; i < ledCount; i++)
                {
                    newColors.Add(gradient.GetColor(i));
                }

                colors = newColors;
            }
            else if (Config.LerpType == LerpType.Nearest)
            {
                var newColors = new List <LedColor>();
                for (var i = 0; i < ledCount; i++)
                {
                    var idx = (int)Math.Round((i / (ledCount - 1d)) * (colors.Count - 1d));
                    newColors.Add(colors[idx]);
                }

                colors = newColors;
            }
        }
示例#2
0
        public override IDictionary <PortIdentifier, List <LedColor> > GenerateColors(List <PortIdentifier> ports, ICacheProvider cache)
        {
            var result = new Dictionary <PortIdentifier, List <LedColor> >();

            foreach (var port in ports)
            {
                var device = cache.GetDeviceConfig(port);
                var colors = new List <LedColor>();
                for (var i = 0; i < device.Zones.Length; i++)
                {
                    if (i >= Config.Effects.Count)
                    {
                        continue;
                    }

                    var effect   = Config.Effects[i];
                    var zoneSize = device.Zones[i];

                    var zoneColors = default(List <LedColor>);
                    try { zoneColors = effect.GenerateColors(zoneSize, cache); }
                    catch (NotImplementedException) { }

                    if (zoneColors == null)
                    {
                        zoneColors = Enumerable.Repeat(new LedColor(), zoneSize).ToList();
                    }

                    if (zoneColors.Count != zoneSize)
                    {
                        var gradient = new LedColorGradient(colors, zoneSize - 1);

                        colors.Clear();
                        for (var j = 0; j < zoneSize; j++)
                        {
                            colors.Add(gradient.GetColor(j));
                        }
                    }

                    colors.AddRange(zoneColors);
                }

                result.Add(port, colors);
            }

            return(result);
        }
示例#3
0
 public LedSpectrum(LedColorGradient colorGradient)
 {
     _colorGradient = colorGradient;
 }
示例#4
0
        public bool DeviceRgbTimerCallback()
        {
            void ApplyConfig(IDictionary <PortIdentifier, List <LedColor> > colorMap)
            {
                foreach (var port in colorMap.Keys.ToList())
                {
                    var config = _cache.GetPortConfig(port);
                    if (config == null)
                    {
                        continue;
                    }

                    var colors   = colorMap[port];
                    var ledCount = _cache.GetDeviceConfig(port).LedCount;
                    var zones    = _cache.GetDeviceConfig(port).Zones;

                    switch (config.LedCountHandling)
                    {
                    case LedCountHandling.Lerp:
                    {
                        if (ledCount == colors.Count)
                        {
                            break;
                        }

                        var newColors = new List <LedColor>();
                        var gradient  = new LedColorGradient(colors, ledCount - 1);

                        for (var i = 0; i < ledCount; i++)
                        {
                            newColors.Add(gradient.GetColor(i));
                        }

                        colors = newColors;
                        break;
                    }

                    case LedCountHandling.Nearest:
                    {
                        if (ledCount == colors.Count)
                        {
                            break;
                        }

                        var newColors = new List <LedColor>();
                        for (var i = 0; i < ledCount; i++)
                        {
                            var idx = (int)Math.Round((i / (ledCount - 1d)) * (colors.Count - 1d));
                            newColors.Add(colors[idx]);
                        }

                        colors = newColors;
                        break;
                    }

                    case LedCountHandling.Wrap:
                        if (colors.Count <= ledCount)
                        {
                            break;
                        }

                        var wrapCount   = (int)Math.Floor(colors.Count / (double)ledCount);
                        var startOffset = (wrapCount - 1) * ledCount;
                        var remainder   = colors.Count - wrapCount * ledCount;

                        colors = colors.Skip(colors.Count - remainder)
                                 .Concat(colors.Skip(startOffset + remainder).Take(ledCount - remainder))
                                 .ToList();
                        break;

                    case LedCountHandling.Trim:
                        if (ledCount < colors.Count)
                        {
                            colors.RemoveRange(ledCount, colors.Count - ledCount);
                        }
                        break;

                    case LedCountHandling.Copy:
                        while (ledCount > colors.Count)
                        {
                            colors.AddRange(colors.Take(ledCount - colors.Count).ToList());
                        }
                        break;

                    case LedCountHandling.DoNothing:
                    default:
                        break;
                    }

                    if (config.LedRotation != null || config.LedReverse != null)
                    {
                        var offset    = 0;
                        var newColors = new List <LedColor>();
                        for (int i = 0; i < zones.Length; i++)
                        {
                            var zoneColors = colors.Skip(offset).Take(zones[i]);

                            if (i < config.LedRotation?.Length && config.LedRotation[i] > 0)
                            {
                                zoneColors = zoneColors.RotateLeft(config.LedRotation[i]);
                            }
                            if (i < config.LedReverse?.Length && config.LedReverse[i])
                            {
                                zoneColors = zoneColors.Reverse();
                            }

                            offset += zones[i];
                            newColors.AddRange(zoneColors);
                        }

                        if (newColors.Count < colors.Count)
                        {
                            newColors.AddRange(colors.Skip(offset));
                        }

                        colors = newColors;
                    }

                    colorMap[port] = colors;
                }
            }

            foreach (var profile in _config.Profiles)
            {
                var effect = _pluginStore
                             .Get <IEffectBase>(profile)
                             .FirstOrDefault(e => e.IsEnabled(_cache.AsReadOnly()));
                if (effect == null)
                {
                    continue;
                }

                IDictionary <PortIdentifier, List <LedColor> > colorMap;
                string effectType;

                try
                {
                    colorMap   = effect.GenerateColors(profile.Ports, _cache.AsReadOnly());
                    effectType = effect.EffectType;
                }
                catch (Exception e)
                {
                    Logger.Fatal("{0} failed with {1}", effect.GetType().Name, e);
                    colorMap = profile.Ports.ToDictionary(p => p, _ => new List <LedColor>()
                    {
                        new LedColor(255, 0, 0)
                    });
                    effectType = "Full";
                }

                if (colorMap == null)
                {
                    continue;
                }

                ApplyConfig(colorMap);

                lock (_deviceManager)
                {
                    foreach (var(port, colors) in colorMap)
                    {
                        if (colors == null)
                        {
                            continue;
                        }

                        if (colors.ContentsEqual(_cache.GetPortColors(port)))
                        {
                            continue;
                        }

                        var controller = _deviceManager.GetController(port);
                        var effectByte = controller?.GetEffectByte(effectType);
                        if (effectByte == null)
                        {
                            continue;
                        }

                        _cache.StorePortColors(port, colors);
                        controller.SetRgb(port.Id, effectByte.Value, colors);
                    }
                }
            }

            return(true);
        }
示例#5
0
        public bool DeviceRgbTimerCallback()
        {
            void ApplyConfig(IDictionary <PortIdentifier, List <LedColor> > colorMap)
            {
                foreach (var port in colorMap.Keys.ToList())
                {
                    var config = _cache.GetPortConfig(port);
                    if (config == null)
                    {
                        continue;
                    }

                    var colors = colorMap[port];

                    if (config.LedRotation > 0)
                    {
                        colors = colors.Skip(config.LedRotation).Concat(colors.Take(config.LedRotation)).ToList();
                    }
                    if (config.LedReverse)
                    {
                        colors.Reverse();
                    }

                    switch (config.LedCountHandling)
                    {
                    case LedCountHandling.Lerp:
                    {
                        if (config.LedCount == colors.Count)
                        {
                            break;
                        }

                        var newColors = new List <LedColor>();
                        var gradient  = new LedColorGradient(colors, config.LedCount - 1);

                        for (var i = 0; i < config.LedCount; i++)
                        {
                            newColors.Add(gradient.GetColor(i));
                        }

                        colors = newColors;
                        break;
                    }

                    case LedCountHandling.Nearest:
                    {
                        if (config.LedCount == colors.Count)
                        {
                            break;
                        }

                        var newColors = new List <LedColor>();
                        for (var i = 0; i < config.LedCount; i++)
                        {
                            var idx = (int)Math.Round((i / (config.LedCount - 1d)) * (colors.Count - 1d));
                            newColors.Add(colors[idx]);
                        }

                        colors = newColors;
                        break;
                    }

                    case LedCountHandling.Wrap:
                        if (config.LedCount < colors.Count)
                        {
                            break;
                        }

                        var remainder = colors.Count % config.LedCount;
                        colors = colors.Skip(colors.Count - remainder)
                                 .Concat(colors.Take(colors.Count - remainder).Skip(colors.Count - config.LedCount))
                                 .ToList();
                        break;

                    case LedCountHandling.Trim:
                        if (config.LedCount < colors.Count)
                        {
                            colors.RemoveRange(config.LedCount, colors.Count - config.LedCount);
                        }
                        break;

                    case LedCountHandling.Copy:
                        while (config.LedCount > colors.Count)
                        {
                            colors.AddRange(colors.Take(config.LedCount - colors.Count).ToList());
                        }
                        break;

                    case LedCountHandling.DoNothing:
                    default:
                        break;
                    }

                    colorMap[port] = colors;
                }
            }

            foreach (var profile in _configManager.CurrentConfig.Profiles)
            {
                var effects = _effectManager.GetEffects(profile.Guid);
                var effect  = effects?.FirstOrDefault(e => e.IsEnabled(_cache.AsReadOnly()));
                if (effect == null)
                {
                    continue;
                }

                IDictionary <PortIdentifier, List <LedColor> > colorMap;
                string effectType;

                try
                {
                    colorMap   = effect.GenerateColors(profile.Ports, _cache.AsReadOnly());
                    effectType = effect.EffectType;
                }
                catch (Exception e)
                {
                    Logger.Fatal("{0} failed with {1}", effect.GetType().Name, e);
                    colorMap = profile.Ports.ToDictionary(p => p, _ => new List <LedColor>()
                    {
                        new LedColor(255, 0, 0)
                    });
                    effectType = "Full";
                }

                if (colorMap == null)
                {
                    continue;
                }

                ApplyConfig(colorMap);

                lock (_deviceManager)
                {
                    foreach (var(port, colors) in colorMap)
                    {
                        if (colors == null)
                        {
                            continue;
                        }

                        var controller = _deviceManager.GetController(port);
                        var effectByte = controller?.GetEffectByte(effectType);
                        if (effectByte == null)
                        {
                            continue;
                        }

                        controller.SetRgb(port.Id, effectByte.Value, colors);
                    }
                }
            }

            return(true);
        }