#include "device-functions.h"
#include "globals.h"
#include "version.h"
#include "error_utils.h"
#include <glib.h>


/*** BeginHeader idn_string */
void idn_string(gchar** response);
/*** EndHeader */
/*----------------------------------------------------------------------------------------------------------*/
void idn_string(gchar** response)
{
	*response = g_strdup_printf ("AVTECH ELECTROSYSTEMS,%s,%s,%s",
	                             globals.Flash.model_num,
	                             globals.Flash.serial_num,
	                             FW_VERSION);
}


/*----------------------------------------------------------------------------------------------------------*/
int Set_frequency(int check_possible_only,int word_override,int range_override,int channel,float set_freq)
{
	// keep, but ignore, the first 3 parameters for now

	// all this does right now is check the frequency range,
	// and store the set value.

	int check_valid;

	/* abandon if high channel selected by user but not enabled by firmware */
	if (channel && !globals.Flash.ChanKey_frequency) {
		return InvalidChannel;
	}

        if (!check_possible_only) {
		int i;
                for (i=0; i<max_channels; ++i) {
                        TestState[i]=globals.ChannelState[i];
                }
                TestState[channel].frequency=set_freq;
                if ((check_valid=Error_check(TestState))) {
                        return check_valid;
                }
        }

	globals.ChannelState[channel].frequency=set_freq;

	return OK;
}


void Main_Rst (void)
{
	int i;

	globals.extended_ampl_min_max=NO;

	// go backwards, so channel 0 overrides everything
	for (i=globals.Flash.channels-1; i>=0; --i) {

		globals.ChannelState[i].route_primary=1;
		globals.ChannelState[i].route_secondary=1;
		globals.ChannelState[i].frequency=globals.Flash.min_freq[i];
		globals.ChannelState[i].delay=globals.Flash.min_delay[i];

		if (globals.Flash.min_pw[i] > 0.0) {
			globals.ChannelState[i].pw=globals.Flash.min_pw[i];
		} else {
			globals.ChannelState[i].pw=0.0;
		}

		if (globals.Flash.ampl_min_max_only[i] || (globals.Flash.min_ampl[i] > 0.0)) {
			globals.ChannelState[i].amplitude = globals.Flash.min_ampl[i];
		} else {
			globals.ChannelState[i].amplitude=0.0;
		}
		globals.ChannelState[i].offset=0.0;

		globals.ChannelState[i].zout=globals.Flash.zout_min[i];
		globals.ChannelState[i].hold_setting = hold_width;
		globals.ChannelState[i].double_pulse = double_off;
		globals.ChannelState[i].ab_mode = pw_normal;
		globals.ChannelState[i].func_mode = pulse_mode_on;
		globals.ChannelState[i].polarity = globals.Flash.invert_by_default[i];
		globals.ChannelState[i].output_state = output_off;
		globals.ChannelState[i].gate_type = gate_sync;
		globals.ChannelState[i].trigger_source = source_internal;
		globals.ChannelState[i].amp_mode = amp_mode_normal;
		globals.ChannelState[i].os_mode = os_mode_normal;
		globals.ChannelState[i].gate_level = gate_low;
		globals.ChannelState[i].load_type= globals.Flash.low_load_type[i];
		globals.ChannelState[i].test_delay_mode = NO;
		globals.ChannelState[i].logic_level = logic_ttl;
		globals.ChannelState[i].burst_count = 1 && !globals.Flash.burst_func[i];
		globals.ChannelState[i].burst_time = globals.Constraints.composite_min_burst_time[i];
		globals.ChannelState[i].rise_time = globals.Flash.min_rise_time[i];
		globals.ChannelState[i].soft_current_limit = globals.Flash.max_soft_current_limit[i];
		globals.ChannelState[i].vcc1 = 0.0;
		globals.ChannelState[i].vcc2 = globals.Flash.vcc2_min[i];
		globals.ChannelState[i].vlogic = 0.0;
		globals.ChannelState[i].slew = globals.Flash.min_slew[i];

		globals.do_check_settings=NO;   /* don't check for conflicting settings */
		/* FIXME - implement Set_* functions!
				Set_Pw(0,0,0,i,globals.ChannelState[i].pw,0);

				Set_Route(i,ROUTE_PRIMARY,globals.ChannelState[i].route_primary);
				Set_Route(i,ROUTE_SECONDARY,globals.ChannelState[i].route_secondary);

				Set_frequency(0,0,0,i,globals.ChannelState[i].frequency);
				Set_Delay(0,0,0,i,globals.ChannelState[i].delay);
				Set_Double(i,globals.ChannelState[i].double_pulse);
				Set_Pwmode(i,globals.ChannelState[i].ab_mode);
				Set_Func(i,globals.ChannelState[i].func_mode);
				Set_Pol(i,globals.ChannelState[i].polarity);
				Set_Gate_Sync(i,globals.ChannelState[i].gate_type);
				Set_Gate_Level(i,globals.ChannelState[i].gate_level);
				Set_EA(i,globals.ChannelState[i].amp_mode);
				Set_EO(i,globals.ChannelState[i].os_mode);
				if (globals.Flash.switchable_zout[i]) {
					Set_zout(i,globals.ChannelState[i].zout,1);
				}

				Set_Load(i,globals.ChannelState[i].load_type);
				Set_Logic_Level(i,globals.ChannelState[i].logic_level);
				Set_rise_time(0,0,0,i,globals.ChannelState[i].rise_time);
				Set_slew(0,0,0,i,globals.ChannelState[i].slew);
				Set_current_limit(0,i,globals.ChannelState[i].soft_current_limit);

				if (globals.Flash.max_burst_count[i]>1) {
					Set_Burst_Count(i,globals.ChannelState[i].burst_count,globals.ChannelState[i].burst_time);
				}

				Set_Trig_Source(i,globals.ChannelState[i].trigger_source);

				Set_Amplitude(0,0,0,0,0,0,i,globals.ChannelState[i].amplitude,0);
				Set_Offset(0,0,0,0,i,globals.ChannelState[i].offset);
				Set_Output_State(i,globals.ChannelState[i].output_state);

				Set_current_limit(0,i,globals.ChannelState[i].soft_current_limit);

				Set_avrq_ampl(0,0,0,2,globals.ChannelState[i].vcc1);
				Set_avrq_ampl(0,0,0,3,globals.ChannelState[i].vcc2);
				Set_avrq_ampl(0,0,0,4,globals.ChannelState[i].vlogic);
		*/
		globals.do_check_settings=YES;  /* check for conflicting settings */
	}


//	Error_check(globals.ChannelState);      /* establishes min/max values for queries, but reports no errors */
//	update_whole_main_menu=YES;
//	Ctrl_PRF_Limiter(1);
//	Menu_Clear_Buttons();
}