#include <Arduino.h>
#include <Adafruit_SCD30.h>
#include <Adafruit_BME280.h>
#include <DS3231.h>
#include <Wire.h>
#include <U8g2lib.h>

DS3231 RTC;
U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
Adafruit_BME280 bme; // I2C
Adafruit_SCD30  scd30; //I2C
#define x_offset 3
#define datapoints_on_screen 128
struct datapoint{
  String time;
  float temperature;
  float humidity;

  int co2;
};


datapoint current_data = {"",0,0,0};
datapoint graphed_data[datapoints_on_screen];
bool h12_flag;
bool pm_flag;
bool century = false;
String year;
String month;
String day;
String hour;
String minute;
String second;
String timestamp;
String date;
String time_hms;
String co2data;
String humiddata;
String tempdata;

float temperature;
float humidity;
float pressure;
float co2;
int screenselect = 0;
void setup() {
  // put your setup code here, to run once:
  Wire.begin();
  u8g2.begin();
  u8g2.clearDisplay();
  scd30.begin();
  bme.begin(0x76);  //bme Sensor

  if(!scd30.setMeasurementInterval(2)){
    Serial.println("Failed to Set Interval");
  }
  Serial.print("Measurement Interval: "); 
  Serial.print(scd30.getMeasurementInterval()); 
  Serial.println(" seconds");
  u8g2.clearBuffer();
  u8g2.setFont(u8g2_font_courR08_tf);
  Serial.begin(57600);
}
String getTime(){
  year = "20"+String(RTC.getYear(),DEC);
  month = String(RTC.getMonth(century),DEC);
  day= String(RTC.getDate(),DEC);
  hour= String(RTC.getHour(h12_flag, pm_flag),DEC);
  minute= String(RTC.getMinute(),DEC);
  second= String(RTC.getSecond(),DEC);
  if ((month.length()) == 1 ) {month= "0" + month;} // eine null voranstellen
  if ((day.length()) == 1 ) {day= "0" + day;}
  if ((hour.length()) == 1 ) {hour= "0" + hour;}
  if ((minute.length()) == 1 ) {minute= "0" + minute;}
  if ((second.length()) == 1 ) {second= "0" + second;}
  date = day+"."+month+"."+year;
  time_hms = hour+":"+minute+":"+second;
  timestamp = date+"-"+time_hms;
  current_data.time = timestamp;
  return timestamp;
}
void getSensorData(){
  current_data.temperature = bme.readTemperature();

  pressure = bme.readPressure() / 100.0F; 
  current_data.humidity = bme.readHumidity();
  if(scd30.dataReady()){
    scd30.read();
    current_data.co2 = scd30.CO2;
  }
}
void drawOnScreen(){
  switch(screenselect){
    case 0:
      u8g2.clearBuffer();
      u8g2.drawStr(x_offset, 10, (date+"-"+time_hms).c_str());
      tempdata = "Temp: "+String(current_data.temperature)+"C";
      humiddata = "Humid: "+String(current_data.humidity)+"%";
      co2data = "CO2: "+String(current_data.co2)+"ppm";
      u8g2.drawStr(x_offset,19,tempdata.c_str());
      u8g2.drawStr(x_offset,28,humiddata.c_str());
      u8g2.drawStr(x_offset,37,co2data.c_str());
      u8g2.sendBuffer();
      break;
    case 1:
      Serial.println("Screen 1");
      //Insert co2 graph
      break;
    case 2:
      Serial.println("Screen 1");
      break;
      //
    default:
      break;
  }
  
}
void combineData(){
  String DataPoint = timestamp+","+String(current_data.temperature)+","+String(current_data.humidity)+","+String(current_data.co2);
  Serial.println(DataPoint);
}
void loop() {
  getSensorData();
  getTime();
  drawOnScreen();
  combineData();
  delay(1000);
}
