/**
 * @file JARTStateMachineGAM.h
 * @brief Header file for class JARTStateMachineGAM
 * @date Nov 26, 2018
 * @author aneto
 *
 * @copyright Copyright 2015 F4E | European Joint Undertaking for ITER and
 * the Development of Fusion Energy ('Fusion for Energy').
 * Licensed under the EUPL, Version 1.1 or - as soon they will be approved
 * by the European Commission - subsequent versions of the EUPL (the "Licence")
 * You may not use this work except in compliance with the Licence.
 * You may obtain a copy of the Licence at: http://ec.europa.eu/idabc/eupl
 *
 * @warning Unless required by applicable law or agreed to in writing,
 * software distributed under the Licence is distributed on an "AS IS"
 * basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the Licence permissions and limitations under the Licence.

 * @details This header file contains the declaration of the class JARTStateMachineGAM
 * with all of its public, protected and private members. It may also include
 * definitions for inline methods which need to be visible to the compiler.
 */

#ifndef GAMS_JARTSTATEMACHINEGAM_H_
#define GAMS_JARTSTATEMACHINEGAM_H_

/*---------------------------------------------------------------------------*/
/*                        Standard header includes                           */
/*---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
/*                        Project header includes                            */
/*---------------------------------------------------------------------------*/

#include "GAM.h"

/*---------------------------------------------------------------------------*/
/*                           Class declaration                               */
/*---------------------------------------------------------------------------*/

/**
 * @brief GAM that provides real-time state machine.
 *
 * The configuration syntax is (names and signal quantity are only given as an example):
 * <pre>
 *         +GAMRealTimeStateMachine = {
 *           Class = JARTStateMachineGAM
 *           ConditionTrigger = 1
 *           mhvps_hvon = 4
 *           aps_hvon = 1
 *           aps_swon = 16
 *           bps_hvon = 2
 *           bps_swon = 8
 *           InputSignals = {
 *               Time = {
 *                   DataSource = DDB1
 *                   Type = uint32
 *               }
 *               PLC_ON = {
 *                   DataSource = DDB1
 *                   Type = uint32
 *               }
 *               MHVPS_DT = {
 *                   DataSource = DDB1
 *                   Type = uint32
 *               }
 *               APS_HVON_DT = {
 *                   DataSource = DDB1
 *                   Type = uint32
 *               }
 *               APS_SWON_DT = {
 *                   DataSource = DDB1
 *                   Type = uint32
 *               }
 *               BPS_HVON_DT = {
 *                   DataSource = DDB1
 *                   Type = uint32
 *               }
 *               BPS_SWON_DT = {
 *                   DataSource = DDB1
 *                   Type = uint32
 *               }
 *               SHOTLEN = {
 *                   DataSource = DDB1
 *                   Type = uint32
 *               }
 *               StopRequest = {
 *               	DataSource = DDB1
 *               	Type = uint32
 *               }
 *               MODE_SHOTLEN_FLAG = {
 *                   DataSource = DDB1
 *                   Type = uint32
 *               }
 *           }
 *           OutputSignals = {
 *               Value = {
 *                   DataSource = DDB1
 *                   Type = uint32
 *                   Trigger = 1
 *               }
 *               BEAM_ON_STAT = {
 *                   DataSource = RealTimeThreadAsyncBridge
 *                   Type = uint32
 *               }
 *               HVARMED = {
 *                   DataSource = RealTimeThreadAsyncBridge
 *                   Type = uint32
 *               }
 *               HVINJECTION = {
 *                   DataSource = RealTimeThreadAsyncBridge
 *                   Type = uint32
 *               }
 *               RFON = {
 *                   DataSource = RealTimeThreadAsyncBridge
 *                   Type = uint32
 *               }
 *               BeamONTime = {
 *                   DataSource = RealTimeThreadAsyncBridge
 *                   Type = uint32
 *               }
 *               RFONTime = {
 *                   DataSource = RealTimeThreadAsyncBridge
 *                   Type = uint32
 *               }
 *               SHOT_ID = {
 *                   DataSource = RealTimeThreadAsyncBridge
 *                   Type = uint32
 *               }
 *           }
 *       }
 * 
 * </pre>
 * 
 */

class JARTStateMachineGAM : public MARTe::GAM, public MARTe::StatefulI {
public:
    CLASS_REGISTER_DECLARATION()

    JARTStateMachineGAM();

    virtual ~JARTStateMachineGAM();

    virtual bool Initialise(MARTe::StructuredDataI & data);

    virtual bool Setup();

    virtual bool Execute();

    virtual bool PrepareNextState(const MARTe::char8 * const currentStateName,
                                  const MARTe::char8 * const nextStateName);

private:
    //The list of possible states
    enum JARealTimeState {
    	WaitTrigger = 0,
		SwitchingHVPS = 1,
		RFON = 2,
		HVTerminate = 3
    };

    //The current rtState
    JARealTimeState currentState;

    //A given condition
    MARTe::uint32 conditionTrigger;

    //What to output in a given state and condition
    MARTe::uint32 mhvps_hvon;
    MARTe::uint32 aps_hvon;
    MARTe::uint32 aps_swon;
    MARTe::uint32 bps_hvon;
    MARTe::uint32 bps_swon;

    //The trigger signal (PLC_ON)
    MARTe::uint32 *triggerSignal;

    //Time signal
    MARTe::uint32 *currentTime;

    // Input signals for trigger delay parameters
    MARTe::uint32 *triggerDelay_mhvps_hvon;
    MARTe::uint32 *triggerDelay_aps_hvon;
    MARTe::uint32 *triggerDelay_aps_swon;
    MARTe::uint32 *triggerDelay_bps_hvon;
    MARTe::uint32 *triggerDelay_bps_swon;
    MARTe::uint32 *triggerDelay_shotlen;

    // Input signal for sequence stop request.
    MARTe::uint32 *stopRequest;
    // Input signal for pulse length limit by mode.
    MARTe::uint32 *modePulseLengthLimit;
    // Input signal for short pulse mode.
    MARTe::uint32 *short_pulse_mode;
    // Input signal for modulation pv.
    MARTe::uint32 *modulation;

    // Output signal to which the output value will be written.
    // One state write One signal.
    MARTe::uint32 *outputSignal;
    // state notify output
    MARTe::uint32 *outputBeamON;
    MARTe::uint32 *outputHVArmed;
    MARTe::uint32 *outputHVInjection;
    MARTe::uint32 *outputRFON;
    // elapsed time notify output;
    MARTe::uint32 *outputBeamONTime;
    MARTe::uint32 *outputRFONTime;
    // shot counter (coutup every RFON time.)
    MARTe::uint32 *shotCounter;

    // Added for HVPS state (20201117)
    MARTe::uint32 *outputAPSHVON;
    MARTe::uint32 *outputAPSSWON;
    MARTe::uint32 *outputBPSHVON;
    MARTe::uint32 *outputBPSSWON;
    MARTe::uint32 *outputMHVPSON;

    // Output signals for NI devices
    MARTe::uint32 *outputSignalNI6259; 
    MARTe::uint8 *outputSignalNI6528P3;
    MARTe::uint8 *outputSignalNI6528P4;

    //////////////////////////////
    //Internal Parameters
    //////////////////////////////
    //PLC_ON time holder
    MARTe::uint32 plcOnTime;
    //APS_SWON time holder
    MARTe::uint32 apsSwonTime;
    MARTe::uint32 apsSwoffTime;
    MARTe::uint64 apsSwonHighResolutionTime;
    
    //PS turn off delay
    MARTe::uint32 turn_off_delay;

    //HVPS state holder
    bool mhvps_hvon_is_on;
    bool aps_hvon_is_on;
    bool aps_swon_is_on;
    bool bps_hvon_is_on;
    bool bps_swon_is_on;

    // terminal values
    MARTe::uint8 p3Value;
    MARTe::uint8 p4Value;
    MARTe::uint8 aps_hvon_state;
    MARTe::uint8 aps_swon_state;
    MARTe::uint8 mhvps_hvon_state;
    MARTe::uint8 bps_hvon_state;
    MARTe::uint8 bps_swon_state;

};



/*---------------------------------------------------------------------------*/
/*                        Inline method definitions                          */
/*---------------------------------------------------------------------------*/

#endif /* GAMS_JARTSTATEMACHINEGAM_H_ */
