/*
 * e_filtr1h_2T.c
 *
 *  Created on: 06-07-2014
 *      Author: Krzysztof Jakubczyk
 */

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

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

int e_filtr1h_2t_initlog(void *arguments, void *logic)
{
	struct e_filtr1h_2t_args *args = (struct e_filtr1h_2t_args *)arguments;
	struct e_filtr1h_2t_logic *log = (struct e_filtr1h_2t_logic *)logic;
	struct analog_in_params *an_params;
	u32 *an_params_ptr;
	u32 *buf_ptr;
	int i;

	an_params_ptr = (u32 *)(log_manager.nets_data + (args->io.param_an_ptr_in >> 3));
	if((u8*)an_params_ptr > (log_manager.nets_data+sizeof(log_manager.nets_data) - sizeof(void *)))
		return -1;

	if(!*an_params_ptr) // param in not connected or wrong elements order (analog_in should be run before)
		return -1;

	an_params=(struct analog_in_params *)*an_params_ptr;

	buf_ptr = (u32 *)(log_manager.nets_data + (args->io.buf_u16_ptr_in >> 3));
	if((u8*)buf_ptr > (log_manager.nets_data+sizeof(log_manager.nets_data) - sizeof(void *)))
		return -1;

	if(!*buf_ptr) // param in not connected or wrong elements order (analog_in should be run before)
		return -1;

	log->buf_ptr=(u16*)*buf_ptr;

	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;

    for(i=0;i<(2*MAIN_FREQ_PERIOD_MS);i++)
    {
		log->wsp_filtr_sin_[i] = an_params->multiplier *
					sin(((float)6.2832 / MAIN_FREQ_PERIOD_MS) * i - ((float)6.2832 / 360)/* *
					nast.pw->nast->n.korekcja_fazy*/)  / MAIN_FREQ_PERIOD_MS;
        log->wsp_filtr_cos_[i] = an_params->multiplier *
					cos(((float)6.2832 / MAIN_FREQ_PERIOD_MS) * i - ((float)6.2832 / 360)/* *
					nast.pw->nast->n.korekcja_fazy*/)  / MAIN_FREQ_PERIOD_MS;
    }

	return 0;
}

void e_filtr1h_2t(void *arguments, void *logic)
{
	struct e_filtr1h_2t_logic *log = (struct e_filtr1h_2t_logic *)logic;
    short i,i1,i2;
	float orta = 0;
	float ortb = 0;
	float pr;

	i2 = bus_an_cur_sample_num;

	for (i=0;i<(2*MAIN_FREQ_PERIOD_MS*SAMPLES_PER_MS);i+=SAMPLES_PER_MS)
	{
		i1 = i2 - i;
	    if(i1<0)
	    	i1 += (2*MAIN_FREQ_PERIOD_MS*SAMPLES_PER_MS);
		pr = (float)log->buf_ptr[i1];
		orta += pr * log->wsp_filtr_sin_[i/SAMPLES_PER_MS];
	    ortb += pr * log->wsp_filtr_cos_[i/SAMPLES_PER_MS];
	}

	*log->orta_ptr=orta;
	*log->ortb_ptr=ortb;
	*log->esk_ptr = (orta * orta + ortb * ortb) * 0.5f;
}
