#include "ieeeio.h"
#include <stdio.h>

char hundred[1700];

main() {
  char   response[256];
  int    i;
  float  voltage,
	 sum;

  int    cklpint(); /* check light pen interrupt function */
  void   isr();     /* handle interrupt */

/* Establish communications with Personal488 */
  if (ieeeinit()==-1) {
    printf("Cannot initialize IEEE system.\n");
    exit(1);
  }

/* Set up interrupt pointers */
  ieee_cki = cklpint;
  ieee_isr = isr;

/* Enable Personal488 interrupt on Service Request */
  ieeewt("arm srq\n");

/* Read the Driver488 revision number */
  ieeewt("hello\n");
  ieeerd(response);
  printf("%s\n",response);

/* Display the Personal488 system status */
  ieeewt("status\n");
  ieeerd(response);
  printf("%s\n",response);

/* Put the 195 at bus address 16 into REMOTE */
  ieeewt("remote 16\n");

/* Enable 195 SRQ on Illegal Command */
  ieeewt("output 16;M2X\n");

/* Set 195 to Autorange, DC Volts */
  ieeewt("output 16;F0R0X\n");

/* Display a reading */
  ieeewt("enter 16\n");
  ieeescnf("%*4s%e",&voltage);
  printf("The read value is %g\n",voltage);

/* Display the average of 10 readings */
  sum=0.0;
  for (i=0; i<10; i++) {
    ieeewt("enter 16\n");
    ieeescnf("%*4s%e",&voltage);
    sum=sum+voltage;
  }
  printf("The average of 10 readings is %g\n",sum/10.0);

  /* Take 100 readings = 1700 bytes from the 195 */
/*  ieeeprtf("enter 16 #1700 buffer %d:%d\n",
           segment(hundred),offset(hundred)); */

  ieeeprtf("enter 16 #1700 buffer %d:%d dma continue\n",
           segment(hundred),offset(hundred));

  /* Check the STATUS both before and after WAITing */
  ieeewt("status\n");
  ieeerd(response);
  printf("%s\n",response);

  ieeewt("wait\n");

  ieeewt("status\n");
  ieeerd(response);
  printf("%s\n",response);

  /* Print the received characters */
  for (i=0; i<1700; i++) putchar(hundred[i]);
}

/* Interrupt service routine for Personal488 interrupts */

void isr()
{ int sp,
      st195;
  int _false_(),
      cklpint();

  printf("Interrupt detected...");

/* Turn off interrupt checking during the ISR */
  ieee_cki=_false_;

/* Check if the interrupt was due to a Service Request */
  ieeewt("spoll\n");
  ieeescnf("%d",&sp);
  if (sp==0) {
    printf("Non-SRQ Interrupt!\n");
    exit();
  }

/* Check that the Service Request was from the 195 */

  ieeewt("spoll 16\n");
  ieeescnf("%d",&st195);
  if ((st195 & 0x40) == 0 ) {
    printf("Non-195 SRQ!\n");
    exit();
  }

/* Interpret the Serial Poll response */
  if ((st195 & 0x20) == 0) {
    if (st195 & 0x01) printf("Overflow\n");
    if (st195 & 0x02) printf("Buffer Full\n");
    if (st195 & 0x04) printf("Buffer 1/2 Full\n");
    if (st195 & 0x08) printf("Reading Done\n");
    if (st195 & 0x10) printf("Busy\n");
  } else {
    if (st195 & 0x01) printf("Illegal Command Option\n");
    if (st195 & 0x02) printf("Illegal Command\n");
    if (st195 & 0x04) printf("No Remote\n");
    if (st195 & 0x08) printf("Trigger Overrun\n");
    if (st195 & 0x10) printf("Failed Selftest\n");
  }
/* Re-enable interrupt checking */
  ieee_cki = cklpint;
}
