program l2Abstraction

%% void kly24_1PhaseResync(char *muxName);
%% void kly24_2PhaseResync(char *muxName);
%% void kly24_1AmplitudeResync(char *muxName);
%% void kly24_2AmplitudeResync(char *muxName);


int      l2_pdes_post_init;
assign   l2_pdes_post_init    to "ACCL:LI24:L2:PDES_POST_INIT";
monitor  l2_pdes_post_init;

int      l2_ades_post_init;
assign   l2_ades_post_init    to "ACCL:LI24:L2:ADES_POST_INIT";
monitor  l2_ades_post_init;


int      l2_resync;
assign   l2_resync            to "ACCL:LI24:L2:RESYNC";
monitor  l2_resync;

char     fancy_ph_ctrl;
assign   fancy_ph_ctrl        to "ACCL:LI24:L2:FANCY_PH_CTRL";
char     ca_fancy_ph_ctrl;
assign   ca_fancy_ph_ctrl     to "SIOC:SYS0:ML00:AO058";
monitor  ca_fancy_ph_ctrl;
evflag   ev_fancy_ph_ctrl;
sync     ca_fancy_ph_ctrl  ev_fancy_ph_ctrl;


char     ph_scan_gui;
assign   ph_scan_gui          to "ACCL:LI24:L2:PH_SCAN_GUI";
char     ca_ph_scan_gui;
assign   ca_ph_scan_gui       to "SIOC:SYS0:ML00:AO017";
monitor  ca_ph_scan_gui;
evflag   ev_ph_scan_gui;
sync     ca_ph_scan_gui  ev_ph_scan_gui;


double   pdes_mdl;
assign   pdes_mdl             to "ACCL:LI24:L2:PDES_MDL";
double   ca_pdes_mdl;
assign   ca_pdes_mdl          to "LLRF:IN20:RH:REF_2_PDES";
monitor  ca_pdes_mdl;
evflag   ev_pdes_mdl;
sync     ca_pdes_mdl    ev_pdes_mdl;


double   l2_flat_total;
assign   l2_flat_total        to "ACCL:LI24:L2:FLAT_TOTAL";
double   ca_l2_flat_total;
assign   ca_l2_flat_total     to "SIOC:SYS0:ML00:AO278";
monitor  ca_l2_flat_total;
evflag   ev_l2_flat_total;
sync     ca_l2_flat_total  ca_l2_flat_total;


double   l2_fudge;
assign   l2_fudge             to "ACCL:LI24:L2:FUDGE";
double   ca_l2_fudge;
assign   ca_l2_fudge          to "SIOC:SYS0:ML00:AO067";
monitor  ca_l2_fudge;
evflag   ev_l2_fudge;
sync     ca_l2_fudge       ev_l2_fudge;


double   l2_phase;
assign   l2_phase            to "ACCL:LI24:L2:ABSTR_PDES";
double   ca_l2_phase;
assign   ca_l2_phase         to "SIOC:SYS0:ML00:AO061";
monitor  ca_l2_phase;
evflag   ev_l2_phase;
sync     ca_l2_phase    ev_l2_phase;

double  l2_energy;
assign  l2_energy            to "ACCL:LI24:L2:ABSTR_ADES";
double  ca_l2_energy;
assign  ca_l2_energy         to "SIOC:SYS0:ML00:AO059";
monitor ca_l2_energy;
evflag  ev_l2_energy;
sync    ca_l2_energy    ev_l2_energy;


double   l2_phase_offset;
assign   l2_phase_offset      to "ACCL:LI24:L2:ABSTR_POC";
double   ca_l2_phase_offset;
assign   ca_l2_phase_offset   to "SIOC:SYS0:ML00:AO062";
monitor  ca_l2_phase_offset;
evflag   ev_l2_phase_offset;
sync     ca_l2_phase_offset   ev_l2_phase_offset;


double put_ref_phase_hihi;
assign put_ref_phase_hihi   to "ACCL:LI24:L2:PUT:REF_PH_HIHI";
double put_ref_phase_high;
assign put_ref_phase_high   to "ACCL:LI24:L2:PUT:REF_PH_HIGH";
double put_ref_phase_low;
assign put_ref_phase_low    to "ACCL:LI24:L2:PUT:REF_PH_LOW";
double put_ref_phase_lolo;
assign put_ref_phase_lolo   to "ACCL:LI24:L2:PUT:REF_PH_LOLO";

double  ref_phase_hihi;
assign  ref_phase_hihi      to "LLRF:IN20:RH:REF_1_S_PA.HIHI";
monitor ref_phase_hihi;
double  ref_phase_high;
assign  ref_phase_high      to "LLRF:IN20:RH:REF_1_S_PA.HIGH";
monitor ref_phase_high;
double  ref_phase_low;
assign  ref_phase_low       to "LLRF:IN20:RH:REF_1_S_PA.LOW";
monitor ref_phase_low;
double  ref_phase_lolo;
assign  ref_phase_lolo      to "LLRF:IN20:RH:REF_1_S_PA.LOLO";
monitor ref_phase_lolo;


ss l2Resync {
    state resync {
        when(1) {
            l2_resync = 0; pvPut(l2_resync);
            put_ref_phase_hihi = ref_phase_hihi;  pvPut(put_ref_phase_hihi);
            put_ref_phase_high = ref_phase_high;  pvPut(put_ref_phase_high);
            put_ref_phase_low  = ref_phase_low;   pvPut(put_ref_phase_low);
            put_ref_phase_lolo = ref_phase_lolo;  pvPut(put_ref_phase_lolo);

            %% kly24_1PhaseResync("ACCL:LI24:100:KLY_PDES");
            %% kly24_2PhaseResync("ACCL:LI24:200:KLY_PDES");
            %% kly24_1AmplitudeResync("ACCL:LI24:100:KLY_ADES");
            %% kly24_2AmplitudeResync("ACCL:LI24:200:KLY_ADES");
        } state wait
        
    }

    state wait {
        when(l2_resync) {} state resync
    }
}

ss l2Abstraction_mon
{
    state init {
        when(1) {
             fancy_ph_ctrl   = ca_fancy_ph_ctrl;     pvPut(fancy_ph_ctrl);
             ph_scan_gui     = ca_ph_scan_gui;       pvPut(ph_scan_gui);
             pdes_mdl        = ca_pdes_mdl;          pvPut(pdes_mdl);
             l2_flat_total   = ca_l2_flat_total;     pvPut(l2_flat_total);
             l2_fudge        = ca_l2_fudge;          pvPut(l2_fudge);
             l2_phase        = ca_l2_phase;          pvPut(l2_phase);
             l2_energy       = ca_l2_energy;         pvPut(l2_energy);
             l2_phase_offset = ca_l2_phase_offset;   pvPut(l2_phase_offset);
        } state l2Abstraction_mon

    }

    state l2Abstraction_mon {
        when(efTestAndClear(ev_fancy_ph_ctrl)) {
            fancy_ph_ctrl = ca_fancy_ph_ctrl;
            pvPut(fancy_ph_ctrl);
        } state l2Abstraction_mon

        when(efTestAndClear(ev_ph_scan_gui)) {
            ph_scan_gui = ca_ph_scan_gui;
            pvPut(ph_scan_gui);
        } state l2Abstraction_mon

        when(efTestAndClear(ev_pdes_mdl)) {
            pdes_mdl = ca_pdes_mdl;
            pvPut(pdes_mdl);
        } state l2Abstraction_mon

        when(efTestAndClear(ev_l2_flat_total)) {
           l2_flat_total = ca_l2_flat_total;
           pvPut(l2_flat_total);
        } state l2Abstraction_mon     

        when(efTestAndClear(ev_l2_fudge)) {
            l2_fudge = ca_l2_fudge;
            pvPut(l2_fudge);
        } state l2Abstraction_mon

        when(efTestAndClear(ev_l2_phase)) {
            l2_phase = ca_l2_phase;
            pvPut(l2_phase);
        } state l2Abstraction_mon

        when(efTestAndClear(ev_l2_energy)) {
           l2_energy = ca_l2_energy;
           pvPut(l2_energy);
        } state l2Abstraction_mon

        when(efTestAndClear(ev_l2_phase_offset)) {
            l2_phase_offset = ca_l2_phase_offset;
            pvPut(l2_phase_offset);
        } state l2Abstraction_mon

    } /* end of state */
}


