#ifndef PHOTO_ELECTRIC_SENSOR
#define PHOTO_ELECTRIC_SENSOR

#include "../types.h"
#include "../common/macros.h"
#include "../common/math.h"

#define PES_TIMEOUT 1600

class PhotoElectricSensor
{
public:
   PhotoElectricSensor(short pin, short interval)
   {
      this->pin = pin;
      this->interval = interval;
      this->highTS = 0;
      this->lowTS = 0;
      this->dt = 0;
      this->now = 0;
      this->moving = 0;
      this->setup();
   }

   short setup(){}
   short loop()
   {
      now = millis();
      if (now - dt > 500)
      {
         this->value = RANGE(analogRead(this->pin), 50 - 10, 50 + 10);
         dt = now;
      }      
      return this->value;
   }
   short ok()
   {
      return this->value;
   }
   short value;
   short moving;

   millis_t highTS; // Last HIGH TS
   millis_t lowTS;  // Last LOW TS
   millis_t dt;     // Last delta time between HIGH / LOW
   millis_t now;    // Temp. variable to store tick TS
   bool last;

protected:
   short pin;
   short interval;
};

#endif
