#ifndef _FLOW_H_
#define _FLOW_H_

mqttValueTiming timing_flow;

//#define FLOW_PIN 16 //move to platformio.ini
uint16_t flow_counter=0; //maximum counts/s measured with Eden 128 Pump was 171

void IRAM_ATTR isr_flow();

unsigned long last_read_flow=0;
#define READINTERVAL_FLOW 10000
float flow_factor=7.5; //F=7.5*flowrate[L/min]
float flow;

uint32_t flow_counter_sum=0;




void flow_setup() {

  timing_flow.minchange=0.0;
  timing_flow.maxchange=0.3;
  timing_flow.mintime=10*1000;
  timing_flow.maxtime=30*60*1000;

  

  pinMode(FLOW_PIN, INPUT_PULLUP);
  
  attachInterrupt(FLOW_PIN, isr_flow, CHANGE);
}

void flow_loop(unsigned long loopmillis) {
  if (loopmillis>=last_read_flow+READINTERVAL_FLOW) {
    flow=flow_counter*1000.0/(loopmillis-last_read_flow)/2.0; //Frequency [Hz]
    flow/=flow_factor; //[L/min]
  
    flow_counter=0;
    last_read_flow=loopmillis;  
  }

}

void IRAM_ATTR isr_flow() {
	flow_counter++;
  flow_counter_sum++;
}



#endif