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


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

#include "tz.h"

int tz_initlog(void *arguments, void *logic)
{
	struct tz_args *args = (struct tz_args *)arguments;
	struct tz_logic *log = (struct tz_logic *)logic;

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

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


	if(log->out_ptr >= (log_manager.nets_data+sizeof(log_manager.nets_data)))
		return -1;

	if(log->in_ptr >= (log_manager.nets_data+sizeof(log_manager.nets_data)))
		return -1;

	return 0;
}

void tz(void *arguments, void *logic)
{
	struct tz_args *args = (struct tz_args *)arguments;
	struct tz_logic *log = (struct tz_logic *)logic;

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