/*@@var:*/
/*
 * DM1 Observer
 *
 * Copyright 2010, Vector Informatik GmbH - All right reserved
 *
 * This CAPL program observers the DM1 message. It evaluates the lamp
 * states from the different nodes and creates the overall lamp
 * state of the warning lamp. The state is stored in System Variables
 * and can be read in the panels.
 *
 */
variables
{
  //
  // Constants
  //

  const DWORD kTimerMS = 100;

  //
  // Global variables
  //

  msTimer gTimer;

  DWORD gCounterRedLamp                  = 0;
  DWORD gCounterMalfunctionIndicatorLamp = 0;
  DWORD gCounterAmberWarningLamp         = 0;
  DWORD gCounterProtectStatusLamp        = 0;
}
/*@@end*/

/*@@pg:*:*/
on pg *
{
  output( this );
}
/*@@end*/

/*@@pg:CAN1.J1939::DM1 (0xFECAFEX):*/
on pg DM1
{
  if (this.RedStopLampState > 0)
  {
    @sysvar::DM1_All::RedStopLamp = 1;
    gCounterRedLamp               = 20;
  }

  if (this.MalfunctionIndicatorLampStatus > 0)
  {
    @sysvar::DM1_All::MalfunctionIndicatorLamp = 1;
    gCounterMalfunctionIndicatorLamp           = 20;
  }

  if (this.AmberWarningLampStatus > 0)
  {
    @sysvar::DM1_All::AmberWarningLamp = 1;
    gCounterAmberWarningLamp           = 20;
  }

  if (this.ProtectLampStatus > 0)
  {
    @sysvar::DM1_All::ProtectLampStatus = 1;
    gCounterProtectStatusLamp           = 20;
  }
}
/*@@end*/

/*@@startStart:Start:*/
on start
{
  gTimer.set( kTimerMS );
}
/*@@end*/

/*@@timer:gTimer:*/
on timer gTimer
{
  //
  // Reset Red Lamp, if no node sets it for more than 2 seconds
  //

  if (gCounterRedLamp > 0)
  {
    gCounterRedLamp--;

    if (gCounterRedLamp == 0)
    {
      @sysvar::DM1_All::RedStopLamp = 0;
    }
  }

  //
  // Reset Malfunction Indicator, if no node sets it for more than 2 seconds
  //

  if (gCounterMalfunctionIndicatorLamp > 0)
  {
    gCounterMalfunctionIndicatorLamp--;

    if (gCounterMalfunctionIndicatorLamp == 0)
    {
      @sysvar::DM1_All::MalfunctionIndicatorLamp = 0;
    }
  }

  //
  // Reset Amber Warning Lamp, if no node sets it for more than 2 seconds
  //

  if (gCounterAmberWarningLamp > 0)
  {
    gCounterAmberWarningLamp--;

    if (gCounterAmberWarningLamp == 0)
    {
      @sysvar::DM1_All::AmberWarningLamp = 0;
    }
  }

  //
  // Reset Protect Status Lamp, if no node sets it for more than 2 seconds
  //

  if (gCounterProtectStatusLamp > 0)
  {
    gCounterProtectStatusLamp--;

    if (gCounterProtectStatusLamp == 0)
    {
      @sysvar::DM1_All::ProtectLampStatus = 0;
    }
  }

  gTimer.set( kTimerMS );
}
/*@@end*/

