Пример #1
0
        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;
        }
Пример #2
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;
        }
Пример #3
0
        private void _ScheduleNextBrightnessChange()
        {
            m_scheduleTimer.Stop();

            double     timeToNextPeriod = 0;
            TimePeriod nextPeriod       = TimePeriod.None;

            _CalculateFollowingPeriod(DateTime.Now.TimeOfDay, out nextPeriod, out timeToNextPeriod);

            if (nextPeriod == TimePeriod.Day)
            {
                CurrentTimePeriod = TimePeriod.Night;
                double timeSincePeriodStarted = DateTime.Now.TimeOfDay.TotalMilliseconds - m_nighttime.TotalMilliseconds;
                if (timeSincePeriodStarted < -1)
                {
                    timeSincePeriodStarted += TimeSpan.FromHours(24).TotalMilliseconds;
                }
                if (timeSincePeriodStarted < m_nightToDayTransitionDuration)
                {
                    //within the transition period, so notify that brightness should transition to daytime levels now
                    OnBrightnessChange?.Invoke((uint)m_nightBrightness, m_dayToNightTransitionDuration - timeSincePeriodStarted);
                }
                else
                {
                    //notify that the current brightness should be at nighttime levels
                    OnBrightnessChange?.Invoke((uint)m_nightBrightness, 0.0);
                }

                //schedule the upcoming transition from night to day
                m_scheduleTimer.Interval = timeToNextPeriod + 1;
            }
            else
            {
                CurrentTimePeriod = TimePeriod.Day;

                double timeSincePeriodStarted = DateTime.Now.TimeOfDay.TotalMilliseconds - m_daytime.TotalMilliseconds;
                if (timeSincePeriodStarted < -1)
                {
                    timeSincePeriodStarted += TimeSpan.FromHours(24).TotalMilliseconds;
                }

                if (timeSincePeriodStarted < m_dayToNightTransitionDuration)
                {
                    //within the transition period, so notify that brightness should transition to nighttime levels now
                    OnBrightnessChange?.Invoke((uint)m_dayBrightness, m_nightToDayTransitionDuration - timeSincePeriodStarted);
                }
                else
                {
                    //notify that the current brightness should be at daytime levels
                    OnBrightnessChange?.Invoke((uint)m_dayBrightness, 0.0);
                }

                //schedule the upcoming transition from day to night
                m_scheduleTimer.Interval = timeToNextPeriod + 1;
            }

            m_scheduleTimer.Start();
        }