示例#1
0
        private static void WriteIndividualByte(Mcp23xxx mcp23xxx)
        {
            // This assumes the device is in default Sequential Operation mode.
            Console.WriteLine("Write Individual Byte");

            using (mcp23xxx)
            {
                Register.Address address = Register.Address.IODIR;

                void IndividualRead(Mcp23xxx mcp, Register.Address addressToRead)
                {
                    byte[] dataRead = mcp23xxx.Read(addressToRead, 1, Port.PortB, Bank.Bank0);
                    Console.WriteLine($"\tIODIRB: 0x{dataRead[0]:X2}");
                }

                Console.WriteLine("Before Write");
                IndividualRead(mcp23xxx, address);

                byte[] dataWrite = new byte[] { 0x12 };
                mcp23xxx.Write(address, dataWrite, Port.PortB, Bank.Bank0);

                Console.WriteLine("After Write");
                IndividualRead(mcp23xxx, address);

                dataWrite = new byte[] { 0xFF };
                mcp23xxx.Write(address, dataWrite, Port.PortB, Bank.Bank0);

                Console.WriteLine("After Writing Again");
                IndividualRead(mcp23xxx, address);
            }
        }
示例#2
0
        public void CacheInvalidatesWhenReset(TestDevice testDevice)
        {
            Mcp23xxx device = testDevice.Device;

            // Check the output latches after enabling and setting
            // different bits.
            device.Enable();
            device.Write(0, PinValue.High);
            Assert.Equal(1, device.ReadByte(Register.OLAT));
            device.Write(1, PinValue.High);
            Assert.Equal(3, device.ReadByte(Register.OLAT));

            // Flush OLAT
            device.WriteByte(Register.OLAT, 0x00);
            Assert.Equal(0, device.ReadByte(Register.OLAT));

            // Now setting the next bit will pick back up our cached 3
            device.Write(2, PinValue.High);
            Assert.Equal(7, device.ReadByte(Register.OLAT));

            // Re-enabling will reset the cache
            device.WriteByte(Register.OLAT, 0x00);
            device.Disable();
            device.Enable();
            device.Write(3, PinValue.High);
            Assert.Equal(8, device.ReadByte(Register.OLAT));

            device.WriteByte(Register.OLAT, 0x02);
            device.Disable();
            device.Enable();
            device.Write(0, PinValue.High);
            Assert.Equal(3, device.ReadByte(Register.OLAT));
        }
示例#3
0
        private static void WriteSequentialBytes(Mcp23xxx mcp23xxx)
        {
            // This assumes the device is in default Sequential Operation mode.
            Console.WriteLine("Write Sequential Bytes");

            using (mcp23xxx)
            {
                void SequentialRead(Mcp23xxx mcp)
                {
                    byte[] dataRead = mcp23xxx.Read(0, 2, Port.PortA, Bank.Bank0);
                    Console.WriteLine($"\tIODIRA: 0x{dataRead[0]:X2}");
                    Console.WriteLine($"\tIODIRB: 0x{dataRead[1]:X2}");
                }

                Console.WriteLine("Before Write");
                SequentialRead(mcp23xxx);

                byte[] dataWrite = new byte[] { 0x12, 0x34 };
                mcp23xxx.Write(0, dataWrite, Port.PortA, Bank.Bank0);

                Console.WriteLine("After Write");
                SequentialRead(mcp23xxx);

                dataWrite = new byte[] { 0xFF, 0xFF };
                mcp23xxx.Write(0, dataWrite, Port.PortA, Bank.Bank0);

                Console.WriteLine("After Writing Again");
                SequentialRead(mcp23xxx);
            }
        }
示例#4
0
        private static void SetupI2CWrite()
        {
            try
            {
                var i2cConnectionSettings = new I2cConnectionSettings(1, I2CAddressWrite);
                var i2cDevice             = I2cDevice.Create(i2cConnectionSettings);

                _mcp23xxxWrite = new Mcp23017(i2cDevice);

                if (_mcp23xxxWrite is Mcp23x1x mcp23x1x)
                {
                    // Input direction for Leds.
                    mcp23x1x.WriteByte(Register.IODIR, 0b0000_0000, Port.PortA);
                    mcp23x1x.WriteByte(Register.IODIR, 0b0000_0000, Port.PortB);

                    Console.WriteLine($"Mcp23017 Write initialized at Write address '0x{I2CAddressWrite:X4}'.");
                }
                else
                {
                    Console.WriteLine("Unable to initialize Mcp23017 Write.");
                }
            }
            catch (Exception ex)
            {
                _mcp23xxxWrite = null;
                Console.WriteLine($"Error when initializing Mcp23017 at Write address '0x{I2CAddressWrite:X4}': {ex.Message}");
            }
        }
示例#5
0
        // This is now Read(pinNumber)
        private static void ReadBits(Mcp23xxx mcp23xxx)
        {
            Console.WriteLine("Read Bits");

            for (int bitNumber = 0; bitNumber < 8; bitNumber++)
            {
                PinValue bit = mcp23xxx.Read(bitNumber);
                Console.WriteLine($"{bitNumber}: {bit}");
            }
        }
        public static async Task LitFlooded(Mcp23xxx mcp23xxxWrite, int interval, bool silentFlooding)
        {
            if (silentFlooding)
            {
                return;
            }

            try
            {
                while (_ledsPlaying)
                {
                    // let the previous light show end.
                    await Task.Delay(5);
                }

                _ledsPlaying = true;

                Mcp23x1x mcp23x1x = null;

                if (mcp23xxxWrite != null)
                {
                    mcp23x1x = mcp23xxxWrite as Mcp23x1x;
                }

                if (mcp23x1x == null)
                {
                    Console.WriteLine("LitFlooded: Unable to cast Mcp23017 Write GPIO.");
                }
                else
                {
                    var sleep = 250;

                    var steps = interval / sleep;

                    for (var i = 0; i < steps; i++)
                    {
                        mcp23x1x.WriteByte(Register.GPIO, 255, Port.PortA);
                        mcp23x1x.WriteByte(Register.GPIO, 255, Port.PortB);

                        mcp23x1x.WriteByte(Register.GPIO, 0, Port.PortA);
                        mcp23x1x.WriteByte(Register.GPIO, 0, Port.PortB);

                        await Task.Delay(sleep);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error when LitFlooded: {ex.Message}");
            }
            finally
            {
                _ledsPlaying = false;
            }
        }
        public static async Task PlayDownScene(Mcp23xxx mcp23xxxWrite, int UpDownInterval)
        {
            try
            {
                while (_ledsPlaying)
                {
                    // let the previous light show end.
                    await Task.Delay(5);
                }

                _ledsPlaying = true;

                Mcp23x1x mcp23x1x = null;

                if (mcp23xxxWrite != null)
                {
                    mcp23x1x = mcp23xxxWrite as Mcp23x1x;
                }

                if (mcp23x1x == null)
                {
                    Console.WriteLine("PlayDownScene: Unable to cast Mcp23017 Write GPIO.");
                }
                else
                {
                    // Use UpDownInterval to predict how long the scen must play

                    var sleepInterval = 100;

                    var j = UpDownInterval / sleepInterval;

                    byte a = 0b_1000_0000;

                    for (var i = 0; i < j; i++)
                    {
                        var shifter = ((i + 8) % 8);

                        int b = a >> shifter;

                        mcp23x1x.WriteByte(Register.GPIO, (byte)b, Port.PortA);
                        mcp23x1x.WriteByte(Register.GPIO, (byte)b, Port.PortB);

                        await Task.Delay(sleepInterval);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error when PlayUpScene: {ex.Message}");
            }
            finally
            {
                _ledsPlaying = false;
            }
        }
示例#8
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello Mcp23xxx!");

            Mcp23xxx mcp23xxx = GetMcp23xxxWithSpi();

            // Uncomment sample to run.
            ReadSwitchesWriteLeds(mcp23xxx);
            // ReadAllRegisters(mcp23xxx);
            // WriteSequentialBytes(mcp23xxx);
        }
        public static async Task <bool> DirectCircus(Mcp23xxx mcp23xxxWrite)
        {
            try
            {
                while (_ledsPlaying)
                {
                    // let the previous light show end.
                    await Task.Delay(5);
                }

                _ledsPlaying = true;

                Mcp23x1x mcp23x1x = null;

                if (mcp23xxxWrite != null)
                {
                    mcp23x1x = mcp23xxxWrite as Mcp23x1x;
                }

                if (mcp23x1x == null)
                {
                    Console.WriteLine("DirectCircus: Unable to cast Mcp23017 Write GPIO.");

                    return(false);
                }
                else
                {
                    Console.WriteLine("Ra da da da da da da da Circus");
                    Console.WriteLine("Da da da da da da da da");
                    Console.WriteLine("Afro Circus, Afro Circus, Afro");
                    Console.WriteLine("Polka dot, polka dot, polka dot, Afro!");

                    for (var i = 0; i < 256; i++)
                    {
                        mcp23x1x.WriteByte(Register.GPIO, (byte)i, Port.PortA);
                        mcp23x1x.WriteByte(Register.GPIO, (byte)i, Port.PortB);

                        await Task.Delay(20);
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                _ledsPlaying = false;
            }

            return(true);
        }
示例#10
0
        private static void ReadBits(Mcp23xxx mcp23xxx)
        {
            Console.WriteLine("Read Bits");

            using (mcp23xxx)
            {
                for (int bitNumber = 0; bitNumber < 8; bitNumber++)
                {
                    bool bit = mcp23xxx.ReadBit(Register.Address.GPIO, bitNumber, Port.PortA, Bank.Bank0);
                    Console.WriteLine($"{bitNumber}: {bit}");
                }
            }
        }
示例#11
0
        private static Mcp23xxx GetMcp23xxxWithSpi()
        {
            Console.WriteLine("Using SPI protocol");

            var connection = new SpiConnectionSettings(0, 0)
            {
                ClockFrequency = 1000000,
                Mode           = SpiMode.Mode0
            };

            var spi      = new UnixSpiDevice(connection);
            var mcp23xxx = new Mcp23xxx(spi);

            return(mcp23xxx);
        }
示例#12
0
        private static void ReadAllRegisters(Mcp23xxx mcp23xxx)
        {
            // This assumes the device is in default Sequential Operation mode.
            Console.WriteLine("Read All Registers");

            using (mcp23xxx)
            {
                // Start at first register.  Total of 22 registers for MCP23x17.
                byte[] data = mcp23xxx.Read(0, 22, Port.PortA, Bank.Bank0);

                for (int index = 0; index < data.Length; index++)
                {
                    Console.WriteLine($"0x{index:X2}: 0x{data[index]:X2}");
                }
            }
        }
        public static async Task <bool> DirectAdvertise(Mcp23xxx mcp23xxxWrite)
        {
            try
            {
                while (_ledsPlaying)
                {
                    // let the previous light show end.
                    await Task.Delay(5);
                }

                _ledsPlaying = true;

                Mcp23x1x mcp23x1x = null;

                if (mcp23xxxWrite != null)
                {
                    mcp23x1x = mcp23xxxWrite as Mcp23x1x;
                }

                if (mcp23x1x == null)
                {
                    Console.WriteLine("DirectAdvertise: Unable to cast Mcp23017 Write GPIO.");

                    return(false);
                }
                else
                {
                    mcp23x1x.WriteByte(Register.GPIO, (byte)255, Port.PortA);
                    mcp23x1x.WriteByte(Register.GPIO, (byte)255, Port.PortB);

                    await Task.Delay(10000);

                    mcp23x1x.WriteByte(Register.GPIO, (byte)0, Port.PortA);
                    mcp23x1x.WriteByte(Register.GPIO, (byte)0, Port.PortB);
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                _ledsPlaying = false;
            }

            return(true);
        }
示例#14
0
        public void Write_GoodPin(TestDevice testDevice)
        {
            Mcp23xxx device = testDevice.Device;

            for (int pin = 0; pin < testDevice.Device.PinCount; pin++)
            {
                bool first = pin < 8;

                device.Write(pin, PinValue.High);
                byte expected = (byte)(1 << (first ? pin : pin - 8));

                Assert.Equal(expected,
                             first ? device.ReadByte(Register.OLAT) : ((Mcp23x1x)device).ReadByte(Register.OLAT, Port.PortB));

                device.Write(pin, PinValue.Low);
                Assert.Equal(0,
                             first ? device.ReadByte(Register.OLAT) : ((Mcp23x1x)device).ReadByte(Register.OLAT, Port.PortB));
            }
        }
示例#15
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello Mcp23xxx!");

            using (Mcp23xxx mcp23xxx = GetMcp23xxxDevice(Mcp23xxxDevice.Mcp23017))
            {
                // Samples are currently written specifically for the 16 bit variant
                if (mcp23xxx is Mcp23x1x mcp23x1x)
                {
                    // Uncomment sample to run.
                    ReadSwitchesWriteLeds(mcp23x1x);
                    //WriteByte(mcp23x1x);
                    //WriteUshort(mcp23x1x);
                    //WriteBits(mcp23x1x);
                }

                //ReadBits(mcp23xxx);
            }
        }
示例#16
0
        public void RegisterInitialization(TestDevice testDevice)
        {
            // All pins should be set to input, all output latches should be low, and pullups should be disabled.
            Mcp23xxx device = testDevice.Device;

            if (testDevice.Controller.PinCount == 8)
            {
                Assert.Equal(0xFF, device.ReadByte(Register.IODIR));
                Assert.Equal(0x00, device.ReadByte(Register.OLAT));
                Assert.Equal(0x00, device.ReadByte(Register.IPOL));
            }
            else
            {
                Mcp23x1x device16 = (Mcp23x1x)device;
                Assert.Equal(0xFFFF, device16.ReadUInt16(Register.IODIR));
                Assert.Equal(0x0000, device16.ReadUInt16(Register.OLAT));
                Assert.Equal(0x00, device.ReadByte(Register.OLAT));
            }
        }
示例#17
0
        public void Read_GoodPin(TestDevice testDevice)
        {
            Mcp23xxx device = testDevice.Device;

            for (int pin = 0; pin < testDevice.Device.PinCount; pin++)
            {
                bool first    = pin < 8;
                int  register = testDevice.Device.PinCount == 16
                    ? (first ? 0x12 : 0x13)
                    : 0x09;

                // Flip the bit on (set the backing buffer directly to simulate incoming data)
                testDevice.ChipMock.Registers[register] = (byte)(1 << (first ? pin : pin - 8));
                Assert.Equal(PinValue.High, device.Read(pin));

                // Clear the register
                testDevice.ChipMock.Registers[register] = 0x00;
                Assert.Equal(PinValue.Low, device.Read(pin));
            }
        }
示例#18
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello Mcp23xxx!");

            using (Mcp23xxx mcp23xxx = GetMcp23xxxDevice(Mcp23xxxDevice.Mcp23017))
            {
                GpioController controllerUsingMcp = new GpioController(PinNumberingScheme.Logical, mcp23xxx);
                // Samples are currently written specifically for the 16 bit variant
                if (mcp23xxx is Mcp23x1x mcp23x1x)
                {
                    // Uncomment sample to run.
                    ReadSwitchesWriteLeds(mcp23x1x);
                    //WriteByte(mcp23x1x);
                    //WriteUshort(mcp23x1x);
                    //WriteBits(mcp23x1x, controllerUsingMcp);
                }

                //ReadBits(controllerUsingMcp);
            }
        }
示例#19
0
        private static void WriteBits(Mcp23xxx mcp23xxx)
        {
            Console.WriteLine("Write Bits");

            // Make port output and set all pins.
            mcp23xxx.Write(Register.Address.IODIR, 0x00, Port.PortB, Bank.Bank0);
            mcp23xxx.Write(Register.Address.GPIO, 0xFF, Port.PortB, Bank.Bank0);

            using (mcp23xxx)
            {
                for (int bitNumber = 0; bitNumber < 8; bitNumber++)
                {
                    mcp23xxx.WriteBit(Register.Address.GPIO, bitNumber, false, Port.PortB, Bank.Bank0);
                    Console.WriteLine($"Bit {bitNumber} low");
                    Thread.Sleep(500);
                    mcp23xxx.WriteBit(Register.Address.GPIO, bitNumber, true, Port.PortB, Bank.Bank0);
                    Console.WriteLine($"Bit {bitNumber} high");
                    Thread.Sleep(500);
                }
            }
        }
        public static async Task SwitchOffAllSlots(Mcp23xxx mcp23xxxWrite, byte lastDataPortA, byte lastDataPortB)
        {
            // all LEDs are dimmed.

            try
            {
                while (_ledsPlaying)
                {
                    // let the previous light show end.
                    await Task.Delay(5);
                }

                _ledsPlaying = true;

                Mcp23x1x mcp23x1x = null;

                if (mcp23xxxWrite != null)
                {
                    mcp23x1x = mcp23xxxWrite as Mcp23x1x;
                }

                if (mcp23x1x == null)
                {
                    Console.WriteLine("SwtichoffAllSlots: Unable to cast Mcp23017 Write GPIO.");
                }
                else
                {
                    mcp23x1x.WriteByte(Register.GPIO, 0, Port.PortA);
                    mcp23x1x.WriteByte(Register.GPIO, 0, Port.PortB);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error when SwtichoffAllSlots: {ex.Message}");
            }
            finally
            {
                _ledsPlaying = false;
            }
        }
示例#21
0
        public void CacheInvalidatesWhenReset(TestDevice testDevice)
        {
            Mcp23xxx       device     = testDevice.Device;
            GpioController controller = testDevice.Controller;

            // Check the output latches after enabling and setting
            // different bits.
            device.Enable();
            for (int i = 0; i < 4; i++)
            {
                controller.OpenPin(i, PinMode.Output);
            }

            controller.Write(0, PinValue.High);
            Assert.Equal(1, device.ReadByte(Register.OLAT));
            controller.Write(1, PinValue.High);
            Assert.Equal(3, device.ReadByte(Register.OLAT));

            // Flush OLAT
            device.WriteByte(Register.OLAT, 0x00);
            Assert.Equal(0, device.ReadByte(Register.OLAT));

            // Now setting the next bit will pick back up our cached 3
            controller.Write(2, PinValue.High);
            Assert.Equal(7, device.ReadByte(Register.OLAT));

            // Re-enabling will reset the cache
            device.WriteByte(Register.OLAT, 0x00);
            device.Disable();
            device.Enable();
            controller.Write(3, PinValue.High);
            Assert.Equal(8, device.ReadByte(Register.OLAT));

            device.WriteByte(Register.OLAT, 0x02);
            device.Disable();
            device.Enable();
            controller.Write(0, PinValue.High);
            Assert.Equal(3, device.ReadByte(Register.OLAT));
        }
示例#22
0
        private static void ReadSwitchesWriteLeds(Mcp23xxx mcp23xxx)
        {
            Console.WriteLine("Read Switches & Write LEDs");

            using (mcp23xxx)
            {
                // Input direction for switches.
                mcp23xxx.Write(Register.Address.IODIR, 0b1111_1111, Port.PortA, Bank.Bank0);
                // Output direction for LEDs.
                mcp23xxx.Write(Register.Address.IODIR, 0b0000_0000, Port.PortB, Bank.Bank0);

                while (true)
                {
                    // Read switches.
                    byte data = mcp23xxx.Read(Register.Address.GPIO, Port.PortA, Bank.Bank0);
                    // Write data to LEDs.
                    mcp23xxx.Write(Register.Address.GPIO, data, Port.PortB, Bank.Bank0);
                    Console.WriteLine(data);
                    Thread.Sleep(500);
                }
            }
        }
        public static async Task <bool> DirectMarkPosition(Mcp23xxx mcp23xxxWrite, int position)
        {
            // Position is a value between 1 and 16 (or 0 is all occupied)

            try
            {
                while (_ledsPlaying)
                {
                    // let the previous light show end.
                    await Task.Delay(5);
                }

                _ledsPlaying = true;

                Mcp23x1x mcp23x1x = null;

                if (mcp23xxxWrite != null)
                {
                    mcp23x1x = mcp23xxxWrite as Mcp23x1x;
                }

                if (mcp23x1x == null)
                {
                    Console.WriteLine("DirectMarkPosition: Unable to cast Mcp23017 Write GPIO.");

                    return(false);
                }
                else
                {
                    mcp23x1x.WriteByte(Register.GPIO, 0, Port.PortA);
                    mcp23x1x.WriteByte(Register.GPIO, 0, Port.PortB);

                    var port = position <= 8 ? Port.PortA : Port.PortB;

                    byte bPos = position <= 8
                                        ? (byte)Math.Pow(2, position - 1)
                                        : (byte)Math.Pow(2, position - 9);

                    for (var i = 0; i < 50; i++)
                    {
                        if (position == 0)
                        {
                            Console.Write("Skip blink. ");
                            continue;
                        }

                        // blink led on i % 2 on else off
                        var j = (i % 2) == 0 ? bPos : 0;

                        mcp23x1x.WriteByte(Register.GPIO, (byte)j, port);

                        await Task.Delay(100);
                    }

                    // TODO : blinking for 0 => alle positions occupied
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                _ledsPlaying = false;
            }

            return(true);
        }
示例#24
0
 public TestDevice(Mcp23xxx device, Mcp23xxxChipMock chipMock)
 {
     Device   = device;
     ChipMock = chipMock;
 }
        public static async Task <bool> DirectFirstEmptySlot(Mcp23xxx mcp23xxxWrite, int firstEmptySlot)
        {
            if (firstEmptySlot == 0)
            {
                Console.WriteLine("Skip blink.");
                return(true);
            }

            try
            {
                while (_ledsPlaying)
                {
                    // let the previous light show end.
                    await Task.Delay(5);
                }

                _ledsPlaying = true;

                Mcp23x1x mcp23x1x = null;

                if (mcp23xxxWrite != null)
                {
                    mcp23x1x = mcp23xxxWrite as Mcp23x1x;
                }

                if (mcp23x1x == null)
                {
                    Console.WriteLine("DirectFirstEmptySlot: Unable to cast Mcp23017 Write GPIO.");

                    return(false);
                }
                else
                {
                    mcp23x1x.WriteByte(Register.GPIO, 0, Port.PortA);
                    mcp23x1x.WriteByte(Register.GPIO, 0, Port.PortB);

                    var port = firstEmptySlot <= 8 ? Port.PortA : Port.PortB;

                    byte bPos = firstEmptySlot <= 8
                                        ? (byte)Math.Pow(2, firstEmptySlot - 1)
                                        : (byte)Math.Pow(2, firstEmptySlot - 9);

                    for (var i = 0; i < 25; i++)
                    {
                        // blink led on i % 2 on else off
                        var j = (i % 2) == 0 ? bPos : 0;

                        Console.Write($"Lit {j}. ");

                        mcp23x1x.WriteByte(Register.GPIO, (byte)j, port);

                        await Task.Delay(100);
                    }

                    Console.WriteLine();
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                _ledsPlaying = false;
            }

            return(true);
        }
示例#26
0
 public TestDevice(Mcp23xxx device, Mcp23xxxChipMock chipMock)
 {
     Device     = device;
     ChipMock   = chipMock;
     Controller = new GpioController(PinNumberingScheme.Logical, Device);
 }