#include "PhiProximity.h"

/*
 ===========================================================
    PhiProximity.cpp — Détection proximité IR
 ===========================================================
*/

PhiProximity::PhiProximity()
: _proximity(0),
  _proximityNorm(0),
  _lpf(0.2f)  // Lissage modéré (α=0.2)
{}

bool PhiProximity::begin() {
    // APDS.begin() doit avoir été appelé avant
    
    Serial.println("✔ PhiProximity initialisé (APDS9960)");
    return true;
}

void PhiProximity::update() {
    if (!APDS.proximityAvailable())
        return;

    // Lecture proximité brute
    int prox = APDS.readProximity();
    
    // Lissage exponentiel
    _proximity = _lpf.step((float)prox);
    
    // Normalisation 0.0-1.0
    _proximityNorm = _proximity / 255.0f;
}

uint8_t PhiProximity::suggestedBrightness() const {
    // Brightness adaptative basé sur proximité
    // Loin: 60 (dim)
    // Proche: 200 (bright)
    
    if (_proximity > 200) {
        return 200;  // Main très proche → très lumineux
    } else if (_proximity > 100) {
        return 120;  // Main proche → lumineux
    } else if (_proximity > 30) {
        return 80;   // Présence → normal
    } else {
        return 60;   // Loin → dim
    }
}
