/*-=-=-=-=-=-=-=-=-=-=-=- RCCL/RCI Notice of Copyright -=-=-=-=-=-=-=-=-=-=-*
 *									    *
 *	Copyright (C) 1987, 1991 by John E. Lloyd and Vincent Hayward.	    *
 *									    *
 *	Information on copyright permissions, as well as a complete	    *
 *	disclaimer of warranty, is given in the file COPYRIGHT.		    *
 *									    *
 *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/

/* quick hack to check for noise reduction on ADC input */

#include <stdio.h>
#include "pumaInterface.h"

#define VSIZE 11
#define NCH 16

main()
{
 int i, j, value1, value2, v_index;
 int base_value [NCH];
 int variance [NCH][VSIZE];
 int num_counts = 500;
 int first_channel = 0, last_channel = 6;
 char cmd[2];

 put6pg (MODE, MESERVO|MEPOSSV);
  
 for (; ;)
  { printf ("\nADCTEST> ");
    scanf ("%1s", cmd);
    
    switch (cmd[0])
     { case '?':
	  printf (" 's' : Take a sample\n");
	  printf (" 'c'  : select a single channel\n");
	  printf (" 'r'  : select a channel range\n");
	  printf (" 'n'  : set the number of samples taken\n");
	  printf (" 'l'  : infinite loop read mode (for board testing)\n");
	  printf (" 'q'  : quit\n");
	  break;
       case 'c':
	  printf ("channel number >> ");
	  scanf ("%d", &value1);
	  if (value1 < 0 || value1 >= NCH)
	   { printf ("Error --- channel out of range\n");
	   }
	  else
	   { first_channel = value1;
	     last_channel = value1;
	   }
	  break;
       case 'r':
	  printf ("first and last channels >> "); 
	  scanf ("%d%d", &value1, &value2);
	  if (value1 < 0 || value1 >= NCH || value2 < 0 || value2 >= NCH)
	   { printf ("Error --- channel values out of range\n");
	   }
	  else
	   { first_channel = value1;
	     last_channel = value2;
	   }
	  break;
       case 'n':
	  printf ("number of iterations >> ");
	  scanf ("%d", &num_counts);
	  if (num_counts < 0)
	   { num_counts = 0;
	   }
	  break;
       case 's':
          for (j = first_channel; j <= last_channel; j++)
           { for (i = 0; i < VSIZE; i++)
       	     { variance [j][i] = 0;
	     }
	   }
	  for (j = first_channel; j <= last_channel; j++)
	   { ADCV (base_value [j], j);
 	   }
          for (i = 1; i <= num_counts; i++)
           { for (j = first_channel; j <= last_channel; j++)
             { ADCV(value1, j);
                v_index = value1 - base_value[j] + (VSIZE-1)/2;
	        if (v_index >= 0 && v_index < VSIZE)
                   ++ variance [j][v_index];
	     }
           }

          printf ("\n\n\nCHAN ");
          for (j = first_channel; j <= last_channel; j++)
           { printf ("    --%1d--", j);
           }
          printf ("\n\nBASE");
          for (j = first_channel; j <= last_channel; j++)
           { printf ("%9d", base_value[j]);
           }
          printf ("\n");
          for (i = 0; i < VSIZE; i++)
           { printf ("\n%3d ", i - (VSIZE-1)/2);
             for (j = first_channel; j <= last_channel; j++)
	      { printf ("%9d", variance[j][i]);
	      }
           }
	  putchar ('\n');
	  break;
       case 'l':
	  while (1)
	   { int value;
	     for (j = first_channel; j <= last_channel; j++)
	      { ADCV (value, j);
 	      }
	   }
	  break;
       case 'q':
	  zexit (0);
	  break;
       case 'e':
	  for (j = first_channel; j <= last_channel; j++)
	   { ADCV (base_value [j], j);
 	   }
          printf ("\n\n\nCHAN ");
          for (j = first_channel; j <= last_channel; j++)
           { printf ("    --%1d--", j);
           }
	  printf ("\n    ");
          for (j = first_channel; j <= last_channel; j++)
	   { printf ("%9d (0x%x)", base_value[j], base_value[j]);
	   }
	  putchar ('\n');
	  break;	  
     }
  }
}
       





zexit (n)
{
 printf ("exit %d\n");
 halt ();
}
