/*
 * ZDistL.c
 *
 *  Created on: 07-03-2017
 *      Author: Krzysztof Jakubczyk
 */

#include <math.h>

#include "../tdefs.h"
#include "../misc.h"
#include "helper.h"

#include "ZDistL_komp.h"
#include "ZDistA_komp.h"


struct ZDistL_nast_komp
{
	double ts0LE;///< Czas wyłączenia strefy 1W zwarcia jednofazowe z ziemią
	double ts0LL;///< Czas wyłączenia strefy 1W zwarcia miedzyfazowe
	double ts1LE;///< Czas wyłączenia strefy 1 zwarcia jednofazowe z ziemią
	double ts1LL;///< Czas wyłączenia strefy 1 zwarcia miedzyfazowe
	double ts2LE;///< Czas wyłączenia strefy 2 zwarcia jednofazowe z ziemią
	double ts2LL;///< Czas wyłączenia strefy 2 zwarcia miedzyfazowe
	double ts3LE;///< Czas wyłączenia strefy 3 zwarcia jednofazowe z ziemią
	double ts3LL;///< Czas wyłączenia strefy 3 zwarcia miedzyfazowe
	double ts4LE;///< Czas wyłączenia strefy 4 zwarcia jednofazowe z ziemią
	double ts4LL;///< Czas wyłączenia strefy 4 zwarcia miedzyfazowe
	double ts5LE;///< Czas wyłączenia strefy 5 zwarcia jednofazowe z ziemią
	double ts5LL;///< Czas wyłączenia strefy 5 zwarcia miedzyfazowe

	long Stf0; ///< Sposób działania strefy 1W
	long Stf1; ///< Sposób działania strefy 1
	long Stf2; ///< Sposób działania strefy 2
	long Stf3; ///< Sposób działania strefy 3
	long Stf4; ///< Sposób działania strefy 4
	long Stf5; ///< Sposób działania strefy 5
};

int ZDistL_initlog_komp(void *arguments, void *logic)
{
	struct ZDistL_args_komp *args = (struct ZDistL_args_komp *)arguments;
	struct ZDistL_logic_komp *log = (struct ZDistL_logic_komp *)logic;
	struct ZDistL_nast_komp znast;

	if(set_bit_ptr_struct(args->io.stan_bl_in,&log->stan_bl))
		return -1;
	if(set_pointer_in_ptr(args->io.we_zdist_ptr_in,(u32 *)&log->WE_Zdist))
		return -1;
	if(set_bit_ptr_struct(args->io.Bl_PS_in,&log->Bl_PS))
		return -1;	
	if(set_bit_ptr_struct(args->io.lacze_OK_in,&log->lacze_OK))
		return -1;
	if(set_bit_ptr_struct(args->io.lacze_odb_in,&log->lacze_odb))
		return -1;
	if(set_bit_ptr_struct(args->io.W_ON_in,&log->W_ON))
		return -1;
	if(set_bit_ptr_struct(args->io.zgoda_1f_in,&log->zgoda_1f))
		return -1;
	if(set_bit_ptr_struct(args->io.Z_in,&log->Z))
		return -1;
	if(set_bit_ptr_struct(args->io.test_in,&log->test))
		return -1;
	if(set_bit_ptr_struct(args->io.blok_1_in,&log->blok_1))
		return -1;
	if(set_bit_ptr_struct(args->io.blok_1W_in,&log->blok_1W))
		return -1;
	if(set_bit_ptr_struct(args->io.blok_2_in,&log->blok_2))
		return -1;
	if(set_bit_ptr_struct(args->io.blok_3_in,&log->blok_3))
		return -1;
	if(set_bit_ptr_struct(args->io.blok_4_in,&log->blok_4))
		return -1;
	if(set_bit_ptr_struct(args->io.blok_5_in,&log->blok_5))
		return -1;
	if(set_bit_ptr_struct(args->io.deakt_in,&log->deakt))
		return -1;

	if(set_bit_ptr_struct(args->io.P_out,&log->P))
		return -1;
	if(set_bit_ptr_struct(args->io.P1W_out,&log->P1W))
		return -1;
	if(set_bit_ptr_struct(args->io.P1_out,&log->P1))
		return -1;
	if(set_bit_ptr_struct(args->io.P2_out,&log->P2))
		return -1;
	if(set_bit_ptr_struct(args->io.P3_out,&log->P3))
		return -1;
	if(set_bit_ptr_struct(args->io.P4_out,&log->P4))
		return -1;
	if(set_bit_ptr_struct(args->io.P5_out,&log->P5))
		return -1;
	if(set_bit_ptr_struct(args->io.R_out,&log->R))
		return -1;
	if(set_bit_ptr_struct(args->io.S_out,&log->S))
		return -1;
	if(set_bit_ptr_struct(args->io.T_out,&log->T))
		return -1;
	if(set_bit_ptr_struct(args->io.E_out,&log->E))
		return -1;
	if(set_bit_ptr_struct(args->io.Z1W_out,&log->Z1W))
		return -1;
	if(set_bit_ptr_struct(args->io.Z1_out,&log->Z1))
		return -1;
	if(set_bit_ptr_struct(args->io.Z2_out,&log->Z2))
		return -1;
	if(set_bit_ptr_struct(args->io.Z3_out,&log->Z3))
		return -1;
	if(set_bit_ptr_struct(args->io.Z4_out,&log->Z4))
		return -1;
	if(set_bit_ptr_struct(args->io.Z5_out,&log->Z5))
		return -1;
	if(set_bit_ptr_struct(args->io.W_out,&log->W))
		return -1;
	if(set_bit_ptr_struct(args->io.W1_out,&log->W1))
		return -1;
	if(set_bit_ptr_struct(args->io.W2_out,&log->W2))
		return -1;
	if(set_bit_ptr_struct(args->io.W3_out,&log->W3))
		return -1;
	if(set_bit_ptr_struct(args->io.bl_LRC_out,&log->bl_LRC))
		return -1;	
	if(set_bit_ptr_struct(args->io.log_odbl_out,&log->log_odbl))
		return -1;
	if(set_bit_ptr_struct(args->io.lacze_nad_out,&log->lacze_nad))
		return -1;
	if(set_bit_ptr_struct(args->io.zzw_akt_out,&log->zzw_akt))
		return -1;
	if(set_bit_ptr_struct(args->io.blk_lacz_out,&log->blk_lacz))
		return -1;
	if(set_bit_ptr_struct(args->io.zadz_echa_out,&log->zadz_echa))
		return -1;	
	if(set_bit_ptr_struct(args->io.zezw_lacz_out,&log->zezw_lacz))
		return -1;
	if(set_bit_ptr_struct(args->io.skr_czas_lacz_out,&log->skr_czas_lacz))
		return -1;
	if(set_bit_ptr_struct(args->io.wyl_lacz_out,&log->wyl_lacz))
		return -1;	
	if(set_bit_ptr_struct(args->io.blok_lacz_out,&log->blok_lacz))
		return -1;
	if(set_bit_ptr_struct(args->io.wydl_czas_lacz_out,&log->wydl_czas_lacz))
		return -1;	
	if(set_bit_ptr_struct(args->io.z_zwarcie_out,&log->z_zwarcie))
		return -1;				

	if(set_bit_ptr_struct(args->io.P_L1_out,&log->P_L1))
		return -1;
	if(set_bit_ptr_struct(args->io.P_L2_out,&log->P_L2))
		return -1;
	if(set_bit_ptr_struct(args->io.P_L3_out,&log->P_L3))
		return -1;
	if(set_bit_ptr_struct(args->io.P_E_out,&log->P_E))
		return -1;
	
	if(set_bit_ptr_struct(args->io.WS1_out,&log->WS1))
		return -1;
	if(set_bit_ptr_struct(args->io.WS1W_out,&log->WS1W))
		return -1;
	if(set_bit_ptr_struct(args->io.WS2_out,&log->WS2))
		return -1;
	if(set_bit_ptr_struct(args->io.WS3_out,&log->WS3))
		return -1;
	if(set_bit_ptr_struct(args->io.WS4_out,&log->WS4))
		return -1;
	if(set_bit_ptr_struct(args->io.WS5_out,&log->WS5))
		return -1;

	if(set_bit_ptr_struct(args->io.W_1f_out,&log->W_1f))
		return -1;
	
	log->nast_.echo_on = (args->params.bity & 0x0001)?1:0;
	log->nast_.wyl_od_echa = (args->params.bity & 0x0002)?1:0;
	log->nast_.log_odbl_on = (args->params.bity & 0x0004)?1:0;
	log->nast_.LCR_on = (args->params.bity & 0x0008)?1:0;
	log->nast_.Bl_PS[1] = (args->params.bity & 0x0010)?1:0;
	log->nast_.Bl_PS[2] = (args->params.bity & 0x0020)?1:0;
	log->nast_.Bl_PS[3] = (args->params.bity & 0x0040)?1:0;
	log->nast_.Bl_PS[4] = (args->params.bity & 0x0080)?1:0;
	log->nast_.Bl_PS[5] = (args->params.bity & 0x0100)?1:0;
	log->nast_.ZZw_ON = (args->params.bity & 0x0200)?1:0;
	log->nast_.bl_ = (args->params.bity & 0x0400)?1:0;
	log->nast_.LRC_s[0] = (args->params.bity & 0x0800)?1:0;
	log->nast_.LRC_s[1] = (args->params.bity & 0x1000)?1:0;
	log->nast_.LRC_s[2] = (args->params.bity & 0x2000)?1:0;
	log->nast_.LRC_s[3] = (args->params.bity & 0x4000)?1:0;
	log->nast_.LRC_s[4] = (args->params.bity & 0x8000)?1:0;
	log->nast_.LRC_s[5] = (args->params.bity & 0x10000)?1:0;
	log->nast_.lacze_on = (args->params.bity & 0x20000)?1:0;
	log->nast_.zgoda_1faz_s[0] = (args->params.bity & 0x40000)?1:0;
	log->nast_.zgoda_1faz_s[1] = (args->params.bity & 0x80000)?1:0;
	log->nast_.zgoda_1faz_s[2] = (args->params.bity & 0x100000)?1:0;
	log->nast_.zgoda_1faz_s[3] = (args->params.bity & 0x200000)?1:0;
	log->nast_.zgoda_1faz_s[4] = (args->params.bity & 0x400000)?1:0;
	log->nast_.zgoda_1faz_s[5] = (args->params.bity & 0x800000)?1:0;
	log->nast_.Bl_PS[0] = (args->params.bity & 0x1000000)?1:0;	
	log->nast_.rozwij = (args->params.bity & 0x2000000)?1:0;
	log->nast_.kryt_IU_ZZW =  (args->params.bity & 0x4000000)?1:0;
	
#define POPRAWKA_1 (0)//	(args->params.bity & 0x08000000)
#define POPRAWKA_2 (0)//	(args->params.bity & 0x10000000)
#define POPRAWKA_3 (0)//	(args->params.bity & 0x20000000)
#define POPRAWKA_4 (1)//	(args->params.bity & 0x40000000)
#define POPRAWKA_5 (0)//	(args->params.bity & 0x80000000)

	log->nast_.ZdistA = log->WE_Zdist;

	unsigned short i;

	znast.ts0LE=args->params.ts1LE;///< Czas wyłączenia strefy 1W zwarcia jednofazowe z ziemią
	znast.ts0LL=args->params.ts1LL;///< Czas wyłączenia strefy 1W zwarcia miedzyfazowe
	znast.ts1LE=args->params.ts1LE;///< Czas wyłączenia strefy 1 zwarcia jednofazowe z ziemią
	znast.ts1LL=args->params.ts1LL;///< Czas wyłączenia strefy 1 zwarcia miedzyfazowe
	znast.ts2LE=args->params.ts2LE;///< Czas wyłączenia strefy 2 zwarcia jednofazowe z ziemią
	znast.ts2LL=args->params.ts2LL;///< Czas wyłączenia strefy 2 zwarcia miedzyfazowe
	znast.ts3LE=args->params.ts3LE;///< Czas wyłączenia strefy 3 zwarcia jednofazowe z ziemią
	znast.ts3LL=args->params.ts3LL;///< Czas wyłączenia strefy 3 zwarcia miedzyfazowe
	znast.ts4LE=args->params.ts4LE;///< Czas wyłączenia strefy 4 zwarcia jednofazowe z ziemią
	znast.ts4LL=args->params.ts4LL;///< Czas wyłączenia strefy 4 zwarcia miedzyfazowe
	znast.ts5LE=args->params.ts5LE;///< Czas wyłączenia strefy 5 zwarcia jednofazowe z ziemią
	znast.ts5LL=args->params.ts5LL;///< Czas wyłączenia strefy 5 zwarcia miedzyfazowe
	
	znast.Stf0=args->params.Stf0;
	znast.Stf1=args->params.Stf1;
	znast.Stf2=args->params.Stf2;
	znast.Stf3=args->params.Stf3;
	znast.Stf4=args->params.Stf4;
	znast.Stf5=args->params.Stf5;
	
	log->nast_.tryb_lacza = (enum _tryb_lacza)args->params.tryb_lacza;
	log->nast_.tryb_zezw = (enum _tryb_zezwalajacy)args->params.tryb_zezw;
	log->nast_.tryb_blok = (enum _tryb_blokujacy)args->params.tryb_blokujacy;
	
	log->nast_.ktora_strefa = args->params.strefa_dzial;
	log->nast_.ktora_strefa_nad = args->params.strefa_nad;
	log->nast_.strefa_pradu_wstecznego = args->params.lrc_strefa;
	
	log->dw.blok_lacza[0] = 0;		
	log->dw.blok_lacza[1] = 0;
	log->dw.blok_lacza[2] = 0;
	log->dw.blok_lacza[3] = 0;
	log->dw.blok_lacza[4] = 0;
	log->dw.blok_lacza[5] = 0;

	log->dw.wydl_czas = 0;
	log->dw.wylacz_od_lacza = 0;
	log->dw.wylacz_od_echa = 0;
	log->dw.wylacz_od_lacza_suma = 0;
	
	log->dw.wy_bl_LRC = 0;
	log->dw.wy_log_odbl = 0;
	log->dw.wy_lacze_nad = 0;
	
	log->dw.wy_blk_lacz = 0;
	
	log->dw.wy_zadz_echa = 0;
	log->dw.wy_zezw_lacz = 0;
	log->dw.wy_skr_czas_lacz = 0; 
	log->dw.wy_wyl_lacz = 0;  
	log->dw.wy_blok_lacz = 0;
	log->dw.wy_wydl_czas_lacz = 0;
	
	log->dw.dodaj_rozw[0] = 0;
	log->dw.dodaj_rozw[1] = 0;
	log->dw.dodaj_rozw[2] = 0;
	log->dw.dodaj_rozw[3] = 0;
	log->dw.dodaj_rozw[4] = 0;
	log->dw.dodaj_rozw[5] = 0;

	log->dw.ZZwP = 0;
	
	if (log->nast_.tryb_lacza == ZEZWALAJACY && log->nast_.tryb_zezw == ZGODA)
	{
		log->dw.blok_lacza[log->nast_.ktora_strefa] = 1;	
	}	
	
		if (args->params.ts0LE < 0.01)
		{
			log->nast_.Zbz[0][0] = 1; 	
			log->nast_.t_stf[0][0] = 0;
		
		}
		else
		{		
			log->nast_.Zbz[0][0] = 0; 	
			log->nast_.t_stf[0][0] = (int16_t)(100 * (args->params.ts0LE - 0.01f));
      
			if (log->nast_.t_stf[0][0] < 0) log->nast_.t_stf[0][0] = 0;   
		}
				
		if (args->params.ts0LL < 0.01)
		{
			log->nast_.Zbz[0][1] = 1; 	
			log->nast_.t_stf[0][1] = 0;
		}
		else
		{		
			log->nast_.Zbz[0][1] = 0;
			log->nast_.t_stf[0][1] = (int16_t)(100 * (args->params.ts0LL - 0.01f));

			if (log->nast_.t_stf[0][1] < 0) log->nast_.t_stf[0][1] = 0; 
		}
		
					
		if (args->params.ts1LE < 0.01)
		{
			log->nast_.Zbz[1][0] = 1; 	
			log->nast_.t_stf[1][0] = 0;		
		}
		else
		{
			log->nast_.Zbz[1][0] = 0; 	
			log->nast_.t_stf[1][0] = (int16_t)(100 * (args->params.ts1LE - 0.01f));

			if (log->nast_.t_stf[1][0] < 0) log->nast_.t_stf[1][0] = 0; 
		}

		if (args->params.ts1LL < 0.01)
		{
			log->nast_.Zbz[1][1] = 1; 	
			log->nast_.t_stf[1][1] = 0;
		}
		else
		{
			log->nast_.Zbz[1][1] = 0;
			log->nast_.t_stf[1][1] = (int16_t)(100 * (args->params.ts1LL - 0.01f));

			if (log->nast_.t_stf[1][1] < 0) log->nast_.t_stf[1][1] = 0; 		
		}
		
		
		if (args->params.ts2LE < 0.01)
		{
			log->nast_.Zbz[2][0] = 1; 	
			log->nast_.t_stf[2][0] = 0;
		}
		else
		{
			log->nast_.Zbz[2][0] = 0;
			log->nast_.t_stf[2][0] = (int16_t)(100 * (args->params.ts2LE - 0.01f));

			if (log->nast_.t_stf[2][0] < 0) log->nast_.t_stf[2][0] = 0; 
		}

		if (args->params.ts2LL < 0.01)
		{
			log->nast_.Zbz[2][1] = 1; 	
			log->nast_.t_stf[2][1] = 0;
		}
		else
		{
			log->nast_.Zbz[2][1] = 0;
			log->nast_.t_stf[2][1] = (int16_t)(100 * (args->params.ts2LL - 0.01f));

			if (log->nast_.t_stf[2][1] < 0) log->nast_.t_stf[2][1] = 0; 
		}

		if (args->params.ts3LE < 0.01)
		{
			log->nast_.Zbz[3][0] = 1; 	
			log->nast_.t_stf[3][0] = 0;
		}
		else
		{
			log->nast_.Zbz[3][0] = 0;
			log->nast_.t_stf[3][0] = (int16_t)(100 * (args->params.ts3LE - 0.01f));

			if (log->nast_.t_stf[3][0] < 0) log->nast_.t_stf[3][0] = 0; 
		}

		if (args->params.ts3LL < 0.01)
		{
			log->nast_.Zbz[3][1] = 1; 	
			log->nast_.t_stf[3][1] = 0;
		}
		else
		{
			log->nast_.Zbz[3][1] = 0;
			log->nast_.t_stf[3][1] = (int16_t)(100 * (args->params.ts3LL - 0.01f));

			if (log->nast_.t_stf[3][1] < 0) log->nast_.t_stf[3][1] = 0; 
		}

		if (args->params.ts4LE < 0.01)
		{
			log->nast_.Zbz[4][0] = 1; 	
			log->nast_.t_stf[4][0] = 0;
		}
		else
		{
			log->nast_.Zbz[4][0] = 0;
			log->nast_.t_stf[4][0] = (int16_t)(100 * (args->params.ts4LE - 0.01f));

			if (log->nast_.t_stf[4][0] < 0) log->nast_.t_stf[4][0] = 0; 
		}

		if (args->params.ts4LL < 0.01)
		{
			log->nast_.Zbz[4][1] = 1; 	
			log->nast_.t_stf[4][1] = 0;
		}
		else
		{
			log->nast_.Zbz[4][1] = 0;
			log->nast_.t_stf[4][1] = (int16_t)(100 * (args->params.ts4LL - 0.01f));

			if (log->nast_.t_stf[4][1] < 0) log->nast_.t_stf[4][1] = 0; 
		}


		if (args->params.ts5LE < 0.01)
		{
			log->nast_.Zbz[5][0] = 1; 	
			log->nast_.t_stf[5][0] = 0;
		}
		else
		{
			log->nast_.Zbz[5][0] = 0;
			log->nast_.t_stf[5][0] = (int16_t)(100 * (args->params.ts5LE - 0.01f));

			if (log->nast_.t_stf[5][0] < 0) log->nast_.t_stf[5][0] = 0; 
		}

		if (args->params.ts5LL < 0.01)
		{
			log->nast_.Zbz[5][1] = 1; 	
			log->nast_.t_stf[5][1] = 0;
		}
		else
		{
			log->nast_.Zbz[5][1] = 0;
			log->nast_.t_stf[5][1] = (int16_t)(100 * (args->params.ts5LL - 0.01f));

			if (log->nast_.t_stf[5][1] < 0) log->nast_.t_stf[5][1] = 0; 
		}
	
	
	log->nast_.ZZw_st = args->params.ZZw_st;

	switch(log->nast_.ZZw_st)
	{
		case 0:
			log->PZZw=log->P1W;
			log->ZZZw=log->Z1W;
			break;
		case 1:
			log->PZZw=log->P1;
			log->ZZZw=log->Z1;
			break;
		case 2:
			log->PZZw=log->P2;
			log->ZZZw=log->Z2;
			break;
		case 3:
			log->PZZw=log->P3;
			log->ZZZw=log->Z3;
			break;
		case 4:
			log->PZZw=log->P4;
			log->ZZZw=log->Z4;
			break;
		case 5:
			log->PZZw=log->P5;
			log->ZZZw=log->Z5;
			break;
		default:
			log->PZZw=log->P1W;
			log->ZZZw=log->Z1W;
			break;
	}

	long *temp_e = &znast.Stf0;

	for (i=0;i<6;i++)
	{

		//ustawienie bitow sposobu dzialania stref
		switch (*(temp_e))
		{
			case 0:
				log->nast_.Stf_ON[i] = 0;
				log->nast_.Stf_W[i] = 0;
				break;
			case 1:
				log->nast_.Stf_ON[i] = 1;
				log->nast_.Stf_W[i] = 0;
				break;
			case 2:
				log->nast_.Stf_ON[i] = 1;
				log->nast_.Stf_W[i] = 1;
				break;
			default:
				break;
		}
		temp_e++;

		//--------------------------
	}
	
	log->nast_.ZZw_ta = (short)(100 * (args->params.ZZw_ta + 0.005f));
	log->nast_.ZZw_to = (short)(100 * (args->params.ZZw_to + 0.005f));

	log->nast_.ZZw_Ir = args->params.ZZw_Ir * args->params.ZZw_Ir;
	log->nast_.ZZw_Ur = args->params.ZZw_Ur * args->params.ZZw_Ur;

	log->nast_.Uecho_pob = args->params.U_echa * args->params.U_echa;
	log->nast_.Uecho_odp = 1.02 * 1.02 * log->nast_.Uecho_pob;

	log->nast_.t_LRC = (int16_t)(100 * (args->params.t_LRC + 0.005f));
	log->nast_.t_lacze_nad = (int16_t)(100 * (args->params.t_lacze_nad + 0.005f));
	log->nast_.t_pod_odbl = (int16_t)(100 * (args->params.t_pod_odbl + 0.005f));
	
	log->nast_.ts_plus = (int16_t)(100 * (args->params.ts_plus - 0.01f));
	if (log->nast_.ts_plus < 0) log->nast_.ts_plus = 0;
		
	log->nast_.czas_wyl_echa =  (int16_t)(100 * (args->params.czas_wyl_echa + 0.005f));;
	log->nast_.max_t_nadawania =  (int16_t)(100 * (args->params.max_t_nadawania + 0.005f));

	for (i = 0; i < 6; i++)
	{
		log->dw.Pf[i][0] = 0;
		log->dw.Pf[i][1] = 0;
		log->dw.Zf[i][0] = 0;
		log->dw.Zf[i][1] = 0;
		
		log->dw.Pzf[i] = 0;
		log->dw.Z[i] = 0;
		log->dw.Ws[i] = 0;
		
		log->dw.blok_LRC[i] = 0;
    
    log->dw.liczt_eldz[i][0] = 0;
    log->dw.liczt_eldz[i][1] = 0;
    
    log->dw.liczts_rozwij[i] = 0;
    log->dw.Zf_rozwij[i] = 0;
    log->dw.liczt_eldz_rozwij[i] = 0;
    //liczts_rozwij
    
	}
	log->dw.lacze_odb_old = 0;
	log->dw.skrocil_strefa = 0; 
	log->dw.skrocil_echo = 0; 

	 for (i = 0; i < 6; i++)
	 {
		log->P_L1_lub_L2[i] = 0;
		log->P_L2_lub_L3[i] = 0;
		log->P_L3_lub_L1[i] = 0;
	 }
//

	for (i=0;i<6;i++)
	{
		if(log->nast_.Stf_ON[i])
		{
			log->WE_Zdist->Zdist_dw->Z_min = log->WE_Zdist->Zdist_dw->modul_zf[i]<log->WE_Zdist->Zdist_dw->Z_min?log->WE_Zdist->Zdist_dw->modul_zf[i]:log->WE_Zdist->Zdist_dw->Z_min;
			log->WE_Zdist->Zdist_dw->Z_min_mf = log->WE_Zdist->Zdist_dw->modul_zmf[i]<log->WE_Zdist->Zdist_dw->Z_min_mf?log->WE_Zdist->Zdist_dw->modul_zmf[i]:log->WE_Zdist->Zdist_dw->Z_min_mf;
		}
	}

	return 0;
}

/**
Funkcja okraślania petli zwarciwej przy wyłaczeniu
\param nr_strefy - numer strefy wyłaczającej
*/
void wybiornik_f_komp(struct ZDistL_logic_komp *log,unsigned short nr_strefy)
{
	log->dw.R = log->pobudzenia[nr_strefy][0]
		|| log->pobudzenia[nr_strefy][3]
		|| log->pobudzenia[nr_strefy][5];

	log->dw.S = log->pobudzenia[nr_strefy][1]
		|| log->pobudzenia[nr_strefy][3]
		|| log->pobudzenia[nr_strefy][4];

	log->dw.T = log->pobudzenia[nr_strefy][2]
		|| log->pobudzenia[nr_strefy][4]
		|| log->pobudzenia[nr_strefy][5];

	log->dw.E = log->nast_.ZdistA->Zdist_dw->Iogr;

	log->dw.jedfaz = (log->dw.R && !(log->dw.S||log->dw.T))
				|| (log->dw.S && !(log->dw.T||log->dw.R))
				|| (log->dw.T && !(log->dw.R||log->dw.S))
				|| (!log->dw.R && !log->dw.S && !log->dw.T); //zeby po wyl 1 fazowym nie robil na krotko 3 faz
}

/**
Funkcja okraślania petli zwarciwej przy wyłaczeniu - działanie bezkierunkowe
\param nr_strefy - numer strefy wyłaczającej
*/

void wybiornik_fbk_komp(struct ZDistL_logic_komp *log, unsigned short nr_strefy)
{
	log->dw.Rbk = log->nast_.ZdistA->Zdist_dw->Pbk[nr_strefy][0]
		|| log->nast_.ZdistA->Zdist_dw->Pbk[nr_strefy][3]
		|| log->nast_.ZdistA->Zdist_dw->Pbk[nr_strefy][5];

	log->dw.Sbk = log->nast_.ZdistA->Zdist_dw->Pbk[nr_strefy][1]
		|| log->nast_.ZdistA->Zdist_dw->Pbk[nr_strefy][3]
		|| log->nast_.ZdistA->Zdist_dw->Pbk[nr_strefy][4];

	log->dw.Tbk = log->nast_.ZdistA->Zdist_dw->Pbk[nr_strefy][2]
		|| log->nast_.ZdistA->Zdist_dw->Pbk[nr_strefy][4]
		|| log->nast_.ZdistA->Zdist_dw->Pbk[nr_strefy][5];

	log->dw.E = log->nast_.ZdistA->Zdist_dw->Iogr;
}


void
wylacz_P_komp(struct ZDistL_logic_komp *log, u8 zgoda_1f)
{
int i;

	for(i=0;i<6;i++)
	{
		if (log->dw.Ws[i])
		{
			wybiornik_f_komp(log,i);
	
			if(log->dw.jedfaz && zgoda_1f && log->nast_.zgoda_1faz_s[i])
			{
				if(log->dw.R)
          			log->dw.W1 = 1;
        		if(log->dw.S)
         			log->dw.W2 = 1;
        		if(log->dw.T)
          			log->dw.W3 = 1;
			
				log->dw.wyl_1f = 1;
			} 
			else
			{
				log->dw.W1 = log->dw.W2 = log->dw.W3 = log->dw.W = 1;				
			}						
		}		
	}
	
	if (log->dw.wylacz_od_lacza_suma == 1)
	{
		log->dw.W1 = log->dw.W2 = log->dw.W3 = log->dw.W = 1;			
	}
}


void ZDistL_komp(void *arguments, void *logic)
{
	struct ZDistL_logic_komp *log = (struct ZDistL_logic_komp *)logic;
	struct ZDistL_args_komp *args = (struct ZDistL_args_komp *)arguments;
	
	u16 i,j;
	log->dw.P = 0;
	log->dw.Zz = 0;

	u8 temp_Bl,temp_Zezwolenie, pob_zab_echo;

	u8 we_stan_bl =  check_struct(&log->stan_bl)?1:0;
	u8 we_Bl_PS =  check_struct(&log->Bl_PS)?1:0;
	u8 we_lacze_OK = check_struct(&log->lacze_OK)?1:0;
	u8 we_lacze_odb = check_struct(&log->lacze_odb)?1:0;
	u8 we_zgoda_1f = check_struct(&log->zgoda_1f)?1:0;
	u8 we_test = check_struct(&log->test)?1:0;
	u8 we_Z =  check_struct(&log->Z)?1:0;
	u8 b_blok[6];
	
	b_blok[0] = check_struct(&log->blok_1W)?1:0;;
	b_blok[1] = check_struct(&log->blok_1)?1:0;;
	b_blok[2] = check_struct(&log->blok_2)?1:0;;
	b_blok[3] = check_struct(&log->blok_3)?1:0;;
	b_blok[4] = check_struct(&log->blok_4)?1:0;;
	b_blok[5] = check_struct(&log->blok_5)?1:0;;

//	u8 poprawka_pawla[6] = {0,0,0,0,0,0};

	if (*log->nast_.ZdistA->on && !(check_struct(&log->deakt)))
	{
		//**************\/********* Poprawka na bledne wylaczenie 3 fazowe przy zwarciu 1 fazowym ************\/***************************

		#define L1 0
		#define L2 1
		#define L3 2
		#define L1L2 3
		#define L2L3 4
		#define L3L1 5
		#define faz_0_85 0.24f
		u8 UL1, UL2, UL3;

		#define st_075kw 0.5625f
		#define st_133kw 1.7689f
		u8 IL1L2, IL2L3, IL3L1;

		//kryteria podanpięciowe
		if (log->nast_.ZdistA->Zdist_dw->U1 < faz_0_85)
			UL1 = 1;
		else
			UL1 = 0;

		if (log->nast_.ZdistA->Zdist_dw->U2 < faz_0_85)
			UL2 = 1;
		else
			UL2 = 0;

		if (log->nast_.ZdistA->Zdist_dw->U3 < faz_0_85)
			UL3 = 1;
		else
			UL3 = 0;

		//kryteria prądowe
		if (log->nast_.ZdistA->Zdist_dw->I1 > 0.1) //zeby nie dzielic przez 0
		{
			float k  = log->nast_.ZdistA->Zdist_dw->I2/ log->nast_.ZdistA->Zdist_dw->I1;
				if ((k > st_075kw) && (k < st_133kw))
					IL1L2 = 1;
				else
					IL1L2 = 0;
		}
		else
			IL1L2 = 0;

		if (log->nast_.ZdistA->Zdist_dw->I2 > 0.1) //zeby nie dzielic przez 0
		{
			float k  = log->nast_.ZdistA->Zdist_dw->I3/ log->nast_.ZdistA->Zdist_dw->I2;
				if ((k > st_075kw) && (k < st_133kw))
					IL2L3 = 1;
				else
					IL2L3 = 0;
		}
		else
			IL2L3 = 0;

		if (log->nast_.ZdistA->Zdist_dw->I3 > 0.1) //zeby nie dzielic przez 0
		{
			float k  = log->nast_.ZdistA->Zdist_dw->I1/ log->nast_.ZdistA->Zdist_dw->I3;
				if ((k > st_075kw) && (k < st_133kw))
					IL3L1 = 1;
				else
					IL3L1 = 0;
		}
		else
			IL3L1 = 0;


		//kryteria pobudzenia doziemnego jednofazowego w opoznieniem odpadu
		for (i=0; i<6; i++)
		{

			if((log->nast_.ZdistA->Zdist_dw->P[i][L1]) || (log->nast_.ZdistA->Zdist_dw->P[i][L2]))
			{
				log->P_L1_lub_L2[i] = 3;
			}
			else
			{
				if (log->P_L1_lub_L2[i] > 0)
					log->P_L1_lub_L2[i]--;

			}

			if ((log->nast_.ZdistA->Zdist_dw->P[i][L2]) || (log->nast_.ZdistA->Zdist_dw->P[i][L3]))
			{
				log->P_L2_lub_L3[i] = 3;
			}
			else
			{
				if (log->P_L2_lub_L3[i] > 0)
					log->P_L2_lub_L3[i]--;
			}

			if ((log->nast_.ZdistA->Zdist_dw->P[i][L3]) || (log->nast_.ZdistA->Zdist_dw->P[i][L1]))
			{
				log->P_L3_lub_L1[i] = 3;
			}
			else
			{
				if (log->P_L3_lub_L1[i] > 0)
					log->P_L3_lub_L1[i]--;
			}

			for (j=0; j<3; j++)
			{
				log->pobudzenia[i][j] = log->nast_.ZdistA->Zdist_dw->P[i][j];
			}

			//blokowanie pobudzen miedzyfazowych gdy strefa moze wylaczyc 1-fazowo a napiecia nie przysiadly

			if (((/*we_zgoda_1f && */log->nast_.zgoda_1faz_s[i]) && (log->nast_.ZdistA->Zdist_dw->P[i][L1L2]) && (log->P_L1_lub_L2[i] > 0) && !POPRAWKA_2)
			|| (POPRAWKA_2 && ((log->nast_.ZdistA->Zdist_dw->P[i][L1L2]) && ((log->nast_.ZdistA->Zdist_dw->P[1][L1]) || (log->nast_.ZdistA->Zdist_dw->P[1][L2]))  && (log->P_L1_lub_L2[i] > 0)))
			)
			{
				if ((UL1 && UL2) || (IL1L2))
					log->pobudzenia[i][L1L2] = 1;
				else
				{

	//				poprawka_pawla[i]=1;
					log->pobudzenia[i][L1L2] = POPRAWKA_4?0:1;
				}
			}
			else
				log->pobudzenia[i][L1L2] = log->nast_.ZdistA->Zdist_dw->P[i][L1L2];


			if (((/*we_zgoda_1f && */log->nast_.zgoda_1faz_s[i]) && (log->nast_.ZdistA->Zdist_dw->P[i][L2L3]) && (log->P_L2_lub_L3[i] > 0) && !POPRAWKA_2)
			|| (POPRAWKA_2 && ((log->nast_.ZdistA->Zdist_dw->P[i][L2L3]) && ((log->nast_.ZdistA->Zdist_dw->P[1][L2]) || (log->nast_.ZdistA->Zdist_dw->P[1][L3]))  && (log->P_L2_lub_L3[i] > 0))))
			{
				if ((UL2 && UL3) || (IL2L3))
					log->pobudzenia[i][L2L3] = 1;
				else
				{
//					poprawka_pawla[i]=1;
					log->pobudzenia[i][L2L3] = POPRAWKA_4?0:1;
				}
			}
			else
				log->pobudzenia[i][L2L3] = log->nast_.ZdistA->Zdist_dw->P[i][L2L3];

			if (((/*we_zgoda_1f && */log->nast_.zgoda_1faz_s[i]) && (log->nast_.ZdistA->Zdist_dw->P[i][L3L1]) && (log->P_L3_lub_L1[i] > 0) && !POPRAWKA_2)
			|| (POPRAWKA_2 && ((log->nast_.ZdistA->Zdist_dw->P[i][L3L1]) && ((log->nast_.ZdistA->Zdist_dw->P[1][L3]) || (log->nast_.ZdistA->Zdist_dw->P[1][L1]))  && (log->P_L3_lub_L1[i] > 0))))
			{
				if ((UL3 && UL1) || (IL3L1))
					log->pobudzenia[i][L3L1] = 1;
				else
				{
	//				poprawka_pawla[i]=1;
					log->pobudzenia[i][L3L1] = POPRAWKA_4?0:1;
				}
			}
			else
				log->pobudzenia[i][L3L1] = log->nast_.ZdistA->Zdist_dw->P[i][L3L1];

		}
		//*************/\******** Poprawka na bledne wylaczenie 3 fazowe przy zwarciu 1 fazowym ***************/\***********************
	
	//Lacze
	
	if ((log->nast_.tryb_lacza == BLOKUJACY) && (log->nast_.tryb_blok == BLOKUJ_PRZEKAZNIK) && (we_lacze_odb == 1) && (we_lacze_OK))
	{
		log->dw.wy_blk_lacz = 1;		
	}
	else
	{
		log->dw.wy_blk_lacz = 0;
	}
	
	
	if (log->nast_.tryb_lacza == ZEZWALAJACY) 
	{

			 if (log->nast_.tryb_zezw == ZGODA )
			 {		 
				 if (((we_lacze_odb == 1)&& (we_lacze_OK)) || (log->dw.logika_dblokowania))
				 {
				 	log->dw.blok_lacza[log->nast_.ktora_strefa] = 0;
				 	log->dw.wy_zezw_lacz = 1;	
				 }
				 else
				 {
					 log->dw.blok_lacza[log->nast_.ktora_strefa] = 1;
					 log->dw.wy_zezw_lacz = 0;		
				 }		 
			 }
			 else if (log->nast_.tryb_zezw == SKROC_CZAS)
			 {
			 	 if ((we_lacze_odb == 1)&& (we_lacze_OK))
			 	 {
					log->dw.liczts[log->nast_.ktora_strefa][0] = 0;		 	
				 	log->dw.liczts[log->nast_.ktora_strefa][1] = 0;	
				 	log->dw.wy_skr_czas_lacz = 1;
			 	 }
			 	 else
			 	 {
			 	 	log->dw.wy_skr_czas_lacz = 0;
			 	 }	 	
			 }
			 else if (log->nast_.tryb_zezw == WYLACZ)
			 {
				if ((we_lacze_odb == 1)&& (we_lacze_OK) && !we_stan_bl)
			 	{
			 		log->dw.wylacz_od_lacza = 1;
			 		log->dw.wy_wyl_lacz = 1;		 	
			 	}
			 	else
			 	{
			 		log->dw.wylacz_od_lacza = 0;
			 		log->dw.wy_wyl_lacz = 0;	
			 	}
			 
		 	}
	}
	else if (log->nast_.tryb_lacza == BLOKUJACY)//tryb blokujący
	{
		 	if (log->nast_.tryb_blok == BLOKUJ)
		 	{
			 	 if ((we_lacze_odb == 1)&& (we_lacze_OK))
				 {
				 	log->dw.blok_lacza[log->nast_.ktora_strefa] = 1;
				 	log->dw.wy_blok_lacz = 1;	
				 }
				 else
				 {
					log-> dw.blok_lacza[log->nast_.ktora_strefa] = 0;
					log->dw.wy_blok_lacz = 0;		
				 }
		 	}
		 	else if (log->nast_.tryb_blok == WYDLUZ_CZAS)
		 	{
		 		if ((we_lacze_odb == 1)&& (we_lacze_OK))
				{
					log->dw.wy_wydl_czas_lacz = 1;
				}
				else
				{
					log->dw.wy_wydl_czas_lacz = 0;	
				}	
		 		
		 		//funkcja wykonywana jest w kodzie ponizej
		 	}
		 	else if (log->nast_.tryb_blok == BLOKUJ_PRZEKAZNIK)
		 	{
		 		
		 	}	 			 	
	}
	else //tryb wylaczony
	{
			log->dw.blok_lacza[0] = 0;		
			log->dw.blok_lacza[1] = 0;
			log->dw.blok_lacza[2] = 0;
			log->dw.blok_lacza[3] = 0;
			log->dw.blok_lacza[4] = 0;
			log->dw.blok_lacza[5] = 0;	
	}
	//-------------------------------------------------------------------------
	
	//logika pradu wstecznego

	if ((log->dw.Pzf[log->nast_.strefa_pradu_wstecznego] == 1) && (log->nast_.LCR_on))
	{
		log->dw.blokada_pradu_wstecznego = 1;
		log->dw.t_pod_blok_wst = log->nast_.t_LRC;
		log->dw.wy_bl_LRC = 1;
	}
	
	if (log->dw.blokada_pradu_wstecznego == 1)
	{
		for (i = 0; i < 6; i++)
		{
			if (log->nast_.LRC_s[i] == 1)
				log->dw.blok_LRC[i] = 1;				
		}		
	}	
	else
	{
		for (i = 0; i < 6; i++)
			log->dw.blok_LRC[i] = 0;				
	}
		
	//-------------------------------------------------------------------------
	//logika słabego zasialnia - echa

	pob_zab_echo = 0;
		
	u8 U_echo_pob_b = log->nast_.ZdistA->Zdist_dw->U1 < log->nast_.Uecho_pob ||
		log->nast_.ZdistA->Zdist_dw->U2 <log->nast_.Uecho_pob ||
		log->nast_.ZdistA->Zdist_dw->U3 <log->nast_.Uecho_pob;
		
	u8 U_echo_odp_b = log->nast_.ZdistA->Zdist_dw->U1 > log->nast_.Uecho_odp &&
		log->nast_.ZdistA->Zdist_dw->U2 > log->nast_.Uecho_odp &&
		log->nast_.ZdistA->Zdist_dw->U3 > log->nast_.Uecho_odp;	
	
	sprawdz_P(&log->dw.pob_echo, U_echo_pob_b, U_echo_odp_b, &log->dw.licznik_Uecho, 3, 3);
			
	for (i=0;i<6;i++)
	{
		if (log->dw.Pzf[i] != 0)
			pob_zab_echo = 1;
	}
	
	if (log->nast_.echo_on && !we_stan_bl)
	{	
		if (!pob_zab_echo && log->dw.pob_echo && (we_lacze_odb == 1) && (we_lacze_OK == 1))
		{
			if ((log->nast_.wyl_od_echa) && (log->dw.wylacz_od_echa == 0))       //poprawka hazardu
			{
				log->dw.wylacz_od_echa = 1;	
				log->dw.t_wyl_od_echa = log->nast_.czas_wyl_echa;
			}
			// i odbij echo
			if (log->dw.skrocil_echo == 0)
			{
			log->dw.lacze_nad = 1;
			log->dw.t_lacze_nad = log->nast_.t_lacze_nad;
			log->dw.wy_lacze_nad = 1;
			}
		}
	
		if (log->dw.wylacz_od_echa)
		{
			log->dw.wy_zadz_echa = 1;
		}
		else
		{
			log->dw.wy_zadz_echa = 0;
		}
	
		if (we_lacze_odb == 0)
			log->dw.skrocil_echo = 0;
	
	}

	log->dw.wylacz_od_lacza_suma = log->dw.wylacz_od_lacza || log->dw.wylacz_od_echa;
	//-------------------------------------------------------------------------
	//logika odblokowania

	if ((log->nast_.log_odbl_on) && (log->nast_.tryb_lacza == ZEZWALAJACY) && (log->nast_.tryb_zezw == ZGODA))
	{
		if ((log->dw.kopia_lacze_ok == 1) && (we_lacze_OK == 0)) //urwanie lacza ok
		{
			log->dw.wy_log_odbl = 1;
			log->dw.logika_dblokowania = 1;
			log->dw.t_log_odblokowania = log->nast_.t_pod_odbl;
		}
	}
	
	log->dw.kopia_lacze_ok = we_lacze_OK;


		temp_Bl = (we_stan_bl & log->nast_.bl_);

		for (i=0;i<6;i++)
		{
			if (log->nast_.Stf_ON[i]) //jesli strefa wlaczona
			{
				temp_Zezwolenie = !(temp_Bl  | (log->nast_.Bl_PS[i] & we_Bl_PS));

				if (b_blok[i] == 1)
					temp_Zezwolenie = 0;

				//obsluga wejscia testu przekaznika	
				if (czy_test_R()) 
				{
					log->dw.Pf[i][0] = log->dw.Pf[i][1] = temp_Zezwolenie & we_test;
				}
				else
				{
					//ustawienie pobudzen
					log->dw.Pf[i][0] = (log->pobudzenia[i][0] |
												log->pobudzenia[i][1] |
																log->pobudzenia[i][2])
																	& temp_Zezwolenie;
					log->dw.Pf[i][1] = (log->pobudzenia[i][3] |
												log->pobudzenia[i][4] |
																log->pobudzenia[i][5])
																	& temp_Zezwolenie;
				}
				log->dw.Pzf[i] = log->dw.Pf[i][0] | log->dw.Pf[i][1];

				//ustawienie zadzialan jesli bezzwloczny
			

                
				 if ((log->nast_.tryb_lacza == BLOKUJACY) &&  (log->nast_.tryb_blok == WYDLUZ_CZAS) && (log->nast_.ktora_strefa == i))                                      
				 {
					 if ((log->dw.Pzf[log->nast_.ktora_strefa] == 1) && (log->dw.wydl_czas == 0) && ( we_lacze_odb == 1) && (we_lacze_OK))
					 {
               			log->dw.wydl_czas = 1;		 			
		 				log->dw.liczts[log->nast_.ktora_strefa][0] += log->nast_.ts_plus;
		 				log->dw.liczts[log->nast_.ktora_strefa][1] += log->nast_.ts_plus; 
						log->nast_.Zbz[log->nast_.ktora_strefa][0] = 0;
						log->nast_.Zbz[log->nast_.ktora_strefa][1] = 0;
						log->dw.dodaj_rozw[log->nast_.ktora_strefa] = log->nast_.ts_plus;
					 }

					if  (log->dw.Pzf[log->nast_.ktora_strefa] == 0)
			 		{
			 			log->dw.wydl_czas = 0;		 			

						if (log->nast_.t_stf[log->nast_.ktora_strefa][0] < 0.01)
							log->nast_.Zbz[log->nast_.ktora_strefa][0] = 1;
						
						if (log->nast_.t_stf[log->nast_.ktora_strefa][1] < 0.01)
							log->nast_.Zbz[log->nast_.ktora_strefa][1] = 1;
			 		}	 	
				 }

  
        	if (log->nast_.Zbz[i][0])
    			{
    				if (log->dw.Pf[i][0] != 0)            
              log->dw.Zf[i][0] = 1;
    			
          
          }
    			if (log->nast_.Zbz[i][1])
    			{
    				if (log->dw.Pf[i][1] != 0)
            log->dw.Zf[i][1] = 1;
    			}

      
      
      	//ustawienie zadzialania strefy
				log->dw.Z[i] = log->dw.Zf[i][0] | log->dw.Zf[i][1];
			

			} else {
				log->dw.Pzf[i] = 0;
				log->dw.Z[i] = 0;
			}

			log->dw.P |= log->dw.Pzf[i];
			log->dw.Zz |= log->dw.Z[i]; //zadzialanie zabezpieczenia
		}

		//lacze nadawanie
			if (log->nast_.lacze_on == 0)
			{
				log->dw.wy_lacze_nad = 0;
			}
			else
			{
				if (log->dw.skrocil_strefa == 0)
				{
					if ((log->dw.Pzf[log->nast_.ktora_strefa_nad] == 1) && !log->dw.blokada_pradu_wstecznego)
					{
						log->dw.lacze_nad = 1;
						log->dw.t_lacze_nad = log->nast_.t_lacze_nad;
						log->dw.wy_lacze_nad = 1;
					}
				}

				if (log->dw.Pzf[log->nast_.ktora_strefa_nad] == 0)
				{
					log->dw.skrocil_strefa = 0; //kasowanie flagi skrocil czas gdy zanikl odpad
				}
			}

			//-------------------------------------------------------------------------

		log->dw.lacze_odb_old = we_lacze_odb && we_lacze_OK; //jesli jest jedae ale nie ok to zero

		//-------------------------------------------------------------------------------------------------------
		//automatyka załaczenia na zwarcie

		if (we_Z && log->nast_.ZZw_ON) {
			log->dw.ZZw |= 1; 
		}
		if (log->dw.ZZw && log->nast_.ZZw_ON) //jeseli jest właczony i jest spełnione kryterium załączenia na zwarcie
		{
			wybiornik_fbk_komp(log, log->nast_.ZZw_st);

			log->dw.bl_WSPZ = 1;

			if (log->dw.Rbk | log->dw.Sbk | log->dw.Tbk)
			{
				if (!temp_Bl)
				{
					log->dw.W1 = log->dw.W2 = log->dw.W3 = log->dw.W = 1;			
					log->dw.ZZwP = 1;
				}
			} else log->dw.ZZwP = 0;

		} else {
			log->dw.Rbk = log->dw.Sbk = log->dw.Tbk = 0;
			log->dw.ZZwP = 0;
		}
		
		//-------------------------------------------------------------------------------------------------------

		for(i=0;i<6;i++) 
		{
			log->dw.Ws[i] = log->dw.Z[i] && log->nast_.Stf_W[i] && !log->dw.blok_lacza[i] && !log->dw.blok_LRC[i];
		}
					
		wylacz_P_komp(log, we_zgoda_1f);
			
		//-------------------------------------------------------------------------------------------------------


		if (log->dw.ZZw && log->nast_.ZZw_ON)
			log->nast_.ZdistA->SOTF_zwrotnie = log->nast_.ZZw_st;
		else
			log->nast_.ZdistA->SOTF_zwrotnie = 0xFF;



		u8 pob_L1 = 0;
		u8 pob_L2 = 0;
		u8 pob_L3 = 0;
		u8 pob_E = 0;
		
		u8 bylo_pob = 0;

		u8 nr_strefy[6]={1,0,2,3,4,5}; // renumeracja -> 1,1w,2,3,4,5

		for (i = 0; i < 6; i++)
		{
			if ((log->nast_.Stf_ON[nr_strefy[i]] != 0) && (b_blok[nr_strefy[i]] == 0))
			{			
				if (log->pobudzenia[nr_strefy[i]][0] || log->pobudzenia[nr_strefy[i]][3] || log->pobudzenia[nr_strefy[i]][5])
				{
					if(!bylo_pob)// || log->dw.Z[nr_strefy[i]] /*|| poprawka_pawla[nr_strefy[i]]*/)
						pob_L1 = 1;
				}		
				if (log->pobudzenia[nr_strefy[i]][1] || log->pobudzenia[nr_strefy[i]][3] || log->pobudzenia[nr_strefy[i]][4])
				{
					if(!bylo_pob) //|| log->dw.Z[nr_strefy[i]]/* || poprawka_pawla[nr_strefy[i]]*/)
						pob_L2 = 1;
				}			
				if (log->pobudzenia[nr_strefy[i]][2] || log->pobudzenia[nr_strefy[i]][4] || log->pobudzenia[nr_strefy[i]][5])
				{
					if(!bylo_pob) // || log->dw.Z[nr_strefy[i]]/* || poprawka_pawla[nr_strefy[i]]*/)
						pob_L3 = 1;
				}			
				if (log->pobudzenia[nr_strefy[i]][0] || log->pobudzenia[nr_strefy[i]][1] || log->pobudzenia[nr_strefy[i]][2])
				{
//					if(!bylo_pob || log->dw.Z[nr_strefy[i]]/* || poprawka_pawla[nr_strefy[i]]*/)
						pob_E = 1;
				}
			}

			if(POPRAWKA_1)
			{

				if((pob_L1||pob_L2||pob_L3)/*&&poprawka_pawla[nr_strefy[i]]*/)
				{
					bylo_pob=1;
					break;
				}
			}
		}

		//wyprowadzenie wyjść
		check_and_set_struct(log->dw.P,&log->P);
		check_and_set_struct(log->dw.Pzf[0],&log->P1W);
		check_and_set_struct(log->dw.Pzf[1],&log->P1);
		check_and_set_struct(log->dw.Pzf[2],&log->P2);
		check_and_set_struct(log->dw.Pzf[3],&log->P3);
		check_and_set_struct(log->dw.Pzf[4],&log->P4);
		check_and_set_struct(log->dw.Pzf[5],&log->P5);

		check_and_set_struct(log->dw.Z[0],&log->Z1W);
		check_and_set_struct(log->dw.Z[1],&log->Z1);
		check_and_set_struct(log->dw.Z[2],&log->Z2);
		check_and_set_struct(log->dw.Z[3],&log->Z3);
		check_and_set_struct(log->dw.Z[4],&log->Z4);
		check_and_set_struct(log->dw.Z[5],&log->Z5);

		check_and_set_struct(log->dw.R || log->dw.Rbk,&log->R);
		check_and_set_struct(log->dw.S || log->dw.Sbk,&log->S);
		check_and_set_struct(log->dw.T || log->dw.Tbk,&log->T);
		check_and_set_struct(log->dw.E,&log->E);

		check_and_set_struct(log->dw.W,&log->W);
		check_and_set_struct(log->dw.W1,&log->W1);
		check_and_set_struct(log->dw.W2,&log->W2);
		check_and_set_struct(log->dw.W3,&log->W3);
	
		if(log->dw.ZZwP)
		{
			set_struct(&log->ZZZw);
			set_struct(&log->PZZw);
		}

		check_and_set_struct(log->dw.wy_bl_LRC, &log->bl_LRC);
		check_and_set_struct(log->dw.wy_log_odbl,&log->log_odbl);
		check_and_set_struct(log->dw.wy_lacze_nad,&log->lacze_nad);
		check_and_set_struct(log->dw.ZZw,&log->zzw_akt);
		check_and_set_struct(log->dw.wy_blk_lacz,&log->blk_lacz);
		
		check_and_set_struct(log->dw.wy_zadz_echa,&log->zadz_echa);
		check_and_set_struct(log->dw.wy_zezw_lacz,&log->zezw_lacz);
		check_and_set_struct(log->dw.wy_skr_czas_lacz,&log->skr_czas_lacz);
		check_and_set_struct(log->dw.wy_wyl_lacz,&log->wyl_lacz);
		check_and_set_struct(log->dw.wy_blok_lacz,&log->blok_lacz);
		check_and_set_struct(log->dw.wy_wydl_czas_lacz,&log->wydl_czas_lacz);
		check_and_set_struct(log->dw.ZZwP,&log->z_zwarcie);

		if(POPRAWKA_3)
		{
			sprawdz_P(&log->pob1_flt,pob_L1, !pob_L1, &log->pob1_flt_cnt, 6, 4);
			sprawdz_P(&log->pob2_flt,pob_L2, !pob_L2, &log->pob2_flt_cnt, 6, 4);
			sprawdz_P(&log->pob3_flt,pob_L3, !pob_L3, &log->pob3_flt_cnt, 6, 4);
			sprawdz_P(&log->pob4_flt,pob_E, !pob_E, &log->pob4_flt_cnt, 6, 4);
			check_and_set_struct(log->pob1_flt, &log->P_L1);
			check_and_set_struct(log->pob2_flt, &log->P_L2);
			check_and_set_struct(log->pob3_flt, &log->P_L3);
			check_and_set_struct(log->pob4_flt, &log->P_E);
		}
		else
		{
			check_and_set_struct(pob_L1, &log->P_L1);
			check_and_set_struct(pob_L2, &log->P_L2);
			check_and_set_struct(pob_L3, &log->P_L3);
			check_and_set_struct(pob_E, &log->P_E);
		}
		
		check_and_set_struct(log->dw.Ws[0],&log->WS1W);
		check_and_set_struct(log->dw.Ws[1],&log->WS1);
		check_and_set_struct(log->dw.Ws[2],&log->WS2);
		check_and_set_struct(log->dw.Ws[3],&log->WS3);
		check_and_set_struct(log->dw.Ws[4],&log->WS4);
		check_and_set_struct(log->dw.Ws[5],&log->WS5);
		
		check_and_set_struct(log->dw.wyl_1f,&log->W_1f);
	} 
	else 
	{
		clear_struct(&log->P);
		clear_struct(&log->P1W);
		clear_struct(&log->P1);
		clear_struct(&log->P2);
		clear_struct(&log->P3);
		clear_struct(&log->P4);
		clear_struct(&log->P5);
		clear_struct(&log->Z1W);
		clear_struct(&log->Z1);
		clear_struct(&log->Z2);
		clear_struct(&log->Z3);
		clear_struct(&log->Z4);
		clear_struct(&log->Z5);
		clear_struct(&log->R);
		clear_struct(&log->S);
		clear_struct(&log->T);
		clear_struct(&log->E);
		clear_struct(&log->W);
		clear_struct(&log->W1);
		clear_struct(&log->W2);
		clear_struct(&log->W3);

		clear_struct(&log->PZZw);
		clear_struct(&log->ZZZw);
		clear_struct(&log->bl_LRC);
		clear_struct(&log->log_odbl);
		clear_struct(&log->lacze_nad);
		clear_struct(&log->blk_lacz);
		
		clear_struct(&log->zadz_echa);
		clear_struct(&log->zezw_lacz);	
		clear_struct(&log->skr_czas_lacz);	
		clear_struct(&log->wyl_lacz);
		clear_struct(&log->blok_lacz);
		clear_struct(&log->wydl_czas_lacz);	
		clear_struct(&log->z_zwarcie);
		
		clear_struct(&log->P_L1);
		clear_struct(&log->P_L2);
		clear_struct(&log->P_L3);
		clear_struct(&log->P_E);
		
		clear_struct(&log->WS1);
		clear_struct(&log->WS1W);
		clear_struct(&log->WS2);
		clear_struct(&log->WS3);
		clear_struct(&log->WS4);
		clear_struct(&log->WS5);
		
		clear_struct(&log->W_1f);
	}

}

void ZDistL_100hz_komp(void *arguments, void *logic)
{
struct ZDistL_logic_komp *log = (struct ZDistL_logic_komp *)logic;

u16 i;
u8 tempbool = 0;

u8 we_W_ON =  check_struct(&log->W_ON)?1:0;
u8 we_Z =  check_struct(&log->Z)?1:0;
	
	if (*log->nast_.ZdistA->on)
	{
		//lacze nad
		if (log->dw.lacze_nad == 1)
		{
			if (--log->dw.t_lacze_nad == 0)
			{
				log->dw.wy_lacze_nad = 0;
				log->dw.lacze_nad = 0;
			}
		}
		//logika pradu wstecznego
		if (log->dw.blokada_pradu_wstecznego == 1)
		{
			if (--log->dw.t_pod_blok_wst == 0)
			{
				log->dw.wy_bl_LRC = 0;
				log->dw.blokada_pradu_wstecznego = 0;
			}
		}
		
		//logika odblokowania
		if (log->dw.logika_dblokowania == 1)
		{
			if (--log->dw.t_log_odblokowania == 0)
			{
				log->dw.logika_dblokowania = 0;
				log->dw.wy_log_odbl = 0;
			}			
		}
	
		//echo
		if (log->dw.wylacz_od_echa == 1)
		{
			if (--log->dw.t_wyl_od_echa == 0)
				log->dw.wylacz_od_echa = 0;				
		}
		
		//urwanie imp nadawania
		if (log->dw.lacze_nad == 1)
		{
			if (++log->dw.trwanie_nad > log->nast_.max_t_nadawania)
			{
			log->dw.wy_lacze_nad = 0;
			log->dw.lacze_nad = 0;
			log->dw.skrocil_strefa = 1;
			log->dw.skrocil_echo = 1;	
			}
		}
		else
		{
			log->dw.trwanie_nad = 0;	
		}
				
		//wczytanie danych o stanie wyłącznika
		log->dw.W_ON = we_W_ON;
		
    if (we_W_ON)
         log->dw.W_OFF = 2;
    else
      if (log->dw.W_OFF > 0)
        log->dw.W_OFF--;
    
		for (i=0;i<6;i++)
		{

			//realizacja przekaznikow czasowych
			if (log->nast_.Stf_ON[i]) //jesli strefa wlaczona
			{

				if (log->dw.Pf[i][0]) 
				{
					if(!log->nast_.Zbz[i][0]) // jesli nie bezzwloczny
					{
						if (!log->dw.liczts[i][0])
						{
							log->dw.Zf[i][0] = 1;
							log->dw.liczts[i][0]++;
						}
						log->dw.liczts[i][0]--;
					}
         			 log->dw.liczt_eldz[i][0] = 2;
				} 
				else//wylaczenie zadzialania (odpad)
				{
           			if (log->dw.liczt_eldz[i][0] > 0)
              			log->dw.liczt_eldz[i][0]--;
            		else
            		{
             			 log->dw.liczts[i][0] = log->nast_.t_stf[i][0];
              			log->dw.Zf[i][0] = 0;
           			 }       
        		}
				if (log->dw.Pf[i][1]) 
				{
					if(!log->nast_.Zbz[i][1]) // jesli nie bezzwloczny
					{
						if (!log->dw.liczts[i][1])
						{
							log->dw.Zf[i][1] = 1;
							log->dw.liczts[i][1]++;
						}
						log->dw.liczts[i][1]--;
          			}
          			log->dw.liczt_eldz[i][1] = 2;
				} 
				else//wylaczenie zadzialania (odpad)
				{
           	 	if (log->dw.liczt_eldz[i][1] > 0)
            	  log->dw.liczt_eldz[i][1]--;
           		 else
				{
              		log->dw.liczts[i][1] = log->nast_.t_stf[i][1];
              		log->dw.Zf[i][1] = 0;
            	}              
			}				
		}
			//-----------------------------------------------------------
		
		
		if ((log->dw.Pf[i][0]) || (log->dw.Pf[i][1]))
		{
			log->dw.liczts_rozwij[i]++;
			log->dw.liczt_eldz_rozwij[i] = 2;	
			if ((log->dw.Pf[i][0]) && (log->dw.liczts_rozwij[i] > (log->nast_.t_stf[i][0] + log->dw.dodaj_rozw[i])))
			{
				log->dw.Zf_rozwij[i] = 1;					
			}
			
			if ((log->dw.Pf[i][1]) && (log->dw.liczts_rozwij[i] > (log->nast_.t_stf[i][1]+ log->dw.dodaj_rozw[i])))
			{
				log->dw.Zf_rozwij[i] = 1;
			}
		}
		else
		{
			if (log->dw.liczt_eldz_rozwij[i] > 0)
            	log->dw.liczt_eldz_rozwij[i]--;
           	else
			{
				log->dw.dodaj_rozw[i] = 0;
				log->dw.Zf_rozwij[i] = 0;
				log->dw.liczts_rozwij[i] = 0;
			}
		}
		
		if (log->nast_.rozwij)
		{
			log->dw.Zf[i][1] |= log->dw.Zf_rozwij[i];
			log->dw.Zf[i][0] |= log->dw.Zf_rozwij[i];
		}	
		
		
		}

		//odpad wylaczen
	
		for(i=0;i<6;i++)
		{
			tempbool |= log->dw.Ws[i];
		}
		tempbool |= log->dw.wylacz_od_lacza_suma;
		
		if (!tempbool) 
		{
			log->dw.W = log->dw.W1 = log->dw.W2 = log->dw.W3 = log->dw.R = log->dw.S = log->dw.T = log->dw.E = 0;
			log->dw.wyl_1f = 0;	
		}
		//automatyka załaczenia na zwarcie - identyfikacja stanu załączenia na zwarcie

		if (log->nast_.ZZw_ON)
		{
			//właczenie od otwarcia wyłącznika
			if (log->nast_.kryt_IU_ZZW |= 0)
			{
			tempbool = 	(log->nast_.ZdistA->Zdist_dw->I1< log->nast_.ZZw_Ir) & 
				(log->nast_.ZdistA->Zdist_dw->I2 < log->nast_.ZZw_Ir) & 
				(log->nast_.ZdistA->Zdist_dw->I3 < log->nast_.ZZw_Ir) & 
				(log->nast_.ZdistA->Zdist_dw->U1 < log->nast_.ZZw_Ur) & 
				(log->nast_.ZdistA->Zdist_dw->U2 < log->nast_.ZZw_Ur) & 
				(log->nast_.ZdistA->Zdist_dw->U3 < log->nast_.ZZw_Ur);

			sprawdz_P_100(&log->dw.ZZw_PIU,tempbool,!tempbool,&log->dw.licznik_PIU,log->nast_.ZZw_to,2);
			}
			else
			{
				log->dw.ZZw_PIU = 0;
			}
			
			log->dw.ZZw_start = log->dw.ZZw_PIU | //włączenie ZZw jeśli brak napięcia i prądu
							we_Z | //włączenie ZZw jeśli aktywny sygnał zamknięcia wyłącznika
								(log->dw.W_OFF == 0); //włączenie ZZw jeśli otwarte styki
	
			log->dw.ZZw |= log->dw.ZZw_start; 
			if(log->dw.ZZw_start)
			{
				log->dw.licznik_ZZwa = 0;
			} 
			else 
			{
				log->dw.licznik_ZZwa++;
				if(log->dw.licznik_ZZwa > log->nast_.ZZw_ta)
				{
					if (!log->dw.ZZwP) log->dw.ZZw = 0;
					log->dw.licznik_ZZwa--;
				}
			}
		}
		else
		{
			log->dw.ZZw = 0;
		}
	}

}
