Exemple #1
0
        /// <summary>
        /// Application entrypoint
        /// </summary>
        public static void Main(string[] args)
        {
            var sr = new ShiftRegister(ShiftRegisterPinMapping.Complete, 8);

            // Uncomment this code to use SPI (and comment the line above)
            // var settings = new SpiConnectionSettings(0, 0);
            // using var spiDevice = SpiDevice.Create(settings);
            // var sr = new Sn74hc595(spiDevice, Sn74hc595.PinMapping.Standard);
            var cancellationSource = new CancellationTokenSource();

            Console.CancelKeyPress += (s, e) =>
            {
                e.Cancel = true;
                cancellationSource.Cancel();
            };

            Console.WriteLine($"Driver for {nameof(ShiftRegister)}");
            Console.WriteLine($"Register bit length: {sr.BitLength}");
            var interfaceType = sr.UsesSpi ? "SPI" : "GPIO";

            Console.WriteLine($"Using {interfaceType}");

            sr.OutputEnable = true;

            DemonstrateShiftingBits(sr, cancellationSource);
            DemonstrateShiftingBytes(sr, cancellationSource);
            BinaryCounter(sr, cancellationSource);
            Console.WriteLine("done");
        }
Exemple #2
0
            /// <summary>
            /// ShiftRegister based interface for the LCD.
            /// </summary>
            /// <remarks>
            /// Pin parameters should be set according to which output pin of the shift register they are connected to
            /// (e.g. 0 to 7 for 8bit shift register).
            /// </remarks>
            /// <param name="registerSelectPin">The pin that controls the register select.</param>
            /// <param name="enablePin">The pin that controls the enable switch.</param>
            /// <param name="dataPins">Collection of pins holding the data that will be printed on the screen.</param>
            /// <param name="backlightPin">The optional pin that controls the backlight of the display.</param>
            /// <param name="shiftRegister">The shift register that drives the LCD.</param>
            /// <param name="shouldDispose">True to dispose the shift register.</param>
            public ShiftRegisterLcdInterface(int registerSelectPin, int enablePin, int[] dataPins, int backlightPin = -1, ShiftRegister?shiftRegister = null, bool shouldDispose = true)
            {
                _registerSelectPin = 1 << registerSelectPin;
                _enablePin         = 1 << enablePin;

                if (dataPins.Length == 8)
                {
                    EightBitMode = true;
                }
                else if (dataPins.Length != 4)
                {
                    throw new ArgumentException("The length of the array must be 4 or 8.", nameof(dataPins));
                }

                _dataPins = new long[dataPins.Length];

                for (var i = 0; i < dataPins.Length; i++)
                {
                    _dataPins[i] = 1 << dataPins[i];
                }

                if (backlightPin != -1)
                {
                    _backlightPin = 1 << backlightPin;
                }

                _shouldDispose = shouldDispose || _shiftRegister is null;
                _shiftRegister = shiftRegister ?? new ShiftRegister(ShiftRegisterPinMapping.Minimal, 8);

                _backlightOn = true;
                Initialize();
            }
Exemple #3
0
        private static void BinaryCounter(ShiftRegister sr, CancellationTokenSource cancellationSource)
        {
            Console.WriteLine($"Write 0 through 255");
            for (int i = 0; i < 256; i++)
            {
                sr.ShiftByte((byte)i);
                Thread.Sleep(50);
                sr.ShiftClear();

                if (IsCanceled(sr, cancellationSource))
                {
                    return;
                }
            }

            sr.ShiftClear();

            if (sr.BitLength > 8)
            {
                Console.WriteLine($"Write 256 through 4095; pick up the pace");
                for (int i = 256; i < 4096; i++)
                {
                    ShiftBytes(sr, i);
                    Thread.Sleep(25);
                    sr.ShiftClear();

                    if (IsCanceled(sr, cancellationSource))
                    {
                        return;
                    }
                }
            }

            sr.ShiftClear();
        }
        static void Main(string[] args)
        {
            Console.WriteLine("This small project demonstrates interoperability between F# and C#.");
            Console.WriteLine();

            Console.WriteLine("Creating a new shift register, which is type defined in an F# project.");
            var shiftReg = new ShiftRegister(0x0123456789ABCDEF, 64);

            Console.WriteLine($"Calling the ShiftRegister's ToString() -> '{shiftReg}'");
            Console.WriteLine();

            Console.WriteLine("Lists have some interesting interoperability.");

            var taps = ListModule.OfSeq(new List <int>()
            {
                3, 5
            });
            var xorLFSR = new ExclusiveOrLFSR(1, taps);

            var set       = new HashSet <BigInteger>();
            var iteration = 0;

            Console.WriteLine("Looping through XOR LFSR until a duplicate value is found...");
            while (!set.Contains(xorLFSR.State))
            {
                iteration++;
                set.Add(xorLFSR.State);
                Console.WriteLine($"Iteration #{iteration}: {xorLFSR.State}");
                xorLFSR.Shift();
            }
        }
Exemple #5
0
    public void SetValueSetsCorrectDataBits()
    {
        //arrange
        var enabled  = new PinStub(1);
        var data     = Substitute.For <IPinInterface>();
        var shift    = new PinStub(2);
        var output   = new PinStub(3);
        var clear    = new PinStub(4);
        var register = new ShiftRegister(enabled, data, shift, output, clear);

        //act
        register.SetValue(146);

        //assert
        var calls = data.ReceivedCalls().ToArray();

        Assert.Equal(PowerValue.On, calls[0].GetArguments()[0]);
        Assert.Equal(PowerValue.Off, calls[1].GetArguments()[0]);
        Assert.Equal(PowerValue.Off, calls[2].GetArguments()[0]);
        Assert.Equal(PowerValue.On, calls[3].GetArguments()[0]);
        Assert.Equal(PowerValue.Off, calls[4].GetArguments()[0]);
        Assert.Equal(PowerValue.Off, calls[5].GetArguments()[0]);
        Assert.Equal(PowerValue.On, calls[6].GetArguments()[0]);
        Assert.Equal(PowerValue.Off, calls[7].GetArguments()[0]);
    }
Exemple #6
0
        private static bool IsCanceled(ShiftRegister sr, CancellationTokenSource cancellationSource)
        {
            if (cancellationSource.IsCancellationRequested)
            {
                sr.ShiftClear();
                return(true);
            }

            return(false);
        }
        static void Main(string[] args)
        {
            Console.WriteLine("multitype list tests");
            Console.SetWindowSize(Console.WindowWidth + 50, Console.WindowHeight);
            MultiTypeList list = new MultiTypeList();

            list.AddTypeList(typeof(string));
            list.AddTypeList(typeof(int));
            list.AddObject(typeof(string), new MultiTypeListObject("Hello!"));
            list.AddObject(typeof(int), new MultiTypeListObject(12345));
            Console.WriteLine(list[0, typeof(int)]);
            Console.WriteLine(list[0, typeof(string)]);
            Console.WriteLine("UI tests");
            bool cancel = true;

            if (cancel)
            {
                Console.WriteLine("Canceled UI tests");
            }
            Form1 f = new Form1(cancel);

            if (f.ShowDialog() == f.DialogResult)
            {
                //do nothing!
            }
            Console.WriteLine("register tests");
            int           size = 2;
            ShiftRegister r    = new ShiftRegister(size, 2, false);

            for (int i = 0; i < (int)Math.Pow(2, size); i++)
            {
                bool v = i % 2 == 0;
                r.SetAdress(i, new bool[] { v, !v, v, !v, v, !v, v, !v, v, !v, v, !v });
            }
            Console.WriteLine(r.ToString());
            for (int i = 0; i < (int)Math.Pow(2, size); i++)
            {
                r.ShiftRight();
                Console.WriteLine(r.ToString());
            }
            for (int i = 0; i < (int)Math.Pow(2, size); i++)
            {
                bool v = i % 2 == 0;
                r.SetAdress(i, new bool[] { v, !v });
            }
            Console.WriteLine(r.ToString());
            for (int i = 0; i < (int)Math.Pow(2, size); i++)
            {
                r.ShiftLeft();
                Console.WriteLine(r.ToString());
            }
            Console.WriteLine("done!");
            Console.WriteLine("gate tests");
            Console.Read();
        }
Exemple #8
0
        [InlineData(0x5E, 0xAF, 7, 0x57)] // 0x5EAF   0101111(0  1010111)1    => 0x57 (87)
        public void OffsetIsUsedDuringRead(byte upperByte, byte lowerByte, int offset, byte expected)
        {
            var shiftRegister = new ShiftRegister();

            Assert.Equal(0, shiftRegister.Read());

            shiftRegister.Write(lowerByte);
            shiftRegister.Write(upperByte);
            shiftRegister.SetOffset(offset);

            Assert.Equal(expected, shiftRegister.Read());

            // Ensure multiple reads aren't mutating the internal value during the offset shift operation.
            Assert.Equal(expected, shiftRegister.Read());
        }
Exemple #9
0
        private static void DemonstrateShiftingBits(ShiftRegister sr, CancellationTokenSource cancellationSource)
        {
            if (sr.UsesSpi)
            {
                return;
            }

            int delay = 1000;

            sr.ShiftClear();

            Console.WriteLine("Light up three of first four LEDs");
            sr.ShiftBit(1);
            sr.ShiftBit(1);
            sr.ShiftBit(0);
            sr.ShiftBit(1);
            sr.Latch();
            Thread.Sleep(delay);

            sr.ShiftClear();

            Console.WriteLine($"Light up all LEDs, with {nameof(sr.ShiftBit)}");

            for (int i = 0; i < sr.BitLength; i++)
            {
                sr.ShiftBit(1);
            }

            sr.Latch();
            Thread.Sleep(delay);

            sr.ShiftClear();

            Console.WriteLine($"Dim up all LEDs, with {nameof(sr.ShiftBit)}");

            for (int i = 0; i < sr.BitLength; i++)
            {
                sr.ShiftBit(0);
            }

            sr.Latch();
            Thread.Sleep(delay);

            if (IsCanceled(sr, cancellationSource))
            {
                return;
            }
        }
Exemple #10
0
        private static void ShiftBytes(ShiftRegister sr, int value)
        {
            if (sr.BitLength > 32)
            {
                throw new ArgumentException($"{nameof(ShiftBytes)}: bit length must be  8-32.");
            }

            for (int i = (sr.BitLength / 8) - 1; i > 0; i--)
            {
                int shift            = i * 8;
                int downShiftedValue = value >> shift;
                sr.ShiftByte((byte)downShiftedValue);
            }

            sr.ShiftByte((byte)value);
        }
Exemple #11
0
        private static void DemonstrateShiftingBytes(ShiftRegister sr, CancellationTokenSource cancellationSource)
        {
            int delay = 1000;

            Console.WriteLine($"Write a set of values with {nameof(sr.ShiftByte)}");
            // this can be specified as ints or binary notation -- its all the same
            var values = new byte[] { 0b1, 23, 56, 127, 128, 170, 0b_1010_1010 };

            foreach (var value in values)
            {
                Console.WriteLine($"Value: {value}");
                sr.ShiftByte(value);
                Thread.Sleep(delay);
                sr.ShiftClear();

                if (IsCanceled(sr, cancellationSource))
                {
                    return;
                }
            }

            byte litPattern = 0b_1111_1111; // 255

            Console.WriteLine($"Write {litPattern} to each register with {nameof(sr.ShiftByte)}");
            for (int i = 0; i < sr.BitLength / 8; i++)
            {
                sr.ShiftByte(litPattern);
            }

            Thread.Sleep(delay);

            Console.WriteLine("Output disable");
            sr.OutputEnable = false;
            Thread.Sleep(delay * 2);

            Console.WriteLine("Output enable");
            sr.OutputEnable = true;
            Thread.Sleep(delay * 2);

            Console.WriteLine($"Write 23 then 56 with {nameof(sr.ShiftByte)}");
            sr.ShiftByte(23);
            Thread.Sleep(delay);
            sr.ShiftByte(56);
            sr.ShiftClear();
        }
Exemple #12
0
    public void ClearSpikesOutputWhileClearIsOn()
    {
        //arrange
        var enabled  = new PinStub(1);
        var data     = new PinStub(2);
        var shift    = new PinStub(3);
        var output   = Substitute.For <IPinInterface>();
        var clear    = Substitute.For <IPinInterface>();
        var register = new ShiftRegister(enabled, data, shift, output, clear);

        //act
        register.Clear();

        //assert
        clear.Received().TurnOn();
        output.Received().Spike();
        clear.Received().TurnOff();
    }
Exemple #13
0
        public void ValuesAreShifted()
        {
            var shiftRegister = new ShiftRegister();

            Assert.Equal(0, shiftRegister.Read());

            shiftRegister.Write(0xAF);

            Assert.Equal(0xAF, shiftRegister.Read());

            shiftRegister.Write(0x5E);

            Assert.Equal(0x5E, shiftRegister.Read());

            shiftRegister.Write(0x29);

            Assert.Equal(0x29, shiftRegister.Read());
        }
Exemple #14
0
        private static void BinaryCounter(ShiftRegister sr, CancellationTokenSource cancellationSource)
        {
            int endValue = 1000;

            Console.WriteLine($"Write 0 through {endValue}");
            var delay = 10;

            for (int i = 0; i < endValue; i++)
            {
                sr.ShiftByte((byte)i);
                Thread.Sleep(delay);
                sr.ShiftClear();

                if (IsCanceled(sr, cancellationSource))
                {
                    return;
                }
            }
        }
Exemple #15
0
        private static void ShiftRegisterCounter()
        {
            var shiftRegisters = new ShiftRegister(
                Pi3Pins.Gpio17.Prepare(), // signal (SER - chip pin 14)
                Pi3Pins.Gpio27.Prepare(), // signal clock (SRCLK - chip pin 11)
                Pi3Pins.Gpio22.Prepare(), // register clock (RCLK - chip pin 12)
                Pi3Pins.Gpio18.Prepare(), // clear (SRCLR - chip pin 10 - Important: tie this high if not connected to Pi!)
                Pi3Pins.Gpio23.Prepare(), // enabled (OE - chip pin 13)
                3);                       // 3 chips to demo chaining, see readme.md

            for (var i = 0; i < 8; i++)   // demo looping through first chips outputs
            {
                shiftRegisters.Clear();
                shiftRegisters[i] = true;
                Thread.Sleep(1000);
            }

            shiftRegisters.Clear();
            shiftRegisters[0] = true;          // turns on Pin 15 on the first chip immediately
            Thread.Sleep(100);
            shiftRegisters[1] = true;          // turns on Pin 1 on the first chip immediately
            Thread.Sleep(100);
            shiftRegisters[2] = true;          // turns on Pin 2 on the first chip immediately
            Thread.Sleep(100);
            shiftRegisters[3] = true;          // turns on Pin 3 on the first chip immediately

            shiftRegisters.AutoCommit = false; // disabled AutoCommit to set multiple values at once
            shiftRegisters.Clear();
            shiftRegisters[4] = true;          // turns on Pin 4 on the first chip immediately
            Thread.Sleep(100);
            shiftRegisters[5] = true;          // turns on Pin 5 on the first chip immediately
            Thread.Sleep(100);
            shiftRegisters[6] = true;          // turns on Pin 6 on the first chip immediately
            Thread.Sleep(100);
            shiftRegisters[7] = true;          // turns on Pin 7 on the first chip immediately
            Thread.Sleep(100);
            shiftRegisters[8] = true;          // Pin 15 on the second chip
            Thread.Sleep(100);
            shiftRegisters[16] = true;         // Pin 15 on the third chip
            shiftRegisters.Commit();           // turns on the above pins simultaneously
        }
Exemple #16
0
    public void AddPhysicalGate(CircuitDevice circuitDevice)
    {
        Primitive newSimGate;

        switch (circuitDevice.logicType)
        {
        case Utils.LogicType.NOT:
            newSimGate = new Not("replaceMe");
            break;

        case Utils.LogicType.NAND:
            newSimGate = new Nand("replaceMe");
            break;

        case Utils.LogicType.AND:
            newSimGate = new And("replaceMe");
            break;

        case Utils.LogicType.OR:
            newSimGate = new Or("replaceMe");
            break;

        case Utils.LogicType.NOR:
            newSimGate = new Nor("replaceMe");
            break;

        case Utils.LogicType.XOR:
            newSimGate = new Xor("replaceMe");
            break;

        case Utils.LogicType.XNOR:
            newSimGate = new Xnor("replaceMe");
            break;

        case Utils.LogicType.Lever:
            newSimGate = new Lever("replaceMe");
            (circuitDevice as LeverDevice).leverGate = newSimGate as Lever;
            break;

        case Utils.LogicType.Indicator:
            newSimGate = new Indicator("replaceMe");
            break;

        case Utils.LogicType.Clock:
            newSimGate = new Clock("replaceMe");
            break;

        case Utils.LogicType.ShiftRegister4:
            newSimGate = new ShiftRegister("replaceMe", 4);
            break;

        case Utils.LogicType.ShiftRegister8:
            newSimGate = new ShiftRegister("replaceMe", 8);
            break;

        case Utils.LogicType.DFF:
            newSimGate = new DFF("replaceMe");
            break;

        case Utils.LogicType.FA1:
            newSimGate = new Adder("replaceMe", 1);
            break;

        default:
            Debug.LogError("Unknown circuit device type, object name: " + circuitDevice.gameObject.name);
            return;
        }

        newSimGate.position          = circuitDevice.transform.localPosition;
        circuitDevice.associatedGuid = newSimGate.guid;

        circuitSimObject.Add(newSimGate);

        physicalDevices.Add(newSimGate.guid, circuitDevice);
    }
Exemple #17
0
 public ShiftRegisterStato()
 {
     _sr       = ShiftRegister.GetInstance();
     Dato      = _sr.Dato;
     Operation = _sr.Operation;
 }