/* BCTEST.C */

#include "bcsys.h"


void test_func(void){
  char menukey, inkey;
  ulong last_time;

  do{
    inkey = ' ';
    SerInFlush();
    printf("\n\n\nBenthic Chamber Test Function Menu");
    printf("\n\n  1 -- Purge Valve");
    printf("\n  2 -- Slide Valve");
    printf("\n  3 -- Stir Motor");
    printf("\n  4 -- Flow Cell/Pump");
    printf("\n  9 -- Exit");
    QueryChar("\nEnter Selection: ",'9',"123456789",&menukey);

    switch(menukey){
      case '1':  /*Purge Valve*/
	SimSetFSys(16e6);
	SerSetBaud(9600, 0);

	sensor_pwr_on();
	DelayMilliSecs(100);
	tpurupt(PURGE_TURNS_CHAN, ON);
	g_purge_ticks = 0;
	do{
	  printf("\n\n\nBattery Voltage: %.2f volts", batt_volt());
	  printf("\nSystem Current: %.1f mA", current());
	  printf("\nTurns: %d", g_purge_ticks/4);
	  if( purge_limit() ) printf("\nPurge Valve Limit!");

	  printf("\n\n  O = Open");
	  printf("\n  C = Close");
	  printf("\n  S = Stop");
	  printf("\n  E = Exit to Test Menu");
	  printf("\n\nEnter Selection: ");
	  fflush(stdout);

	  last_time = MilliSecs();
	  while( !timer(last_time, 3000) ){
	    if( kbhit() ){
	      inkey = tolower( SerGetByte() );       /* Get command in lower case */
	      move_purge_mot(inkey);
	    }
	    if( inkey == 'o' && purge_limit() ) move_purge_mot('s');
	    if(inkey == 'e') break;
	  }
	} while(inkey != 'e');

	move_purge_mot('s');
	tpurupt(PURGE_TURNS_CHAN, OFF);
	sensor_pwr_off();
	SimSetFSys(320000);
	SerSetBaud(9600, 0);
	break;


      case '2':   /*Slide Valve*/
	SimSetFSys(16e6);
	SerSetBaud(9600, 0);

	sensor_pwr_on();
	DelayMilliSecs(100);
	tpurupt(SLIDE_TURNS_CHAN, ON);
	g_slide_ticks = 0;
	do{
	  printf("\n\n\nBattery Voltage: %.2f volts", batt_volt() );
	  printf("\nSystem Current: %.1f mA", current() );
	  printf("\nTurns: %d", g_slide_ticks/4);
	  if( slide_limit() ) printf("\nSlide Valve Limit!");

	  printf("\n\n  A = Ambient");
	  printf("\n  C = Chamber");
	  printf("\n  S = Stop");
	  printf("\n  E = Exit to Test Menu");
	  printf("\n\nEnter Selection: ");
	  fflush(stdout);

	  last_time = MilliSecs();
	  while( !timer(last_time, 3000) ){
	    if( kbhit() ){
	      inkey = tolower( SerGetByte() );       /* Get command in lower case */
	      move_slide_mot(inkey);
	    }
	    if( inkey == 'a' && slide_limit() ) move_slide_mot('s');
	    if(inkey == 'e') break;
	  }
	} while(inkey != 'e');
	move_slide_mot('s');
	tpurupt(SLIDE_TURNS_CHAN, OFF);
	sensor_pwr_off();
	SimSetFSys(320000);
	SerSetBaud(9600, 0);
	break;


      case '3':   /* Stir Motor */
	g_stir_ticks = 0;
	do{
	  printf("\n\n\nBattery Voltage: %.2f volts", batt_volt() );
	  printf("\nSystem Current: %.1f mA", current() );
	  printf("\nStir Turns: %d", g_stir_ticks/2);

	  printf("\n\n  G = Go");
	  printf("\n  S = Stop");
	  printf("\n  E = Exit to Test Menu");
	  printf("\n\nEnter Selection: ");
	  fflush(stdout);

	  last_time = MilliSecs();
	  while( !timer(last_time, 3000) ){
	    if( kbhit() ){
	      inkey = tolower( SerGetByte() );       /* Get command in lower case */
	      if( inkey == 'g') stir_motor(ON);
	      else if( inkey == 's') stir_motor(OFF);
	    }
	    if(inkey == 'e') break;
	  }
	} while(inkey != 'e');
	stir_motor(OFF);
	break;


      case '4':   /*Flow Cell/Pump*/

	do{
	  printf("\n\n\nBattery Voltage: %.2f volts", batt_volt());
	  printf("\nSystem Current: %.1f mA", current());
	  printf("\nInternal O2 Cell: %hd", o2a());
	  printf("\nExternal O2 Cell: %hd", o2b());
	  printf("\nIso +5V: %.2f volts", iso_plus_volt());
	  printf("\nIso -5V: %.2f volts", iso_minus_volt());

	  printf("\n\n O = Pump On");
	  printf("\n  S = Stop");
	  printf("\n  E = Exit to Test Menu");
	  printf("\n\nEnter Selection: ");
	  fflush(stdout);

	  last_time = MilliSecs();
	  while( !timer(last_time, 3000) ){
	    if( kbhit() ){
	      inkey = tolower( SerGetByte() );       /* Get command in lower case */
	      if(inkey == 'o') flow_pump_on();
	      else flow_pump_off();
	    }
	    if(inkey == 'e') break;
	  }

	} while(inkey != 'e');

	flow_pump_off();
	break;
    }
  } while(menukey != '9');
}