/*
 * mux_t.c
 *
 *  Created on: 02-02-2017
 *      Author: PS
 */

#include <math.h>

//#include "pawel_usun_to.h"

#include "mux_t.h"

#include "helper.h"
#include "config.h"

int mux_t_initlog(void *arguments, void *logic)
{
	struct mux_t_args *args = (struct mux_t_args *)arguments;
	struct mux_t_logic *log = (struct mux_t_logic *)logic;
	
	int i = 0;
	//wejscia
	for (i = 0; i < 21; i++)
	{
		if(set_float_ptr(args->io.we_in[i],&log->we[i]))
			return -1;
	}	
	if (set_bit_ptr_mask(args->io.wb1_in, &log->wb1, &log->wb1_bit_mask))
		return -1;
	if (set_bit_ptr_mask(args->io.wb2_in, &log->wb2, &log->wb2_bit_mask))
		return -1;	
	if (set_bit_ptr_mask(args->io.wb3_in, &log->wb3, &log->wb3_bit_mask))
		return -1;		
	if (set_bit_ptr_mask(args->io.wyl_in, &log->wyl, &log->wyl_bit_mask))
		return -1;
	
	//wyjscia
	for (i = 0; i < 7; i++)
	{
		if(set_float_ptr(args->io.wy_out[i],&log->wy[i]))
			return -1;
	}
	if (set_bit_ptr_mask(args->io.wb_mux_out, &log->wb_mux, &log->wb_mux_bit_mask))
		return -1;
	if(set_bit_ptr_mask(args->io.wyl_s1_out, &log->wyl_s1 ,&log->wyl_s1_bit_mask))
		return -1;
	if(set_bit_ptr_mask(args->io.wyl_s2_out, &log->wyl_s2 ,&log->wyl_s2_bit_mask))
		return -1;
	if(set_bit_ptr_mask(args->io.wyl_s3_out, &log->wyl_s3 ,&log->wyl_s3_bit_mask))
		return -1;
	
	log->wyl_calosc = (args->params.bity & 0x0001) ? 1: 0;
	log->strona = (u8)(args->params.strona);	
	
	return 0;
}
void mux_t(void *arguments, void *logic)
{
	struct mux_t_logic *log = (struct mux_t_logic *)logic;

	// multiplekser wejsc analogowych (3 zestawy wejsc po 7)
	int i,start = 0;
	
	if (log->strona == 1)
		start = 7;
	else if (log->strona == 2)
		start = 14;
	else
		start = 0;

	for (i = 0; i < 7; i++)
	{
	*log->wy[i] = *log->we[i+start];	
	}
	
	// multiplekser binarny
	switch (log->strona)
	{
		case 0:
			if ((*log->wb1 & log->wb1_bit_mask) != 0)
				*log->wb_mux |= log->wb_mux_bit_mask;
			else
				*log->wb_mux &= ~log->wb_mux_bit_mask;		
			break;
		case 1:
			if ((*log->wb2 & log->wb2_bit_mask) != 0)
				*log->wb_mux |= log->wb_mux_bit_mask;
			else
				*log->wb_mux &= ~log->wb_mux_bit_mask;		
			break;
		case 2:
			if ((*log->wb3 & log->wb3_bit_mask) != 0)
				*log->wb_mux |= log->wb_mux_bit_mask;
			else
				*log->wb_mux &= ~log->wb_mux_bit_mask;		
			break;
		default:
			break;		
	}
	
	// demultiplekser binarny
	u8 we_wyl = ((*log->wyl & log->wyl_bit_mask) != 0) ? 1 : 0;
	if (we_wyl)
	{
		if (log->wyl_calosc)
		{
			*log->wyl_s1 |= log->wyl_s1_bit_mask;
			*log->wyl_s2 |= log->wyl_s2_bit_mask;
			*log->wyl_s3 |= log->wyl_s3_bit_mask;
		}
		else
		{
			switch (log->strona)
			{
			case 0:
				*log->wyl_s1 |= log->wyl_s1_bit_mask;
				*log->wyl_s2 &= ~log->wyl_s2_bit_mask;
				*log->wyl_s3 &= ~log->wyl_s3_bit_mask;
				break;
			case 1:
				*log->wyl_s1 &= ~log->wyl_s1_bit_mask;
				*log->wyl_s2 |= log->wyl_s2_bit_mask;
				*log->wyl_s3 &= ~log->wyl_s3_bit_mask;
				break;
			case 2:
				*log->wyl_s1 &= ~log->wyl_s1_bit_mask;
				*log->wyl_s2 &= ~log->wyl_s2_bit_mask;
				*log->wyl_s3 |= log->wyl_s3_bit_mask;
				break;
			default:
				break;
			}			
		}	
	}
	else
	{
		*log->wyl_s1 &= ~log->wyl_s1_bit_mask;
		*log->wyl_s2 &= ~log->wyl_s2_bit_mask;
		*log->wyl_s3 &= ~log->wyl_s3_bit_mask;
	}
}

