/////////////////////////////////////////////////////////////////////////////
// Rezhim_ind.c
// 
/////////////////////////////////////////////////////////////////////////////

//	Set up gauge header
GAUGE_CALLBACK Rezhim_cb;
char Rezhim_ind_gauge_name[]	= GAUGE_NAME;
extern PELEMENT_HEADER	Rezhim_ind_list;
extern MOUSERECT      	Rezhim_ind_mouse_rect[];

GAUGE_HEADER_FS700(GAUGE_W, Rezhim_ind_gauge_name, &Rezhim_ind_list, \
						NULL, Rezhim_cb, 0, 0, 0);

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

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

MAKE_ICON
(		
	Rezhim_ind_icon,
	BMP_Rezhim_ind,
	NULL,
	NULL,
	IMAGE_USE_ERASE | IMAGE_USE_TRANSPARENCY,
	0,
	0,0,

	MODULE_VAR_NONE, Rezhim_ind_icon_cb,

	ICON_SWITCH_TYPE_SET_CUR_ICON,
	1,
	0,
	0
)

PELEMENT_HEADER		Rezhim_ind_icon_list[] =
{
	&Rezhim_ind_icon.header,
	NULL
};

/////////////////////////////////////////////////////////////////////////////
MAKE_STATIC
(
	Rezhim_ind_background,
	BMP_Light_bg,
	&Rezhim_ind_icon_list,
	NULL,
	IMAGE_USE_TRANSPARENCY,
	0,
	0,0
)

PELEMENT_HEADER		Rezhim_ind_list= &Rezhim_ind_background.header;
//---------------------------------------------------------------------------
//-----------------------   Call back guage   -------------------------------
//---------------------------------------------------------------------------
void FSAPI	Rezhim_cb (PGAUGEHDR pgauge, int service_id, UINT32 extra_data)
{
	PPOWER_SUPLY_STATUS_var = (PINT)POWER_SUPLY_STATUS_var.var_ptr;  // If power suply on?
	switch(service_id)
	{

/* "install_routine()" */

		case    PANEL_SERVICE_PRE_INSTALL:
			register_var_by_name(&apu_status, TYPE_BOOL, "APU_STATUS");
		break;

/* "initialize_routine()" */

		case    PANEL_SERVICE_PRE_INITIALIZE:
			initialize_var_by_name(&POWER_SUPLY_STATUS_var, "POWER_SUPLY_STATUS");
		break;

/* "update_routine()" */

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

			// Indicators updates
			lookup_var(&TICK18_var);
			if (Ctrl_key == -1 ){ // If not indicators check enabled
			
				if (Stvorki_ind == -1) time_counter = TICK18_var.var_value.n; // Still closed
				if (start_vsu == 0) time_counter_2 = TICK18_var.var_value.n; // if not started
				
				if (Main_switch == 1){ // Main swithc on
					if( Rezhim_ind == -1 ) Stvorki_ind = 0;
					else Stvorki_ind = -1;
					P_topliva_ind = 0;
					if ( start_vsu == 0 && RPM < 29 &&
						(time_counter + 18*3 < TICK18_var.var_value.n ) ) Zapusk_ind = 0; // delay 3 sec and not started
					else Zapusk_ind = -1;
				}
				else{ // Main switch off
					Stvorki_ind = -1;
					P_topliva_ind = -1;
					Zapusk_ind= -1;
					start_vsu = 0;
				}

				if ( RPM >= 93 && (time_counter_2 + 18*30 < TICK18_var.var_value.n) ) { Rezhim_ind = 0 ; apu_status = TRUE; }
				else if ( RPM < 93) { Rezhim_ind = -1 ; apu_status = FALSE;}
				
				if ( Zaslonka_switch == 1) Zaslonka_ind = 0;
				else Zaslonka_ind = -1;

				if (Zaslonka_ind == 0 && RPM < 93 ) start_vsu = 0; // Stop start if Zaslonka opened

				if ( power_good == FALSE && RPM < 93) start_vsu =0; // Stop start if AKKs off 
			}

		break;

/* "draw_routine()" */

		case    PANEL_SERVICE_PRE_DRAW:
		break;

/* "kill_routine()" */

		case    PANEL_SERVICE_PRE_KILL:
			unregister_var_by_name ("APU_STATUS");
		break;
	}

}
/////////////////////////////////////////////////////////////////////////////

#undef GAUGE_NAME
#undef GAUGEHDR_VAR_NAME
#undef GAUGE_W
