/////////////////////////////////////////////////////////////////////////////
// AVT_1.c
// Key of accumulator num.1
/////////////////////////////////////////////////////////////////////////////

//	Set up gauge header
GAUGE_CALLBACK			AVT_1_cb;
char AVT_1_gauge_name[]	= GAUGE_NAME;
extern PELEMENT_HEADER	AVT_1_list;
extern MOUSERECT      	AVT_1_mouse_rect[];

GAUGE_HEADER_FS700(GAUGE_W, AVT_1_gauge_name, &AVT_1_list, \
						AVT_1_mouse_rect, AVT_1_cb, 0, 0, 0);


/*---------------------------------------------------------------------------
---- Functions that ilustrate status of Keys --------------------------------
---------------------------------------------------------------------------*/
FLOAT64 FSAPI	AVT_1_icon_cb( PELEMENT_ICON pelement )
{
	return AVT_1;
}

/*---------------------------------------------------------------------------
------------ Make ICONS macroses     ----------------------------------------
---------------------------------------------------------------------------*/

MAKE_ICON
(		
	AVT_1_icon,
	BMP_AVT_off,
	NULL,
	NULL,
	IMAGE_USE_ERASE | IMAGE_USE_BRIGHT | IMAGE_USE_TRANSPARENCY,
	0,
	0,0,

	MODULE_VAR_NONE, AVT_1_icon_cb,

	ICON_SWITCH_TYPE_SET_CUR_ICON,
	2,
	0,
	0
)

PELEMENT_HEADER		AVT_1_icon_list[] =
{
	&AVT_1_icon.header,
	NULL
};
/////////////////////////////////////////////////////////////////////////////
MAKE_STATIC
(
	AVT_1_background,
	BMP_AVT_off,
	&AVT_1_icon_list,
	NULL,
	IMAGE_USE_TRANSPARENCY,
	0,
	0,0
)

PELEMENT_HEADER		AVT_1_list= &AVT_1_background.header;

/*---------------------------------------------------------------------------
---------------- Call back mouse functions ----------------------------------
---------------------------------------------------------------------------*/

BOOL FSAPI AVT_1_mouse_cb( PPIXPOINT relative_point, FLAGS32 mouse_flags )
{	
	lookup_var(&MASTER_BATTERY_var);
	if ( AVT_1 == 0 && MASTER_BATTERY_var.var_value.n == 1 && Main_switch == 1 ) 
		AVT_1 = 1;
	else
		AVT_1 = 0;
	
	return FALSE;
}
/////////////////////////////////////////////////////////////////////////////

MOUSE_BEGIN( AVT_1_mouse_rect, HELP_NONE, 0, 0 )
	MOUSE_CHILD_FUNCT( 0,0,50,40, CURSOR_HAND, MOUSE_LEFTSINGLE, AVT_1_mouse_cb )
MOUSE_END

/////////////////////////////////////////////////////////////////////////////
//---------------------------------------------------------------------------
//-----------------------   Call back guage   -------------------------------
//---------------------------------------------------------------------------


void FSAPI	AVT_1_cb (PGAUGEHDR pgauge, int service_id, UINT32 extra_data)
{
	PPOWER_SUPLY_STATUS_var = (PINT)POWER_SUPLY_STATUS_var.var_ptr;  // If power suply on?
	pfuel_sys_status = (PBOOL)fuel_sys_status.var_ptr;

	switch(service_id)
	{
	case    PANEL_SERVICE_PRE_INSTALL:
		break;

	case    PANEL_SERVICE_PRE_INITIALIZE:
		initialize_var_by_name(&POWER_SUPLY_STATUS_var, "POWER_SUPLY_STATUS");
		initialize_var_by_name (&fuel_sys_status, "FUEL_SYS_STATUS"); // Does fuel pumps connected
		break;

	case    PANEL_SERVICE_PRE_UPDATE:
		
		// Check if power supported by power suply system
		if ( PPOWER_SUPLY_STATUS_var != NULL && *PPOWER_SUPLY_STATUS_var == 2 )// defined in PS gauge
			power_good = TRUE;
		else power_good = FALSE;


		lookup_var(&MASTER_BATTERY_var);
		lookup_var(&TICK18_var);
		lookup_var(&TURB_ENGINE_2_CORRECTED_N2_var);
		lookup_var(&TURB_ENGINE_1_CORRECTED_N2_var);
		
		// Engine 1 start up
		if ( AVT_1 == 0)
		{
			tmp_e = 0;  
			tmp_d = 0;
		}
		else if ( AVT_1 == 1 && TURB_ENGINE_1_CORRECTED_N2_var.var_value.n < 24)
		{
			if ( AVT_2 == 0 && power_good == TRUE ){
				if (tmp_e == 0 ) tmp_e = TICK18_var.var_value.n + 18*30 ; // ~= 30 seconds
				else if (tmp_e != 0 && tmp_e >= TICK18_var.var_value.n){
					trigger_key_event( KEY_TOGGLE_STARTER1,0);
				}
			}
		}
		else if ( AVT_1 == 1 && TURB_ENGINE_1_CORRECTED_N2_var.var_value.n > 25)
		{
			if( tmp_d == 0) tmp_d = TICK18_var.var_value.n + 18*3 ; // ~= 3 seconds
			else if (tmp_d != 0 && tmp_d <= TICK18_var.var_value.n){
				AVT_1 = 0;
			}
		}
		// Engine 2 start up
		if ( AVT_2 == 0)
		{
			tmp2_e = 0;  
			tmp2_d = 0;
		}
		else if ( AVT_2 == 1 && TURB_ENGINE_2_CORRECTED_N2_var.var_value.n < 24)
		{
			if ( AVT_1 == 0 && power_good == TRUE ){
				if (tmp2_e == 0 ) tmp2_e = TICK18_var.var_value.n + 18*70 ; // ~= 70 seconds
				else if (tmp2_e != 0 && tmp2_e >= TICK18_var.var_value.n){
					trigger_key_event( KEY_TOGGLE_STARTER2,0);
				}
			}
		}
		else if ( AVT_2 == 1 && TURB_ENGINE_2_CORRECTED_N2_var.var_value.n > 25)
		{
			if( tmp2_d == 0) tmp2_d = TICK18_var.var_value.n + 18*5 ; // ~= 5 seconds
			else if (tmp2_d != 0 && tmp2_d <= TICK18_var.var_value.n){
				AVT_2 = 0;
			}
		}
		break;

	case    PANEL_SERVICE_PRE_DRAW:
		break;

		/* "kill_routine()" */
	case    PANEL_SERVICE_PRE_KILL:
		break;
	}

}

#undef GAUGE_NAME
#undef GAUGEHDR_VAR_NAME
#undef GAUGE_W
