#include "esphome.h"

static const char *TAG = "GP2Y1010AU0F.sensor";

// measure VCC while the sensor is running
#define VOLTAGE_VREF 5.11

#define PIN_ADC A0
#define PIN_LED 13

// measure every 12 seconds = 5 times per minute
#define UPDATE_INTERVAL 12000 // update interval in ms

class GP2Y1010AU0F : public PollingComponent, public Sensor
{
public:
    GP2Y1010AU0F() : PollingComponent(UPDATE_INTERVAL) {}

    float get_setup_priority() const override { return esphome::setup_priority::DATA; }

    void setup() override
    {
        ESP_LOGCONFIG(TAG, "Setting up sensor...");

        pinMode(PIN_LED, OUTPUT); // sensor led pin
        pinMode(PIN_ADC, INPUT);  // output form sensor
    }

    void update() override
    {
        float rawValue, voltage, density = 0;
        // float voltage = 0;
        // float density = 0;

        // enable led in sensor
        digitalWrite(PIN_LED, LOW);
        delay(280);

        // measure voltage
        rawValue = analogRead(PIN_ADC);
        delay(40);

        // led off
        digitalWrite(PIN_LED, HIGH);

        // calculate voltage
        voltage = rawValue * (VOLTAGE_VREF / 1024.0);

        // for calibration
        ESP_LOGCONFIG(TAG, "Measurements done, VRef: %.2f, ADC Value: %.0f, Calculated Voltage: %.3f, pm2.5: %.2f", VOLTAGE_VREF, rawValue, voltage, 170 * voltage - 0.1);
	
        if(voltage > 0)
        {
            // taken from https://www.howmuchsnow.com/arduino/airquality/
            publish_state(170 * voltage - 0.1); // publish
        }
    }
};