#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>


#define PRESCALER 1024
#define TIME_SLOT 20
#define BAUDRATE	103

//// INIT SERIAL /////

void init_serial(void)
{
	/* ACHTUNG : we suppose UBRR value < 0xff */
	/* Not true in all case */
	uint8_t baudrate = BAUDRATE;
	/* Set baud rate */
	UBRR0H = 0;
	UBRR0L = baudrate;
	
	UCSR0B = (1<<TXEN0);
	/* Set frame format */
	UCSR0C = 0x06;
    
}

//// ENVOI DE MESSAGES /////

void send_serial(unsigned char c)
{
	loop_until_bit_is_set(UCSR0A, UDRE0);
	UDR0 = c;
}


void send_msg2(char Donnee[]){
 
    while (( UCSR0A & (1<<UDRE0))  == 0){};
            
            for (int i = 0; i < strlen(Donnee); i++){ 
                while (( UCSR0A & (1<<UDRE0))  == 0){};
                UDR0 = Donnee[i]; 
                if (i == (strlen(Donnee) - 1)){
                    send_serial('\n');
                    send_serial('\r');
                }
            } 
}

void send_msg(char Donnee[]){
        int i=0;
      for(i=0; i<strlen(Donnee); i++){
          if (i==strlen(Donnee)-1){
              send_serial(Donnee[i]);
              send_serial('\n');
              send_serial('\r');
        }
          else{
              send_serial(Donnee[i]);
        } 
        _delay_ms(100);
        }
}

//////// RETOUR /////////
void retour(char* show_a){
        send_msg2(show_a);
}
  
 
