#include "board.hpp"
#include <avrstuff/serial_avrdx.hpp>
#include <avrstuff/bufserial.hpp>
#include <avr/interrupt.h>
#include <assert.h>

static serial_avrdx<2, 8> usbmidi_usart;
static bufserial<63, 63> usbmidi_buf(usbmidi_usart);

// Plan: main pulls from an event queue. Events are generated from the MIDI
// receive vectors as well as from other sources (e.g. timers to implement
// portamento), in close to raw MIDI format via a struct. "Proprietary"
// commands are sent as a SysEx message with an ASCII command string. There is
// one global SysEx buffer per MIDI receiver, so a second message may not be
// sent until the acknowledge is received.

int main(void)
{
	board_pins_init();

	usbmidi_usart.set_baud<31250>();
	usbmidi_usart.start(true, true, 'N', false);
	sei();

	for (;;)
	{
		int c = usbmidi_buf.recv();
		if (c >= 0)
			usbmidi_buf.send(c);
	}
}

void operator delete(void * p, unsigned int sz)
{
	assert(false && "ouch!");
	for(;;);
}

ISR(USART2_DRE_vect)
{
	usbmidi_buf.dre();
}

ISR(USART2_RXC_vect)
{
	usbmidi_buf.rxc();
}
