/*
 * e_addvec.c
 *
 *  Created on: 10-06-2016
 *      Author: Krzysztof Jakubczyk
 */

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

#include "e_addvec.h"
#include "analog_in.h"
#include <math.h>

int e_addvec_initlog(void *arguments, void *logic)
{
	struct e_addvec_args *args = (struct e_addvec_args *)arguments;
	struct e_addvec_logic *log = (struct e_addvec_logic *)logic;

	log->orta1_ptr = (float *)(log_manager.nets_data + (args->io.orta1_float_in >> 3));
	if((u8 *)log->orta1_ptr > (log_manager.nets_data+sizeof(log_manager.nets_data) - sizeof(float)))
		return -1;

	log->ortb1_ptr = (float *)(log_manager.nets_data + (args->io.ortb1_float_in >> 3));
	if((u8 *)log->ortb1_ptr > (log_manager.nets_data+sizeof(log_manager.nets_data) - sizeof(float)))
		return -1;

	log->orta2_ptr = (float *)(log_manager.nets_data + (args->io.orta2_float_in >> 3));
	if((u8 *)log->orta2_ptr > (log_manager.nets_data+sizeof(log_manager.nets_data) - sizeof(float)))
		return -1;

	log->ortb2_ptr = (float *)(log_manager.nets_data + (args->io.ortb2_float_in >> 3));
	if((u8 *)log->ortb2_ptr > (log_manager.nets_data+sizeof(log_manager.nets_data) - sizeof(float)))
		return -1;

	log->orta_ptr = (float *)(log_manager.nets_data + (args->io.orta_float_out >> 3));
	if((u8 *)log->orta_ptr > (log_manager.nets_data+sizeof(log_manager.nets_data) - sizeof(float)))
		return -1;

	log->ortb_ptr = (float *)(log_manager.nets_data + (args->io.ortb_float_out >> 3));
	if((u8 *)log->ortb_ptr > (log_manager.nets_data+sizeof(log_manager.nets_data) - sizeof(float)))
		return -1;

	log->esk_ptr = (float *)(log_manager.nets_data + (args->io.esk_float_out >> 3));
	if((u8 *)log->esk_ptr > (log_manager.nets_data+sizeof(log_manager.nets_data) - sizeof(float)))
		return -1;

	return 0;
}

void e_addvec(void *arguments, void *logic)
{
	struct e_addvec_args *args = (struct e_addvec_args *)arguments;
	struct e_addvec_logic *log = (struct e_addvec_logic *)logic;

	float orta, ortb;

	orta = *log->orta1_ptr + *log->orta2_ptr;
	ortb = *log->ortb1_ptr + *log->ortb2_ptr;

	if(args->io.esk_float_out)
		*log->esk_ptr=(orta*orta + ortb*ortb)/2;

	if(args->io.orta_float_out)
		*log->orta_ptr = orta;

	if(args->io.ortb_float_out)
		*log->ortb_ptr = ortb;
}
