/*
 * tz.c
 *
 *  Created on: 30-04-2014
 *      Author: KJ
 */



#include "tdefs.h"
#include "misc.h"

#include "tz_8.h"

//struct lg_men log_manager; //USUN

int tz_8_initlog(void *arguments, void *logic)
{
	struct tz_8_args *args = (struct tz_8_args *)arguments;
	struct tz_8_logic *log = (struct tz_8_logic *)logic;
	int i = 0;

	log->out_ptr[0] = log_manager.nets_data + (args->io.out1 >> 3);
	log->out_bit_no[0] = args->io.out1 & 0x07;

	log->out_ptr[1] = log_manager.nets_data + (args->io.out2 >> 3);
	log->out_bit_no[1] = args->io.out2 & 0x07;
	
	log->out_ptr[2] = log_manager.nets_data + (args->io.out3 >> 3);
	log->out_bit_no[2] = args->io.out3 & 0x07;
	
	log->out_ptr[3] = log_manager.nets_data + (args->io.out4 >> 3);
	log->out_bit_no[3] = args->io.out4 & 0x07;
	
	log->out_ptr[4] = log_manager.nets_data + (args->io.out5 >> 3);
	log->out_bit_no[4] = args->io.out5 & 0x07;
	
	log->out_ptr[5] = log_manager.nets_data + (args->io.out6 >> 3);
	log->out_bit_no[5] = args->io.out6 & 0x07;

	log->out_ptr[6] = log_manager.nets_data + (args->io.out7 >> 3);
	log->out_bit_no[6] = args->io.out7 & 0x07;
	
	log->out_ptr[7] = log_manager.nets_data + (args->io.out8 >> 3);
	log->out_bit_no[7] = args->io.out8 & 0x07;

	log->in_ptr[0] = log_manager.nets_data + (args->io.in1 >> 3);
	log->in_bit_no[0] = args->io.in1 & 0x07;

	log->in_ptr[1] = log_manager.nets_data + (args->io.in2 >> 3);
	log->in_bit_no[1] = args->io.in2 & 0x07;
	
	log->in_ptr[2] = log_manager.nets_data + (args->io.in3 >> 3);
	log->in_bit_no[2] = args->io.in3 & 0x07;
	
	log->in_ptr[3] = log_manager.nets_data + (args->io.in4 >> 3);
	log->in_bit_no[3] = args->io.in4 & 0x07;
	
	log->in_ptr[4] = log_manager.nets_data + (args->io.in5 >> 3);
	log->in_bit_no[4] = args->io.in5 & 0x07;
	
	log->in_ptr[5] = log_manager.nets_data + (args->io.in6 >> 3);
	log->in_bit_no[5] = args->io.in6 & 0x07;
	
	log->in_ptr[6] = log_manager.nets_data + (args->io.in7 >> 3);
	log->in_bit_no[6] = args->io.in7 & 0x07;
	
	log->in_ptr[7] = log_manager.nets_data + (args->io.in8 >> 3);
	log->in_bit_no[7] = args->io.in8 & 0x07;

	for (i = 0; i < 8; i++)
	{
		if(log->out_ptr[i] >= (log_manager.nets_data+sizeof(log_manager.nets_data)))
			return -1;
	
		if(log->in_ptr[i] >= (log_manager.nets_data+sizeof(log_manager.nets_data)))
			return -1;
	
		log->t_cnt[i] = 0;
	}

	return 0;
}

void tz_8(void *arguments, void *logic)
{
	struct tz_8_args *args = (struct tz_8_args *)arguments;
	struct tz_8_logic *log = (struct tz_8_logic *)logic;

	int i = 0;

	for (i = 0; i < 8; i++)
	{
		if(!(*log->in_ptr[i] & (1<<log->in_bit_no[i])))
		{
			*log->out_ptr[i]&=~(1<<log->out_bit_no[i]);
			log->t_cnt[i]=0;
		}
		else
		{
			if(log->t_cnt[i]>=args->params.t_delay)
				*log->out_ptr[i]|=(1<<log->out_bit_no[i]);
			else
			{
				*log->out_ptr[i]&=~(1<<log->out_bit_no[i]);
				log->t_cnt[i]+=LOOP_CYCLE_MS;
			}
		}
	}	
}
