#define F_CPU 20000000UL
#include <stdint.h>
#include <avr/io.h>
#include <util/delay.h>
#define BAUD 9600
#include <util/setbaud.h>




void uart_init(void) {
    #define BAUDRATE ((F_CPU)/(BAUD*8UL)-1) // set baud rate value for UBRR
    UBRR0H = (BAUDRATE>>8);  // shift the register right by 8 bits to get the upper 8 bits
    UBRR0L = BAUDRATE;       // set baud rate
	
	// UCSR0A |= (1 << U2X0);   // double transmission speed
	// UCSR0B = (1 << TXEN0) | (1 << RXEN0);
    // UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);
    
	// UBRR0H = UBRRH_VALUE;
    // UBRR0L = UBRRL_VALUE;
    

	UCSR0A |= _BV(U2X0);
	// UCSR0A &= ~(_BV(U2X0));


    // UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); /* 8-bit data */
    UCSR0C = 0x06; /* 8-bit data */
    UCSR0B = _BV(RXEN0) | _BV(TXEN0) | _BV(RXCIE0);   /* Enable RX and TX */

     
}

/*
// f=20MHz -> T=0,05 µs
uint8_t T0H = 6;  // == 0.3  µs ~ 0.4 µs (+- 150ns) == [0.25, 0.55] µs
uint8_t T1H = 14; // == 0.7  µs ~ 0.8 µs (+- 150ns) == [0.65, 0.95] µs
uint8_t T0L = 15; // == 0.75 µs ~ 0.85µs (+- 150ns) == [0.70, 1.00] µs
uint8_t T1L = 8;  // == 0.35 µs ~ 0.45µs (+- 150ns) == [0.30, 0.60] µs
uint8_t RES = 51; //  > 50   µs
*/

#define nop __asm__("nop\n\t")
#define wait6 __asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t")
#define wait8 __asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t")
#define wait14 __asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t")
#define wait15 __asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t")

uint8_t const CMAX = 32;
uint8_t const STEPWIDTH = 4;

void writeZero();
void writeOne();
void writeRGB(uint8_t red, uint8_t green, uint8_t blue);

static uint8_t dNext[] = {1,2,0};

int  main() {
	DDRC = 1; // PORT C0 output
	// uart_init();
	uint8_t d = 0;
	uint8_t r = 120, g = 120, b = 0;

/*
  255   0   0
  255 255   0
    0   0 255
 */ 


	do{
		for(uint8_t i = 120; i > 0; i -= STEPWIDTH) {
			if( i >= 60 )
				writeRGB( 120, (120-i)*2 , 0 );
			if( i <= 60 )
				writeRGB( i, (60+i)/2, 120-i);
		}
		_delay_ms(500);
		
			
		/*if( d == 0 ) {
			do{
				for( uint8_t i = 0; i < 10; i++) {
					writeRGB(r,g,b);
				}
				for( uint8_t i = 0; i < 10; i++) {
					writeRGB(b,g,r);
				}
				for( uint8_t i = 0; i < 10; i++) {
					writeRGB(g,r,b);
				}
				_delay_ms(1);
				r -=1;
				b +=1;
			}while(r > 0);
		
		}
		if( d == 1 ) {
			do{
				for( uint8_t i = 0; i < 10; i++) {
					writeRGB(r,g,b);
				}
				for( uint8_t i = 0; i < 10; i++) {
					writeRGB(b,g,r);
				}
				for( uint8_t i = 0; i < 10; i++) {
					writeRGB(g,r,b);
				}
				_delay_ms(1);
				r +=1;
				g -=1;
			}while(g > 0);
		
		}
		if( d == 2 ) {
			do{
				for( uint8_t i = 0; i < 10; i++) {
					writeRGB(r,g,b);
				}
				for( uint8_t i = 0; i < 10; i++) {
					writeRGB(b,g,r);
				}
				for( uint8_t i = 0; i < 10; i++) {
					writeRGB(g,r,b);
				}
				_delay_ms(1);
				b -=1;
				g +=1;
			}while(b > 0);
		
		}
			/* for( int i = 0; i<30; i++) {
				// if( d == 0 )
				writeRGB(r,g,b);
				if( d == 1 )
					writeRGB(0,128,0);
				if( d == 2 )
					writeRGB(0,0,128); 
				d = dNext[d];
		}*/
		// _delay_ms(500);
		d = dNext[d];
		
		
		// loop_until_bit_is_set(UCSR0A, UDRE0);
		// UDR0 = 'D'; 
	
	}while(1);
}

int main2( void )
{
    DDRC = 0xff; // (1 << PC0);

    while(1) {
        PORTC ^= 0xff; //(1 << PC0);
        _delay_ms(1000);
    }
    return 0;
}

void writeZero() {
	PORTC = 1;
	wait6;
	PORTC = 0;
	wait15;
}

void writeOne() {
	PORTC = 1;
	wait14;
	PORTC = 0;
	wait8;
}

void writeRGB(uint8_t red, uint8_t green, uint8_t blue) {
	int i,j;
	
	for( i = 128; i > 0; i >>= 1 ) {
		if( green & i ){
			writeOne();			
		} else {
			writeZero();
		}
	}
	
	for( i = 128; i > 0; i >>= 1 ) {
		if( red & i ){
			writeOne();			
		} else {
			writeZero();
		}
	}
	
	for( i = 128; i > 0; i >>= 1 ) {
		if( blue & i ){
			writeOne();			
		} else {
			writeZero();
		}
	}
}
