/*
 * counter.c
 *
 *  Created on: 04-06-2018
 *      Author: Krzysztof Jakubczyk
 */

#include <math.h>

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

#include "helper.h"
#include "counter.h"

int counter_initlog(void *arguments, void *logic)
{
	struct counter_args *args = (struct counter_args *)arguments;
	struct counter_logic *log = (struct counter_logic *)logic;

	if(set_bit_ptr_struct(args->io.in_in,&log->in))
		return -1;

	if(set_bit_ptr_struct(args->io.clr_in,&log->clr))
		return -1;

	if(set_float_ptr(args->io.out_float_out,&log->out))
		return -1;

	return 0;
}

void counter(void *arguments, void *logic)
{
	struct counter_logic *log = (struct counter_logic *)logic;

	if(!check_struct(&log->clr))
	{
		if(check_struct(&log->in))
			log->cnt++;
	}
	else
		log->cnt=0;

	*log->out=(float)log->cnt;
}
