private static DigitalSignal Connect(DigitalSignalProvider provider, DigitalSignalConsumer consumer)
        {
            if (consumer.IsConnected && provider.IsConnected)
            {
                if (consumer.DigitalSignal != provider.DigitalSignal)
                {
                    throw new InvalidOperationException("Both Consumer and Provider are already connected to different signals.");
                }

                return(consumer.DigitalSignal);
            }

            DigitalSignal signal = null;

            if (consumer.IsConnected)
            {
                signal = consumer.DigitalSignal;
                provider.ConnectTo(signal);
            }
            else if (provider.IsConnected)
            {
                signal = provider.DigitalSignal;
                consumer.ConnectTo(signal);
            }

            return(signal);
        }
Exemple #2
0
        public void Connected_Level_IsFloating()
        {
            var uut = new DigitalSignalProvider();

            uut.ConnectTo(new DigitalSignal());

            uut.Level.Should().Be(DigitalLevel.Floating);
        }
Exemple #3
0
        public void Connected_WithName_BuildName()
        {
            var name = "Test";
            var uut  = new DigitalSignalProvider();

            uut.ConnectTo(new DigitalSignal(name));

            uut.Name.Should().StartWith(name);
        }
Exemple #4
0
        public void MultipleProviders_OneWrites_NoConflicts()
        {
            var provider1 = new DigitalSignalProvider();
            var provider2 = new DigitalSignalProvider();
            var signal    = new DigitalSignal();

            provider1.ConnectTo(signal);
            provider2.ConnectTo(signal);

            provider1.Write(DigitalLevel.High);
        }
Exemple #5
0
        public void Connected_WriteHigh_LevelIsHigh()
        {
            var uut    = new DigitalSignalProvider();
            var signal = new DigitalSignal();

            uut.ConnectTo(signal);

            uut.Write(DigitalLevel.High);

            uut.Level.Should().Be(DigitalLevel.High);
            signal.Level.Should().Be(DigitalLevel.High);
        }
        public void Connect_WriteProvider_ConsumerSeesChanges()
        {
            var provider = new DigitalSignalProvider();
            var consumer = new DigitalSignalConsumer();
            var signal   = new DigitalSignal();

            provider.ConnectTo(signal);
            consumer.ConnectTo(signal);

            provider.Write(DigitalLevel.High);

            consumer.Level.Should().Be(DigitalLevel.High);
        }
        public void Connect_WriteProvider_EventTriggered()
        {
            var provider = new DigitalSignalProvider();
            var consumer = new DigitalSignalConsumer();
            var signal   = new DigitalSignal();

            provider.ConnectTo(signal);
            consumer.ConnectTo(signal);
            consumer.MonitorEvents();

            provider.Write(DigitalLevel.High);

            consumer.ShouldRaise("OnChanged");
        }
Exemple #8
0
        public void Connected_WriteHigh_TriggersEvent()
        {
            var uut    = new DigitalSignalProvider();
            var signal = new DigitalSignal();

            uut.ConnectTo(signal);

            signal.MonitorEvents();
            uut.Write(DigitalLevel.High);

            signal.ShouldRaise("OnChanged");
            uut.Level.Should().Be(DigitalLevel.High);
            signal.Level.Should().Be(DigitalLevel.High);
        }
Exemple #9
0
        public void MultipleProviders_OneWritesAtaTime_NoConflicts()
        {
            var provider1 = new DigitalSignalProvider();
            var provider2 = new DigitalSignalProvider();
            var signal    = new DigitalSignal();

            provider1.ConnectTo(signal);
            provider2.ConnectTo(signal);

            provider1.Write(DigitalLevel.High);
            provider1.Write(DigitalLevel.Floating);
            provider2.Write(DigitalLevel.Low);
            provider2.Write(DigitalLevel.Floating);
            provider1.Write(DigitalLevel.PosEdge);
        }
        public void Write_MultipleConsumers_AllSeeChangedLevel()
        {
            var provider  = new DigitalSignalProvider();
            var consumer1 = new DigitalSignalConsumer();
            var consumer2 = new DigitalSignalConsumer();
            var signal    = new DigitalSignal();

            provider.ConnectTo(signal);
            consumer1.ConnectTo(signal);
            consumer2.ConnectTo(signal);

            provider.Write(DigitalLevel.High);

            consumer1.Level.Should().Be(DigitalLevel.High);
            consumer2.Level.Should().Be(DigitalLevel.High);
        }
        public static DigitalSignal GetOrAddSignal(this DigitalSignalProvider provider, string signalName = null)
        {
            DigitalSignal signal = null;

            if (!provider.IsConnected)
            {
                signal = new DigitalSignal(signalName);
                provider.ConnectTo(signal);
            }
            else
            {
                signal = provider.DigitalSignal;
            }

            return(signal);
        }
        public static DigitalSignal ConnectTo(this DigitalSignalConsumer consumer, DigitalSignalProvider provider, string signalName = null)
        {
            DigitalSignal signal = Connect(provider, consumer);

            if (signal == null)
            {
                if (signalName == null)
                {
                    signalName = consumer.Name;
                }
                signal = consumer.GetOrAddSignal(signalName);
                provider.ConnectTo(signal);
            }

            return(signal);
        }
Exemple #13
0
        public void Connected_WriteHighTwice_NoChange()
        {
            var uut    = new DigitalSignalProvider();
            var signal = new DigitalSignal();

            uut.ConnectTo(signal);
            uut.Write(DigitalLevel.High);

            var levels = new List <DigitalLevel>();

            signal.OnChanged += (s, e) => levels.Add(e.Level);

            var count = uut.Write(DigitalLevel.High);

            count.Should().Be(0);
            levels.Should().HaveCount(count);
        }
        public static DigitalSignalConsumer CreateConnection(this DigitalSignalProvider provider, DigitalSignalConsumer consumer = null, string signalName = null)
        {
            if (signalName == null && provider.Name != null)
            {
                signalName = provider.Name;
            }

            var signal = new DigitalSignal(signalName);

            if (consumer == null)
            {
                consumer = new DigitalSignalConsumer();
            }

            consumer.ConnectTo(signal);
            provider.ConnectTo(signal);
            return(consumer);
        }
Exemple #15
0
        public void Connected_WriteHigh_MovesToHigh()
        {
            var uut    = new DigitalSignalProvider();
            var signal = new DigitalSignal();

            uut.ConnectTo(signal);

            var levels = new List <DigitalLevel>();

            signal.OnChanged += (s, e) => levels.Add(e.Level);

            var count = uut.Write(DigitalLevel.High);

            count.Should().Be(3);
            levels.Should().HaveCount(count);
            levels[0].Should().Be(DigitalLevel.Low);
            levels[1].Should().Be(DigitalLevel.PosEdge);
            levels[2].Should().Be(DigitalLevel.High);
        }