/***************************************************************************
  This is a library for the BME680 gas, humidity, temperature & pressure sensor

  Designed specifically to work with the Adafruit BME680 Breakout
  ----> http://www.adafruit.com/products/3660

  These sensors use I2C or SPI to communicate, 2 or 4 pins are required
  to interface.

  Adafruit invests time and resources providing this open source code,
  please support Adafruit and open-source hardware by purchasing products
  from Adafruit!

  Written by Limor Fried & Kevin Townsend for Adafruit Industries.
  BSD license, all text above must be included in any redistribution

  Modified by George Gollin, March 3, 2021

  I am only using the I2C commmunication lines.
 ***************************************************************************/

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_BME680.h"


#define SEALEVELPRESSURE_HPA (1013.25)

// I2C:
Adafruit_BME680 bme;

// SPI:
// #define BME_SCK 13
// #define BME_MISO 12
// #define BME_MOSI 11
// #define BME_CS 10
// Adafruit_BME680 bme(BME_CS);
// Adafruit_BME680 bme(BME_CS, BME_MOSI, BME_MISO,  BME_SCK);
uint32_t tstart;

// do a speed test?
bool speed_test = false;

int times_into_loop = 0;
int last = 1000;

void setup() 
{
  // open the serial monitor, wait for it to be ready.
  Serial.begin(115200);
  while (!Serial);

  Serial.println(F("\nBME680 test, using I2C communication lines"));
  Serial.println(F("set speed_test true to turn off oven and see how fast we can read the BME680."));
  if(speed_test)
  {
    Serial.println("...we will do a speed test now."); 
  } else {
    Serial.println("...we will read BME680 once per second now."); 
  }

  // instantiate the BME object, see if it opens OK.
  if (!bme.begin()) 
  {
    Serial.println("Could not find a valid BME680 sensor, check wiring!");
    while (1);
  }

  // Set up oversampling and filter initialization. See the manufacturer's documentation:
  // https://learn.adafruit.com/adafruit-bme680-humidity-temperature-barometic-pressure-voc-gas/ 
  // bme.setTemperatureOversampling(BME680_OS_8X);
  // bme.setHumidityOversampling(BME680_OS_2X);
  // bme.setPressureOversampling(BME680_OS_4X);
  // bme.setIIRFilterSize(BME680_FILTER_SIZE_3);
  bme.setTemperatureOversampling(BME680_OS_1X);
  bme.setHumidityOversampling(BME680_OS_1X);
  bme.setPressureOversampling(BME680_OS_1X);
  bme.setIIRFilterSize(BME680_FILTER_SIZE_1);

  if(speed_test)
  {
    // disable the "pizza oven" VOC device
    bme.setGasHeater(0, 0); 
  } else {
    // 320*C for 150 ms
    bme.setGasHeater(320, 150);
  }

  Serial.print("about to start; millis = ");
  tstart = millis();
  Serial.println(tstart);
  tstart = millis();
}
void loop() {

  times_into_loop++;

  if(speed_test)
  {
    // let's see how fast we can read things out...

    if(times_into_loop <= last)
    {
      if (! bme.performReading()) 
      {
        Serial.println("Failed to perform reading :(");
        return;
      }

    } else {

    Serial.print("time per read (ms): ");
    Serial.println((millis() - tstart) / last);
    Serial.println("we are finished.");

    while(1){}
    }

  } else {

    // just read and echo values.

    if (! bme.performReading()) 
    {
      Serial.println("Failed to perform reading :(");
      return;
    }

    Serial.print("Temperature = ");
    Serial.print(bme.temperature);
    Serial.print(" *C (");
    Serial.print(1.8 * bme.temperature + 32.);
    Serial.println(" *F)");

    Serial.print("Pressure = ");
    Serial.print(bme.pressure / 100.0);
    Serial.println(" hPa");

    Serial.print("Humidity = ");
    Serial.print(bme.humidity);
    Serial.println(" %");

    Serial.print("Gas (VOC) = ");
    Serial.print(bme.gas_resistance / 1000.0);
    Serial.println(" KOhms");

    Serial.print("Approx. Altitude = ");
    Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
    Serial.println(" m");

    Serial.println();
    delay(1000);
    
  }
}