示例#1
0
        /// <summary>
        /// Constructor for a single MCP23S17 device.
        /// </summary>
        /// <param name="dev">SPI slave device object.</param>
        public Device(IO.Interfaces.SPI.Device dev)
        {
            this.dev = dev;

            // Configure the MCP23S17

            WriteRegister8(IOCON, 0x00);
            WriteRegister16(IODIR, 0xFFFF);
            WriteRegister16(IPOL, 0x0000);
            WriteRegister16(GPINTEN, 0x0000);
            WriteRegister16(DEFVAL, 0x0000);
            WriteRegister16(INTCON, 0x0000);
        }
示例#2
0
        /// <summary>
        /// Constructor for a chain of one or more SN74HC595 shift registers.
        /// </summary>
        /// <param name="dev">SPI device object.</param>
        /// <param name="stages">Number of stages in the chain.</param>
        /// <param name="initialstate">Initial shift register chain state.</param>
        public Device(IO.Interfaces.SPI.Device dev, int stages = 1, byte[] initialstate = null)
        {
            // Validate parameters

            if (stages < 1)
            {
                throw new System.Exception("stages paramter is invalid");
            }

            if (initialstate != null)
            {
                if (initialstate.Length != stages)
                {
                    throw new System.Exception("initialstate parameter length is invalid");
                }
            }

            // Save the SPI device object

            spidev = dev;

            // Allocate shift register state buffer

            statebuf = new byte[stages];

            // Initialize shift register state buffer

            if (initialstate == null)
            {
                for (int i = 0; i < state.Length; i++)
                {
                    statebuf[i] = 0;
                }
            }
            else
            {
                statebuf = initialstate;
            }

            // Shift out initial register state

            spidev.Write(statebuf, statebuf.Length);
        }