/**
 * Name        : main.c
 * Version     :
 * Description : main definition for FreeRTOS application
 */

/*
 * FreeRTOS includes
 */
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "semphr.h"
#include "LPC17xx.h"
#include "i2c.h"
#include <stdio.h>

#define USERTASK_STACK_SIZE configMINIMAL_STACK_SIZE

void __error__(char *pcFilename, unsigned long ulLine) {
}

static void setupHardware(void) {
	// TODO: Put hardware configuration and initialisation in here
	
	// Warning: If you do not initialize the hardware clock, the timings will be inaccurate
}

/**
 * Simple task that just toggles between to states
 */
void vUserTask1(void *pvParameters) {
	static int iState = 0;

	while (1) {
		if (iState == 0) {
			iState = 1;
		} else {
			iState = 0;
		}
		vTaskDelay(100);
	}
}

/**
 * Simple task that increments a counter
 */
void vUserTask2(void *pvParameters) {
	char tmp[8];
	int i;

	i2c_init();

	i2c_read(0xA0, 0, 8, tmp);
	for(i = 0; i < 8; i++) {
		//printf("%02X ", tmp[i]);
	}
	//printf("\n");
	i2c_writeb(0xA0, 0, tmp[0] + 1);
	/* little delay to allow write to complete (spec. say max 5 ms) */
	vTaskDelay(configTICK_RATE_HZ / 200);
	i2c_read(0xA0, 0, 8, tmp);
	for(i = 0; i < 8; i++) {
		//printf("%02X ", tmp[i]);
	}
	//printf("\n");

	int count = 0;
	while (1) {
		count++;
		vTaskDelay(101) ;
	}
}

/**
 * Program entry point 
 */
int main(void) {
	setupHardware();

	/* 
	 * Start the tasks defined within this file/specific to this demo. 
	 */
	//xTaskCreate( vUserTask1, ( signed portCHAR * ) "Task1", USERTASK_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
	xTaskCreate( vUserTask2, ( signed portCHAR * ) "Task2", USERTASK_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );

	/* 
	 * Start the scheduler. 
	 */
	vTaskStartScheduler();

	/* 
	 * Will only get here if there was insufficient memory to create the idle task. 
	 */
	return 1;
}
