/* BlueDataDistributor - data distribution module from ethernet to 32 serial ports
 * version 0.1.1 date 2006-10-07
 * Copyright (C) 2006 Stefan Schuermans <stefan@blinkenarea.org>
 * a BlinkenArea project - http://www.blinkenarea.org/
 */

#include <avr/io.h>

#include "status.h"

// number of times to blink status LED
#define StatusBlinkCntSerialData 1
#define StatusBlinkCntTimeout 2
unsigned char StatusBlinkCnt = StatusBlinkCntTimeout;

// interval counter for blinking status LED
unsigned char StatusBlinkInterval = 0;

// number of serial data events in last blink cycle
unsigned char StatusEventSerialCnt = 0;

// initialize
void StatusInit( void )
{
	//set up IO pin of status LED as output
	DDRG |= 0x08;
}

// status event - received serial data to output
void StatusEventSerial( void )
{
	// count number of serial data events
	StatusEventSerialCnt++;
}

// tick procedure - call every 200ms
void StatusTick200( void )
{
	// turn out status LED only in even intervals and if not yet blinked StatusBlinkCnt times
	if( (StatusBlinkInterval & 0x01) == 0 && StatusBlinkInterval >> 1 < StatusBlinkCnt )
		PORTG |= 0x08;
	else
		PORTG &= ~0x08;

	// next interval
	StatusBlinkInterval++;

	// blink cycle finished
	if( StatusBlinkInterval >= 10 ) {
		StatusBlinkInterval = 0;

		// set new blink count
		StatusBlinkCnt = StatusEventSerialCnt > 0 ? StatusBlinkCntSerialData : StatusBlinkCntTimeout;
		StatusEventSerialCnt = 0;
	}
}

