/*-*-C-*-*/
	program Bic_Current_Expand("STN=HB60")
/*
      **MEMBER**=SLCLIBS:xxxSHRLIB
      **CMS**=xxxUTIL 
==============================================================
 
  Abs:  This EPICS sequence produces an expanded
        200 bucket region of the full 
        range of bucket currents
 
  Name: BIC_CURRENT_EXPAND.ST

  Proto: none

  Auth: 23-Jul-1997, Ron Chestnut
  Rev:  DD-MMM-YYYY, Reviewer's Name (.NE. Author's Name)
 
-------------------------------------------------------------
 
  Mod:  (newest to oldest)  
        09-Jul-1999, Ron Chestnut
           Fix faulty update logic
        26-Feb-1999, Ron Chestnut
           Use full waveform; no more mode 2 and 3
        04-Nov-1998, Ron Chestnut
           Fix midpoint and data copy logic
        23-Mar-1998, Ron Chestnut:
           100 channels become 200
 
=============================================================*/

option +r;  /* make reentrant */
option -w;  /* turn off SNL warnings */

#include "bic_common.h"

/* Map to EPICs database channels */

	long midpoint; 	assign midpoint to "{STN}:BNCHCURR:MIDP";

        long begpoint; assign begpoint to "{STN}:BNCHCURR:LOWLIM";

        long endpoint; assign endpoint to "{STN}:BNCHCURR:HILIM";

	float ywf[200]; assign ywf to "{STN}:BNCHCURR:EXP:Y";

	long xwf[200]; assign xwf to "{STN}:BNCHCURR:EXP:X";

        float goal_a[N_BUCKET]; assign goal_a to "{STN}:FILL:GOAL";

        float goal_ex[200]; assign goal_ex to "{STN}:FILL:GOAL:EXPY";

	float wf[N_BUCKET]; assign wf to "{STN}:BNCHCURR:GOOD";

        long w1[N_BUCKET]; assign w1 to "{STN}:BNCHCURR:PHASE1";

        long w2[N_BUCKET]; assign w2 to "{STN}:BNCHCURR:PHASE2";

        float phase; assign phase to "{STN}:BNCHEXP:PHASE";

        double w1s; assign w1s to "{STN}:BNCHEXP:PHAS1S";

        double w2s; assign w2s to "{STN}:BNCHEXP:PHAS2S";
 
/* Local variables */

	int i; 

        int j;

        int old_midpoint;

%%#include "taskLib.h"
%%#include "math.h"        /* fabs                  */

	ss arrayZoomSS
	{
          state start
          {
            when(1)
            { 
              midpoint=1000; 
              old_midpoint=0;
              pvPut(midpoint);
            } state spin
           }

	  state spin
	  {

/* Expand the goal and actual currents around the middle point */

           when(1)
	     {  
                pvGet(wf); pvGet(midpoint);
		pvGet(w1); pvGet(w2);
		w1s = w1[midpoint]; w2s = w2[midpoint];
		phase = 57.3*atan2(fabs(w1s),fabs(w2s));
                if(midpoint!=old_midpoint)
                { 
                  pvGet(goal_a);
                  begpoint=midpoint-99;
                  endpoint=midpoint+100;
                }
                j = max(begpoint,0)-1;
		for (i=0; i<200; i++)
                    { if( (j++) < N_BUCKET ) 
                      {
                        ywf[i]=wf[j];
                        if(midpoint!=old_midpoint) goal_ex[i]=goal_a[j];
                      }
                      else goal_ex[i]=ywf[i]=0;
   		      if(midpoint!=old_midpoint) xwf[i]=j;
		    }
	     }  state write

	  }

/* Output the results */

          state write
          {
            when(delay(1.0))
            {	
	        pvPut(ywf);
                if(midpoint!=old_midpoint)
                { 
                  pvPut(xwf); 
                  pvPut(goal_ex);
                  pvPut(begpoint); 
                  pvPut(endpoint);
                  old_midpoint = midpoint;
                }
		pvPut(w1s);
		pvPut(w2s);
		pvPut(phase);
            } state spin
          }
	}

exit{}