#include "CoolingSystem.h"

void C_CoolingSystem::Init() {
    sensors.begin();
    pinMode(FAN_PWM_PIN, OUTPUT);
    digitalWrite(FAN_PWM_PIN, 1);
}

void C_CoolingSystem::Tasks() {
    unsigned long durationUs = pulseIn(FAN_TACHO_PIN, LOW, 100000);
    RPM = 0;
    if (durationUs != 0) {
        RPM = 60 * (1e6 / (2 * durationUs));
    }

    sensors.requestTemperatures();
    temperatureC = sensors.getTempCByIndex(0);

    fanPwm = FAN_PWM_MAX;
    if (temperatureC != DEVICE_DISCONNECTED_C) {
        fanPwm = map(constrain(temperatureC, TEMP_MIN, TEMP_MAX), TEMP_MIN, TEMP_MAX, FAN_PWM_MIN, FAN_PWM_MAX);
    }
    analogWrite(5, fanPwm);
}
