#include <p24FJ128GA006.h>
#include "../mcu/delay.h"

#include "led.h"

void led_init(void)
{
	TRISBbits.TRISB8 = 0;		// PORTB.8 as output
	asm ("NOP");
	TRISCbits.TRISC13 = 0;		// PORTC.13 as output
	asm ("NOP");
	TRISCbits.TRISC14 = 0;		// PORTC.14 as output
	asm ("NOP");

	LATBbits.LATB8 = LED_OFF;

}

void led_on(u_char led)
{
	unsigned int tmp = PORTC;
	
	tmp &= ~(1 << (LED_PIN_0+led));
	LATC = tmp;
	asm ("NOP");
}

void sys_led_on(void)
{
	unsigned int tmp = PORTB;
	
	tmp &= ~(1 << LED_PIN_x);
	LATB = tmp;
	asm ("NOP");
}

void led_off(u_char led)
{
	unsigned int tmp = PORTC;

	tmp |= (1 << (LED_PIN_0+led));
	LATC = tmp;
	asm ("NOP");
}

void sys_led_off(void)
{
	unsigned int tmp = PORTB;

	tmp |= (1 << LED_PIN_x);
	LATB = tmp;
	asm ("NOP");
}

u_char led_state(u_char led)
{
	unsigned int tmp = PORTC;

	return ( ( (tmp & (1 << (LED_PIN_0+led)))) ? LED_OFF : LED_ON);
}

u_char sys_led_state(void)
{
	unsigned int tmp = LATB;
	return ( ( (tmp & (1 << LED_PIN_x))) ? LED_OFF : LED_ON);
}

void led_toggle(u_char led)
{
	if(led_state(led)==LED_OFF)
		led_on(led);
	else
		led_off(led);
}

void sys_led_toggle(void)
{
	if(sys_led_state()==LED_OFF)
		sys_led_on();
	else
		sys_led_off();
}
