/******************************************************************************
** Copyright 2015 MBARI - Monterey Bay Aquarium Research Institute
*******************************************************************************
*******************************************************************************
** Summary  : Routines to control Heraeus Fiber Light Deuterium Lamp
** Filename : fibrlite.c
** Author   : Luke Coletti
** Project  :
** Version  : 1.0
** Compiler : MetroWerks Code Warrior v8.3
** Created  : 12/15/14
** Archived :
*******************************************************************************
** Modification History:
** 01/02/15 : CF2 Port, LJC
*******************************************************************************/

#include    <cfxpico.h>
#include    <stdio.h>
#include    <stdlib.h>
#include    <math.h>
#include    <time.h>
#include    <msc_main.h>

extern uchar isus_lamp_pwr;
extern uchar isus_spec_pwr;
extern uchar isus_ref_pwr;
extern uchar sys_message_enable;
extern ushort isus_ref_limit;
extern float DataMean, DataStdDev;


int LampPwrOn( void )
{
	
	if( isus_lamp_pwr == TRUE )
		return( TRUE );

	PCAL6416_WritePortBit(PCAL6416_OUTPUT_PORT0, LAMP_PWR_ON, HIGH);
	DelaySecs( 1 );
	
	PCAL6416_WritePortBit(PCAL6416_OUTPUT_PORT0, (LAMP_DU_ENABLE | LAMP_W_ENABLE | LAMP_SHUT_ENABLE), HIGH);
	
	DelayMilliSecs( 500 );
	PCAL6416_WritePortBit(PCAL6416_OUTPUT_PORT0, LAMP_W_ENABLE, LOW);
	DelayMilliSecs( 500 );
	PCAL6416_WritePortBit(PCAL6416_OUTPUT_PORT0, LAMP_W_ENABLE, HIGH);
	DelayMilliSecs( 500 );
	PCAL6416_WritePortBit(PCAL6416_OUTPUT_PORT0, LAMP_W_ENABLE, LOW);
	DelayMilliSecs( 500 );
	PCAL6416_WritePortBit(PCAL6416_OUTPUT_PORT0, LAMP_W_ENABLE, HIGH);
	DelayMilliSecs( 500 );
	PCAL6416_WritePortBit(PCAL6416_OUTPUT_PORT0, LAMP_W_ENABLE, LOW);
	DelayMilliSecs( 500 );
	
	isus_lamp_pwr = TRUE;

	return( TRUE );
}

int LampPwrOnUsingSpec( TUPort *tup, ushort *Data, uchar ShowRef )
{
char ebuf[ERRBUFSIZE];
ulong  scan_period;
ushort scan_min, scan_max;
int    i;
float  volts, amps;
RTCTimer TmOut;
	
	if( isus_lamp_pwr == TRUE )
		return( TRUE );
	
	if( isus_spec_pwr != TRUE ){
	  sprintf(ebuf, "Powerup Failure, Lamp requires speectromter to be powered");
	  LogErrorMsg(ebuf, (int)LAMP_PWR_ON_FAILURE, sys_message_enable);
	  if( ShowRef == TRUE )
        printf("  %s\n", ebuf);	  
	  return( ERROR );
	}

	if( ShowRef == TRUE )
      printf("  RefLimit = %hu\n", isus_ref_limit);
	
	PCAL6416_WritePortBit(PCAL6416_OUTPUT_PORT0, LAMP_PWR_ON, HIGH);
	DelaySecs( 1 );
	
	PCAL6416_WritePortBit(PCAL6416_OUTPUT_PORT0, (LAMP_DU_ENABLE | LAMP_W_ENABLE | LAMP_SHUT_ENABLE), HIGH);
	DelayMilliSecs( 500 );
	
    i = 0;
  	scan_max = 0;
	scan_period = 250UL;
	
	RTCCountdownTimerSetup(&TmOut, APF_LAMP_IGNITE_TIMEOUT); 

    do{

	  PCAL6416_WritePortBit(PCAL6416_OUTPUT_PORT0, LAMP_W_ENABLE, HIGH);
	  DelayMilliSecs( 100 );
		
	  SingleScanSpectra( tup, scan_period, Data );
	  ShortVectorMinMax( 100, Data, &scan_min, &scan_max );

	  PCAL6416_WritePortBit(PCAL6416_OUTPUT_PORT0, LAMP_W_ENABLE, LOW); 
	  DelayMilliSecs( 100 );
	  
      ++i;
	  scan_period = scan_period + (ulong)(i*50);
	  
      if( ShowRef == TRUE )
        printf("  %d, %hu\n", i, scan_max);

      if( (scan_max < isus_ref_limit) && (i > 4) ){
		LampCloseShutter();
		LampOpenShutter();
        }

	  if( RTCCountdownTimeout(&TmOut) == true ){
		 INA219_ReadVoltageCurrent( &volts, &amps );
		 sprintf(ebuf, "Powerup Failure, Du Lamp could not turn on, RefLimit = %hu, VSYS = %.3f, ISYS = %.4e", isus_ref_limit, volts, amps);
		 LogErrorMsg(ebuf, (int)LAMP_PWR_ON_FAILURE, sys_message_enable);
		 if( ShowRef == TRUE )
           printf("  %s\n", ebuf);	  
		 return( TIMEOUT );
		}

      }while( (scan_max < isus_ref_limit) );
  
	DataMean = (float)scan_max;
	DataStdDev = (float)scan_min;
	
	isus_lamp_pwr = TRUE;
	
	return( TRUE );
	
}

int LampPwrOff( void )
{

	// Turn Off all Lamp I/O Pins
	PCAL6416_WritePortBit(PCAL6416_OUTPUT_PORT0, (LAMP_DU_ENABLE | LAMP_W_ENABLE | LAMP_SHUT_ENABLE), LOW);
	DelayMilliSecs( 250 );

	// Now Turn of 12VDC Lamp Power
	PCAL6416_WritePortBit(PCAL6416_OUTPUT_PORT0, LAMP_PWR_ON, LOW);

	isus_lamp_pwr = FALSE;

	return( TRUE );

}

void LampOpenShutter(void)
{
    PCAL6416_WritePortBit(PCAL6416_OUTPUT_PORT0, LAMP_SHUT_ENABLE, HIGH);
    DelayMilliSecs( 500 );
}

void LampCloseShutter(void)
{
    PCAL6416_WritePortBit(PCAL6416_OUTPUT_PORT0, LAMP_SHUT_ENABLE, LOW);
    DelayMilliSecs( 500 );
}

int RefPwrOn(void)
{
	isus_ref_pwr = TRUE;

	return( TRUE );
}

int RefPwrOff(void)
{
	isus_ref_pwr = FALSE;

	return( TRUE );
}

