#include "DSP2833x_Device.h"     // DSP281x Headerfile Include File
#include "filter_bat2.h"
#include "measure.h"
#include "RS485.h"
#include "message.h"

#include "package.h"

#include "peripher.h"
#include "GPIO_table.h"


int Mode,Desk,Addrr,TermoAD=0,TermoRS=0,TermoSW=0,Currentoz=0;
LONGE Inputs;

int ExtraCanal[24];

void get_Mode()
{
  int i,qua;

	EALLOW;

	GpioCtrlRegs.GPAMUX1.all &= 0xFF000000;	//	00—11
	GpioCtrlRegs.GPAMUX2.all &= 0xFF00003F;	//	19—27
	GpioCtrlRegs.GPBMUX1.all &= 0xFFFFFCC0;	//	32—34, 36
	GpioCtrlRegs.GPBMUX2.all &= 0x000FF000;	//	48—53, 58—63

	GpioCtrlRegs.GPADIR.bit.GPIO20 = 0;
	GpioCtrlRegs.GPADIR.bit.GPIO21 = 0;
	GpioCtrlRegs.GPADIR.bit.GPIO22 = 0;
	GpioCtrlRegs.GPBDIR.bit.GPIO51 = 0;

	EDIS; 

	Mode=0; 
	
	qua=0;
	for(i=0;i<100;i++) 
	qua += !GpioDataRegs.GPADAT.bit.GPIO22;
	if(qua>50) Mode += 1;

	qua=0;
	for(i=0;i<100;i++) 
	qua += !GpioDataRegs.GPADAT.bit.GPIO20;
	if(qua>50) Mode += 2;
	
	qua=0;
	for(i=0;i<100;i++) 
	qua += !GpioDataRegs.GPADAT.bit.GPIO21;
	if(qua>50) Mode += 4;

	Addrr = Mode*2;

	Mode+= 1;

	qua=0;
	for(i=0;i<100;i++) 
	qua += !GpioDataRegs.GPBDAT.bit.GPIO51;
	if(qua>50) Addrr += 1;

	if(	(Mode==adr_REC1)||(Mode==adr_REC2))	Currentoz = 1;
	if(	(Mode==adr_REC1)||(Mode==adr_REC2)||
		(Mode==adr_INV1)||(Mode==adr_INV2))	Desk = dsk_COMM;
	if	(Mode==adr_SHKF)					Desk = dsk_SHKF;
	if	(Mode==adr_ISOL)					Desk = dsk_ISOL;

	EALLOW;
	switch(Desk)
	{
		case dsk_COMM:	GpioCtrlRegs.GPADIR.all = COMM_GPADIR;
						GpioCtrlRegs.GPBDIR.all = COMM_GPBDIR;	break;

		case dsk_SHKF:	GpioCtrlRegs.GPADIR.all = VEPP_GPADIR;
						GpioCtrlRegs.GPBDIR.all = VEPP_GPBDIR;	break;

		case dsk_ISOL:	GpioCtrlRegs.GPADIR.all = ISOL_GPADIR;
						GpioCtrlRegs.GPBDIR.all = ISOL_GPBDIR;	break;
	}
	EDIS;
}

void get_Inputs()
{
  static long butthurt[2] ={0,0};
  unsigned long butt=0;

	if(Desk==dsk_COMM)
	{
		if(!GpioDataRegs.GPADAT.bit.GPIO7)	butthurt[0]=0;
		else if(butthurt[0]<MAX_BUTTHURT)	butthurt[0]++;
		if(butthurt[0]<MAX_BUTTHURT) butt =1;

		if(!GpioDataRegs.GPADAT.bit.GPIO6)	butthurt[1]=0;
		else if(butthurt[1]<MAX_BUTTHURT)	butthurt[1]++;
		if(butthurt[1]<MAX_BUTTHURT) butt +=2;

		if(Mode<adr_INV1)
		{
			if(!GpioDataRegs.GPADAT.bit.GPIO26) butt += 4;
			if(!GpioDataRegs.GPADAT.bit.GPIO23) butt += 8;
	}	}

	if(Desk==dsk_SHKF)
	{
		if(GpioDataRegs.GPBDAT.bit.GPIO58)	butt += 0x0000001;
		if(GpioDataRegs.GPBDAT.bit.GPIO61)	butt += 0x0000002;

		if(GpioDataRegs.GPBDAT.bit.GPIO53)	butt += 0x00000010;
		if(GpioDataRegs.GPBDAT.bit.GPIO52)	butt += 0x00000020;
		if(GpioDataRegs.GPADAT.bit.GPIO24)	butt += 0x00000040;
		if(GpioDataRegs.GPADAT.bit.GPIO23)	butt += 0x00000080;
		if(GpioDataRegs.GPADAT.bit.GPIO27)	butt += 0x00000100;
		if(GpioDataRegs.GPADAT.bit.GPIO26)	butt += 0x00000200;
		if(GpioDataRegs.GPBDAT.bit.GPIO36)	butt += 0x00000400;
		if(GpioDataRegs.GPADAT.bit.GPIO25)	butt += 0x00000800;
		if(GpioDataRegs.GPADAT.bit.GPIO0)	butt += 0x00001000;
		if(GpioDataRegs.GPADAT.bit.GPIO2)	butt += 0x00002000;
		if(GpioDataRegs.GPADAT.bit.GPIO4)	butt += 0x00004000;
		if(GpioDataRegs.GPADAT.bit.GPIO6)	butt += 0x00008000;
		if(GpioDataRegs.GPADAT.bit.GPIO8)	butt += 0x00010000;
		if(GpioDataRegs.GPADAT.bit.GPIO10)	butt += 0x00020000;

		if(GpioDataRegs.GPADAT.bit.GPIO1)	butt += 0x00400000;
		if(GpioDataRegs.GPADAT.bit.GPIO3)	butt += 0x00800000;
		if(GpioDataRegs.GPADAT.bit.GPIO7)	butt += 0x01000000;
		if(GpioDataRegs.GPADAT.bit.GPIO9)	butt += 0x02000000;

//		ExtraCanal1 = !GpioDataRegs.GPADAT.bit.GPIO5;
//		ExtraCanal2 = !GpioDataRegs.GPADAT.bit.GPIO11;

		ExtraCanal[15] = (!GpioDataRegs.GPADAT.bit.GPIO1) | (!GpioDataRegs.GPADAT.bit.GPIO3);
		ExtraCanal[16] = (!GpioDataRegs.GPADAT.bit.GPIO7) | (!GpioDataRegs.GPADAT.bit.GPIO9);
	}

	if(Desk==dsk_ISOL)
	{
		if(!GpioDataRegs.GPBDAT.bit.GPIO62)	butt += 0x0000001;
	}

	Inputs.all = butt;
}

