#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include "uart0.h"




char rx_buf[128];
int tail = 0, head = 0;

void init_Serial0(long baud)
{

#ifndef F_CPU 
#define F_CPU 16000000L
#endif

// set baud 
UBRR0H = ((F_CPU / 16 + baud / 2) / baud - 1) >> 8;
UBRR0L = ((F_CPU / 16 + baud / 2) / baud - 1);


// enable rx abd tx
UCSR0B |= _BV(RXCIE0) | _BV(RXEN0) | _BV(TXEN0);

}

char read0()
{
  if (head == tail)
  {
    return 0;
  }
  else
  {
    if (head<127)
    {
	  head++;
      return rx_buf[head -1];
	}
	else
	{
	  head = 1;
	  return rx_buf[0];
	}
  }

}

void store(char c)
{
  if (c != 0)
  {
    if (tail <127)
	{
      tail++;
	  rx_buf[tail-1] = c;
	}
	else
	{
	  tail = 0;
	  rx_buf[127] = c;
	}
  } 
}

void flush0()
{
  head = 0;
  tail = 0;
}








