Exemplo n.º 1
0
        private void RefreshFields()
        {
            rbLinearLighting.Checked    = Settings.UseLinearLighting;
            rbNonLinearLighting.Checked = !Settings.UseLinearLighting;

            numericUpDownSpotsX.Value = Settings.SpotsX;

            numericUpDownSpotsY.Value = Settings.SpotsY;

            numericUpDownLedsPerSpot.Value = Settings.LedsPerSpot;

            SetTrackBarValue(trackBarOffsetX, Settings.OffsetX);

            SetTrackBarValue(trackBarOffsetY, Settings.OffsetY);

            checkBoxTransferActive.Checked = Settings.TransferActive;

            checkBoxOverlayActive.Checked = Settings.OverlayActive;

            comboBoxComPort.Text = Settings.ComPort;
            comboBoxComPort.Items.Clear();
            foreach (string s in SerialPort.GetPortNames())
            {
                comboBoxComPort.Items.Add(s);
            }
            if (comboBoxComPort.Text.Equals("") && comboBoxComPort.Items.Count > 0)
            {
                comboBoxComPort.Text = (string)comboBoxComPort.Items[0];
            }

            numericUpDownSaturationTreshold.Value = Settings.SaturationTreshold;

            SetTrackBarValue(trackBarSpotWidth, Settings.SpotWidth);

            SetTrackBarValue(trackBarSpotHeight, Settings.SpotHeight);

            checkBoxMirrorX.Checked = Settings.MirrorX;

            checkBoxMirrorY.Checked = Settings.MirrorY;

            numericUpDownLedOffset.Value = Settings.OffsetLed;

            SetTrackBarValue(trackBarBorderDistanceX, Settings.BorderDistanceX);

            SetTrackBarValue(trackBarBorderDistanceY, Settings.BorderDistanceY);

            checkBoxAutostart.Checked = Settings.Autostart;

            checkBoxStartMinimized.Checked = Settings.StartMinimized;

            numericUpDownMinimumRefreshRateMs.Value = Settings.MinimumRefreshRateMs;

            groupBoxLEDs.Text = "LEDs (" + (SpotSet.CountLeds(Settings.SpotsX, Settings.SpotsY) * Settings.LedsPerSpot) + ")";
        }
Exemplo n.º 2
0
        static void Main()
        {
#if DEBUG
            var config         = new LoggingConfiguration();
            var debuggerTarget = new DebuggerTarget()
            {
                Layout = "${processtime} ${message:exceptionSeparator=\n\t:withException=true}"
            };
            config.AddTarget("debugger", debuggerTarget);
            config.LoggingRules.Add(new LoggingRule("*", LogLevel.Debug, debuggerTarget));

            LogManager.Configuration = config;
#endif

            _log.Debug($"adrilight {VersionNumber}: Main() started.");

            AppDomain.CurrentDomain.UnhandledException +=
                (sender, args) => ApplicationOnThreadException(sender, args.ExceptionObject as Exception);

            Settings.Load();

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.ThreadException += (sender, args) => ApplicationOnThreadException(sender, args.Exception);

            Application.ApplicationExit   += (s, e) => _log.Debug("Application exit!");
            SystemEvents.PowerModeChanged += (s, e) => _log.Debug("Changing Powermode to {0}", e.Mode);

            SetupNotifyIcon();

            //subscribe for changes in the settings
            Properties.Settings.Default.PropertyChanged += (s, e) => SpotSet.Refresh();
            //exeucte once to setup the leds
            SpotSet.Refresh();

            //subscribe for changes in the settings
            Properties.Settings.Default.PropertyChanged += (s, e) => RefreshCapturingState();
            //exeucte once to start the capturing initially
            RefreshCapturingState();


            //subscribe for changes in the settings
            Properties.Settings.Default.PropertyChanged += (s, e) => RefreshTransferState();
            //exeucte once to start the serial stream initially
            RefreshTransferState();


            if (!Settings.StartMinimized)
            {
                OpenSettingsWindow();
            }
            Application.Run();
        }
Exemplo n.º 3
0
        private TimeSpan ProvideDelayDuration(int index)
        {
            if (index < 10)
            {
                // First second
                return(TimeSpan.FromMilliseconds(100));
            }

            if (index < 10 + 256)
            {
                // Steps where there is also led dimming
                SpotSet.IndicateMissingValues();
                return(TimeSpan.FromMilliseconds(5000d / 256));
            }
            return(TimeSpan.FromMilliseconds(1000));
        }
Exemplo n.º 4
0
 private void RefreshAll()
 {
     SpotSet.Refresh();
     RefreshFields();
     RefreshOverlay();
 }
Exemplo n.º 5
0
        public async void Run(CancellationToken token)
        {
            while (RunState == RunStateEnum.Stopping)
            {
                await Task.Yield();
            }

            if (RunState != RunStateEnum.Stopped)
            {
                throw new Exception($"{nameof(DesktopDuplicatorReader)} is already running!");
            }

            RunState = RunStateEnum.Running;
            _log.Debug("Started Desktop Duplication Reader.");
            Bitmap image = null;

            try
            {
                var bitmapData = new BitmapData();

                while (!token.IsCancellationRequested)
                {
                    var frameTime = Stopwatch.StartNew();
                    var context   = new Context
                    {
                        { "image", image }
                    };
                    var newImage = _retryPolicy.Execute(c => GetNextFrame(c["image"] as Bitmap), context);
                    TraceFrameDetails(newImage);
                    if (newImage == null)
                    {
                        // There was a timeout before there was the next frame, simply retry!
                        continue;
                    }
                    image = newImage;

                    var isPreviewRunning = SettingsViewModel.IsSettingsWindowOpen && SettingsViewModel.IsPreviewTabOpen;
                    if (isPreviewRunning)
                    {
                        SettingsViewModel.SetPreviewImage(image);
                    }
                    image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb, bitmapData);
                    lock (SpotSet.Lock)
                    {
                        var useLinearLighting = UserSettings.UseLinearLighting;

                        if (image.Width != SpotSet.ExpectedScreenWidth || image.Height != SpotSet.ExpectedScreenHeight)
                        {
                            // The screen was resized or this is some kind of powersaving state
                            SpotSet.IndicateMissingValues();
                            return;
                        }
                        else
                        {
                            Parallel.ForEach(SpotSet.Spots,
                                             spot =>
                            {
                                const int numberOfSteps = 15;
                                var stepx = Math.Max(1, spot.Rectangle.Width / numberOfSteps);
                                var stepy = Math.Max(1, spot.Rectangle.Height / numberOfSteps);

                                GetAverageColorOfRectangularRegion(spot.Rectangle, stepy, stepx, bitmapData,
                                                                   out var sumR, out var sumG, out var sumB, out var count);

                                var countInverse = 1f / count;

                                ApplyColorCorrections(sumR * countInverse, sumG * countInverse, sumB * countInverse,
                                                      out var finalR, out var finalG, out var finalB, useLinearLighting,
                                                      UserSettings.SaturationTreshold, spot.Red, spot.Green, spot.Blue);

                                spot.SetColor(finalR, finalG, finalB, isPreviewRunning);
                            });
                        }
        public void Run(CancellationToken token)
        {
            if (IsRunning)
            {
                throw new Exception(nameof(DesktopDuplicatorReader) + " is already running!");
            }

            IsRunning = true;
            _log.Debug("Started Desktop Duplication Reader.");
            try
            {
                BitmapData bitmapData = new BitmapData();

                while (!token.IsCancellationRequested)
                {
                    var frame = _retryPolicy.Execute <DesktopFrame>(GetNextFrame);
                    TraceFrameDetails(frame);
                    if (frame == null)
                    {
                        //there was a timeout before there was the next frame, simply retry!
                        continue;
                    }


                    var image = frame.DesktopImage;
                    image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb, bitmapData);
                    lock (SpotSet.Lock)
                    {
                        var useLinearLighting = Settings.UseLinearLighting;

                        var imageRectangle = new Rectangle(0, 0, image.Width, image.Height);
                        Parallel.ForEach(SpotSet.Spots
                                         , spot =>
                        {
                            const int numberOfSteps = 15;
                            int stepx = Math.Max(1, spot.Rectangle.Width / numberOfSteps);
                            int stepy = Math.Max(1, spot.Rectangle.Height / numberOfSteps);

                            int sumR;
                            int sumG;
                            int sumB;
                            int count;
                            if (imageRectangle.Width != SpotSet.ExpectedScreenBound.Width || imageRectangle.Height != SpotSet.ExpectedScreenBound.Height)
                            {
                                //the screen was resized or this is some kind of powersaving state
                                SpotSet.IndicateMissingValues();
                                return;
                            }
                            GetAverageColorOfRectangularRegion(spot.Rectangle, stepy, stepx, bitmapData, out sumR, out sumG, out sumB, out count);

                            var countInverse = 1f / count;
                            byte finalR, finalG, finalB;
                            ApplyColorCorrections(sumR * countInverse, sumG * countInverse, sumB * countInverse, out finalR, out finalG, out finalB, useLinearLighting
                                                  , Settings.SaturationTreshold, spot.Red, spot.Green, spot.Blue);

                            spot.SetColor(finalR, finalG, finalB);
                        });
                    }
                    image.UnlockBits(bitmapData);
                }
            }
            finally
            {
                _desktopDuplicator?.Dispose();
                _desktopDuplicator = null;

                _log.Debug("Stopped Desktop Duplication Reader.");
                IsRunning = false;
            }
        }
        public void Run(CancellationToken token)
        {
            if (IsRunning)
            {
                throw new Exception(nameof(DesktopDuplicatorReader) + " is already running!");
            }

            IsRunning = true;
            _log.Debug("Started Desktop Duplication Reader.");
            Bitmap image = null;

            try
            {
                BitmapData bitmapData = new BitmapData();

                while (!token.IsCancellationRequested)
                {
                    var newImage = _retryPolicy.Execute(() => GetNextFrame(image));
                    TraceFrameDetails(newImage);
                    if (newImage == null)
                    {
                        //there was a timeout before there was the next frame, simply retry!
                        continue;
                    }
                    image = newImage;

                    bool isPreviewRunning = SettingsViewModel.IsSettingsWindowOpen && SettingsViewModel.IsPreviewTabOpen;
                    if (isPreviewRunning)
                    {
                        SettingsViewModel.SetPreviewImage(image);
                    }
                    image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb, bitmapData);
                    lock (SpotSet.Lock)
                    {
                        var useLinearLighting = UserSettings.UseLinearLighting;

                        var imageRectangle = new Rectangle(0, 0, image.Width, image.Height);
                        Parallel.ForEach(SpotSet.Spots
                                         , spot =>
                        {
                            const int numberOfSteps = 15;
                            int stepx = Math.Max(1, spot.Rectangle.Width / numberOfSteps);
                            int stepy = Math.Max(1, spot.Rectangle.Height / numberOfSteps);

                            if (imageRectangle.Width != SpotSet.ExpectedScreenBound.Width || imageRectangle.Height != SpotSet.ExpectedScreenBound.Height)
                            {
                                //the screen was resized or this is some kind of powersaving state
                                SpotSet.IndicateMissingValues();
                                return;
                            }
                            GetAverageColorOfRectangularRegion(spot.Rectangle, stepy, stepx, bitmapData,
                                                               out int sumR, out int sumG, out int sumB, out int count);

                            var countInverse = 1f / count;

                            ApplyColorCorrections(sumR * countInverse, sumG * countInverse, sumB * countInverse
                                                  , out byte finalR, out byte finalG, out byte finalB, useLinearLighting
                                                  , UserSettings.SaturationTreshold, spot.Red, spot.Green, spot.Blue);

                            spot.SetColor(finalR, finalG, finalB, isPreviewRunning);
                        });

                        if (isPreviewRunning)
                        {
                            //copy all color data to the preview
                            var needsNewArray = SettingsViewModel.PreviewSpots?.Length != SpotSet.Spots.Length;

                            SettingsViewModel.PreviewSpots = SpotSet.Spots;
                        }
                    }