/*
   this program does the communication with the halder vka.
   It is mainly used to read out the vka, but can also be used to send specific
   commands to it.
*/
#include <stdio.h>
#include <sys/types.h>
#include <signal.h>
#include <time.h>
#include <auxcfg.h>
#include <spec.h>
#define TMPFILE "tica.all"

#define DATSIZ 67160

int flg16k,fdhalder;
float *spc, *err;

help()
{
printf("This program reads data from the Halder VKA and writes back the\n");
printf("spectrum with corresponding error and time arrays\n");
printf("vka [options]\n");
printf("possible options:\n");
printf("  -o name writes output to file (instead of stdout)\n");
printf("  -c text uses text as comment. should be the LAST OPTION !\n");
printf("  -notim  supress appending actual time to comment\n");
printf("  -t n.m  uses time calibration specified (instead of %s)\n",TMPFILE);
printf("  -cl     clear VKA, don't read out !\n");
printf("  -do str send command to Halder, don't read out\n");
printf("  -dev str   use device entry str instead of 'halder'");
printf("BE SHURE TO HAVE CONFIGURED Aux_Config FOR YOUR VKA !");
}

main(argc,argv)
{
int i,n,m,ymax;
time_t izeit;
char c,comment[80],s[80],z[80],zeit[80],vkaname[30];
FILE *fp;
struct tm *now;

   _tica=1.0;
   fp=fopen(TMPFILE,"r"); 
   if(fp!=NULL) {
      fscanf(fp,"%s\n",s); _tica = atosf(s);
      fclose(fp);
   }

   strcpy(vkaname,"halder");
   if(checkopt(argc,argv,"-dev",s)) strcpy(vkaname,s);
   fdhalder=auxopen("halder");

   flg16k=FALSE;
   if(instr("16k",auxparams[4])>0) flg16k=TRUE;

   if(checkopt(argc,argv,"-t",s)) _tica = atosf(s); 

   if(checkopt(argc,argv,"-cl",s)) {
      strcpy(s,"\x1bCL00008191Y");
      if(flg16k) strcpy(s,"\x1bCL0000016383Y");
      write(fdhalder,s,strlen(s));
      close(fdhalder);
      exit(0);
   }
   if(checkopt(argc,argv,"-do",s)) {
      strcpy(z,"\x1b"); strcat(z,s);
      write(fdhalder,z,strlen(z));
      close(fdhalder);
      exit(0);
   }
      
   spc= (float *)calloc(_MAXSPCLEN+2,sizeof(float));
   err= (float *)calloc(_MAXSPCLEN+2,sizeof(float));

   read_vka(spc);
   ymax=0;                             /* calculate error and time arrays */
   for(n=0;n<=_MAXSPCLEN;n++) {
      err[n] = sqrt(fabs(spc[n]));
      if(spc[n]>ymax) ymax = spc[n];     /* to determine the best int size */
   }
   strcpy(comment,"life display");
   get_comment(argc,argv,"-c",comment);

   izeit=time(NULL);                        /* get actual time */
   now=localtime(&izeit);
   strftime(zeit,20,"   %H:%M %d.%m.%y",now);
 
   if(!checkopt(argc,argv,"-notim",s)) strcat(comment,zeit);
   n=0;
   if(ymax>32766) n=1;
   writespec("",spc,err,_MAXSPCLEN,n,comment);
   close(fdhalder);
   free(spc); free(err);
}

get_comment(argc,argv,s,z)
int argc;
char *argv[],s[],z[];
{
int n,m,i;
char c;

   for(n=0;n<=argc;n++) {
      if(strcmp(argv[n],s)==0) {
	 strcpy(z,"");
	 for(i=n+1;i<=argc;i++) {
	    strcat(z,argv[i]);
	    strcat(z," ");
         }
	 break;
      }
   }
}

read_vka(spc)
float spc[];
{
int i,n,m;
char c,s[80],z[80],*bigbuf;

devclr(fdhalder);

if(instr("PB",auxparams[5])>0) {   /* Hex mode without echo */
   strcpy(s,"\x1bPB00008191");
   if(flg16k) strcpy(s,"\x1bPB0000008191");
   write(fdhalder,s,strlen(s));
/*     bigbuf = (char *) calloc(8192*8,sizeof(char));
       read(fdhalder,bigbuf,2*8192);
       m=0;
       for(n=0;n<8192;n++) {
          for(i=0;i<8;i++) s[i]=bigbuf[m++];
          s[8]=0;
          i=xtoi(s);
          printf("%d  %s %d\n",n,s,i);
          spc[n]=i;
       }
       free(bigbuf);
*/
   for(n=0;n<8192;n++) {
      read(fdhalder,s,7);
      s[7]=0;
      i=xtoi(s);
      spc[n]=i;
   }
}

if(instr("PR",auxparams[5])>0) {    /* dez. mode with echo (very slow) */
   strcpy(s,"\x1bPR0000819106N");
   if(flg16k) strcpy(s,"\x1bPR000000819106N");
   write(fdhalder,s,strlen(s));

   bigbuf=(char *) calloc(DATSIZ+3,sizeof(char));
   
   read(fdhalder,bigbuf,DATSIZ);
   bigbuf[DATSIZ]='0';
   bigbuf[DATSIZ+1]=0;

   n=0; m=0;
   while(m<DATSIZ) {
      while(bigbuf[m]<48) m++ ;  /* skip blank / CR / LF */
      i=0;
      while(bigbuf[m]>47) s[i++]=bigbuf[m++];
      s[i]=0;
      i=atoi(s);
      spc[n++]=i;
      if(n>8191) break;
   }
   free(bigbuf);
   devclr(fdhalder);
}

strcpy(s,"\x1b");
sscanf(auxparams[6],"%s",z); strcat(s,z);
write(fdhalder,s,strlen(s));
}

xtoi(s)
char s[];
{
int i,m,l,e;
char c;

   l=strlen(s); e=0;
   for(i=0;i<l;i++) {
      c=s[i];
      if((c>'F') || (c<'0')) {
	 printf("Warning ! file may be completely in a mess !\n");
	 printf("Reset halder and CONVERTER ! before your next trial\n");
	 printf("and use <devclr halder> to clear the line\n");
	 printf("%s\n",s);
      }
      m=c-'0';
      if(m>9) m=m-7;
      e=16*e+m;
   }
   return(e);
}

devclr(fd)
int fd;
{
int pid;
char s[80];

#ifdef UNIX
/* here we start a short UNIX routine to clear the communication line. */
pid=fork();
if(pid==0) {       /* child process to read the line */
   while(1==1) read(fd,s,80);
} else {           /* the parent process just waits two seconds */
   sleep(2);
   kill(pid,SIGKILL); /* kill child */
}
#endif
}
