/*
 * klapacz.c
 *
 *  Created on: 07-04-2014
 *      Author: Krzysztof Jakubczyk
 */

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

#include "klapacz.h"

//volatile struct klapacz_args tkl;

int klapacz_initlog(void *arguments, void *logic)
{
	struct klapacz_args *args = (struct klapacz_args *)arguments;
	struct klapacz_logic *log = (struct klapacz_logic *)logic;

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

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

	return 0;
}

void klapacz(void *arguments, void *logic)
{
	struct klapacz_args *args = (struct klapacz_args *)arguments;
	struct klapacz_logic *log = (struct klapacz_logic *)logic;

	if(args->io.out)
	{
		if(*log->ptr & (1<<log->bit_no) && log->t_cnt>=args->params.interval1)
		{
			log->t_cnt=0;
			*log->ptr &= ~(1<<log->bit_no);
		}
		else if(!(*log->ptr & (1<<log->bit_no)) && log->t_cnt>=args->params.interval2)
		{
			log->t_cnt=0;
			*log->ptr |= (1<<log->bit_no);
		}

		log->t_cnt+=LOOP_CYCLE_MS;
	}
}
