#include "DSP2833x_Device.h"     // DSP2833x Headerfile Include File
#include "DSP2833x_SWPrioritizedIsrLevels.h" 

#include "RS485.h"
#include "message.h"
#include "filter_bat2.h"
#include "measure.h"
#include "package.h"
#include "pulto.h"
#include "kanal.h"
#include "peripher.h"

unsigned long isMask;
int isNumb;

int quaLamp = 6;

unsigned long Lonely=0;

void what_is()
{
  static unsigned int count_blink;
  static int numb=0;

	if(++count_blink >= BLINK_TIME)
	{
		count_blink=0;

		numb++; if(numb==10) numb=0;
		isNumb = numb*1000 + numb*100 + numb*10 +numb;

		if(isMask)	isMask = 0;
		else		isMask = 0xFFFFFFFF;
}	}

interrupt void cpu_timer1_isr_PULT(void)
{
  static int cownt=0,count_bright;
  unsigned int light=0, i;
  unsigned int Alone = 20*READY_FREQ;

	EALLOW;
	CpuTimer1.InterruptCount++;
	IER |= MINT13;                 // Set "global" priority
	EINT;
  	EDIS;    // This is needed to disable write to EALLOW protected registers

	if(!cReset)	ServiceDog();

	if(++CanPowse >= CANPOWSE)
	{
		CanPowse = 0;
		CanGO = 1;
	}

	toggle_ONLINE();

	toggle_RES_OUT_1();	//	типа это готовность
	toggle_RES_OUT_2();	//	на всакий

	if(++Lonely > Alone) Lonely = Alone;

	if(++count_bright >= maximum_bright) count_bright = 0 ;

	for(i=0; i<quaLamp; i++)
	{
		if(count_bright < Bright[i])	light+=(1<<i);
	}

	if(++cownt>2) cownt=0;

	if(cTestLamp|bTestLamp)
	{
		what_is();

		if(cownt == 0)	kanal_Send(0,isNumb,4);
		if(cownt == 1)	kanal_Send(1,isNumb,4);
						kanal_Send(2,isMask,0);
		if(cownt == 2)	kanal_Send(3,isMask,0);
	}
	else
	if(cLiteFire)
	{
		if(cownt == 0)	kanal_Send(0,8888,1);
		if(cownt == 1)	kanal_Send(1,8888,1);
						kanal_Send(2,0xFFFF & light,0);
		if(cownt == 2)	kanal_Send(3,0xFFFF,0);
	}
	else
	if((Lonely >= Alone))
	{
		if(cownt == 0)	kanal_Send(0,0,0);
		if(cownt == 1)	kanal_Send(1,0,0);
						kanal_Send(2,0,0);
		if(cownt == 2)	kanal_Send(3,0,0);
	}
	else
	{
#if OBOROT == STRAIGHT
		if(cownt == 0)	kanal_Send(0,modbus[0],1);
		if(cownt == 1)	kanal_Send(1,modbus[1],1);
#else
		if(cownt == 0)	kanal_Send(0,modbus[1],1);
		if(cownt == 1)	kanal_Send(1,modbus[0],1);
#endif
						kanal_Send(2,modbus[2] & light,0);
		if(cownt == 2)	kanal_Send(3,modbus[3],0);
}	}
