Exemplo n.º 1
0
        public int Write(DigitalLevel newLevel)
        {
            if (!IsConnected)
            {
                Level = newLevel;
                return(0);
            }

            if (newLevel == DigitalLevel.Floating)
            {
                Level = newLevel;
                _digitalSignal.OnNewProviderValue(this);
                return(1);
            }

            var count = 0;

            foreach (var level in new DigitalLevelCycler(
                         DigitalLevelCycler.NextLevel(_digitalSignal.Level), newLevel))
            {
                Level = level;
                _digitalSignal.OnNewProviderValue(this);
                count++;
            }

            return(count);
        }
Exemplo n.º 2
0
        public void ConnectTo(DigitalSignal digitalSignal)
        {
            if (digitalSignal == null)
            {
                throw new ArgumentNullException(nameof(digitalSignal));
            }
            if (_digitalSignal != null)
            {
                throw new InvalidOperationException("This DigitalSignalProvider is already connected.");
            }

            _digitalSignal = digitalSignal;
            _digitalSignal.Attach(this);

            // catch up with set level value
            if (Level != DigitalLevel.Floating)
            {
                var newLevel = Level;
                foreach (var level in new DigitalLevelCycler(
                             DigitalLevelCycler.NextLevel(_digitalSignal.Level), newLevel))
                {
                    Level = level;
                    _digitalSignal.OnNewProviderValue(this);
                }
            }
        }
Exemplo n.º 3
0
        private int SetNewLevel(DigitalLevel newLevel)
        {
            if (DigitalSignal.Level == newLevel)
            {
                return(0);
            }

            if (newLevel == DigitalLevel.Floating)
            {
                Level = newLevel;
                _digitalSignal.OnNewProviderValue(this);
                return(1);
            }

            var currentLevel = _digitalSignal.Level;
            var nextLevel    = DigitalLevelCycler.NextLevel(currentLevel);

            if (nextLevel == newLevel ||
                currentLevel == DigitalLevel.Floating)
            {
                Level = newLevel;
                _digitalSignal.OnNewProviderValue(this);
                return(1);
            }

            int count = 0;

            foreach (var level in new DigitalLevelCycler(nextLevel, newLevel))
            {
                Level = level;
                _digitalSignal.OnNewProviderValue(this);
                count++;
            }

            return(count);
        }