/*
===============================================================================
 Name        : main.c
 Author      : $(author)
 Version     :
 Copyright   : $(copyright)
 Description : main definition
===============================================================================
*/

#ifdef __USE_CMSIS
#include "LPC17xx.h"
#endif

#include <cr_section_macros.h>
#include <NXP/crp.h>

// Variable to store CRP value in. Will be placed automatically
// by the linker when "Enable Code Read Protect" selected.
// See crp.h header for more information
__CRP const unsigned int CRP_WORD = CRP_NO_CRP ;

#include <stdio.h>

// TODO: insert other definitions and declarations here
#if 1
// override default (dummy) systick handler
static volatile uint32_t ticks;
static volatile uint32_t sec100;
static volatile uint32_t sec;


void SysTick_Handler(void)
{
	ticks++;
	if(++sec100==100) {
		sec100 = 0;
		sec++;
	}
}

unsigned int get_ticks(void)
{
	return ticks;
}
#endif


void led_set(int led, int state)
{
	switch(led) {
	case 1:
		if(state) LPC_GPIO0->FIOSET |= (1 << 15); // exp. board led 1
		else LPC_GPIO0->FIOCLR |= (1 << 15); // exp. board led 1
		break;
	case 2:
		if(state) LPC_GPIO0->FIOSET |= (1 << 16); // exp. board led 2
		else LPC_GPIO0->FIOCLR |= (1 << 16); // exp. board led 2
		break;
	case 3:
		if(state) LPC_GPIO0->FIOSET |= (1 << 23); // exp. board led 3
		else LPC_GPIO0->FIOCLR |= (1 << 23); // exp. board led 3
		break;
	case 4:
		if(state) LPC_GPIO0->FIOSET |= (1 << 24); // exp. board led 4
		else LPC_GPIO0->FIOCLR |= (1 << 24); // exp. board led 4
		break;
	case 5:
		if(state) LPC_GPIO0->FIOSET |= (1 << 22); // LPCXpresso user led
		else LPC_GPIO0->FIOCLR |= (1 << 22); // LPCXpresso user led
		break;
	case 6:
		if(state) LPC_GPIO0->FIOSET |= (1 << 29); // ethernet led
		else LPC_GPIO0->FIOCLR |= (1 << 29); // ethernet led
		break;
	case 7:
		if(state) LPC_GPIO0->FIOSET |= (1 << 30); // ethernet led
		else LPC_GPIO0->FIOCLR |= (1 << 30); // ethernet led
		break;
	}
}

int button_read(int button)
{
	int value = 0;

	switch(button) {
	case 1:
		value = LPC_GPIO0->FIOPIN & (1 << 9);
		break;
	case 2:
		value = LPC_GPIO0->FIOPIN & (1 << 8);
		break;
	case 3:
		value = LPC_GPIO0->FIOPIN & (1 << 7);
		break;
	case 4:
		value = LPC_GPIO0->FIOPIN & (1 << 6);
		break;
	}
	return value;
}


void hw_init(void)
{
	LPC_GPIO0->FIODIR |= (1 << 22); // LPCXpresso user led
	LPC_GPIO0->FIODIR |= (1 << 15); // exp. board led 1
	LPC_GPIO0->FIODIR |= (1 << 16); // exp. board led 2
	LPC_GPIO0->FIODIR |= (1 << 23); // exp. board led 3
	LPC_GPIO0->FIODIR |= (1 << 24); // exp. board led 4
	LPC_GPIO0->FIODIR |= (1 << 29); // ETH LED
	LPC_GPIO0->FIODIR |= (1 << 30); // ETH LED

	LPC_GPIO0->FIODIR &= ~(1 << 6);
	LPC_GPIO0->FIODIR &= ~(1 << 7);
	LPC_GPIO0->FIODIR &= ~(1 << 8);
	LPC_GPIO0->FIODIR &= ~(1 << 9);

}

int main(void) {

	hw_init();

	SystemCoreClockUpdate();

	// set systick timer to interrupt at 10 ms intervals
	SysTick_Config(SystemCoreClock / 100);

	//printf("Hello World\n");
	

	// Enter an infinite loop, just incrementing a counter
	volatile static int i = 0 ;
	while(1) {
		int b;
		i++ ;
		led_set(5, sec & 1);
		led_set(6, sec % 3);
		led_set(7, sec % 4);

		for(b = 1; b <= 4; b++) {
			led_set(b, !button_read(b));
		}
	}
	return 0 ;
}


