// Generated by STruC++ - IEC 61131-3 Structured Text to C++ Compiler
// Do not edit this file manually.

#include "generated.hpp"

namespace strucpp {

FB_HEADROOM::FB_HEADROOM()
    : SCAN_S(0.1), TAU_S(30.0), AREA_M2(120.0), SPILL_M(6.0), LSHH_M(5.5), MIN_NET(0.5), NO_TIME(32767), MAX_TIME(32767.0), PRIMED(false)
{
    // Initialize variables
}

void FB_HEADROOM::operator()() {
    if (!PRIMED) {
        INFLOWFILT = INFLOW;
        PRIMED = true;
    } else {
        INFLOWFILT = INFLOWFILT + (INFLOW - INFLOWFILT) * SCAN_S / TAU_S;
    }
    NETINFLOW = INFLOWFILT - TOTALDISCHARGE;
    VOLTOSPILL = (SPILL_M - LEVEL) * AREA_M2;
    VOLTOLSHH = (LSHH_M - LEVEL) * AREA_M2;
    if (VOLTOSPILL < 0.0) {
        VOLTOSPILL = 0.0;
    }
    if (VOLTOLSHH < 0.0) {
        VOLTOLSHH = 0.0;
    }
    if (NETINFLOW <= MIN_NET) {
        TIMETOSPILL = NO_TIME;
    } else {
        T = VOLTOSPILL * 1000.0 / NETINFLOW;
        if (T >= MAX_TIME) {
            TIMETOSPILL = NO_TIME;
        } else if (T < 0.0) {
            TIMETOSPILL = 0;
        } else {
            TIMETOSPILL = TO_INT(T);
        }
    }
    if (NETINFLOW <= MIN_NET) {
        TIMETOLSHH = NO_TIME;
    } else {
        T = VOLTOLSHH * 1000.0 / NETINFLOW;
        if (T >= MAX_TIME) {
            TIMETOLSHH = NO_TIME;
        } else if (T < 0.0) {
            TIMETOLSHH = 0;
        } else {
            TIMETOLSHH = TO_INT(T);
        }
    }
}

}  // namespace strucpp