Ejemplo n.º 1
0
        public static MCP23008GpioController CreateDevice(string i2cBus, int address = HARDWARE_BASE_ADDRESS, I2cBusSpeed busSpeed = I2cBusSpeed.StandardMode, I2cSharingMode sharingMode = I2cSharingMode.Exclusive, IGpioPin interruptPin = null)
        {
            // Create the I2c connection settings instance.
            I2cConnectionSettings settings = new I2cConnectionSettings(address)
            {
                BusSpeed = busSpeed, SharingMode = sharingMode
            };

            // Create the I2c device instance
            var i2cDevice = I2cDevice.FromId(i2cBus, settings);

            if (i2cDevice == null)
            {
                // No device was created
                throw new Exception("Unable to create I2c instance.");
            }

            try
            {
                var instance = new MCP23008GpioController(i2cDevice, interruptPin);

                // Set the defaults for our device. These are power on defaults
                instance.WriteRegister(MCP23008_IODIR, 0xFF);
                instance.WriteRegister(MCP23008_IPOL, 0x00);
                instance.WriteRegister(MCP23008_GPINTEN, 0x00);
                instance.WriteRegister(MCP23008_DEFVAL, 0x00);
                instance.WriteRegister(MCP23008_INTCON, 0x00);
                instance.WriteRegister(MCP23008_IOCON, 0x00);
                instance.WriteRegister(MCP23008_GPPU, 0x00);
                instance.WriteRegister(MCP23008_GPIO, 0x00);
                instance.WriteRegister(MCP23008_OLAT, 0x00);

                return(instance);
            }
            catch (Exception)
            {
                i2cDevice?.Dispose();
                throw;
            }
        }
Ejemplo n.º 2
0
 internal MCP23008GpioPin(MCP23008GpioController gpioController, int pinNumber, GpioSharingMode sharingMode)
 {
     _gpioController = gpioController;
     PinNumber       = pinNumber;
     SharingMode     = sharingMode;
 }