/*
 * edge_det.c
 *
 *  Created on: 28-06-2014
 *      Author: KJ
 */


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

#include "edge_det.h"

int edge_det_initlog(void *arguments, void *logic)
{
	struct edge_det_args *args = (struct edge_det_args *)arguments;
	struct edge_det_logic *log = (struct edge_det_logic *)logic;

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

	log->in_ptr = log_manager.nets_data + (args->io.in >> 3);
	log->in_bit_mask = 1<<(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 edge_det(void *arguments, void *logic)
{
	struct edge_det_args *args = (struct edge_det_args *)arguments;
	struct edge_det_logic *log = (struct edge_det_logic *)logic;
	u8 st = *log->in_ptr & log->in_bit_mask;

	if(st)
	{
		if(!log->prev_state && args->params.rising)
			*log->out_ptr|=log->out_bit_mask;
		else
			*log->out_ptr&=~log->out_bit_mask;
	}
	else
	{
		if(log->prev_state && args->params.falling)
			*log->out_ptr|=log->out_bit_mask;
		else
			*log->out_ptr&=~log->out_bit_mask;
	}

	log->prev_state=st;
}
