/*
 * ProviewR   Open Source Process Control.
 * Copyright (C) 2005-2023 SSAB EMEA AB.
 *
 * This file is part of ProviewR.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with ProviewR. If not, see <http://www.gnu.org/licenses/>
 *
 * Linking ProviewR statically or dynamically with other modules is
 * making a combined work based on ProviewR. Thus, the terms and
 * conditions of the GNU General Public License cover the whole
 * combination.
 *
 * In addition, as a special exception, the copyright holders of
 * ProviewR give you permission to, from the build function in the
 * ProviewR Configurator, combine ProviewR with modules generated by the
 * ProviewR PLC Editor to a PLC program, regardless of the license
 * terms of these modules. You may copy and distribute the resulting
 * combined work under the terms of your choice, provided that every
 * copy of the combined work is accompanied by a complete copy of
 * the source code of ProviewR (the version used to produce the
 * combined work), being distributed under the terms of the GNU
 * General Public License plus this exception.
 */
/* rt_plc_macro_sup.h -- Runtime environment  -  PLC
   Contains macros for analog and digital supervision of parameters.
   This code is used in the PLC-program environment.  */
/*
 Name:
   Sup_init(object, In, con)
 Description:
   Initialize a supervisor object
   This macro is not used.
*/
#define Sup_init(o) ;
/*
* Name:
*   ASup_init(object, In, con)
*
* Description:
*   Initialize analog supervisor object.
*   This macro is not used.
*/
#define ASup_init(o, In, con) ;

ASup


#define ASup_exec(o, In, con)                                                  \
  timer2_scan(tp, o);                                                          \
  if ((o->High && In <= o->CtrlLimit - o->Hysteres)                            \
      || (!o->High && In >= o->CtrlLimit + o->Hysteres)) {                     \
    if (o->Action)                                                             \
      o->Action = FALSE;                                                       \
    if (o->ReturnCheck) {                                                      \
      time_GetTime(&o->ReturnTime);                                            \
      o->ReturnCheck = FALSE;                                                  \
      o->ReturnSend = TRUE;                                                    \
    }                                                                          \
    if (o->AlarmCheck && !o->DetectCheck) {                                    \
      o->TimerCount = 0;                                                       \
      o->DetectCheck = TRUE;                                                   \
    }                                                                          \
  } else if (con && ((o->High && In > o->CtrlLimit)                            \
                        || (!o->High && In < o->CtrlLimit))) {                 \
    if (!o->Action)                                                            \
      o->Action = TRUE;                                                        \
    if (o->Suppressed) {                                                       \
      if (o->ReturnCheck) {                                                    \
        time_GetTime(&o->ReturnTime);                                          \
        o->ReturnCheck = FALSE;                                                \
        o->ReturnSend = TRUE;                                                  \
      } else if (o->DetectSend)                                                \
        o->DetectSend = FALSE;                                                 \
    }                                                                          \
    if (o->AlarmCheck && o->DetectOn && !o->Blocked && !o->Suppressed) {       \
      if (o->DetectCheck) {                                                    \
        o->ActualValue = In;                                                   \
        timer2_in(tp, o);                                                      \
        time_GetTime(&o->DetectTime);                                          \
        o->DetectCheck = FALSE;                                                \
      }                                                                        \
      if (!o->TimerFlag) {                                                     \
        o->DetectSend = TRUE;                                                  \
        o->ReturnCheck = TRUE;                                                 \
        o->Acked = FALSE;                                                      \
        o->AlarmCheck = FALSE;                                                 \
      }                                                                        \
    }                                                                          \
  }                                                                            \
  if (o->Blocked || o->Suppressed) {                                           \
    o->TimerCount = 0;                                                         \
    o->DetectCheck = TRUE;                                                     \
  }
/*
* Name:
*   DSup_init(object, In, con)
*
* Description:
*   Initialize digital supervisor object
*   This macro is not used.
*/
#define DSup_init(o, In, con) ;

DSup


#define DSup_exec(o, In, con)                                                  \
  timer2_scan(tp, o);                                                          \
  if ((In) != o->CtrlPosition) {					\
    if (o->Action)                                                             \
      o->Action = FALSE;                                                       \
    if (o->ReturnCheck) {                                                      \
      time_GetTime(&o->ReturnTime);                                            \
      o->ReturnCheck = FALSE;                                                  \
      o->ReturnSend = TRUE;                                                    \
    }                                                                          \
    if (o->AlarmCheck && !o->DetectCheck) {                                    \
      o->TimerCount = 0;                                                       \
      o->DetectCheck = TRUE;                                                   \
    }                                                                          \
  } else if (con) {                                                            \
    if (!o->Action)                                                            \
      o->Action = TRUE;                                                        \
    if (o->Suppressed) {                                                       \
      if (o->ReturnCheck) {                                                    \
        time_GetTime(&o->ReturnTime);                                          \
        o->ReturnCheck = FALSE;                                                \
        o->ReturnSend = TRUE;                                                  \
      } else if (o->DetectSend)                                                \
        o->DetectSend = FALSE;                                                 \
    }                                                                          \
    if (o->AlarmCheck && o->DetectOn && !o->Blocked && !o->Suppressed) {       \
      if (o->DetectCheck) {                                                    \
        o->ActualValue = In;                                                   \
        timer2_in(tp, o);                                                      \
        time_GetTime(&o->DetectTime);                                          \
        o->DetectCheck = FALSE;                                                \
      }                                                                        \
      if (!o->TimerFlag) {                                                     \
        o->DetectSend = TRUE;                                                  \
        o->ReturnCheck = TRUE;                                                 \
        o->Acked = FALSE;                                                      \
        o->AlarmCheck = FALSE;                                                 \
      }                                                                        \
    }                                                                          \
  }                                                                            \
  if (o->Blocked || o->Suppressed) {                                           \
    o->TimerCount = 0;                                                         \
    o->DetectCheck = TRUE;                                                     \
  }

DSupCompFo


#define DSupCompFo_init(o)                                                     \
  {                                                                            \
    pwr_tDlid dlid;                                                            \
    pwr_tStatus sts;                                                           \
    sts = gdh_DLRefObjectInfoAttrref(                                          \
        &o->PlcConnect, (void**)&o->PlcConnectP, &dlid);                       \
    if (EVEN(sts))                                                             \
      o->PlcConnectP = 0;                                                      \
    else                                                                       \
      ((pwr_sClass_DSupComp*)o->PlcConnectP)->TimerDO                          \
          = &(((pwr_sClass_DSupComp*)o->PlcConnectP)->TimerDODum);             \
  }
#define DSupCompFo_exec(o, In, con)                                            \
  {                                                                            \
    pwr_sClass_DSupComp* co = (pwr_sClass_DSupComp*)o->PlcConnectP;            \
    if (!co)                                                                   \
      return;                                                                  \
    co->Control = con;                                                         \
    timer2_scan(tp, co);                                                       \
    if ((In) != co->CtrlPosition) {                                            \
      if (co->Action)                                                          \
        co->Action = FALSE;                                                    \
      if (co->ReturnCheck) {                                                   \
        time_GetTime(&co->ReturnTime);                                         \
        co->ReturnCheck = FALSE;                                               \
        co->ReturnSend = TRUE;                                                 \
      }                                                                        \
      if (co->AlarmCheck && !co->DetectCheck) {                                \
        co->TimerCount = 0;                                                    \
        co->DetectCheck = TRUE;                                                \
      }                                                                        \
    } else if (con) {                                                          \
      if (!co->Action)                                                         \
        co->Action = TRUE;                                                     \
      if (co->Suppressed) {                                                    \
        if (co->ReturnCheck) {                                                 \
          time_GetTime(&co->ReturnTime);                                       \
          co->ReturnCheck = FALSE;                                             \
          co->ReturnSend = TRUE;                                               \
        } else if (co->DetectSend)                                             \
          co->DetectSend = FALSE;                                              \
      }                                                                        \
      if (co->AlarmCheck && co->DetectOn && !co->Blocked && !co->Suppressed) { \
        if (co->DetectCheck) {                                                 \
          co->ActualValue = In;                                                \
          timer2_in(tp, co);                                                   \
          time_GetTime(&co->DetectTime);                                       \
          co->DetectCheck = FALSE;                                             \
        }                                                                      \
        if (!co->TimerFlag) {                                                  \
          co->DetectSend = TRUE;                                               \
          co->ReturnCheck = TRUE;                                              \
          co->Acked = FALSE;                                                   \
          co->AlarmCheck = FALSE;                                              \
        }                                                                      \
      }                                                                        \
    }                                                                          \
    if (co->Blocked || co->Suppressed) {                                       \
      co->TimerCount = 0;                                                      \
      co->DetectCheck = TRUE;                                                  \
    }                                                                          \
    o->Action = co->Action;                                                    \
    o->Acked = co->Acked;                                                      \
    o->Blocked = co->Blocked;                                                  \
  }

ASupCompFo


#define ASupCompFo_init(o)                                                     \
  {                                                                            \
    pwr_tDlid dlid;                                                            \
    pwr_tStatus sts;                                                           \
    sts = gdh_DLRefObjectInfoAttrref(                                          \
        &o->PlcConnect, (void**)&o->PlcConnectP, &dlid);                       \
    if (EVEN(sts))                                                             \
      o->PlcConnectP = 0;                                                      \
    else                                                                       \
      ((pwr_sClass_ASupComp*)o->PlcConnectP)->TimerDO                          \
          = &(((pwr_sClass_ASupComp*)o->PlcConnectP)->TimerDODum);             \
  }
#define ASupCompFo_exec(o, In, con)                                            \
  {                                                                            \
    pwr_sClass_ASupComp* co = (pwr_sClass_ASupComp*)o->PlcConnectP;            \
    if (!co)                                                                   \
      return;                                                                  \
    co->Control = con;                                                         \
    timer2_scan(tp, co);                                                       \
    if ((co->High && In <= co->CtrlLimit - co->Hysteres)                       \
        || (!co->High && In >= co->CtrlLimit + co->Hysteres)) {                \
      if (co->Action)                                                          \
        co->Action = FALSE;                                                    \
      if (co->ReturnCheck) {                                                   \
        time_GetTime(&co->ReturnTime);                                         \
        co->ReturnCheck = FALSE;                                               \
        co->ReturnSend = TRUE;                                                 \
      }                                                                        \
      if (co->AlarmCheck && !co->DetectCheck) {                                \
        co->TimerCount = 0;                                                    \
        co->DetectCheck = TRUE;                                                \
      }                                                                        \
    } else if (con && ((co->High && In > co->CtrlLimit)                        \
                          || (!co->High && In < co->CtrlLimit))) {             \
      if (!co->Action)                                                         \
        co->Action = TRUE;                                                     \
      if (co->Suppressed) {                                                    \
        if (co->ReturnCheck) {                                                 \
          time_GetTime(&co->ReturnTime);                                       \
          co->ReturnCheck = FALSE;                                             \
          co->ReturnSend = TRUE;                                               \
        } else if (co->DetectSend)                                             \
          co->DetectSend = FALSE;                                              \
      }                                                                        \
      if (co->AlarmCheck && co->DetectOn && !co->Blocked && !co->Suppressed) { \
        if (co->DetectCheck) {                                                 \
          co->ActualValue = In;                                                \
          timer2_in(tp, co);                                                   \
          time_GetTime(&co->DetectTime);                                       \
          co->DetectCheck = FALSE;                                             \
        }                                                                      \
        if (!co->TimerFlag) {                                                  \
          co->DetectSend = TRUE;                                               \
          co->ReturnCheck = TRUE;                                              \
          co->Acked = FALSE;                                                   \
          co->AlarmCheck = FALSE;                                              \
        }                                                                      \
      }                                                                        \
    }                                                                          \
    if (co->Blocked || co->Suppressed) {                                       \
      co->TimerCount = 0;                                                      \
      co->DetectCheck = TRUE;                                                  \
    }                                                                          \
    o->Action = co->Action;                                                    \
    o->Acked = co->Acked;                                                      \
    o->Blocked = co->Blocked;                                                  \
  }