/// <summary> /// Constructor for a PCA9534 (or similar) GPIO Expander. /// </summary> /// <param name="bus">I<sup>2</sup>C bus controller.</param> /// <param name="addr">I<sup>2</sup>C slave address.</param> /// <param name="config">GPIO pin configuration.</param> /// <param name="states">Initial output states.</param> public Device(IO.Interfaces.I2C.Bus bus, int addr, byte config = AllInputs, byte states = AllOff) { this.dev = new IO.Interfaces.I2C.Device(bus, addr); this.Write(ConfigurationReg, config); this.Write(InputPolarityReg, AllNormal); this.Write(OutputPortReg, states); }
/// <summary> /// Constructor for an HDC1080 temperature and humidity sensor object. /// </summary> /// <param name="bus">I<sup>2</sup>C bus controller.</param> public Device(IO.Interfaces.I2C.Bus bus) { this.dev = new IO.Interfaces.I2C.Device(bus, 0x40); // Issue software reset this.Write(RegConfiguration, 0x8000); System.Threading.Thread.Sleep(100); // Heater off, acquire temp or humidity, 14 bit resolutions this.Write(RegConfiguration, 0x0000); }
/// <summary> /// Constructor for a single MCP23017 device. /// </summary> /// <param name="bus">I<sup>2</sup>C bus controller object.</param> /// <param name="addr">I<sup>2</sup>C slave address.</param> public Device(IO.Interfaces.I2C.Bus bus, int addr) { // Create an I2C device object dev = new IO.Interfaces.I2C.Device(bus, addr); // Configure the MCP23017 WriteRegister8(IOCON, 0x00); WriteRegister16(IODIR, 0xFFFF); WriteRegister16(IPOL, 0x0000); WriteRegister16(GPINTEN, 0x0000); WriteRegister16(DEFVAL, 0x0000); WriteRegister16(INTCON, 0x0000); }
/// <summary> /// Constructor for a single AD5593R device. /// </summary> /// <param name="bus">I<sup>2</sup>C bus controller object.</param> /// <param name="addr">I<sup>2</sup>C slave address.</param> public Device(IO.Interfaces.I2C.Bus bus, int addr) { // Create an I2C device object dev = new IO.Interfaces.I2C.Device(bus, addr); // Configure the ADC5593R. WriteRegister(MODE_CONFIGURATION, REG_CONTROL, reg_control); WriteRegister(MODE_CONFIGURATION, REG_POWERDOWN, reg_powerdown); WriteRegister(MODE_CONFIGURATION, REG_LDAC_MODE, 0x0000); WriteRegister(MODE_CONFIGURATION, REG_ADC_PIN_CONFIG, reg_pin_config_adc); WriteRegister(MODE_CONFIGURATION, REG_DAC_PIN_CONFIG, reg_pin_config_dac); WriteRegister(MODE_CONFIGURATION, REG_GPIO_READ_CONFIG, reg_pin_config_gpio_read); WriteRegister(MODE_CONFIGURATION, REG_GPIO_WRITE_CONFIG, reg_pin_config_gpio_write); WriteRegister(MODE_CONFIGURATION, REG_PULLDOWN_CONFIG, reg_pin_config_pulldown); WriteRegister(MODE_CONFIGURATION, REG_OPENDRAIN_CONFIG, reg_pin_config_opendrain); WriteRegister(MODE_CONFIGURATION, REG_TRISTATE_CONFIG, reg_pin_config_tristate); WriteRegister(MODE_CONFIGURATION, REG_GPIO_WRITE_DATA, reg_gpio_write_data); }
/// <summary> /// Constructor for a single PCA9685 device. /// </summary> /// <param name="bus">I<sup>2</sup>C bus controller object.</param> /// <param name="addr">I<sup>2</sup>C slave address.</param> /// <param name="freq">PWM pulse frequency. Default is 50 Hz.</param> /// <param name="clock">PCA9685 clock source. /// Use <c>INTERNAL_CLOCK</c>c> to select the internal 25 MHz clock generator. /// </param> public Device(IO.Interfaces.I2C.Bus bus, int addr, int freq = 50, int clock = INTERNAL_CLOCK) { // Validate parameters if (freq < 1) { throw new System.Exception("Invalid PWM pulse frequency"); } if ((clock != INTERNAL_CLOCK) && ((clock < MIN_CLOCK) || (clock > MAX_CLOCK))) { throw new System.Exception("Invalid clock frequency"); } dev = new IO.Interfaces.I2C.Device(bus, addr); this.freq = freq; cmd = new byte[5]; resp = new byte[4]; if (clock == INTERNAL_CLOCK) { // Use internal 25 MHz clock WriteRegister((byte)Registers.MODE1, 0x30); // Set SLEEP WriteRegister((byte)Registers.PRE_SCALE, (byte)(25000000 / 4096 / freq - 1)); WriteRegister((byte)Registers.MODE1, 0x20); // Clear SLEEP } else { // Use external clock WriteRegister((byte)Registers.MODE1, 0x70); // Set SLEEP WriteRegister((byte)Registers.PRE_SCALE, (byte)(clock / 4096 / freq - 1)); WriteRegister((byte)Registers.MODE1, 0x60); // Clear SLEEP } WriteRegister((byte)Registers.MODE2, 0x0C); }
/// <summary> /// Constructor for an TH02 temperature and humidity sensor object. /// </summary> /// <param name="bus">I<sup>2</sup>C bus controller.</param> public Device(IO.Interfaces.I2C.Bus bus) { dev = new IO.Interfaces.I2C.Device(bus, 0x40); Write(regConfig, cmdInit); }
/// <summary> /// Constructor for a PCA8574 (or similar) GPIO Expander. /// </summary> /// <param name="bus">I<sup>2</sup>C bus controller.</param> /// <param name="addr">I<sup>2</sup>C slave address.</param> /// <param name="states">Initial output states.</param> public Device(IO.Interfaces.I2C.Bus bus, int addr, byte states = 0xFF) { this.dev = new IO.Interfaces.I2C.Device(bus, addr); this.Write(states); }