public BrightnessDimmer() { m_lock = new object(); m_dimmerTimer = new System.Timers.Timer(); m_dimmerTimer.AutoReset = true; m_dimmerTimer.Elapsed += new System.Timers.ElapsedEventHandler((object source, System.Timers.ElapsedEventArgs e) => { int brightnessChange = (m_currentBrightness > m_targetBrightness) ? -1 : 1; lock ( m_lock ) { m_currentBrightness = (brightnessChange == 1) ? m_currentBrightness + 1 : m_currentBrightness - 1; } SetDisplayBrightness(m_currentBrightness); OnBrightnessChange?.Invoke(m_currentBrightness, brightnessChange); if (m_currentBrightness == m_targetBrightness) { this.Stop(); } }); m_status = DimmerStatus.Idle; m_currentBrightness = 0; m_targetBrightness = 0; }
/// <summary> /// adjusts the brightness from one value to another over a set period of time /// </summary> /// <param name="startBrightness">brightness to start at</param> /// <param name="targetBrightness">brightness to end at</param> /// <param name="interval">how long it should take, in milliseconds, to go from start to target brightness</param> public void AdjustBrightness(uint startBrightness, uint targetBrightness, double interval) { if (startBrightness == targetBrightness || interval <= 0) { SetDisplayBrightness(targetBrightness); OnBrightnessChange?.Invoke(targetBrightness, (int)targetBrightness - (int)startBrightness); return; } m_dimmerTimer.Stop(); lock ( m_lock ) { m_currentBrightness = startBrightness; m_targetBrightness = targetBrightness; } m_dimmerTimer.Interval = interval / (double)Math.Abs((int)startBrightness - (int)targetBrightness); m_dimmerTimer.Start(); m_status = (startBrightness < targetBrightness) ? DimmerStatus.Brightening : DimmerStatus.Dimming; }
public MainWindowViewModel(MainWindow window) { m_previewResetTimer = new System.Timers.Timer(); m_systemClockTimer = new System.Timers.Timer(); m_vmLock = new object(); m_latitude = ""; m_longitude = ""; m_timeToNextPeriod = "--:--:--"; m_windowTitle = "Unblind"; m_currentBrightnessTransitionStatus = DimmerStatus.Idle; m_clockElapsedTime = 0; m_currentBrightness = 100; m_maxDisplayBrightness = 0; m_minDisplayBrightness = 1; m_firstRun = false; m_locationEnabled = false; m_noSupportDetected = false; m_multipleDisplaysConnected = false; m_startWithWindows = false; m_startMinimized = false; m_brightnessDimmer = new BrightnessDimmer(); m_brightnessDimmer.OnBrightnessChange += _OnBrightnessChangeEvent; m_brightnessScheduler = new BrightnessScheduler(TimeSpan.FromHours(7), TimeSpan.FromHours(18), 50, 90, 60000, 60000); m_brightnessScheduler.OnBrightnessChange += _OnScheduledBrightnessEvent; m_brightnessScheduler.PropertyChanged += _OnSchedulerPropertyChanged; //add listener for display changes (display plugged in/unplugged) window.OnDisplayChangeDetected += () => { _OnDisplayChangeDetected(); }; m_previewResetTimer.AutoReset = false; m_previewResetTimer.Interval = m_previewDuration; m_previewResetTimer.Elapsed += new System.Timers.ElapsedEventHandler((object source, System.Timers.ElapsedEventArgs e) => { m_brightnessDimmer.AdjustBrightness(m_currentBrightness, m_currentBrightness, 0.0); }); m_systemClockTimer.AutoReset = true; m_systemClockTimer.Interval = 1000; m_systemClockTimer.Elapsed += _ClockTick; _InitializeApp(window); }
public void Stop() { m_dimmerTimer.Stop(); m_status = DimmerStatus.Idle; }