#include "DHT.h"

// Define pin constants
const int DHTPIN = D8;       // Pin where the DHT22 is connected
const int RED_PIN = D2;      // Red LED pin
const int GREEN_PIN = D3;    // Green LED pin
const int BLUE_PIN = D4;     // Blue LED pin
const int BUZZER_PIN = D7;   // Buzzer pin

// Define temperature thresholds
const float LOW_TEMP_THRESHOLD = 20.0;
const float HIGH_TEMP_THRESHOLD = 30.0;

// Define LED colors
enum class LEDColor {
    RED,
    GREEN,
    BLUE
};

// Temperature sensor class
class TemperatureSensor {
private:
    DHT dht;

public:
    TemperatureSensor(int pin) : dht(pin, DHT22) {
        dht.begin();
    }

    float readTemperature() {
        return dht.readTemperature();
    }
};

// LED control class
class LEDControl {
private:
    int redPin;
    int greenPin;
    int bluePin;
    int buzzerPin;

public:
    LEDControl(int rPin, int gPin, int bPin, int bzzrPin)
        : redPin(rPin), greenPin(gPin), bluePin(bPin), buzzerPin(bzzrPin) {
        pinMode(redPin, OUTPUT);
        pinMode(greenPin, OUTPUT);
        pinMode(bluePin, OUTPUT);
        pinMode(buzzerPin, OUTPUT);
    }

    void setColor(LEDColor color) {
        digitalWrite(redPin, LOW);
        digitalWrite(greenPin, LOW);
        digitalWrite(bluePin, LOW);

        switch (color) {
            case LEDColor::RED:
                digitalWrite(redPin, HIGH);
                break;
            case LEDColor::GREEN:
                digitalWrite(greenPin, HIGH);
                break;
            case LEDColor::BLUE:
                digitalWrite(bluePin, HIGH);
                break;
            default:
                break;
        }
    }

    void activateBuzzer(bool activate) {
        digitalWrite(buzzerPin, activate ? HIGH : LOW);
    }
};

// Main program
TemperatureSensor tempSensor(DHTPIN);
LEDControl ledControl(RED_PIN, GREEN_PIN, BLUE_PIN, BUZZER_PIN);

void setup() {
    Serial.begin(115200);
    Serial.println("Hello, STM32!");
}

void loop() {
    delay(2000); // Wait a few seconds between measurements.

    float temperature = tempSensor.readTemperature();

    // Print temperature.
    Serial.print("Temperature: ");
    Serial.print(temperature);
    Serial.println(" °C");

    // Change LED color and activate buzzer based on temperature ranges
    if (temperature < LOW_TEMP_THRESHOLD) {
        ledControl.setColor(LEDColor::BLUE);
        ledControl.activateBuzzer(false);
    } else if (temperature >= LOW_TEMP_THRESHOLD && temperature <= HIGH_TEMP_THRESHOLD) {
        ledControl.setColor(LEDColor::GREEN);
        ledControl.activateBuzzer(false);
    } else {
        ledControl.setColor(LEDColor::RED);
        ledControl.activateBuzzer(true);
    }
}