//******* Temp and humidity for your printer***************
//                  By JimBo  
//*********************************************************

//****************************************************************
//  Change what is in the quotes below to name your printer in the yellow bar
//  on the top of the screen
char Printername[15] = "Shop Printer";  //<<< this.  change this to your printer name.  
//  Keep it under 15 characters
//*****************************************************************

#include "U8glib.h"             //display library
#include <BME280_Arduino_I2C.h> //BME280 Library

BME280_Arduino_I2C bme;         //calling in the bme

U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NO_ACK);  //defining the display

char sT[8];         //Setting up strings from floats used laer
char sH[4];


void setup() {
    
  Serial.begin(9600);                             //Setting up Serial for troubleshooting

  u8g.setColorIndex(1);                          //Prepping the screen
  
  if (bme.begin() == 0 ) {                        //Checking if the bme is responding
    Serial.println(F("bme initialized!"));
  }
  else Serial.println(F("bme init failed!"));     //Check your wiring if you get this.  Or make sure it's in the breadboard
 


}


void loop(void) {

  float T, H;                           //Temp and humidity.  Guess which one is which!

  BME280Data* data = bme.read();        //This is where the measurement happens
  
  if (data != nullptr) {                //If it comes back with anything other than null, it will start assiging data
    
    T = (data->temperature);            //Making T the data for temp
    H = (data->humidity);               //take a wild guess
  
      u8g.firstPage();                  //Doing the display stuff
      do {
        draw(T, H);                    //the draw function is what puts the data on your display (see below the Loop for the draw function)
      } while ( u8g.nextPage() );      //Honestly, I dont understand the do while loop here.  I just use it because it works.
      u8g.firstPage();                 //I'd dig deeper, but I'd rather make more projects.  Later, I'll dig in to it.
  }
  
  delay(100);

}



void draw(float T, float H) {           //Get out your picture pages!  We're going to make some pictures!

  u8g.setFont(u8g_font_ncenB12r);       //Sets the font for the yellow title part of the screen
  u8g.drawStr( 0, 12, Printername);     //Writes your printer name the numbers are coordinates, Printername is a string from line8

  dtostrf(T, 4, 1, sT);                 //Changes float T to string T
  dtostrf(H, 2, 0, sH);                 //Same for H
  
  u8g.setFont(u8g_font_ncenB18r);       //Font for the C and %
  u8g.drawStr( 60, 38, "C");            //The numbers tell the display where to put what
  u8g.drawStr( 40, 63, "%");

  u8g.setFont(u8g_font_ncenB18r);       //Font for Temp and humidity
  u8g.drawStr( 10, 38, sT);             //Displaying the Temp
  u8g.drawStr( 10, 63, sH);             //And the humidity
  
}
