#ifndef SHIELD_KEYESTUDIO_H
#define SHIELD_KEYESTUDIO_H

#include <Arduino.h>
#include "KK_ButtonHandler.h"
#include "KK_dht11.h"

class Shield_Keyestudio {
  public:
    Shield_Keyestudio();
    
    void begin();
    void update(); // Muss in den loop()

    // --- Taster (SW1 & SW2) ---
    KK_ButtonHandler btn1;
    KK_ButtonHandler btn2;

    // --- Ausgaben ---
    void setSingleLED(uint8_t ledNum, bool state); // 1 = Rot(D12), 2 = Blau(D13)
    void setRGB(uint8_t r, uint8_t g, uint8_t b);  // PWM an D9, D10, D11
    void playTone(uint16_t freq, uint32_t ms);

    // --- Eingaben / Sensoren ---
    uint16_t readPoti();
    uint8_t readLDR();       // Gibt 0 - 100% zurück
    float readLM35();        // Direkte Temperatur in °C
    
    float readDHTTemp();     // DHT11 Temperatur
    float readDHTHum();      // DHT11 Luftfeuchtigkeit

    // --- IR Empfänger ---
    void printIRCode();
    bool checkIRCode(uint32_t hexCode);

  private:
    KK_dht11 _dht;
    
    // Pin Definitionen 
    const int _pinBtn1    = 2;
    const int _pinBtn2    = 3;
    const int _pinDHT     = 4;
    const int _pinBuzzer  = 5;
    const int _pinIR      = 6;
    
    const int _pinLedR    = 9;
    const int _pinLedG    = 10;
    const int _pinLedB    = 11;
    const int _pinLedRed  = 12; // Single LED
    const int _pinLedBlue = 13; // Single LED

    const int _pinPoti    = A0;
    const int _pinLDR     = A1;
    const int _pinLM35    = A2;
    
    // Interne IR Variablen
    uint32_t _currentIRCode;
    bool _newIRCodeAvailable;
    
    // Interner Timer für DHT11 (darf max alle 1-2 Sekunden gelesen werden)
    unsigned long _lastDHTRead;
    float _lastDHTTemp;
    float _lastDHTHum;
    void _updateDHT();
};

#endif