/*
 * dfr_drv.c
 *
 *  Created on: 23-10-2014
 *      Author: KJ
 */


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

#include <string.h>
#include "rec_bin.h"
#include "rec_an.h"
#include "rec_buf.h"
#include "rec_float.h"
#include "dfr.h"
#include "dfr_drv.h"

struct dfr_drv_logic *dfr_drv_log_ptr = NULL;

int dfr_drv_initlog(void *arguments, void *logic)
{
	struct dfr_drv_logic *log = (struct dfr_drv_logic *)logic;
	int i;

	log->an_count=0;
	log->bin_count=0;

	for(i=0;i<MAX_LOG_ELEMENTS;i++) /* find analog channels */
	{
		if(log_manager.log_element[i].flags & (FUNCTION_IS_REC_AN|FUNCTION_IS_REC_FLOAT|FUNCTION_IS_REC_BUF))
		{
			if(log_manager.log_element[i].flags & FUNCTION_IS_REC_AN)
			{
				struct rec_an_args *args;

				args =  (struct rec_an_args *)log_manager.log_element[i].fun_args_ptr;
				if(!(args->params.bits & REC_CHAN_ENABLED_DFR))
					continue;
			}
			else if(log_manager.log_element[i].flags & FUNCTION_IS_REC_BUF)
			{
				struct rec_buf_args *args;

				args =  (struct rec_buf_args *)log_manager.log_element[i].fun_args_ptr;
				if(!(args->params.bits & REC_CHAN_ENABLED_DFR))
					continue;
			}
			else
			{
				struct rec_float_args *args;

				args =  (struct rec_float_args *)log_manager.log_element[i].fun_args_ptr;
				if(!(args->params.bits & REC_CHAN_ENABLED_DFR))
					continue;
			}


			if((u8 *)&log->element_num[log->an_count]>(u8 *)(log_manager.buf+sizeof(log_manager.buf)-sizeof(u16)))
				return -1;

			log->element_num[log->an_count]=i|((log_manager.log_element[i].flags & FUNCTION_IS_REC_FLOAT)?ELEMENT_IS_REC_FLOAT:(log_manager.log_element[i].flags & FUNCTION_IS_REC_BUF)?ELEMENT_IS_REC_BUF:0);
			log->an_count++;
		}
	}

	log->sample_size=log->an_count * sizeof(u16);

	for(i=0;i<MAX_LOG_ELEMENTS;i++) /* find binary channels */
	{
		struct rec_bin_args *args;

		args =  (struct rec_bin_args *)log_manager.log_element[i].fun_args_ptr;

		if((log_manager.log_element[i].flags & FUNCTION_IS_REC_BIN) && (args->params.bits & REC_CHAN_ENABLED_DFR))
		{
			if((u8 *)&log->element_num[log->an_count+log->bin_count]>(u8 *)(log_manager.buf+sizeof(log_manager.buf)-sizeof(u16)))
				return -1;

			log->element_num[log->an_count+log->bin_count]=i;
			log->bin_count++;
		}
	}

	log->sample_size+=((log->bin_count/16)*2);
	if(log->bin_count%16)
		log->sample_size+=2;

	log->sample_size+=8; // sample number && time stamp

	if(log->sample_size>MAX_SAMPLE_SIZE)
		return -1;

	for(i=0;i<MAX_LOG_ELEMENTS;i++)
	{
		if(log_manager.log_element[i].flags & FUNCTION_IS_DFR)
		{
			struct dfr_args *args;

			args =  (struct dfr_args *)log_manager.log_element[i].fun_args_ptr;

			if(args->params.max_reg_time > ((REG_BANK_SIZE / log->sample_size) - 2))
				args->params.max_reg_time=((REG_BANK_SIZE / log->sample_size) - 2);
				//return -1;

			if((args->params.post_trigger_time + args->params.pre_trigger_time) > args->params.max_reg_time)
			{
				args->params.post_trigger_time=args->params.max_reg_time/2;
				args->params.pre_trigger_time=args->params.max_reg_time/2;
//				return -1;
			}
		}
	}

	log->max_offset=REG_BANK_SIZE/dfr_drv_log_ptr->sample_size*dfr_drv_log_ptr->sample_size;

	dfr_drv_log_ptr=(struct dfr_drv_logic *)logic;

	return 0;
}

