/* dev17K66A.c */
/*
 *      Author: Makoto Tobiyama
 *      Date:   11_Aug_2009
 */
#include <epicsStdio.h>
#include <devCommonGpib.h>
/***********************************************************************
* asynDriver is distributed subject to a Software License Agreement
* found in file LICENSE that is included with this distribution.
***********************************************************************/
#define	DSET_AI		devAi17K66A
#define	DSET_AO		devAo17K66A
#define	DSET_BI		devBi17K66A
#define	DSET_BO		devBo17K66A
#define	DSET_LI		devLi17K66A
#define	DSET_LO		devLo17K66A
#define	DSET_MBBO      	devMbbo17K66A
#define	DSET_MBBI		devMbbi17K66A
#define	DSET_SI		devSi17K66A
#define	DSET_SO		devSo17K66A
#define DSET_WF		devWf17K66A

#include <devGpib.h>
#include <string.h>
#include <stdlib.h>

#define	TIMEOUT	5.0
#define TIMEWINDOW  2.0

static int readString(gpibDpvt *pdpvt,int P1, int P2, char **P3);
static int rd_data(char wav[], long temp[]);
static int rd_wf_data(struct gpibDpvt *pdpvt, int p1, int p2, char **p3);

/*
 * Define end-of-string character(s) here to allow
 * easier changes when testing the driver.
 */


#define EOSNL ""


static struct gpibCmd gpibCmds[] = { 

    /* COMAND 0 set_delay */
  {&DSET_LO, GPIBWRITE, IB_Q_LOW, NULL, "DLY%05ld\r", 511, 511, NULL, 0, 0, NULL, NULL, EOSNL },

    /* COMAND 1 set_dev */
  {&DSET_LO, GPIBWRITE, IB_Q_LOW, NULL, "DVD%05ld\r", 511, 511, NULL, 0, 0, NULL, NULL, EOSNL },

  /* COMAND 2 read status */
  {&DSET_WF, GPIBREAD, IB_Q_LOW, "GET\r",NULL, 0, 8191, rd_wf_data, 0,0,NULL,NULL, EOSNL }

};




/* The following is the number of elements in the command array above.  */
#define NUMPARAMS	sizeof(gpibCmds)/sizeof(struct gpibCmd)

/******************************************************************************
 *
 * Initialization for device support
 * This is called one time before any records are initialized with a parm
 * value of 0.  And then again AFTER all record-level init is complete
 * with a param value of 1.
 *
 ******************************************************************************/
static long init_ai(int parm)
{
  if(parm==0)  {
    devSupParms.name = "dev17K66A";
    devSupParms.gpibCmds = gpibCmds;
    devSupParms.numparams = NUMPARAMS;
    devSupParms.timeout = TIMEOUT;
    devSupParms.timeWindow = TIMEWINDOW;
    devSupParms.respond2Writes = 1.0;
  }
  return(0);
}

static int readString(gpibDpvt *pdpvt,int P1, int P2, char **P3)
{
    stringinRecord *prec = (stringinRecord*)pdpvt->precord;
    strncpy(prec->val,pdpvt->msg,sizeof(prec->val));
    prec->val[sizeof(prec->val) - 1] = 0;
    return(0);
}

static int rd_data(char wav[], long data[])
{
  /*  012345678901234567890
      OEN/DVD12345/DLY12345
  */
  int i;
  char wa1[32767];
  /* printf("input %s\n",wav);  */
  strcpy(wa1,strtok(wav,"/"));
   if (strcmp(wa1,"OEN")==0) {
    data[0]=0;
  }
  else {
    data[0]=1;
  }

  for (i=7;i<12;i++)
    {
      wa1[i-7]=wav[i];
    }
  /*  printf("DVD %s\n",wa1); */
  data[1]=atoi(wa1);

  for (i=16;i<21;i++)
    {
      wa1[i-16]=wav[i];
    }
  
  data[2]= atoi(wa1);
  return 0;
}


static int rd_wf_data(struct gpibDpvt *pdpvt, int p1, int p2, char **p3)
{
  struct waveformRecord *pwf = (struct waveformRecord *) (pdpvt->precord);
  char *craw;
  long *temparray;
  long temp[500], *ptemp;
  int ok_flag;
  unsigned long numElem;

  temparray = (long *) pwf->bptr;
  craw = pdpvt->msg;

  /* printf("craw %s\n",craw); */
  if (strlen(craw)==0)
    {
      printf("ERROR NULL\n");
      return(-1);
    }
  ok_flag = rd_data(craw,temp);
  numElem=3;
  pwf->nord =numElem;
  ptemp=temp;

  while (numElem--)
    {
      *temparray++ = (int) *ptemp++;
    }
  return(0);

}