/*
 *
 *
 */

program mrLlrfInterlock
option +r;    /* re-tentrant */
option +c;    /* wait until all connection are established */

%{
    #include <math.h>
    #define  MANUAL           0
    #define  FULLAUTO         1
    #define  BYPASS           2


    #define  HVON             1
    #define  HVOFF            0
    #define  NOFAULT          0
    #define  FAULT            1
    #define  RESET            1
    
    #define  RAMP_DONE        2
    #define  RAMP_UP          1
    #define  RAMP_STOP        0
    
    /* for trigger control */
    #define  TRIG_DISABLE     0
    #define  TRIG_STDBYONLY   1
    #define  TRIG_NORMALOP    2
    
    #define  STATUS1_WAIT     0
    #define  STATUS1_FAULT    1
    #define  STATUS1_RAMPUP   2
    
    #define  STATUS2_WAIT     0
    #define  STATUS2_RAMPUP   1
    
    #define  FINALMODE_STANDBY 0
    #define  FINALMODE_ACCELERATE1 1
    #define  FINALMODE_ACCELERATE2 2

}%


int        reset;
assign     reset               to "{PRIMARY}:{MICRO}:{UNIT}:FAULTSEQ:RESET";
monitor    reset;
int        status1;
assign     status1             to "{PRIMARY}:{MICRO}:{UNIT}:FAULTSEQ:STATUS1";
int        status2;
assign     status2             to "{PRIMARY}:{MICRO}:{UNIT}:FAULTSEQ:STATUS2";
int        mode;
assign     mode                to "{PRIMARY}:{MICRO}:{UNIT}:FAULTSEQ:MODE";
monitor    mode;
double     ramp_time;        
assign     ramp_time           to "{PRIMARY}:{MICRO}:{UNIT}:FAULTSEQ:RAMPTIME";
monitor    ramp_time;
int        ramp_up;
assign     ramp_up             to "{PRIMARY}:{MICRO}:{UNIT}:FAULTSEQ:RAMPUP";
monitor    ramp_up;
int        fault_out;
assign     fault_out           to "{PRIMARY}:{MICRO}:{UNIT}:RTM:FAULTOUT";
monitor    fault_out;
int        fault_clear;
assign     fault_clear         to "{PRIMARY}:{MICRO}:{UNIT}:RTM:CLEARFAULT";
int        trig_control;
assign     trig_control        to "{PRIMARY}:{MICRO}:{UNIT}:TRIGMODE";
double     corrscale_control;
assign     corrscale_control   to "{PRIMARY}:{MICRO}:{UNIT}:CORRSCALE";
int        mod_status;
assign     mod_status          to "{MODFAULT}";
monitor    mod_status;

int        final_mode;
assign     final_mode          to "{FINAL_MODE}";
monitor    final_mode;

double    stored_corrscale;



ss recovery
{
    state wait {
        option +t;    /* reset timer everytime */
        option +e;    /* inhibit entry execution for self-state-transition */
        
        entry{
            status1 = STATUS1_WAIT;
            pvPut(status1);
        }
        
        when(mode == BYPASS) {} state wait
       
        when(fault_out == FAULT || mod_status == HVOFF) {    /* when fault is couurred */
            trig_control = TRIG_STDBYONLY;            /* move trigger to standby */
            pvPut(trig_control);
        
            pvGet(corrscale_control);                 /* get current corrscale amplitude */
            stored_corrscale = corrscale_control;     /* ans store into local vaiable */
            corrscale_control = 0.;
            pvPut(corrscale_control);                 /* RF power down */
            
        } state fault
    
    }
    
    
    state fault {
        option +t;    /* reset timer everytime */
        option +e;    /* inhibit entry execution for self-state-transition */
        
        entry {
            status1 = STATUS1_FAULT;
            pvPut(status1);
            trig_control = TRIG_STDBYONLY;            /* move trigger to standby */
            pvPut(trig_control);
            corrscale_control = 0.;
            pvPut(corrscale_control);
        }
        when(reset == RESET) {} state wait           /* forece reset */
        when(mode == BYPASS) {} state wait
        when(mode == MANUAL) {} state fault
        
        when(mod_status == HVON) {
            fault_clear = 1;
            pvPut(fault_clear);
            %% epicsThreadSleep(1.);
            fault_clear = 0;
            pvPut(fault_clear);
        } state ramp
   
    }
    
    
    state ramp {
        option +t;    /* reset timer everytime */
        option +e;    /* inhibit entry execution for self-state-transition */
        
        entry {
            status1 = STATUS1_RAMPUP;
            pvPut(status1);
            ramp_up = RAMP_UP;
            pvPut(ramp_up);
            
        }
        when(reset == RESET) {} state wait          /* forece reset */
        when(mode == BYPASS) {} state wait
        when(fault_out == FAULT || mod_status == HVOFF) { 
            ramp_up = RAMP_STOP;
            pvPut(ramp_up);
        } state fault
        
        when(corrscale_control >= stored_corrscale) {  /* finished ramp up, decide to make accelerate mode or standby mode */
            if(final_mode == FINALMODE_STANDBY) {
                trig_control = TRIG_STDBYONLY;
                pvPut(trig_control);
            }
            
            if(final_mode == FINALMODE_ACCELERATE1 || final_mode == FINALMODE_ACCELERATE2) {
                trig_control = TRIG_NORMALOP;
                pvPut(trig_control);
            }
        
        } state wait
               
        when(fault_out == NOFAULT) {} state ramp
        
    }

}

ss rampup
{
    
    state wait {
        option +t;    /* reset timer everytime */
        option +e;    /* inhibit entry execution for self-state-transition */
         
        entry {
            status2 = STATUS2_WAIT;
            pvPut(status2);
        }
        
        when(mode == BYPASS) {} state wait
      
        when(ramp_up == RAMP_UP) {} state ramp
    }
    
    state ramp {
        option +t;    /* reset timer everytime */
        option +e;    /* inhibit entry execution for self-state-transition */
        
        entry {
            status2 = STATUS2_RAMPUP;
            pvPut(status2);
        }
        
        when(reset == RESET) {} state wait            /* forece reset */
        when(mode == BYPASS) {} state wait
        when(ramp_up == RAMP_STOP) {} state wait
        
        when(corrscale_control >= stored_corrscale) {
            ramp_up = RAMP_DONE;
            pvPut(ramp_up);
        } state wait
        
        when(delay(ramp_time/10.)) {
            corrscale_control += (stored_corrscale/10.);
            if(corrscale_control >= stored_corrscale) corrscale_control = stored_corrscale;
            pvPut(corrscale_control); 
        } state ramp
    }
    
}
