/// <summary> /// Force the sensor to make a reading and update the relevanyt properties. /// </summary> /// <param name="startAddress">Start address for the read operation.</param> /// <param name="amount">Amount of data to read from the EEPROM.</param> public byte[] Read(ushort startAddress, ushort amount) { CheckAddress(startAddress, amount); var address = new byte[2]; address[0] = (byte)((startAddress >> 8) & 0xff); address[1] = (byte)(startAddress & 0xff); _eeprom.WriteBytes(address); return(_eeprom.ReadBytes(amount)); }
/// <summary> /// Send a command to the display. /// </summary> /// <param name="command">Command byte to send to the display.</param> private void SendCommand(byte command) { if (connectionType == ConnectionType.SPI) { dataCommandPort.State = Command; spiDisplay.WriteByte(command); } else { i2cPeriferal.WriteBytes(new byte[] { 0x00, command }); } }
/// <summary> /// Send a command to the display. /// </summary> /// <param name="command">Command byte to send to the display.</param> protected virtual void SendCommand(byte command) { if (connectionType == ConnectionType.SPI) { dataCommandPort.State = Command; _spiPeripheral.WriteByte(command); } else { _I2cPeripheral.WriteBytes(new byte[] { 0x00, command }); } }
/// <summary> /// Update the temperature and pressure from the sensor and set the Pressure property. /// </summary> public async Task Update() { // // Tell the sensor to take a temperature and pressure reading, wait for // 3ms (see section 2.2 of the datasheet) and then read the ADC values. // mpl115a2.WriteBytes(new byte[] { Registers.StartConversion, 0x00 }); await Task.Delay(5); var data = mpl115a2.ReadRegisters(Registers.PressureMSB, 4); // // Extract the sensor data, note that this is a 10-bit reading so move // the data right 6 bits (see section 3.1 of the datasheet). // var pressure = (ushort)(((data[0] << 8) + data[1]) >> 6); var temperature = (ushort)(((data[2] << 8) + data[3]) >> 6); Conditions.Temperature = (float)((temperature - 498.0) / -5.35) + 25; // // Now use the calculations in section 3.2 to determine the // current pressure reading. // const double PRESSURE_CONSTANT = 65.0 / 1023.0; var compensatedPressure = coefficients.A0 + ((coefficients.B1 + (coefficients.C12 * temperature)) * pressure) + (coefficients.B2 * temperature); Conditions.Pressure = (float)(PRESSURE_CONSTANT * compensatedPressure) + 50; }
//Set sensor resolution /*******************************************************************************************/ //Sets the sensor resolution to one of four levels //Page 12: // 0/0 = 12bit RH, 14bit Temp // 0/1 = 8bit RH, 12bit Temp // 1/0 = 10bit RH, 13bit Temp // 1/1 = 11bit RH, 11bit Temp //Power on default is 0/0 void SetResolution(SensorResolution resolution) { byte userData = si7021.ReadRegister(READ_USER_REGISTER); //Go get the current register state //userRegister &= 0b01111110; //Turn off the resolution bits //resolution &= 0b10000001; //Turn off all other bits but resolution bits //userRegister |= resolution; //Mask in the requested resolution bits var res = (byte)resolution; userData &= 0x73; //Turn off the resolution bits res &= 0x81; //Turn off all other bits but resolution bits userData |= res; //Mask in the requested resolution bits //Request a write to user register si7021.WriteBytes(new byte[] { WRITE_USER_REGISTER }); //Write to the user register si7021.WriteBytes(new byte[] { userData }); //Write the new resolution bits }
private async Task Update() { byte[] readBuffer; BitArray bufferBits; //Send the command to the sensor to tell it to do the thing. i2cPeripheral.WriteBytes(mprlsMeasurementCommand); //Datasheet says wait 5 ms. await Task.Delay(5); while (true) { readBuffer = i2cPeripheral.ReadBytes(1); bufferBits = new BitArray(readBuffer); //From section 6.5 of the datasheet. this.IsDevicePowered = bufferBits[6]; this.IsDeviceBusy = bufferBits[5]; this.HasMemoryIntegrityFailed = bufferBits[2]; this.InternalMathSaturated = bufferBits[0]; if (this.InternalMathSaturated) { throw new InvalidOperationException("Sensor pressure has exceeded max value!"); } if (this.HasMemoryIntegrityFailed) { throw new InvalidOperationException("Sensor internal memory integrity check failed!"); } if (!(this.IsDeviceBusy)) { break; } } readBuffer = i2cPeripheral.ReadBytes(4); RawPSIMeasurement = (readBuffer[1] << 16) | (readBuffer[2] << 8) | readBuffer[3]; //Console.WriteLine(RawPSIMeasurement); //From Section 8.0 of the datasheet. CalculatedPSIMeasurement = (RawPSIMeasurement - 1677722) * (psiMax - psiMin); //Console.WriteLine(CalculatedPSIMeasurement); CalculatedPSIMeasurement /= 15099494 - 1677722; //Console.WriteLine(CalculatedPSIMeasurement); CalculatedPSIMeasurement += psiMin; //Console.WriteLine(CalculatedPSIMeasurement); //https://www.justintools.com/unit-conversion/pressure.php?k1=psi&k2=millibars CalculatedhPAMeasurement = CalculatedPSIMeasurement * 68.947572932; //Console.WriteLine(CalculatedhPAMeasurement); Conditions.Pressure = CalculatedPSIMeasurement; }
private long ReadUncompensatedTemperature() { // write register address bmp085.WriteBytes(new byte[] { 0xF4, 0x2E }); // Required as per datasheet. Thread.Sleep(5); // write register address bmp085.WriteBytes(new byte[] { 0xF6 }); // get MSB and LSB result byte[] data = new byte[2]; data = bmp085.ReadBytes(2); return((data[0] << 8) | data[1]); }
/// <summary> /// Get ASG01DB VOC Gas Concentration /// </summary> /// <returns>Concentration (ppm)</returns> public double GetConcentration() { var data = new byte[] { ASG_DATA_MSB, ASG_DATA_LSB }; sensor.WriteBytes(data); var readBuffer = sensor.ReadBytes(3); // CRC check // if (!CheckCrc8(readBuffer, 2, readBuffer[1])) // { // return -1; // } ushort res = BinaryPrimitives.ReadUInt16BigEndian(readBuffer.AsSpan(0, 2)); return(res / 10.0); }
public byte[] Execute(int returnBytes, out UInt16 ResultCode) { lock (lockObject) { sas.WriteBytes(WriteBufferData); #if DEBUGSERIAL Console.WriteLine($"TX: {BitConverter.ToString(WriteBufferData,0,WriteBufferData.Length)}"); #endif return(Read(returnBytes, out ResultCode)); } }
void TransmitData() { i2cPeripheral.WriteBytes(transmissionData); Thread.Sleep(100); }