Esempio n. 1
0
        /// <summary>
        /// Gets the default plugins used when a new plugin is created
        /// </summary>
        #region Default Plugins
        /// <summary>
        /// Trys to get a configured Light Setup Plugin (no specific ordering),
        /// failing that it will attempt to create a new Light Setup Plugin from the available types (no specific ordering)
        /// </summary>
        /// <returns>A list with one Light Setup Plugin</returns>
        /// <exception cref="ArgumentNullException"></exception>
        public SerializableInterfaceList<ILightSetupPlugin> DefaultLightSetupPlugins()
        {
            SerializableInterfaceList<ILightSetupPlugin> lightSetupPlugins = new SerializableInterfaceList<ILightSetupPlugin>();
            Type type = AvailableLightSetupPlugins.FirstOrDefault();
            if (type == null)
            {
                AfterglowRuntime.Logger.Fatal("No ILightSetupPlugin's have been loaded, please check the install and try again");
            }
            else
            {
                ILightSetupPlugin plugin = null;
                if (this.Profiles.Any())
                {
                    plugin = this.Profiles.Last().LightSetupPlugin;
                }
                else
                {
                    plugin = Activator.CreateInstance(type) as ILightSetupPlugin;
                }

                //Add at least one light
                if (!plugin.Lights.Any())
                {
                    plugin.Lights.Add(new Light() { Id = 1, Index = 0, Height = 1, Width = 1 });
                }

                plugin.Id = this.GetNewId<ILightSetupPlugin>();
                lightSetupPlugins.Add(plugin);
            }

            return lightSetupPlugins;
        }
Esempio n. 2
0
        public ILightSetupPlugin SetLightSetupPlugin(Type pluginType)
        {
            ILightSetupPlugin plugin = Activator.CreateInstance(pluginType, new object[] { Table.Database.AddTable(), Logger, Runtime }) as ILightSetupPlugin;

            this.LightSetupPlugin = plugin;
            SaveToStorage(() => this.LightSetupPlugin, this.LightSetupPlugin);
            return(plugin);
        }
Esempio n. 3
0
        private LightSetup GetLightSetup(ILightSetupPlugin plugin)
        {
            LightSetup lightSetup = new LightSetup();

            if (plugin.Lights != null && plugin.Lights.Any())
            {
                //convert back to 2d array for setup
                lightSetup.LightRows = new List<LightRow>();

                lightSetup.NumberOfRows = plugin.Lights.Max(l => l.Top);
                lightSetup.NumberOfColumns = plugin.Lights.Max(l => l.Left);

                for (int row = 0; row <= lightSetup.NumberOfRows; row++)
                {
                    LightRow lightRow = new LightRow();
                    lightRow.RowIndex = row;
                    lightRow.LightColumns = new List<LightColumn>();
                    for (int column = 0; column <= lightSetup.NumberOfColumns; column++)
                    {
                        LightColumn lightColumn = new LightColumn();
                        lightColumn.ColumnIndex = column;
                        if (column == 0 || column == lightSetup.NumberOfColumns
                            || row == 0 || row == lightSetup.NumberOfRows)
                        {

                            Light light = (from l in plugin.Lights
                                           where l.Top == row
                                               && l.Left == column
                                           select l).FirstOrDefault();
                            if (light != null)
                            {
                                lightColumn.Id = light.Id.ToString();
                                lightColumn.Index = light.Index.ToString();

                                lightColumn.Enabled = !string.IsNullOrEmpty(lightColumn.Id);
                            }
                        }
                        else
                        {
                            lightColumn.Enabled = false;
                        }

                        lightRow.LightColumns.Add(lightColumn);
                    }
                    lightSetup.LightRows.Add(lightRow);
                }
            }
            return lightSetup;
        }
        private LightSetup GetLightSetup(ILightSetupPlugin plugin)
        {
            LightSetup lightSetup = new LightSetup();

            if (plugin.Lights != null && plugin.Lights.Any())
            {
                //convert back to 2d array for setup
                lightSetup.LightRows = new List <LightRow>();

                lightSetup.NumberOfRows    = plugin.Lights.Max(l => l.Top);
                lightSetup.NumberOfColumns = plugin.Lights.Max(l => l.Left);

                for (int row = 0; row <= lightSetup.NumberOfRows; row++)
                {
                    LightRow lightRow = new LightRow();
                    lightRow.RowIndex     = row;
                    lightRow.LightColumns = new List <LightColumn>();
                    for (int column = 0; column <= lightSetup.NumberOfColumns; column++)
                    {
                        LightColumn lightColumn = new LightColumn();
                        lightColumn.ColumnIndex = column;
                        if (column == 0 || column == lightSetup.NumberOfColumns ||
                            row == 0 || row == lightSetup.NumberOfRows)
                        {
                            Light light = (from l in plugin.Lights
                                           where l.Top == row &&
                                           l.Left == column
                                           select l).FirstOrDefault();
                            if (light != null)
                            {
                                lightColumn.Id    = light.Id.ToString();
                                lightColumn.Index = light.Index.ToString();

                                lightColumn.Enabled = !string.IsNullOrEmpty(lightColumn.Id);
                            }
                        }
                        else
                        {
                            lightColumn.Enabled = false;
                        }

                        lightRow.LightColumns.Add(lightColumn);
                    }
                    lightSetup.LightRows.Add(lightRow);
                }
            }
            return(lightSetup);
        }
Esempio n. 5
0
        public IDictionary <Core.Light, Core.PixelReader> Capture(ILightSetupPlugin lightSetup)
        {
            _graphics.CopyFromScreen(_dispBounds.Left, _dispBounds.Top, 0, 0, new Size(_dispBounds.Width, _dispBounds.Height));

            _fastBitmap = new FastBitmap(_img);


            GetCaptureSize();

            IDictionary <Core.Light, Core.PixelReader> dictionary = new Dictionary <Core.Light, Core.PixelReader>();

            foreach (Light light in lightSetup.GetLightsForBounds(_captureWidth, _captureHeight, _leftOffset, _topOffset))
            {
                dictionary[light] = new PixelReader(_fastBitmap, light.Region);
            }

            return(dictionary);
        }
Esempio n. 6
0
        public IDictionary <Core.Light, Core.PixelReader> Capture(ILightSetupPlugin lightSetup)
        {
            IDictionary <Core.Light, Core.PixelReader> dictionary = new Dictionary <Core.Light, Core.PixelReader>();

            if (_currentResponse != null)
            {
                _capturedImage = _currentResponse.CapturedBitmapAsImage;
                _fastBitmap    = new FastBitmap(_capturedImage);

                foreach (var light in lightSetup.GetLightsForBounds(_capturedImage.Width, _capturedImage.Height, 0, 0))
                {
                    dictionary[light] = new PixelReader(_fastBitmap, light.Region);
                }
            }
            if (_captures > 0 && _captures % 10 == 0)
            {
                Console.WriteLine("Time per capture: {0}", new TimeSpan(0, 0, 0, 0, (int)(DateTime.Now - _startTime).TotalMilliseconds / _captures));
            }

            return(dictionary);
        }
Esempio n. 7
0
        public IDictionary <Core.Light, Core.PixelReader> Capture(ILightSetupPlugin lightSetup)
        {
            IDictionary <Core.Light, Core.PixelReader> dictionary = new Dictionary <Core.Light, Core.PixelReader>();

            Capture.Interface.Screenshot response = null;

            if (_capturedProcess != null && _capturedProcess.CaptureInterface != null)
            {
                response = _capturedProcess.CaptureInterface.GetScreenshot();
            }

            if (response != null)
            {
                Interlocked.Increment(ref _captures);
            }

            Interlocked.Exchange(ref _currentResponse, response);

            if (_currentResponse != null)
            {
                using (System.IO.MemoryStream ms = new System.IO.MemoryStream(_currentResponse.CapturedBitmap))
                    _capturedImage = new Bitmap(ms);
                _fastBitmap = new FastBitmap(_capturedImage);

                foreach (var light in lightSetup.GetLightsForBounds(_capturedImage.Width, _capturedImage.Height, 0, 0))
                {
                    dictionary[light] = new PixelReader(_fastBitmap, light.Region);
                }
            }

            if (_captures > 0 && _captures % 10 == 0)
            {
                AfterglowRuntime.Logger.Debug("Time per capture: {0}", new TimeSpan(0, 0, 0, 0, (int)((DateTime.Now - _startTime).TotalMilliseconds / _captures)));
            }

            return(dictionary);
        }
        private IList <ILightSetupPlugin> GetLookupValues()
        {
            PropertyInfo         prop            = _profile.GetType().GetProperties().Where(p => p.Name == "LightSetupPlugin").FirstOrDefault();
            ConfigTableAttribute configAttribute = Attribute.GetCustomAttribute(prop, typeof(ConfigTableAttribute)) as ConfigTableAttribute;

            Type pluginType   = _profile.GetType();
            Type propertyType = prop.PropertyType;

            IEnumerable <Type> availableValues = null;

            if (configAttribute.RetrieveValuesFrom != null)
            {
                var member = pluginType.GetMember(configAttribute.RetrieveValuesFrom);
                if (member.Length > 0)
                {
                    if (member[0].MemberType == MemberTypes.Method)
                    {
                        MethodInfo mi = pluginType.GetMethod(configAttribute.RetrieveValuesFrom);

                        var propertyValue = mi.Invoke(_profile, null);

                        availableValues = propertyValue as IEnumerable <Type>;
                    }
                }
            }

            List <ILightSetupPlugin> result = new List <ILightSetupPlugin>();

            foreach (Type item in availableValues)
            {
                ILightSetupPlugin plugin = Activator.CreateInstance(item) as ILightSetupPlugin;
                result.Add(plugin);
            }

            return(result);
        }
Esempio n. 9
0
        public IDictionary<Core.Light, Core.PixelReader> Capture(ILightSetupPlugin lightSetup)
        {
            IDictionary<Core.Light, Core.PixelReader> dictionary = new Dictionary<Core.Light, Core.PixelReader>();

            if (_currentResponse != null)
            {
                _capturedImage = _currentResponse.CapturedBitmapAsImage;
                _fastBitmap = new FastBitmap(_capturedImage);

                foreach (var light in lightSetup.GetLightsForBounds(_capturedImage.Width, _capturedImage.Height, 0, 0))
                {
                    dictionary[light] = new PixelReader(_fastBitmap, light.Region);
                }
            }
            if (_captures > 0 && _captures % 10 == 0)
                Console.WriteLine("Time per capture: {0}", new TimeSpan(0, 0, 0, 0, (int)(DateTime.Now - _startTime).TotalMilliseconds / _captures));

            return dictionary;
        }
Esempio n. 10
0
        public IDictionary<Core.Light, Core.PixelReader> Capture(ILightSetupPlugin lightSetup)
        {
            IDictionary<Core.Light, Core.PixelReader> dictionary = new Dictionary<Core.Light, Core.PixelReader>();

            Capture.Interface.Screenshot response = null;

            if (_capturedProcess != null && _capturedProcess.CaptureInterface != null)
            {
                response = _capturedProcess.CaptureInterface.GetScreenshot();
            }

            if (response != null)
            {
                Interlocked.Increment(ref _captures);
            }

            Interlocked.Exchange(ref _currentResponse, response);
            
            if (_currentResponse != null)
            {    
                using (System.IO.MemoryStream ms = new System.IO.MemoryStream(_currentResponse.CapturedBitmap))
                    _capturedImage = new Bitmap(ms);
                _fastBitmap = new FastBitmap(_capturedImage);

                foreach (var light in lightSetup.GetLightsForBounds(_capturedImage.Width, _capturedImage.Height, 0, 0))
                {
                    dictionary[light] = new PixelReader(_fastBitmap, light.Region);
                }
            }

            if (_captures > 0 && _captures % 10 == 0)
            {
                AfterglowRuntime.Logger.Debug("Time per capture: {0}", new TimeSpan(0, 0, 0, 0, (int)((DateTime.Now - _startTime).TotalMilliseconds / _captures)));
            }

            return dictionary;
        }
Esempio n. 11
0
        private void MainLoop()
        {
            /*
             * 1. Capture regions
             * 2. Perform colour extraction
             * 3. Perform post processing
             * 4. Cleanup Captured Regions
             * 5. Perform Output(s)
             * 6. Perform timing waits / whatever
             * 7. Check for messages (stop etc)
             * */

            CurrentProfile.CapturePlugin.Start();
            CurrentProfile.ColourExtractionPlugin.Start();
            CurrentProfile.PostProcessPlugins.ToList().ForEach(p => p.Start());
            CurrentProfile.OutputPlugins.ToList().ForEach(o => o.Start());

            // TODO: until Logging is complete assign a default logger

            try
            {
                //int cycles = 0;
                while (_active)
                {
                    ILightSetupPlugin lightSetupPlugin = CurrentProfile.LightSetupPlugin;

                    foreach (var light in lightSetupPlugin.Lights)
                    {
                        light.OldLEDColour    = light.LEDColour;
                        light.OldSourceColour = light.SourceColour;
                    }

                    IDictionary <Light, PixelReader> ledSources = CurrentProfile.CapturePlugin.Capture(lightSetupPlugin);
                    try
                    {
                        foreach (var keyValue in ledSources)
                        {
                            keyValue.Key.SourceColour = CurrentProfile.ColourExtractionPlugin.Extract(keyValue.Key, keyValue.Value);
                            keyValue.Key.LEDColour    = keyValue.Key.SourceColour;
                        }
                    }
                    finally
                    {
                        foreach (var keyValue in ledSources)
                        {
                            keyValue.Value.Dispose();
                        }

                        CurrentProfile.CapturePlugin.ReleaseCapture();
                    }

                    foreach (var postProcessPlugin in CurrentProfile.PostProcessPlugins)
                    {
                        foreach (var led in lightSetupPlugin.Lights)
                        {
                            postProcessPlugin.Process(led);
                        }
                    }

                    CurrentProfile.OutputPlugins.ToList().ForEach(o => o.Output(lightSetupPlugin.Lights.ToList()));

                    // TODO: implement timing logic

                    //TODO implement debug logging
                    //cycles++;
                    //if (cycles % 5 == 0)
                    //{
                    //    Debug.WriteLine("");
                    //}
                }


                // Set all to black
                Thread.Sleep(50);

                CurrentProfile.LightSetupPlugin.Lights.ToList().ForEach(led => led.LEDColour = Color.Black);
                CurrentProfile.OutputPlugins.ToList().ForEach(o => o.Output(CurrentProfile.LightSetupPlugin.Lights.ToList()));
            }
            finally
            {
                CurrentProfile.CapturePlugin.Stop();
                CurrentProfile.ColourExtractionPlugin.Stop();
                CurrentProfile.PostProcessPlugins.ToList().ForEach(p => p.Stop());
                CurrentProfile.OutputPlugins.ToList().ForEach(o => o.Stop());
            }
        }
Esempio n. 12
0
        private void OutputLoop()
        {
            try
            {
                //Get Light Setup Plugin
                ILightSetupPlugin lightSetupPlugin = CurrentProfile.LightSetupPlugin;

                Stopwatch timer     = new Stopwatch();
                double    waitTicks = 1.0 / CurrentProfile.OutputFrequency;
                double    freq      = 1.0 / Stopwatch.Frequency;

                long lastUpdate = 0;

                // Initialise temporary lightData
                LightData lightData = new LightData(lightSetupPlugin.Lights.Count);

                while (Active)
                {
                    timer.Restart();

                    // If available, replace _currentLightData with _nextLightData
                    lock (sync)
                    {
                        if (lastUpdate < _nextLightData.Time)
                        {
                            var t = lightData;
                            lightData      = _nextLightData;
                            _nextLightData = t;
                            lastUpdate     = lightData.Time;
                        }
                    }

                    if (lightData != null)
                    {
                        // Run pre-output plugins
                        CurrentProfile.PreOutputPlugins.ToList().ForEach(po => po.PreOutput(lightSetupPlugin.Lights, lightData));

                        //Run Output Plugin(s)
                        CurrentProfile.OutputPlugins.ToList().ForEach(o => o.Output(lightSetupPlugin.Lights, lightData));
                    }

                    // Copy the last frame to _prevLightData
                    lock (previousSync)
                    {
                        Buffer.BlockCopy(lightData.ColourData, 0, _prevLightData.ColourData, 0, lightData.ColourData.Length);
                    }

                    // Throttle Output Frequency
                    while (timer.ElapsedTicks * freq < waitTicks)
                    {
                        Task.Delay(5);
                    }

                    _outputLoopFPS.Tick();
                }
            }
            catch (Exception e)
            {
                lock (sync)
                {
                    Active = false;
                    AfterglowRuntime.Logger.Error(e, "Afterglow Runtime - Output Loop");
                }
            }
            finally
            {
                // Allow plugins to clean up
                CurrentProfile.PreOutputPlugins.ForEach(po => po.Stop());
                CurrentProfile.OutputPlugins.ForEach(o => o.Stop());
            }
        }
Esempio n. 13
0
        private void CaptureLoop()
        {
            try
            {
                //Get Light Setup Plugin
                ILightSetupPlugin lightSetupPlugin = CurrentProfile.LightSetupPlugin;

                Stopwatch timer     = Stopwatch.StartNew();
                long      lastTicks = 0;
                double    waitTicks = 1.0 / CurrentProfile.CaptureFrequency;
                double    freq      = 1.0 / Stopwatch.Frequency;

                LightData newLightData = new LightData(_nextLightData);

                while (Active)
                {
                    lastTicks = timer.ElapsedTicks;

                    //Get screen segments
                    IDictionary <Light, PixelReader> ledSources = CurrentProfile.CapturePlugin.Capture(lightSetupPlugin);
                    newLightData.Time = timer.ElapsedTicks;
                    try
                    {
                        //Extract Colours from segments
                        int i = 0;
                        foreach (var keyValue in ledSources)
                        {
                            newLightData[i] = CurrentProfile.ColourExtractionPlugin.Extract(keyValue.Key, keyValue.Value);
                            i++;
                        }
                    }
                    finally
                    {
                        //Dispose of screen segments
                        foreach (var keyValue in ledSources)
                        {
                            keyValue.Value.Dispose();
                        }

                        //Dispose of whole screen
                        CurrentProfile.CapturePlugin.ReleaseCapture();
                    }

                    //Run any Post Process Plugins
                    foreach (var postProcessPlugin in CurrentProfile.PostProcessPlugins)
                    {
                        (postProcessPlugin as IPostProcessPlugin).Process(lightSetupPlugin.Lights, newLightData);
                    }

                    // Thread safe swap of new light data into _nextLightData
                    lock (sync)
                    {
                        var t = _nextLightData;
                        _nextLightData = newLightData;
                        newLightData   = t;
                    }

                    // Throttle Capture Frequency
                    while ((timer.ElapsedTicks - lastTicks) * freq < waitTicks)
                    {
                        Task.Delay(5);
                    }

                    // Update capture FPS counter
                    _captureLoopFPS.Tick();
                }
            }
            catch (Exception e)
            {
                lock (sync)
                {
                    Active = false;
                    AfterglowRuntime.Logger.Error(e, "Afterglow Runtime - Capture Loop");
                }
            }
            finally
            {
                // Allow plugins to clean up
                CurrentProfile.CapturePlugins.ForEach(cp => cp.Stop());
                CurrentProfile.PostProcessPlugins.ForEach(pp => pp.Stop());
            }
        }
Esempio n. 14
0
        public IDictionary<Core.Light, Core.PixelReader> Capture(ILightSetupPlugin lightSetup)
        {
            _graphics.CopyFromScreen(_dispBounds.Left, _dispBounds.Top, 0, 0, new Size(_dispBounds.Width, _dispBounds.Height));

            _fastBitmap = new FastBitmap(_img);

            //TODO every n th frame check for Black banding as config as sub plugin

            IDictionary<Core.Light, Core.PixelReader> dictionary = new Dictionary<Core.Light, Core.PixelReader>();
            foreach (Light light in lightSetup.GetLightsForBounds(_dispBounds.Width, _dispBounds.Height))
            {
                dictionary[light] = new PixelReader(_fastBitmap, light.Region);
            }

            return dictionary;
        }
Esempio n. 15
0
        public IDictionary<Core.Light, Core.PixelReader> Capture(ILightSetupPlugin lightSetup)
        {
            _graphics.CopyFromScreen(_dispBounds.Left, _dispBounds.Top, 0, 0, new Size(_dispBounds.Width, _dispBounds.Height));

            _fastBitmap = new FastBitmap(_img);

            GetCaptureSize();

            IDictionary<Core.Light, Core.PixelReader> dictionary = new Dictionary<Core.Light, Core.PixelReader>();
            foreach (Light light in lightSetup.GetLightsForBounds(_captureWidth, _captureHeight, _leftOffset, _topOffset))
            {
                dictionary[light] = new PixelReader(_fastBitmap, light.Region);
            }

            return dictionary;
        }