/*  TT8 specific includes  */
#include        <TT8.h>                 /* Tattletale Model 8 Definitions */
#include        <tat332.h>              /* 68332 Tattletale (7,8) Hardware Definitions */
#include        <sim332.h>              /* 68332 System Integration Module Definitions */
#include        <qsm332.h>              /* 68332 Queued Serial Module Definitions */
#include        <dio332.h>              /* 68332 Digital I/O Port Pin Definitions */
#include        <tt8lib.h>              /* definitions and prototypes for Model 8 library */

#include		<stdio.h>
#include		<stdlib.h>

#include		"eeprom.h"
#include		"assert.h"

//  allocate the data
struct setup_variables setup_data;


//  eeprom_write_setup_data (address)
//
//  Writes the setup variables to the specified address.  The address
//  is normally 0.  The option for an alternative is here only for
//  debugging so we can avoid writing any one section many times.
//
int eeprom_write_setup_data (short address)  {
	int temp;
	int err;
	ASSERT (address >= 0);
	ASSERT ((address + sizeof(setup_data)) < UeeSize());

	err = UeeWriteBlock (address,
							(uchar*)&setup_data,
							(short) sizeof(setup_data));
	if (err)  {
		printf("Error %d writing to UEEPROM address %d\n", err, address);
		return -1;
	}
//	printf("Write of UEEPROM successful\n");
	return 0;
}



//  eeprom_read_setup_data (short address)
//
//  Reads the setup variables from the specified address.  If the read
//  fails then this routine will set all variables to a default and
//  return non-zero to indicate the error
//
int eeprom_read_setup_data (short address)  {
	int err;
	ASSERT (address >= 0);
	ASSERT ((address + sizeof(setup_data)) < UeeSize());

	err = UeeReadBlock(address,
						(uchar*)&setup_data,
						(short)sizeof(setup_data));
	if (err)  {
		printf("Error %d reading from UEEPROM address %d\n", err, address);
		printf ("Setup data set to defaults.\n");
		eeprom_set_default_setup_data ();
		return -1;
	}
//	printf("Read of UEEPROM successful\n");
	return 0;
}




//  eeprom_set_default_setup_data
//
//  This routine assigns the default variables any time we want to reset
//  the logger configuration to a standard default.  This is useful mainly
//  when we first init the EEPROM with a special program.
//  
void eeprom_set_default_setup_data (void)  {
	int n;

	for (n = 0;  n < N_ENG_CHANNELS;  n++)
		setup_data.eng_rate[n] = 0;
		
	setup_data.science_rate = 128.571426;
	for (n = 0;  n < N_SCI_CHANNELS;  n++)  {
		setup_data.science_gain[n] = 2.5;
		setup_data.trigger_level[n] = 3.0;
	}	
		
	setup_data.science_chans = 0x0;
	setup_data.trigger_chans = 0x0;
	setup_data.t_STA = 0.5;
	setup_data.t_LTA = 100.0;
	setup_data.pre_event_time = 10.0;
	setup_data.post_event_time = 100;
	setup_data.tick_offset = 0;
	setup_data.science_n_to_average = 1;
//	setup_data.min_disk_write = 1024;
}




//  eeprom_verify_setup_data ()
//
//  Checks all the setup data for error in self consistency or inconsistency
//  with the available hardware.  Corrects any error which are found
//
short eeprom_verify_setup_data (void)  {
	if (setup_data.science_rate < 127)
		return -1;
	if (setup_data.science_rate > 258)
		return -2;
	if (setup_data.science_n_to_average < 1)
		return -3;
	if (setup_data.science_n_to_average > 4)
		return -4;
	//  need to finish
	return 0;
}




//  eeprom_print_setup_data
//
//  This would normally be used only during debugging.
//
void eeprom_print_setup_data (void)  {
    int n, nchans;
    
    printf ("Basic science A/D conversion rate: %.3f per second\n", setup_data.science_rate);
    printf ("Number of samples which are then averaged: %d\n", setup_data.science_n_to_average);
    printf ("Trigger STA length: %.3f seconds;  LTA length: %.2f seconds\n",
    						 				setup_data.t_STA, setup_data.t_LTA);
	printf ("Pre-event length: %.2f seconds;  post-event length %.2f seconds\n",
								setup_data.pre_event_time, setup_data.post_event_time);    						 				
    printf ("Science Channel  |  State  |  Gain  |  Trigger Level  |\n");
    printf ("-------------------------------------------------------\n");
    for (n = 0;  n < N_SCI_CHANNELS;  n++)  {
    	printf ("       %d         |   ", n);
		if ((0x01 << n)&setup_data.science_chans)
			printf ("On ");
		else
			printf ("Off");
		printf ("   |  %4.1f  |    %7.2f      |\n",
					setup_data.science_gain[n], setup_data.trigger_level[n]);
	}
    printf ("-------------------------------------------------------\n");
	
	printf ("\nEngineering Channel  |  Time Between Samples  |\n");
	printf ("-----------------------------------------------\n");
	for (n = 0;  n < N_ENG_CHANNELS;  n++)  {
		printf ("       %d             |       ", n);
    	if (setup_data.eng_rate[n] == 0)
    		printf ("  Off");
    	else
    		printf ("%5ld", setup_data.eng_rate[n]);
    	printf ("            |\n");
    }
	printf ("-----------------------------------------------\n");
	return;
}		    							
