#include <avr/io.h>
#include <avr/interrupt.h>
#include "adc.h"
#include "led.h"
#include "button.h"
#include "main.h"

/* Communication driver */
#define SLAVE_ENABLE_DDR DDRA
#define SLAVE_ENABLE_REG PINA
#define SLAVE_ENABLE_PIN 1

#include <interface.h>

#include "main.h"

void comm_init();

// the public instance of the app buffer
struct app app = {};

uint8_t channel_map[ADC_BUFFER_SIZE] = {
	0,
	1,
	2,
	3,
	7
};

// initalizes the application modules
void init() 
{
	// init button module (making the button light up on press)
	// module also updates app.buffer.button for us
	button_init();

	// init adc module
	// will sample 0x8F (CHANNEL 8 and 0-3) 
	// this module also updates app.buffer.poti and slider for us
	adc_init(channel_map);

	// init communication module
	comm_init();
	
	// start all modules
	adc_start();

	// activate interrupts (effectivly starting the application)
	sei();
}

void comm_init()
{
	DDRA |= (1<<PA5); // setup MOSI as output
	USICR = (1<<USIWM0)|(1<<USICS1);
}

volatile enum {
	COMM_WAIT       = 0,
	COMM_SEND_START ,
	COMM_SEND_WAIT  ,
	COMM_SEND_END   ,
} comm_state;

static inline void comm_service()
{
	static uint8_t count = 0;
	static uint16_t temp = 0xFF;

	if (USISR & (1<<USIOIF))
	{
		led_on();
		cli();

		switch(comm_state)
		{
		case COMM_WAIT:
			if (IS_EOM(USIDR)) {
				// pass package through
				USIDR &= ~0x80;
				comm_state = COMM_SEND_START;
				adc_stop();
			}
			break;
		case COMM_SEND_START:
			temp = adc.buffer[count];
			USIDR = temp & 0x7F;
			comm_state = COMM_SEND_WAIT;
			break;
		case COMM_SEND_WAIT:
			USIDR = (temp>>7)&0x7F;

			if (++count == ADC_BUFFER_SIZE) {
				comm_state = COMM_SEND_END;
			} else {
				comm_state = COMM_SEND_START;
			}
			break;
		case COMM_SEND_END:
			count = 0;
			USIDR = app.state.state.button | 0x80;
		default:
			comm_state = COMM_WAIT;
			adc_start();
			break;
		}

		USISR |= (1<<USIOIF);

		sei();
		led_off();
	}
}
#include <util/delay.h>

// little debug helper function
// FIXME: remove when not needed
void delay(uint16_t d) 
{
	while(d--) 
	{
		_delay_ms(1);
	}
}

int main() 
{
	init();

	while(1) {
		// service serial interface
		// slightly more stable then interrupt
		comm_service();
	}
}
