Fuckup with I2C
This commit is contained in:
		
							parent
							
								
									588e778f10
								
							
						
					
					
						commit
						311b8d2b1b
					
				@ -5,232 +5,270 @@ using Unosquare.RaspberryIO.Gpio;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
namespace BlubbFish.Iot.Thermometer.Librarys {
 | 
					namespace BlubbFish.Iot.Thermometer.Librarys {
 | 
				
			||||||
  class Bme280 {
 | 
					  class Bme280 {
 | 
				
			||||||
    public Bme280(Int32 address) => this.w = Pi.I2C.AddDevice(address);
 | 
					    public Bme280(Int32 address) => this.w = Pi.I2C.AddDevice(address);
 | 
				
			||||||
 | 
					 | 
				
			||||||
    public void Begin() {
 | 
					 | 
				
			||||||
      if(this.Read8((Byte)Register.BME280_REG_ID) != 0x60) {
 | 
					 | 
				
			||||||
        Helper.WriteError("Fail to init Barometer\n");
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
      this.ReadTrimming();
 | 
					 | 
				
			||||||
      //Humidity oversampling 16X oversampling [2..0]
 | 
					 | 
				
			||||||
      this.WriteRegister((Byte)Register.BME280_REG_CTRL_HUM, 0b00000101);
 | 
					 | 
				
			||||||
      //Pressure oversampling 16X oversampling [7..5], Temperature oversampling 16X oversampling [4..2], Mode Normal mode [1..0]
 | 
					 | 
				
			||||||
      this.WriteRegister((Byte)Register.BME280_REG_CTRL_MEAS, 0b10110111);
 | 
					 | 
				
			||||||
      //inactive duration 250 ms [7..5], IIR filter Filter coefficient 16 [4..2], SPI Interface off [0]
 | 
					 | 
				
			||||||
      this.WriteRegister((Byte)Register.BME280_REG_CONFIG, 0b01110000);
 | 
					 | 
				
			||||||
      Console.WriteLine("Barometer ok\n");
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public void Measure() {
 | 
					 | 
				
			||||||
      this._adc_T = (Int32)this.Read20((Byte)Register.BME280_REG_TEMP_DATA);
 | 
					 | 
				
			||||||
      this._adc_P = (Int32)this.Read20((Byte)Register.BME280_REG_PRESS_DATA);
 | 
					 | 
				
			||||||
      this._adc_H = this.Read16((Byte)Register.BME280_REG_HUM_DATA);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public Double GetTemperature() {
 | 
					 | 
				
			||||||
      Int32 adc_T = this._adc_T;
 | 
					 | 
				
			||||||
      Int32 var1, var2;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      var1 = (((adc_T >> 3) - (this._dig_T1 << 1)) * this._dig_T2) >> 11;
 | 
					 | 
				
			||||||
      var2 = (((((adc_T >> 4) - this._dig_T1) * ((adc_T >> 4) - this._dig_T1)) >> 12) * this._dig_T3) >> 14;
 | 
					 | 
				
			||||||
      this._t_fine = var1 + var2;
 | 
					 | 
				
			||||||
      return ((Double)(this._t_fine * 5 + 128 >> 8)) / 100;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
      /*adc_T >>= 4;
 | 
					    public void Begin() {
 | 
				
			||||||
      var1 = (((adc_T >> 3) - ((int32_t)(this->_dig_T1 << 1))) * ((int32_t)this->_dig_T2)) >> 11;
 | 
					      if(this.Read8((Byte)Register.BME280_REG_ID) != 0x60) {
 | 
				
			||||||
      var2 = (((((adc_T >> 4) - ((int32_t)this->_dig_T1)) * ((adc_T >> 4) - ((int32_t)this->_dig_T1))) >> 12) * ((int32_t)this->_dig_T3)) >> 14;
 | 
					        Helper.WriteError("Fail to init Barometer\n");
 | 
				
			||||||
      t_fine = var1 + var2;
 | 
					      }
 | 
				
			||||||
      float T = (t_fine * 5 + 128) >> 8;
 | 
					      this.ReadTrimming();
 | 
				
			||||||
      return T/100;*/
 | 
					      //Humidity oversampling 16X oversampling [2..0]
 | 
				
			||||||
    }
 | 
					      this.WriteRegister((Byte)Register.BME280_REG_CTRL_HUM, 0b00000101);
 | 
				
			||||||
 | 
					      //Pressure oversampling 16X oversampling [7..5], Temperature oversampling 16X oversampling [4..2], Mode Normal mode [1..0]
 | 
				
			||||||
    public Double GetPressure() {
 | 
					      this.WriteRegister((Byte)Register.BME280_REG_CTRL_MEAS, 0b10110111);
 | 
				
			||||||
      Int32 adc_P = this._adc_P;
 | 
					      //inactive duration 250 ms [7..5], IIR filter Filter coefficient 16 [4..2], SPI Interface off [0]
 | 
				
			||||||
      Int64 var1, var2, P;
 | 
					      this.WriteRegister((Byte)Register.BME280_REG_CONFIG, 0b01110000);
 | 
				
			||||||
 | 
					      Console.WriteLine("Barometer ok\n");
 | 
				
			||||||
      var1 = ((Int64)this._t_fine) - 128000;
 | 
					 | 
				
			||||||
      var2 = var1 * var1 * this._dig_P6;
 | 
					 | 
				
			||||||
      var2 = var2 + ((var1 * this._dig_P5) << 17);
 | 
					 | 
				
			||||||
      var2 = var2 + (((Int64)this._dig_P4) << 35);
 | 
					 | 
				
			||||||
      var1 = ((var1 * var1 * this._dig_P3) >> 8) + ((var1 * this._dig_P2) << 12);
 | 
					 | 
				
			||||||
      var1 = ((((Int64)1) << 47) + var1) * this._dig_P1 >> 33;
 | 
					 | 
				
			||||||
      if(var1 == 0) {
 | 
					 | 
				
			||||||
        return 0.0; // avoid exception caused by division by zero
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
      P = 1048576 - adc_P;
 | 
					 | 
				
			||||||
      P = (((P << 31) - var2) * 3125) / var1;
 | 
					 | 
				
			||||||
      var1 = (this._dig_P9 * (P >> 13) * (P >> 13)) >> 25;
 | 
					 | 
				
			||||||
      var2 = (this._dig_P8 * P) >> 19;
 | 
					 | 
				
			||||||
      P = ((P + var1 + var2) >> 8) + (((Int64)this._dig_P7) << 4);
 | 
					 | 
				
			||||||
      return ((Double)((UInt32)P)) / 25600;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      /*adc_P >>= 4;
 | 
					 | 
				
			||||||
      var1 = ((int64_t)t_fine) - 128000;
 | 
					 | 
				
			||||||
      var2 = var1 * var1 * (int64_t)this->_dig_P6;
 | 
					 | 
				
			||||||
      var2 = var2 + ((var1*(int64_t)this->_dig_P5)<<17);
 | 
					 | 
				
			||||||
      var2 = var2 + (((int64_t)this->_dig_P4)<<35);
 | 
					 | 
				
			||||||
      var1 = ((var1 * var1 * (int64_t)this->_dig_P3)>>8) + ((var1 * (int64_t)this->_dig_P2)<<12);
 | 
					 | 
				
			||||||
      var1 = (((((int64_t)1)<<47)+var1))*((int64_t)this->_dig_P1)>>33;
 | 
					 | 
				
			||||||
      if (var1 == 0) {
 | 
					 | 
				
			||||||
        return 0; // avoid exception caused by division by zero
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
      p = 1048576-adc_P;
 | 
					 | 
				
			||||||
      p = (((p<<31)-var2)*3125)/var1;
 | 
					 | 
				
			||||||
      var1 = (((int64_t)this->_dig_P9) * (p>>13) * (p>>13)) >> 25;
 | 
					 | 
				
			||||||
      var2 = (((int64_t)this->_dig_P8) * p) >> 19;
 | 
					 | 
				
			||||||
      p = ((p + var1 + var2) >> 8) + (((int64_t)this->_dig_P7)<<4);
 | 
					 | 
				
			||||||
      return ((float)(uint32_t)p/256)/100;*/
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public Double GetHumidity() {
 | 
					 | 
				
			||||||
      Int32 adc_H = this._adc_H;
 | 
					 | 
				
			||||||
      Int32 H;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      H = this._t_fine - 76800;
 | 
					 | 
				
			||||||
      H = ((((adc_H << 14) - (this._dig_H4 << 20) - (this._dig_H5 * H)) + 16384) >> 15) *
 | 
					 | 
				
			||||||
        (((((((H * this._dig_H6) >> 10) * (((H * this._dig_H3) >> 11) + 32768)) >> 10) + 2097152) * this._dig_H2 + 8192) >> 14);
 | 
					 | 
				
			||||||
      H = H - (((((H >> 15) * (H >> 15)) >> 7) * this._dig_H1) >> 4);
 | 
					 | 
				
			||||||
      H = H < 0 ? 0 : H;
 | 
					 | 
				
			||||||
      H = H > 419430400 ? 419430400 : H;
 | 
					 | 
				
			||||||
      return ((Double)(H >> 12)) / 1024;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public void ReadTrimming() {
 | 
					 | 
				
			||||||
      this._dig_T1 = this.Read16LE((Byte)Calibration.BME280_REG_CALIB00);
 | 
					 | 
				
			||||||
      this._dig_T2 = this.ReadS16LE((Byte)Calibration.BME280_REG_CALIB02);
 | 
					 | 
				
			||||||
      this._dig_T3 = this.ReadS16LE((Byte)Calibration.BME280_REG_CALIB04);
 | 
					 | 
				
			||||||
      this._dig_P1 = this.Read16LE((Byte)Calibration.BME280_REG_CALIB06);
 | 
					 | 
				
			||||||
      this._dig_P2 = this.ReadS16LE((Byte)Calibration.BME280_REG_CALIB08);
 | 
					 | 
				
			||||||
      this._dig_P3 = this.ReadS16LE((Byte)Calibration.BME280_REG_CALIB10);
 | 
					 | 
				
			||||||
      this._dig_P4 = this.ReadS16LE((Byte)Calibration.BME280_REG_CALIB12);
 | 
					 | 
				
			||||||
      this._dig_P5 = this.ReadS16LE((Byte)Calibration.BME280_REG_CALIB14);
 | 
					 | 
				
			||||||
      this._dig_P6 = this.ReadS16LE((Byte)Calibration.BME280_REG_CALIB16);
 | 
					 | 
				
			||||||
      this._dig_P7 = this.ReadS16LE((Byte)Calibration.BME280_REG_CALIB18);
 | 
					 | 
				
			||||||
      this._dig_P8 = this.ReadS16LE((Byte)Calibration.BME280_REG_CALIB20);
 | 
					 | 
				
			||||||
      this._dig_P9 = this.ReadS16LE((Byte)Calibration.BME280_REG_CALIB22);
 | 
					 | 
				
			||||||
      this._dig_H1 = this.Read8((Byte)Calibration.BME280_REG_CALIB25);
 | 
					 | 
				
			||||||
      this._dig_H2 = this.ReadS16LE((Byte)Calibration.BME280_REG_CALIB26);
 | 
					 | 
				
			||||||
      this._dig_H3 = this.Read8((Byte)Calibration.BME280_REG_CALIB28);
 | 
					 | 
				
			||||||
      this._dig_H4 = (Int16)((this.Read8((Byte)Calibration.BME280_REG_CALIB29) << 4) | (0b00001111 & this.Read8((Byte)Calibration.BME280_REG_CALIB30)));
 | 
					 | 
				
			||||||
      this._dig_H5 = (Int16)((this.Read8((Byte)Calibration.BME280_REG_CALIB31) << 4) | ((0b11110000 & this.Read8((Byte)Calibration.BME280_REG_CALIB30)) >> 4));
 | 
					 | 
				
			||||||
      this._dig_H6 = (SByte)this.Read8((Byte)Calibration.BME280_REG_CALIB32);
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private readonly I2CDevice w;
 | 
					    public void Measure() {
 | 
				
			||||||
    private Int32 _adc_T;
 | 
					      this._adc_T = (Int32)this.Read20((Byte)Register.BME280_REG_TEMP_DATA);
 | 
				
			||||||
    private Int32 _adc_P;
 | 
					      this._adc_P = (Int32)this.Read20((Byte)Register.BME280_REG_PRESS_DATA);
 | 
				
			||||||
    private Int32 _adc_H;
 | 
					      //this._adc_H = this.Read16((Byte)Register.BME280_REG_HUM_DATA);
 | 
				
			||||||
    private Int32 _t_fine;
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private UInt16 _dig_T1;
 | 
					    public Double GetTemperature() {
 | 
				
			||||||
    private Int16 _dig_T2;
 | 
					      Int32 adc_T = this._adc_T;
 | 
				
			||||||
    private Int16 _dig_T3;
 | 
					      //Int32 var1, var2;
 | 
				
			||||||
    private UInt16 _dig_P1;
 | 
					
 | 
				
			||||||
    private Int16 _dig_P2;
 | 
					      Double var1, var2;
 | 
				
			||||||
    private Int16 _dig_P3;
 | 
					      var1 = (adc_T / 16384.0 - this._dig_T1 / 1024.0) * this._dig_T2;
 | 
				
			||||||
    private Int16 _dig_P4;
 | 
					      var2 = ((adc_T / 131072.0 - this._dig_T1 / 8192.0) * (adc_T / 131072.0 - this._dig_T1 / 8192.0)) * this._dig_T3;
 | 
				
			||||||
    private Int16 _dig_P5;
 | 
					      this._t_fine = (Int32)(var1 + var2);
 | 
				
			||||||
    private Int16 _dig_P6;
 | 
					      return (var1 + var2) / 5120.0;
 | 
				
			||||||
    private Int16 _dig_P7;
 | 
					
 | 
				
			||||||
    private Int16 _dig_P8;
 | 
					      /*var1 = (((adc_T >> 3) - (this._dig_T1 << 1)) * this._dig_T2) >> 11;
 | 
				
			||||||
    private Int16 _dig_P9;
 | 
					      var2 = (((((adc_T >> 4) - this._dig_T1) * ((adc_T >> 4) - this._dig_T1)) >> 12) * this._dig_T3) >> 14;
 | 
				
			||||||
    private Byte _dig_H1;
 | 
					      this._t_fine = var1 + var2;
 | 
				
			||||||
    private Int16 _dig_H2;
 | 
					      return ((Double)(this._t_fine * 5 + 128 >> 8)) / 100;*/
 | 
				
			||||||
    private Byte _dig_H3;
 | 
					
 | 
				
			||||||
    private Int16 _dig_H4;
 | 
					      /*adc_T >>= 4;
 | 
				
			||||||
    private Int16 _dig_H5;
 | 
					      var1 = (((adc_T >> 3) - ((int32_t)(this->_dig_T1 << 1))) * ((int32_t)this->_dig_T2)) >> 11;
 | 
				
			||||||
    private SByte _dig_H6;
 | 
					      var2 = (((((adc_T >> 4) - ((int32_t)this->_dig_T1)) * ((adc_T >> 4) - ((int32_t)this->_dig_T1))) >> 12) * ((int32_t)this->_dig_T3)) >> 14;
 | 
				
			||||||
 | 
					      t_fine = var1 + var2;
 | 
				
			||||||
    //Calibration Register Adresses
 | 
					      float T = (t_fine * 5 + 128) >> 8;
 | 
				
			||||||
    private enum Calibration {
 | 
					      return T/100;*/
 | 
				
			||||||
      BME280_REG_CALIB00 = 0b10001000,
 | 
					    }
 | 
				
			||||||
      BME280_REG_CALIB02 = 0b10001010,
 | 
					
 | 
				
			||||||
      BME280_REG_CALIB04 = 0b10001100,
 | 
					    public Double GetPressure() {
 | 
				
			||||||
      BME280_REG_CALIB06 = 0b10001110,
 | 
					      Int32 adc_P = this._adc_P;
 | 
				
			||||||
      BME280_REG_CALIB08 = 0b10010000,
 | 
					
 | 
				
			||||||
      BME280_REG_CALIB10 = 0b10010010,
 | 
					      Double var1, var2, p;
 | 
				
			||||||
      BME280_REG_CALIB12 = 0b10010100,
 | 
					      var1 = (this._t_fine / 2.0) - 64000.0;
 | 
				
			||||||
      BME280_REG_CALIB14 = 0b10010110,
 | 
					      var2 = var1 * var1 * this._dig_P6 / 32768.0;
 | 
				
			||||||
      BME280_REG_CALIB16 = 0b10011000,
 | 
					      var2 = var2 + var1 * this._dig_P5 * 2.0;
 | 
				
			||||||
      BME280_REG_CALIB18 = 0b10011010,
 | 
					      var2 = (var2 / 4.0) + (this._dig_P4 * 65536.0);
 | 
				
			||||||
      BME280_REG_CALIB20 = 0b10011100,
 | 
					      var1 = (this._dig_P3 * var1 * var1 / 524288.0 + this._dig_P2 * var1) / 524288.0;
 | 
				
			||||||
      BME280_REG_CALIB22 = 0b10011110,
 | 
					      var1 = (1.0 + var1 / 32768.0) * this._dig_P1;
 | 
				
			||||||
      BME280_REG_CALIB25 = 0b10100001,
 | 
					      if(var1 == 0.0) {
 | 
				
			||||||
      BME280_REG_CALIB26 = 0b11100001,
 | 
					        return 0; // avoid exception caused by division by zero
 | 
				
			||||||
      BME280_REG_CALIB28 = 0b11100011,
 | 
					      }
 | 
				
			||||||
      BME280_REG_CALIB29 = 0b11100100,
 | 
					      p = 1048576.0 - adc_P;
 | 
				
			||||||
      BME280_REG_CALIB30 = 0b11100101,
 | 
					      p = (p - (var2 / 4096.0)) * 6250.0 / var1;
 | 
				
			||||||
      BME280_REG_CALIB31 = 0b11100110,
 | 
					      var1 = this._dig_P9 * p * p / 2147483648.0;
 | 
				
			||||||
      BME280_REG_CALIB32 = 0b11100111
 | 
					      var2 = p * this._dig_P8 / 32768.0;
 | 
				
			||||||
    };
 | 
					      return (p + (var1 + var2 + this._dig_P7) / 16.0) / 100.0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    //Register Adresses
 | 
					      /*Int64 var1, var2, P;
 | 
				
			||||||
    private enum Register {
 | 
					
 | 
				
			||||||
      BME280_REG_ID = 0b11010000,
 | 
					      var1 = ((Int64)this._t_fine) - 128000;
 | 
				
			||||||
      BME280_REG_RESET = 0b11100000,
 | 
					      var2 = var1 * var1 * this._dig_P6;
 | 
				
			||||||
      BME280_REG_CTRL_HUM = 0b11110010,
 | 
					      var2 = var2 + ((var1 * this._dig_P5) << 17);
 | 
				
			||||||
      BME280_REG_STATUS = 0b11110011,
 | 
					      var2 = var2 + (((Int64)this._dig_P4) << 35);
 | 
				
			||||||
      BME280_REG_CTRL_MEAS = 0b11110100,
 | 
					      var1 = ((var1 * var1 * this._dig_P3) >> 8) + ((var1 * this._dig_P2) << 12);
 | 
				
			||||||
      BME280_REG_CONFIG = 0b11110101,
 | 
					      var1 = ((((Int64)1) << 47) + var1) * this._dig_P1 >> 33;
 | 
				
			||||||
      BME280_REG_PRESS_DATA = 0b11110111,
 | 
					      if(var1 == 0) {
 | 
				
			||||||
      BME280_REG_TEMP_DATA = 0b11111010,
 | 
					        return 0.0; // avoid exception caused by division by zero
 | 
				
			||||||
      BME280_REG_HUM_DATA = 0b11111101
 | 
					      }
 | 
				
			||||||
 | 
					      P = 1048576 - adc_P;
 | 
				
			||||||
 | 
					      P = (((P << 31) - var2) * 3125) / var1;
 | 
				
			||||||
 | 
					      var1 = (this._dig_P9 * (P >> 13) * (P >> 13)) >> 25;
 | 
				
			||||||
 | 
					      var2 = (this._dig_P8 * P) >> 19;
 | 
				
			||||||
 | 
					      P = ((P + var1 + var2) >> 8) + (((Int64)this._dig_P7) << 4);
 | 
				
			||||||
 | 
					      return ((Double)((UInt32)P)) / 25600;*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      /*adc_P >>= 4;
 | 
				
			||||||
 | 
					      var1 = ((int64_t)t_fine) - 128000;
 | 
				
			||||||
 | 
					      var2 = var1 * var1 * (int64_t)this->_dig_P6;
 | 
				
			||||||
 | 
					      var2 = var2 + ((var1*(int64_t)this->_dig_P5)<<17);
 | 
				
			||||||
 | 
					      var2 = var2 + (((int64_t)this->_dig_P4)<<35);
 | 
				
			||||||
 | 
					      var1 = ((var1 * var1 * (int64_t)this->_dig_P3)>>8) + ((var1 * (int64_t)this->_dig_P2)<<12);
 | 
				
			||||||
 | 
					      var1 = (((((int64_t)1)<<47)+var1))*((int64_t)this->_dig_P1)>>33;
 | 
				
			||||||
 | 
					      if (var1 == 0) {
 | 
				
			||||||
 | 
					        return 0; // avoid exception caused by division by zero
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					      p = 1048576-adc_P;
 | 
				
			||||||
 | 
					      p = (((p<<31)-var2)*3125)/var1;
 | 
				
			||||||
 | 
					      var1 = (((int64_t)this->_dig_P9) * (p>>13) * (p>>13)) >> 25;
 | 
				
			||||||
 | 
					      var2 = (((int64_t)this->_dig_P8) * p) >> 19;
 | 
				
			||||||
 | 
					      p = ((p + var1 + var2) >> 8) + (((int64_t)this->_dig_P7)<<4);
 | 
				
			||||||
 | 
					      return ((float)(uint32_t)p/256)/100;*/
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public Double GetHumidity() {
 | 
				
			||||||
 | 
					      Int32 adc_H = this._adc_H;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      Double var_H;
 | 
				
			||||||
 | 
					      var_H = this._t_fine - 76800.0;
 | 
				
			||||||
 | 
					      var_H = (adc_H - (this._dig_H4 * 64.0 + this._dig_H5 / 16384.0 * var_H)) * (this._dig_H2 / 65536.0 * (1.0 + this._dig_H6 / 67108864.0 * var_H * (1.0 + this._dig_H3 / 67108864.0 * var_H)));
 | 
				
			||||||
 | 
					      var_H = var_H * (1.0 - this._dig_H1 * var_H / 524288.0);
 | 
				
			||||||
 | 
					      return var_H;
 | 
				
			||||||
 | 
					      return var_H > 100 ? 100 : var_H < 0 ? 0: var_H;
 | 
				
			||||||
 | 
					      /*if(var_H > 100.0) {
 | 
				
			||||||
 | 
					        var_H = 100.0;
 | 
				
			||||||
 | 
					      } else if(var_H < 0.0) {
 | 
				
			||||||
 | 
					        var_H = 0.0;
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      return var_H;*/
 | 
				
			||||||
 | 
					      /*Int32 H;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      H = this._t_fine - 76800;
 | 
				
			||||||
 | 
					      H = ((((adc_H << 14) - (this._dig_H4 << 20) - (this._dig_H5 * H)) + 16384) >> 15) *
 | 
				
			||||||
 | 
					        (((((((H * this._dig_H6) >> 10) * (((H * this._dig_H3) >> 11) + 32768)) >> 10) + 2097152) * this._dig_H2 + 8192) >> 14);
 | 
				
			||||||
 | 
					      H = H - (((((H >> 15) * (H >> 15)) >> 7) * this._dig_H1) >> 4);
 | 
				
			||||||
 | 
					      H = H < 0 ? 0 : H;
 | 
				
			||||||
 | 
					      H = H > 419430400 ? 419430400 : H;
 | 
				
			||||||
 | 
					      return ((Double)(H >> 12)) / 1024;*/
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public void ReadTrimming() {
 | 
				
			||||||
 | 
					      this._dig_T1 = this.Read16LE((Byte)Calibration.BME280_REG_CALIB00);
 | 
				
			||||||
 | 
					      this._dig_T2 = this.ReadS16LE((Byte)Calibration.BME280_REG_CALIB02);
 | 
				
			||||||
 | 
					      this._dig_T3 = this.ReadS16LE((Byte)Calibration.BME280_REG_CALIB04);
 | 
				
			||||||
 | 
					      this._dig_P1 = this.Read16LE((Byte)Calibration.BME280_REG_CALIB06);
 | 
				
			||||||
 | 
					      this._dig_P2 = this.ReadS16LE((Byte)Calibration.BME280_REG_CALIB08);
 | 
				
			||||||
 | 
					      this._dig_P3 = this.ReadS16LE((Byte)Calibration.BME280_REG_CALIB10);
 | 
				
			||||||
 | 
					      this._dig_P4 = this.ReadS16LE((Byte)Calibration.BME280_REG_CALIB12);
 | 
				
			||||||
 | 
					      this._dig_P5 = this.ReadS16LE((Byte)Calibration.BME280_REG_CALIB14);
 | 
				
			||||||
 | 
					      this._dig_P6 = this.ReadS16LE((Byte)Calibration.BME280_REG_CALIB16);
 | 
				
			||||||
 | 
					      this._dig_P7 = this.ReadS16LE((Byte)Calibration.BME280_REG_CALIB18);
 | 
				
			||||||
 | 
					      this._dig_P8 = this.ReadS16LE((Byte)Calibration.BME280_REG_CALIB20);
 | 
				
			||||||
 | 
					      this._dig_P9 = this.ReadS16LE((Byte)Calibration.BME280_REG_CALIB22);
 | 
				
			||||||
 | 
					      this._dig_H1 = this.Read8((Byte)Calibration.BME280_REG_CALIB25);
 | 
				
			||||||
 | 
					      this._dig_H2 = this.ReadS16LE((Byte)Calibration.BME280_REG_CALIB26);
 | 
				
			||||||
 | 
					      this._dig_H3 = this.Read8((Byte)Calibration.BME280_REG_CALIB28);
 | 
				
			||||||
 | 
					      this._dig_H4 = (Int16)((this.Read8((Byte)Calibration.BME280_REG_CALIB29) << 4) | (0b00001111 & this.Read8((Byte)Calibration.BME280_REG_CALIB30)));
 | 
				
			||||||
 | 
					      this._dig_H5 = (Int16)((this.Read8((Byte)Calibration.BME280_REG_CALIB31) << 4) | ((0b11110000 & this.Read8((Byte)Calibration.BME280_REG_CALIB30)) >> 4));
 | 
				
			||||||
 | 
					      this._dig_H6 = (SByte)this.Read8((Byte)Calibration.BME280_REG_CALIB32);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private readonly I2CDevice w;
 | 
				
			||||||
 | 
					    private Int32 _adc_T;
 | 
				
			||||||
 | 
					    private Int32 _adc_P;
 | 
				
			||||||
 | 
					    private Int32 _adc_H;
 | 
				
			||||||
 | 
					    private Int32 _t_fine;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private UInt16 _dig_T1;
 | 
				
			||||||
 | 
					    private Int16 _dig_T2;
 | 
				
			||||||
 | 
					    private Int16 _dig_T3;
 | 
				
			||||||
 | 
					    private UInt16 _dig_P1;
 | 
				
			||||||
 | 
					    private Int16 _dig_P2;
 | 
				
			||||||
 | 
					    private Int16 _dig_P3;
 | 
				
			||||||
 | 
					    private Int16 _dig_P4;
 | 
				
			||||||
 | 
					    private Int16 _dig_P5;
 | 
				
			||||||
 | 
					    private Int16 _dig_P6;
 | 
				
			||||||
 | 
					    private Int16 _dig_P7;
 | 
				
			||||||
 | 
					    private Int16 _dig_P8;
 | 
				
			||||||
 | 
					    private Int16 _dig_P9;
 | 
				
			||||||
 | 
					    private Byte _dig_H1;
 | 
				
			||||||
 | 
					    private Int16 _dig_H2;
 | 
				
			||||||
 | 
					    private Byte _dig_H3;
 | 
				
			||||||
 | 
					    private Int16 _dig_H4;
 | 
				
			||||||
 | 
					    private Int16 _dig_H5;
 | 
				
			||||||
 | 
					    private SByte _dig_H6;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    //Calibration Register Adresses
 | 
				
			||||||
 | 
					    private enum Calibration {
 | 
				
			||||||
 | 
					      BME280_REG_CALIB00 = 0b10001000,
 | 
				
			||||||
 | 
					      BME280_REG_CALIB02 = 0b10001010,
 | 
				
			||||||
 | 
					      BME280_REG_CALIB04 = 0b10001100,
 | 
				
			||||||
 | 
					      BME280_REG_CALIB06 = 0b10001110,
 | 
				
			||||||
 | 
					      BME280_REG_CALIB08 = 0b10010000,
 | 
				
			||||||
 | 
					      BME280_REG_CALIB10 = 0b10010010,
 | 
				
			||||||
 | 
					      BME280_REG_CALIB12 = 0b10010100,
 | 
				
			||||||
 | 
					      BME280_REG_CALIB14 = 0b10010110,
 | 
				
			||||||
 | 
					      BME280_REG_CALIB16 = 0b10011000,
 | 
				
			||||||
 | 
					      BME280_REG_CALIB18 = 0b10011010,
 | 
				
			||||||
 | 
					      BME280_REG_CALIB20 = 0b10011100,
 | 
				
			||||||
 | 
					      BME280_REG_CALIB22 = 0b10011110,
 | 
				
			||||||
 | 
					      BME280_REG_CALIB25 = 0b10100001,
 | 
				
			||||||
 | 
					      BME280_REG_CALIB26 = 0b11100001,
 | 
				
			||||||
 | 
					      BME280_REG_CALIB28 = 0b11100011,
 | 
				
			||||||
 | 
					      BME280_REG_CALIB29 = 0b11100100,
 | 
				
			||||||
 | 
					      BME280_REG_CALIB30 = 0b11100101,
 | 
				
			||||||
 | 
					      BME280_REG_CALIB31 = 0b11100110,
 | 
				
			||||||
 | 
					      BME280_REG_CALIB32 = 0b11100111
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    //Register Adresses
 | 
				
			||||||
 | 
					    private enum Register {
 | 
				
			||||||
 | 
					      BME280_REG_ID = 0b11010000,
 | 
				
			||||||
 | 
					      BME280_REG_RESET = 0b11100000,
 | 
				
			||||||
 | 
					      BME280_REG_CTRL_HUM = 0b11110010,
 | 
				
			||||||
 | 
					      BME280_REG_STATUS = 0b11110011,
 | 
				
			||||||
 | 
					      BME280_REG_CTRL_MEAS = 0b11110100,
 | 
				
			||||||
 | 
					      BME280_REG_CONFIG = 0b11110101,
 | 
				
			||||||
 | 
					      BME280_REG_PRESS_DATA = 0b11110111,
 | 
				
			||||||
 | 
					      BME280_REG_TEMP_DATA = 0b11111010,
 | 
				
			||||||
 | 
					      BME280_REG_HUM_DATA = 0b11111101
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    //TWI Writes
 | 
					    //TWI Writes
 | 
				
			||||||
    private Byte Read8(Byte reg) => this.w.ReadAddressByte(reg);
 | 
					    private Byte Read8(Byte reg) => this.w.ReadAddressByte(reg);
 | 
				
			||||||
    /*this->w->beginTransmission(address);
 | 
					    /*this->w->beginTransmission(address);
 | 
				
			||||||
      this->w->write(reg);
 | 
					      this->w->write(reg);
 | 
				
			||||||
      this->w->endTransmission();
 | 
					      this->w->endTransmission();
 | 
				
			||||||
      this->w->requestFrom(address, 1);
 | 
					      this->w->requestFrom(address, 1);
 | 
				
			||||||
      while(!this->w->available())
 | 
					      while(!this->w->available())
 | 
				
			||||||
        ;
 | 
					        ;
 | 
				
			||||||
      return this->w->read();*/
 | 
					      return this->w->read();*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private UInt16 Read16(Byte reg) => this.w.ReadAddressWord(reg);
 | 
					    private UInt16 Read16LE(Byte reg) => this.w.ReadAddressWord(reg);
 | 
				
			||||||
    /*uint8_t msb, lsb;
 | 
					    /*uint8_t msb, lsb;
 | 
				
			||||||
      this->w->beginTransmission(address);
 | 
					      this->w->beginTransmission(address);
 | 
				
			||||||
      this->w->write(reg);
 | 
					      this->w->write(reg);
 | 
				
			||||||
      this->w->endTransmission();
 | 
					      this->w->endTransmission();
 | 
				
			||||||
      this->w->requestFrom(address, 2);
 | 
					      this->w->requestFrom(address, 2);
 | 
				
			||||||
      while(this->w->available() < 2)
 | 
					      while(this->w->available() < 2)
 | 
				
			||||||
        ;
 | 
					        ;
 | 
				
			||||||
      msb = this->w->read();
 | 
					      msb = this->w->read();
 | 
				
			||||||
      lsb = this->w->read();
 | 
					      lsb = this->w->read();
 | 
				
			||||||
      return (uint16_t)msb << 8 | lsb;*/
 | 
					      return (uint16_t)msb << 8 | lsb;*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private UInt16 Read16LE(Byte reg) {
 | 
					    private UInt16 Read16(Byte reg) {
 | 
				
			||||||
      UInt16 data = this.Read16(reg);
 | 
					      UInt16 data = this.Read16LE(reg);
 | 
				
			||||||
      return (UInt16)((data >> 8) | (data << 8));
 | 
					      return (UInt16)((data >> 8) | (data << 8));
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private Int16 ReadS16LE(Byte reg) => (Int16)this.Read16LE(reg);
 | 
					    private Int16 ReadS16LE(Byte reg) => (Int16)this.Read16LE(reg);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private UInt32 Read20(Byte reg) {
 | 
					    private UInt32 Read20(Byte reg) {
 | 
				
			||||||
      UInt32 data;
 | 
					      UInt32 data;
 | 
				
			||||||
      this.w.Write(reg);
 | 
					      this.w.Write(reg);
 | 
				
			||||||
      Byte[] dr = this.w.Read(3);
 | 
					      Byte[] dr = this.w.Read(3);
 | 
				
			||||||
      /*this->w->beginTransmission(address);
 | 
					      /*this->w->beginTransmission(address);
 | 
				
			||||||
      this->w->write(reg);
 | 
					      this->w->write(reg);
 | 
				
			||||||
      this->w->endTransmission();
 | 
					      this->w->endTransmission();
 | 
				
			||||||
      this->w->requestFrom(address, 3);
 | 
					      this->w->requestFrom(address, 3);
 | 
				
			||||||
      while(this->w->available() < 3)
 | 
					      while(this->w->available() < 3)
 | 
				
			||||||
        ;*/
 | 
					        ;*/
 | 
				
			||||||
      data = dr[0];
 | 
					      data = dr[0];
 | 
				
			||||||
      data <<= 8;
 | 
					      data <<= 8;
 | 
				
			||||||
      data |= dr[1];
 | 
					      data |= dr[1];
 | 
				
			||||||
      data <<= 8;
 | 
					      data <<= 8;
 | 
				
			||||||
      data |= dr[2];
 | 
					      data |= dr[2];
 | 
				
			||||||
      data >>= 4;
 | 
					      data >>= 4;
 | 
				
			||||||
      return data;
 | 
					      return data;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private void WriteRegister(Byte reg, Byte val) => this.w.WriteAddressByte(reg, val);
 | 
					    private void WriteRegister(Byte reg, Byte val) => this.w.WriteAddressByte(reg, val);
 | 
				
			||||||
    /*this->w->beginTransmission(address);
 | 
					    /*this->w->beginTransmission(address);
 | 
				
			||||||
      this->w->write(reg);
 | 
					      this->w->write(reg);
 | 
				
			||||||
      this->w->write(val);
 | 
					      this->w->write(val);
 | 
				
			||||||
      this->w->endTransmission();*/
 | 
					      this->w->endTransmission();*/
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -12,12 +12,14 @@ namespace BlubbFish.Iot.Thermometer {
 | 
				
			|||||||
      this.tls.Begin();
 | 
					      this.tls.Begin();
 | 
				
			||||||
      this.bme.Begin();
 | 
					      this.bme.Begin();
 | 
				
			||||||
      while(true) {
 | 
					      while(true) {
 | 
				
			||||||
        this.tls.Measure();
 | 
					        //this.tls.Measure();
 | 
				
			||||||
        this.bme.Measure();
 | 
					        this.bme.Measure();
 | 
				
			||||||
        Console.WriteLine(this.tls.GetLux()+" lux");
 | 
					        //Console.WriteLine(this.tls.GetLux()+" lux");
 | 
				
			||||||
        Console.WriteLine(this.bme.GetHumidity() + " Hm%");
 | 
					 | 
				
			||||||
        Console.WriteLine(this.bme.GetPressure() + " mbHp");
 | 
					 | 
				
			||||||
        Console.WriteLine(this.bme.GetTemperature() + " °C");
 | 
					        Console.WriteLine(this.bme.GetTemperature() + " °C");
 | 
				
			||||||
 | 
					        Console.WriteLine(this.bme.GetPressure() + " mbHp");
 | 
				
			||||||
 | 
					        Console.WriteLine(this.bme.GetHumidity() + " Hm%");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        System.Threading.Thread.Sleep(1000);
 | 
					        System.Threading.Thread.Sleep(1000);
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user