/*
 * Agilent 53230A counter device support
 */

#include <epicsStdio.h>
#include <devCommonGpib.h>
#include <string.h>
#include <strings.h>
#include <stdio.h>
#include <stdlib.h>

/******************************************************************************
 *
 * The following define statements are used to declare the names to be used
 * for the dset tables.   
 *
 * A DSET_AI entry must be declared here and referenced in an application
 * database description file even if the device provides no AI records.
 *
 ******************************************************************************/
#define DSET_AI     devAi53230A
#define DSET_AO     devAo53230A
#define DSET_BI     devBi53230A
#define DSET_BO     devBo53230A
#define DSET_EV     devEv53230A
#define DSET_LI     devLi53230A
#define DSET_LO     devLo53230A
#define DSET_MBBI   devMbbi53230A
#define DSET_MBBID  devMbbid53230A
#define DSET_MBBO   devMbbo53230A
#define DSET_MBBOD  devMbbod53230A
#define DSET_SI     devSi53230A
#define DSET_SO     devSo53230A
#define DSET_WF     devWf53230A

#include <devGpib.h> /* must be included after DSET defines */

#define TIMEOUT     1    /* I/O must complete within this time */
#define TIMEWINDOW  0.1    /* Wait this long after device timeout */


static int rd_wf_data2(struct gpibDpvt *pdpvt, int p1, int p2, char **p3);

/*static int rd_efast_data(struct gpibDpvt *pdpvt,  int p1, int p2, char **p3);
 */

/******************************************************************************
 * Strings used by the init routines to fill in the znam,onam,...
 * fields in BI and BO record types.
 ******************************************************************************/

/******************************************************************************
 * Structures used by the init routines to fill in the onst,twst,... and the
 * onvl,twvl,... fields in MBBI and MBBO record types.
 *
 * Note that the intExtSsBm and intExtSsBmStop structures use the same
 * intExtSsBmStopList and intExtSsBmStopVal lists but have a different number
 * of elements in them that they use... The intExtSsBm structure only represents
 * 4 elements,while the intExtSsBmStop structure represents 5.
 ******************************************************************************/


/******************************************************************************
 * String arrays for EFAST operations. The last entry must be 0.
 *
 * On input operations,only as many bytes as are found in the string array
 * elements are compared.  Additional bytes are ignored.
 * The first matching string  will be used as a match.
 *
 * For the input operations,the strings are compared literally!  This
 * can cause problems if the instrument is returning things like  and \n
 * characters.  When defining input strings so you include them as well.
 ******************************************************************************/
#define EOSLN NULL


/******************************************************************************
 * Array of structures that define all GPIB messages
 * supported for this type of instrument.
 ******************************************************************************/
static struct gpibCmd gpibCmds[] = { 

    /* COMMAND 0 initialize */
  {&DSET_BO, GPIBWRITE, IB_Q_LOW, NULL,"ABOR;:FORM:DATA ASC;:CONF:FREQ 508E6, (@3);:TRIG:SOUR IMM\n", 0, 2047, NULL, 0, 0, NULL, NULL, EOSLN },
  /* Command 1 frequency read  */
  {&DSET_AI, GPIBREAD, IB_Q_LOW, "INIT;:FETCH?\n","%lf\n",0,2047,NULL,0,0,NULL,NULL,EOSLN },
  /* command 2 seq-read set */ 
  {&DSET_BO, GPIBWRITE, IB_Q_LOW, NULL, "ABOR;:FORM:DATA REAL;:FORM:BORDER SWAP;:CONF:FREQ 508E6, (@3);:TRIG:SOUR IMM;:INIT:IMM\n", 0, 255, NULL, 0, 0, NULL, NULL, EOSLN},
  /* command 3 seq read */
  {&DSET_WF, GPIBREAD, IB_Q_HIGH, "R? 50;:INIT:IMM\n", NULL, 0,450000, rd_wf_data2, 0, 0, NULL, NULL,NULL},
  /* command 4 abort */
  {&DSET_BO, GPIBWRITE, IB_Q_LOW, NULL,"ABOR\n", 0, 2047, NULL, 0, 0, NULL, NULL, EOSLN },

 
};




/* The following is the number of elements in the command array above.  */
#define NUMPARAMS sizeof(gpibCmds)/sizeof(struct gpibCmd)

/******************************************************************************
 * Initialize device support parameters
 *
 *****************************************************************************/
static long init_ai(int parm)
{
    if(parm==0) {
        devSupParms.name = "dev53230A";
        devSupParms.gpibCmds = gpibCmds;
        devSupParms.numparams = NUMPARAMS;
        devSupParms.timeout = TIMEOUT;
        devSupParms.timeWindow = TIMEWINDOW;
        devSupParms.respond2Writes = -1;
    }
    return(0);
}

/******************************************************************************
 *
 * c code
 *
 *****************************************************************************/

static int rd_wf_data2(struct gpibDpvt *pdpvt, int p1, int p2, char **p3)
{
  struct waveformRecord *pwf = (struct waveformRecord *)(pdpvt ->precord);
  char *craw;
  char ll[800000];
  int n,n1;
  double *temparray, *lx,lf[2000000];
  unsigned long numElem;
  int i;
    
  lx=(double * )lf;
  temparray = (double *)pwf->bptr;
  craw = pdpvt->msg;

  strncpy(ll,craw,1);
  ll[1]=NULL;
 
  /*  
   printf("craw 1st %s\n",ll);
  if (ll !="#"){
    printf("Invalid data  %x\n",ll[0]);
    return(-1);
  }
  */
 
  strncpy(ll,craw+1,1);
  ll[1]=NULL;
  n = atoi(ll);

  strncpy(ll,craw+2,n);
  ll[n]=NULL;
  n1 = atoi(ll);
 
  /*  
  printf("n1 =%ld\n",n1);
  */  

  numElem = n1 >> 3;

  /*
  printf("numElem %ld\n",numElem);  
  */
  
  memcpy(ll,craw+2+n,n1); 
lx = (double *)ll; pwf->nord = numElem; while(numElem --){ *temparray++ = *lx++; } return(0); }