//----------------------Screen Bits
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// The pins for I2C are defined by the Wire-library. 
// On an arduino UNO:       A4(SDA), A5(SCL)
// On an arduino MEGA 2560: 20(SDA), 21(SCL)
// On an arduino LEONARDO:   2(SDA),  3(SCL), ...
#define OLED_RESET     4 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

void InitDisp(void)
{
  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }

  // Show initial display buffer contents on the screen --
  // the library initializes this with an Adafruit splash screen.
  // display.display();
  // delay(2000); // Pause for 2 seconds

  // Clear the buffer
  display.clearDisplay();
}

void DisplayTemp(float cel)
{
  display.clearDisplay();

  display.setTextSize(2);      // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE); // Draw white text
  display.setCursor(0, 0);     // Start at top-left corner
  display.cp437(true);         // Use full 256 char 'Code Page 437' font

  // Not all the characters will fit on the display. This is normal.
  // Library will draw what it can and the rest will be clipped.
  char buf[256];
  int integerPart = (int)cel;
  int decimalPart = ((int)(cel*100)%100);
  sprintf(buf, "Temp:%d.%d", (int)cel, decimalPart);
  for(int16_t i=0; i<strlen(buf); i++) {
    display.write(buf[i]);        
  }

  display.display();
}
//---------------------------------------------------------------------
//---------------------- Temperature Sensor
#include <OneWire.h>
OneWire  ds(10);  // on pin 10 (a 4.7K resistor is necessary)
byte g_addr[8];
byte g_type_s;
void InitTemp(void)
{
  if ( !ds.search(g_addr)) {
    Serial.println("No addresse.");
    Serial.println();
    ds.reset_search();
    delay(250);
    while(true){};
  }

  Serial.print("ROM =");
  for(int i = 0; i < 8; i++) {
    Serial.write(' ');
    Serial.print(g_addr[i], HEX);
  }

  if (OneWire::crc8(g_addr, 7) != g_addr[7]) {
      Serial.println("CRC is not valid!");
      while(true){};
  }
  Serial.println();
 
  // the first ROM byte indicates which chip
  switch (g_addr[0]) {
    case 0x10:
      Serial.println("  Chip = DS18S20");  // or old DS1820
      g_type_s = 1;
      break;
    case 0x28:
      Serial.println("  Chip = DS18B20");
      g_type_s = 0;
      break;
    case 0x22:
      Serial.println("  Chip = DS1822");
      g_type_s = 0;
      break;
    default:
      Serial.println("Device is not a DS18x20 family device.");
      return;
  } 
}
//----------------------------------------------------------------------
//--------------------- Digital pins
const char SW1 = 2;

//---------------------- PROGRAM
void setup(void) {
  Serial.begin(9600);

  pinMode( SW1, OUTPUT);
  digitalWrite(SW1, LOW);

  InitTemp();
  InitDisp();
}

void loop(void) {
  byte i;
  byte present = 0;
  byte data[12];
  
  float celsius, fahrenheit;
  
  ds.reset();
  ds.select(g_addr);
  ds.write(0x44, 1);        // start conversion, with parasite power on at the end
  
  delay(1000);     // maybe 750ms is enough, maybe not
  // we might do a ds.depower() here, but the reset will take care of it.
  
  present = ds.reset();
  ds.select(g_addr);    
  ds.write(0xBE);         // Read Scratchpad

  /*
  Serial.print("  Data = ");
  Serial.print(present, HEX);
  Serial.print(" ");
  */
  for ( i = 0; i < 9; i++) {           // we need 9 bytes
    data[i] = ds.read();
    /*
    Serial.print(data[i], HEX);
    Serial.print(" ");
    */
  }
  /*
  Serial.print(" CRC=");
  Serial.print(OneWire::crc8(data, 8), HEX);
  Serial.println();
  */
  
  // Convert the data to actual temperature
  // because the result is a 16 bit signed integer, it should
  // be stored to an "int16_t" type, which is always 16 bits
  // even when compiled on a 32 bit processor.
  int16_t raw = (data[1] << 8) | data[0];
  if (g_type_s) {
    raw = raw << 3; // 9 bit resolution default
    if (data[7] == 0x10) {
      // "count remain" gives full 12 bit resolution
      raw = (raw & 0xFFF0) + 12 - data[6];
    }
  } else {
    byte cfg = (data[4] & 0x60);
    // at lower res, the low bits are undefined, so let's zero them
    if (cfg == 0x00) raw = raw & ~7;  // 9 bit resolution, 93.75 ms
    else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
    else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
    //// default is 12 bit resolution, 750 ms conversion time
  }
  celsius = (float)raw / 16.0;
  fahrenheit = celsius * 1.8 + 32.0;
  Serial.print("  Temperature = ");
  Serial.print(celsius);
  Serial.print(" Celsius, ");
  Serial.print(fahrenheit);
  Serial.println(" Fahrenheit");

  // Do something with the temperature.
  if (celsius <= 21)
    digitalWrite(SW1, LOW);
  else if (celsius > 21)
    digitalWrite(SW1, HIGH);

  // Display temperature
  DisplayTemp(celsius);
  
}