#include "config.h"
#include "led.h"
#include "timer.h"

int16_t __led_pattern;

/* Debug LED */
void TISR_HANDLER(6)
{
	timer_clear_txif(6);

	if (__led_pattern == -1)
		return;

	__led_pattern <<= 1;
	if (__led_pattern & 0x100) {
		PORTBbits.RB13 = 0;  /* on */
		__led_pattern |= 1;
	} else {
		PORTBbits.RB13 = 1;  /* off */
	}

	__led_pattern &= 0xff;
}

void led_init(void)
{
	TRISBbits.TRISB13 = 0;
	PORTBbits.RB13 = 1;
	__led_pattern = -1;

	timer_setup_16bit(6, LED_BLINK_RATE, 1);
	timer_set_priority(6, 1);    /* low priority */
}

/* Set a pattern (8-bit binary pattern). */
void led_pattern(uint8_t pattern)
{
	__led_pattern = pattern;
}
