/*
 * analog_in.c
 *
 *  Created on: 06-05-2014
 *      Author: Krzysztof Jakubczyk
 */

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

#include "analog_in.h"

int analog_in_initlog(void *arguments, void *logic)
{
	struct analog_in_args *args = (struct analog_in_args *)arguments;
	struct analog_in_logic *log = (struct analog_in_logic *)logic;
	u32 *params_ptr;
	u32 *samples_buf_ptr;

	log->kob_ptr = log_manager.nets_data + (args->io.kob_out >> 3);
	log->kob_bit_no = args->io.kob_out & 0x07;

	if(log->kob_ptr >= (log_manager.nets_data+sizeof(log_manager.nets_data)))
		return -1;

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

	log->raw_sample_ptr = (short *)(log_manager.nets_data + (args->io.sample_raw_short_out >> 3));
	if((u8*)log->raw_sample_ptr > (log_manager.nets_data+sizeof(log_manager.nets_data)-sizeof(short)))
		return -1;

	params_ptr = (u32 *)(log_manager.nets_data+(args->io.params_ptr_out>>3));

	if((u8 *)params_ptr > (log_manager.nets_data+sizeof(log_manager.nets_data)-sizeof(void *)) )
		return -1;

	*params_ptr = (u32)&log->params_ext;//args->params;

	if(args->params.card_addr>(MAX_AN_CARDS-1))
		return -1;

	if(args->params.phase_num>3)
		return -1;

	samples_buf_ptr = (u32 *)(log_manager.nets_data+(args->io.samples_buf_ptr_out>>3));

	if((u8 *)samples_buf_ptr > (log_manager.nets_data+sizeof(log_manager.nets_data)-sizeof(void *)) )
		return -1;

	*samples_buf_ptr = (u32)&bus_an_samples_buf[args->params.card_addr][args->params.phase_num][0];

	if(args->params.bits & ANALOG_NEGATION)
		bus_an_samples_neg[args->params.card_addr][args->params.phase_num]=1;
	else
		bus_an_samples_neg[args->params.card_addr][args->params.phase_num]=0;

	log->params_ext.bits=args->params.bits;
	log->params_ext.card_addr=args->params.card_addr;
	log->params_ext.jednostka=args->params.jednostka;
	log->params_ext.multiplier=args->params.multiplier;
	log->params_ext.offset=args->params.offset;
	log->params_ext.phase_num=args->params.phase_num;
	log->params_ext.znam_wtor=args->params.znam_wtor;
	log->params_ext.znam_pierw=args->params.znam_pierw;
	log->params_ext.nast.offset=args->params.offset;
	log->params_ext.nast.mnoznik=args->params.multiplier;
	//nast.offset = n.korekcja_offsetu * 32767 / n.zasieg / 1.4142136f;
	//nast.mnoznik = 1.4142136f * n.korekcja_ampl * n.zasieg / 32767;

	return 0;
}

void analog_in(void *arguments, void *logic)
{
	struct analog_in_args *args = (struct analog_in_args *)arguments;
	struct analog_in_logic *log = (struct analog_in_logic *)logic;
	//u16 sample;
	short sample;

	if(ic->kob_an & (1<<args->params.card_addr))
		*log->kob_ptr|=(1<<log->kob_bit_no);
	else
		*log->kob_ptr&=~(1<<log->kob_bit_no);

	sample = bus_an_samples_buf[args->params.card_addr][args->params.phase_num][bus_an_cur_sample_num] - 0x7FFF;
	*log->cur_sample_ptr=((float)sample - args->params.offset ) * args->params.multiplier;
	*log->raw_sample_ptr=sample;
}
