/// <summary> /// Read sensor in single-shot mode. /// </summary> /// <param name="setting"></param> /// <returns></returns> public async Task <ADS1115SensorData> readSingleShot(ADS1115SensorSetting setting) { if (setting.Mode != AdcMode.SINGLESHOOT_CONVERSION) { throw new InvalidOperationException("You can only read in single shot mode"); } var sensorData = new ADS1115SensorData(); int temp = await ReadSensorAsync(configA(setting), configB(setting)); //read sensor with the generated configuration bytes sensorData.DecimalValue = temp; //calculate the voltage with different resolutions in single ended and in differential mode if ((byte)setting.Input <= 0x03) { sensorData.VoltageValue = DecimalToVoltage(setting.Pga, temp, ADC_RES); } else { sensorData.VoltageValue = DecimalToVoltage(setting.Pga, temp, ADC_HALF_RES); } fastReadAvailable = false; return(sensorData); }
/// <summary> /// You have to initialize before use readContinuous and you have to do it every time when you write into the configuration register. /// </summary> /// <param name="setting"></param> /// <returns></returns> public async Task readContinuousInit(ADS1115SensorSetting setting) { if (setting.Mode != AdcMode.CONTINOUS_CONVERSION) { throw new InvalidOperationException("You can only read in continuous mode"); } var command = new byte[] { ADC_REG_POINTER_CONFIG, configA(setting), configB(setting) }; adc.Write(command); await Task.Delay(10); var writeBuffer = new byte[] { ADC_REG_POINTER_CONVERSION }; adc.Write(writeBuffer); fastReadAvailable = true; }
// creates the second byte of the configuration register from the ADS1115SensorSetting object private byte configB(ADS1115SensorSetting setting) { byte configB; return(configB = (byte)((byte)setting.DataRate << 5 | (byte)setting.ComMode << 4 | (byte)setting.ComPolarity << 3 | (byte)setting.ComLatching << 2 | (byte)setting.ComQueue)); }
// creates the byte byte of the configuration register from the ADS1115SensorSetting object private byte configA(ADS1115SensorSetting setting) { byte configA = 0; return(configA = (byte)((byte)setting.Mode << 7 | (byte)setting.Input << 4 | (byte)setting.Pga << 1 | (byte)setting.Mode)); }