#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DHT.h>

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET    -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);

void drawGardenScene() {
  // Clear the display
  display.clearDisplay();
  

  // Draw the person
  display.drawCircle(30, 35, 5, SSD1306_WHITE); // Head
  display.drawLine(30, 40, 30, 50, SSD1306_WHITE); // Body
  display.drawLine(30, 50, 20, 60, SSD1306_WHITE); // Left leg
  display.drawLine(30, 50, 40, 60, SSD1306_WHITE); // Right leg
  display.drawLine(30, 45, 20, 45, SSD1306_WHITE); // Left arm
  display.drawLine(30, 45, 40, 45, SSD1306_WHITE); // Right arm
  
  
  // Update the display with the drawn image
  display.display();
  delay(2000); // Display the scene for 2 seconds
}

void setup() {
  Serial.begin(9600);
  dht.begin();
  
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { 
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  }

  // Show the garden scene at startup
  drawGardenScene();
}

void loop() {
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  if (isnan(h) || isnan(t)) {
    Serial.println(F("Failed to read from DHT sensor!"));
    return;
  }

  display.clearDisplay();

  // Afficher la température en texte
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(0, 0);
  display.print("Temp: ");
  display.print(t);
  display.print(" C");

  // Afficher l'humidité en texte
  display.setCursor(0, 10);
  display.print("Hum: ");
  display.print(h);
  display.print(" %");

  // Dessiner la barre de progression pour l'humidité
  int hBarLength = map(h, 0, 100, 0, 100); // Mapping de l'humidité pour une barre de 100 pixels de long
  display.drawRect(10, 20, 100, 8, SSD1306_WHITE); // Contour de la barre
  display.fillRect(10, 20, hBarLength, 8, SSD1306_WHITE); // Dessiner la barre remplie
  
  // Dessiner la barre de progression pour la température
  int tBarLength = map(t, -10, 50, 0, 100); // Mapping de la température pour une barre de 100 pixels de long
  display.drawRect(10, 35, 100, 8, SSD1306_WHITE); // Contour de la barre
  display.fillRect(10, 35, tBarLength, 8, SSD1306_WHITE); // Dessiner la barre remplie
  
  // Afficher les valeurs numériques à côté des barres
  display.setCursor(115, 20);
  display.print("%");

  display.setCursor(115, 35);
  display.print("C");

  display.display();
  delay(2000);
}