/*
 * Tek 8200 device support (only histgram)
 */

#include <epicsStdio.h>
#include <devCommonGpib.h>
#include <string.h>
#include <strings.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     devAiTDS8200
#define DSET_AO     devAoTDS8200
#define DSET_BI     devBiTDS8200
#define DSET_BO     devBoTDS8200
#define DSET_EV     devEvTDS8200
#define DSET_LI     devLiTDS8200
#define DSET_LO     devLoTDS8200
#define DSET_MBBI   devMbbiTDS8200
#define DSET_MBBID  devMbbidTDS8200
#define DSET_MBBO   devMbboTDS8200
#define DSET_MBBOD  devMbbodTDS8200
#define DSET_SI     devSiTDS8200
#define DSET_SO     devSoTDS8200
#define DSET_WF     devWfTDS8200

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

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



/*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 \r and \n
 * characters.  When defining input strings so you include them as well.
 ******************************************************************************/



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

    /* CMMAND 0 histgram restart */
  {&DSET_BO, GPIBWRITE, IB_Q_LOW, NULL, "HISTOGRAM:COUNT RESET\r\n", 0, 255, NULL, 0, 0, NULL, NULL, NULL },

    /* CMMAND 1 Header off */
  {&DSET_BO, GPIBWRITE, IB_Q_LOW, NULL, "HEADER OFF\r\n", 0, 255, NULL, 0, 0, NULL, NULL, NULL },
  
    /* CMMAND 2 Histgram hits read */
  {&DSET_AI, GPIBREAD, IB_Q_LOW, "HIS:STAT:HITS?\r\n", "%lf\r\n", 0, 2047, NULL, 0, 0, NULL, NULL, NULL },

    /* CMMAND 3 Histogram mean read */
  {&DSET_AI, GPIBREAD, IB_Q_LOW, "HIS:STAT:MEAN?\r\n", "%le\r\n", 0, 2047, NULL, 0, 0, NULL, NULL, NULL },
 
     /* CMMAND 4 Histogram sigma 1 read*/
  {&DSET_AI, GPIBREAD, IB_Q_LOW, "HIST:STAT:SIGMA1?\r\n", "%le\r\n", 0, 2047, NULL, 0, 0, NULL, NULL, NULL },

     /* CMMAND 5 Histogram STD read */
  {&DSET_AI, GPIBREAD, IB_Q_LOW, "HIST:STAT:STD?\r\n", "%le\r\n", 0, 2047, NULL, 0, 0, NULL, NULL, NULL },
  /* command 6 Horizontal position read */
  {&DSET_AI,GPIBREAD, IB_Q_LOW, "HORIZONTAL:MAIN:POSITION?\r\n","%le\r\n",0,2047,NULL,0,0, NULL, NULL, NULL},
  /* command 7 Horizontal positon set */
  {&DSET_AO, GPIBWRITE, IB_Q_LOW, NULL, "HORIZONTAL:MAIN:POSITION %le\r\n", 0, 2047, NULL, 0, 0, NULL, NULL, NULL},
  /* command 8 horizontal scale read */
  {&DSET_AI, GPIBREAD, IB_Q_LOW, "HOR:MAI:SCA?\r\n","%le\r\n", 0, 2047, NULL, 0, 0, NULL, NULL, NULL},
  /* command 9 horizontal scale set */
  {&DSET_AO, GPIBWRITE, IB_Q_LOW, NULL ,"HOR:MAI:SCA %le\r\n", 0, 2047, NULL, 0, 0, NULL, NULL, NULL},

  /* command 10 horozontal ref point set */
  {&DSET_AO,GPIBWRITE, IB_Q_LOW,NULL, "HOR:MAI:REFP %lf\r\n",0, 2047, NULL, 0, 0, NULL, NULL, NULL}

};




/* 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 = "devTDS8200";
        devSupParms.gpibCmds = gpibCmds;
        devSupParms.numparams = NUMPARAMS;
        devSupParms.timeout = TIMEOUT;
        devSupParms.timeWindow = TIMEWINDOW;
        devSupParms.respond2Writes = -1;
    }
    return(0);
}

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