/*
 * e_f_3f.c
 *
 *  Created on: 18-01-2017
 *      Author: Krzysztof Jakubczyk
 */

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

#include "e_f_3f.h"
#include "analog_in.h"
#include "helper.h"
#include <math.h>

const float mn[3] = {1.0f,0.5f,0.33333333f};

int e_f_3f_initlog(void *arguments, void *logic)
{
	struct e_f_3f_args *args = (struct e_f_3f_args *)arguments;
	struct e_f_3f_logic *log = (struct e_f_3f_logic *)logic;

	if(set_pointer_in_ptr(args->io.f1_ptr_in,(u32 *)&log->f1))
		return -1;

	if(set_pointer_in_ptr(args->io.f2_ptr_in,(u32 *)&log->f2))
		return -1;

	if(set_pointer_in_ptr(args->io.f3_ptr_in,(u32 *)&log->f3))
		return -1;

	if(set_float_ptr(args->io.f_float_out,(u32 *)&log->f_ptr))
		return -1;

	if(set_bit_ptr(args->io.ok_out,&log->ok_ptr,&log->ok_bit_no))
		return -1;

	return 0;
}

void e_f_3f_100hz(void *arguments, void *logic)
{
	struct e_f_3f_logic *log = (struct e_f_3f_logic *)logic;

    float temp = 0;
    short temp1 = 0;
    struct e_f_wyjscie *w;

	w = log->f1;
    if(w->OK_[w->idx_]) //jesli pomiar w kanale 1 sprawny
    {
        temp += w->f_[w->idx_];
        temp1 +=1;
    }

	w = log->f2;
    if(w->OK_[w->idx_]) //jesli pomiar w kanale 2 sprawny
    {
        temp += w->f_[w->idx_];
        temp1 +=1;
    }

	w = log->f3;
    if(w->OK_[w->idx_]) //jesli pomiar w kanale 3 sprawny
    {
        temp += w->f_[w->idx_];
        temp1 +=1;
    }


	if(temp1)//jesli co najmniej jeden pomiar sprawny
    {
		*log->f_ptr = temp * mn[temp1-1]; // wyliczenie sredniej czestotliwosci
		*log->ok_ptr|=(1<<log->ok_bit_no);
    } else {
		*log->ok_ptr&=~(1<<log->ok_bit_no);
		*log->f_ptr = 0;
    }

}
