public async Task<bool> OpenConnection(string i2cMasterId) { //Establish I2C connection __connection = await I2cDevice.FromIdAsync(i2cMasterId, new I2cConnectionSettings(this.__i2cAddress)); // soft reset I2cTransferResult result = __connection.WritePartial(new byte[] { Registers.MPR121_SOFTRESET, 0x63 }); if (result.Status == I2cTransferStatus.SlaveAddressNotAcknowledged) { throw new Exception(string.Format("MPR121 at address {0} not responding.", this.__i2cAddress)); } await Task.Delay(1); writeRegister(Registers.MPR121_ECR, 0x0); byte c = readRegister8(Registers.MPR121_CONFIG2); if (c != 0x24) return false; SetThresholds(12, 6); //Section A Registers - Ref: AN3944, MPR121 Quick Start Guide //This group of setting controls the filtering of the system when the data is greater than the baseline. //Settings from Adafruit Libary.. Most probably callibrated for the Adafruit's MPR121 breakout board. writeRegister(Registers.MPR121_MHDR, 0x01); writeRegister(Registers.MPR121_NHDR, 0x01); writeRegister(Registers.MPR121_NCLR, 0x0E); writeRegister(Registers.MPR121_FDLR, 0x00); //Section B Registers - Ref: AN3944, MPR121 Quick Start Guide writeRegister(Registers.MPR121_MHDF, 0x01); writeRegister(Registers.MPR121_NHDF, 0x05); writeRegister(Registers.MPR121_NCLF, 0x01); writeRegister(Registers.MPR121_FDLF, 0x00); writeRegister(Registers.MPR121_NHDT, 0x00); writeRegister(Registers.MPR121_NCLT, 0x00); writeRegister(Registers.MPR121_FDLT, 0x00); writeRegister(Registers.MPR121_DEBOUNCE, 0); writeRegister(Registers.MPR121_CONFIG1, 0x10); // default, 16uA charge current writeRegister(Registers.MPR121_CONFIG2, 0x20); // 0.5uS encoding, 1ms period writeRegister(Registers.MPR121_ECR, 0x8F); // start with first 5 bits of baseline tracking InitMPR121TouchInterrupt(); return true; }
public async Task I2CInit(byte slaveAddress, I2cBusSpeed busSpeed, I2cSharingMode sharingMode) { // get aqs filter to find i2c device string aqs = I2cDevice.GetDeviceSelector("I2C1"); // Find the I2C bus controller with our selector string var dis = await DeviceInformation.FindAllAsync(aqs); if (dis.Count == 0) throw new Exception("There is no I2C device"); // bus not found I2cConnectionSettings settings = new I2cConnectionSettings(slaveAddress); // Create an I2cDevice with our selected bus controller and I2C settings _i2CDevice = await I2cDevice.FromIdAsync(dis[0].Id, settings); if (_i2CDevice == null) return; var status = _i2CDevice.WritePartial(new byte[] { 0x00 }); if (status.Status == I2cTransferStatus.FullTransfer) _isConnected = true; else if (status.Status == I2cTransferStatus.PartialTransfer) throw new Exception("There was an error during sending test data."); else if (status.Status == I2cTransferStatus.SlaveAddressNotAcknowledged) throw new Exception("I2C Device is not connected."); }