#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 Lonely=0;

unsigned long isMask = 0;
int isLamp = 0;
int isBrit = 0;
int isNumb = 1111;

int quaLamp = 6;

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 count_bright=0;
  unsigned int light=0, i;
  static unsigned int cownt;
  const unsigned long Alone = READY_FREQ * 10;

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

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

	toggle_ONLINE();

	toggle_RES_OUT_1();

	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((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(cownt == 0)	kanal_Send(0,Modbus[0].all,1);
		if(cownt == 1)	kanal_Send(1,Modbus[1].all,1);
						kanal_Send(2,Modbus[2].all & light,0);
		if(cownt == 2)	kanal_Send(3,Modbus[3].all,0);
	}

	if(!cReset)	ServiceDog();
}
