#ifndef _INTERNAL_SENSORS
/*
   A container to interface with all of the sensors
   in the EyeRIS camera housing.
   
   This wraps two BME280 env. sensors and two INA260 current
   monitors.

   Author: pldr
   Created: 05/31/2019
*/
#define _INTERNAL_SENSORS

// BME280 Sensor
#include "SparkFunBME280.h"
BME280 bme280_0;
BME280 bme280_1;

#include "INA260.h"
static INA260 ina260_0(0);
static INA260 ina260_1(1);

// Sofwire for I2C
SoftwareWire myWire( 13, 6);

void ina260Setup() {
  myWire.begin();
  ina260_0.beginI2C(myWire);
  ina260_1.beginI2C(myWire);
  myWire.end();
}

String readINA260() {
  String output = "";
  double value = 0.0;
  myWire.begin();
  if (ina260_0.readCurrentRegisterInAmps(value)) {
    output += String(value,3);
  }
  else{
    output += String(-1.0,3);
  }
  if (ina260_0.readBusVoltageRegisterInVolts(value)) {
    output += "," + String(value,3);
  }
  else{
    output += "," + String(-1.0,3);
  }
  if (ina260_1.readCurrentRegisterInAmps(value)) {
    output += "," + String(value,3);
  }
  else{
    output += "," + String(-1.0,3);
  }
  if (ina260_1.readBusVoltageRegisterInVolts(value)) {
    output += "," + String(value,3);
  }
  else{
    output += "," + String(-1.0,3);
  }
  myWire.end();
  return output;

}

String readBME280() {
  String output = "";
  myWire.begin();
  output += String(bme280_0.readTempC(),3);
  output += "," + String(bme280_0.readFloatPressure(),3);
  output += "," + String(bme280_0.readFloatHumidity(),3);
  output += "," + String(bme280_1.readTempC(),3);
  output += "," + String(bme280_1.readFloatPressure(),3);
  output += "," + String(bme280_1.readFloatHumidity(),3);
  myWire.end();
  return output;
}

void bme280Setup(BME280 &bme,int addr) {
  myWire.begin();
 //***Driver settings********************************//
    //commInterface can be I2C_MODE or SPI_MODE
    //specify chipSelectPin using arduino pin names
    //specify I2C address.  Can be 0x77(default) or 0x76

    //For I2C, enable the following and disable the SPI section
    bme.settings.commInterface = I2C_MODE;
    bme.settings.I2CAddress = addr;
    

    //For SPI enable the following and dissable the I2C section
    //mySensor.settings.commInterface = SPI_MODE;
    //mySensor.settings.chipSelectPin = 10;


    //***Operation settings*****************************//

    //runMode can be:
    //  0, Sleep mode
    //  1 or 2, Forced mode
    //  3, Normal mode
    bme.settings.runMode = 3; //Forced mode

    //tStandby can be:
    //  0, 0.5ms
    //  1, 62.5ms
    //  2, 125ms
    //  3, 250ms
    //  4, 500ms
    //  5, 1000ms
    //  6, 10ms
    //  7, 20ms
    bme.settings.tStandby = 0;

    //filter can be off or number of FIR coefficients to use:
    //  0, filter off
    //  1, coefficients = 2
    //  2, coefficients = 4
    //  3, coefficients = 8
    //  4, coefficients = 16
    bme.settings.filter = 4;

    //tempOverSample can be:
    //  0, skipped
    //  1 through 5, oversampling *1, *2, *4, *8, *16 respectively
    bme.settings.tempOverSample = 1;

    //pressOverSample can be:
    //  0, skipped
    //  1 through 5, oversampling *1, *2, *4, *8, *16 respectively
    bme.settings.pressOverSample = 1;

    //humidOverSample can be:
    //  0, skipped
    //  1 through 5, oversampling *1, *2, *4, *8, *16 respectively
    bme.settings.humidOverSample = 1;
    delay(10);  //Make sure sensor had enough time to turn on. BME280 requires 2ms to start up.         Serial.begin(57600);

    Serial.print("Starting BME280... result of .begin(): 0x");
    //Calling .begin() causes the settings to be loaded
    Serial.println(bme.beginI2C(myWire), HEX);
    myWire.end();
}

void bme280Setup() {
  bme280Setup(bme280_0,0x76);
  bme280Setup(bme280_1,0x77);
}

#endif
