#include <stdio.h>
#include <avr/io.h>

class TUSART0stdio
{
private:
  FILE* usart0;

	static int mputc(char c, FILE *stream)
	{
		while(!(UCSR0A & (1 << UDRE0)));
		
		UDR0 = c;
		return c;
	}

  static int mgetc(FILE *stream)
  {
		char c;
		
	  while(!(UCSR0A & (1 << RXC0)));
		
		c = UDR0;
	  return c;
  }
public:
  TUSART0stdio()
	{
		 usart0 = fdevopen(&mputc, &mgetc);
		 stdout = stdin = usart0;
		 
		 UBRR0 = 103;
		 
		 UCSR0B = (1 << TXEN0)|(1 << RXEN0);
		 UCSR0C = (0 << USBS0)|(3 << UCSZ00);
	}	

	~TUSART0stdio()
	{
		fclose(usart0);
	}
};

static TUSART0stdio USART0stdio;