#include <avr/io.h>			
#include <util/delay.h>     
#include "lcd.h"      

// *************************************************
// FCE: ovladani znakoveho displeje
// INC: "lcd.h"
// DAT: 3.3.2012
// VER: 4 
// *************************************************

unsigned char x,y ;				// pomocné souøadnice pozice kurzoru



void lcd_clrscr( void )			// smaze displej
{
	lcd_command( 0x00000001 ) ;
	x = 0 ;
	y = 0 ;
	_delay_ms( 16 ) ;
}


void lcd_command( char data )	// zapis 1-bytove instrukce
{
	lcd_write( data, 0 ) ;
}



void lcd_data( char data )		// zapis 1-bytoveho datoveho slova
{
	lcd_write( data, 1 ) ;
}



void lcd_write( char data, char rs )	// zapis instrukce nebo dat do lcd displeje
{
	unsigned char temp ;

    if( rs ) lcd_rs_high() ;
	else lcd_rs_low() ;

	temp = LCD_PORT & 0x0f ;
    LCD_PORT = temp | ( data & 0xf0 ) ;
    lcd_e_toggle() ;
    LCD_PORT = temp | (( data & 0x0f ) << 4 ) ;
    lcd_e_toggle() ;
    LCD_PORT = temp | 0xf0 ;
    _delay_ms( 2 ) ;
}



void toggle_e( void )		// enable puls
{
    lcd_e_high() ;
    lcd_e_delay() ;
    lcd_e_low() ;
}



void lcd_gotoxy( char x_, char y_ )		// presun kurzoru
{
	if( y_ == 0 )
    {	lcd_command(( 1<<LCD_DDRAM ) + LCD_START_LINE1 + x_ ) ;
		y = 0 ;
	}
    else if( y_ == 1 )
    {	lcd_command(( 1<<LCD_DDRAM ) + LCD_START_LINE2 + x_ ) ;
		y = 1 ;
	}
	else if( y_ == 2 )
    {	lcd_command(( 1<<LCD_DDRAM ) + LCD_START_LINE3 + x_ ) ;
		y = 2 ;
	}
	else if( y_ == 3 )
    {	lcd_command(( 1<<LCD_DDRAM ) + LCD_START_LINE4 + x_ ) ;
		y = 3 ;
	}
	x = x_ ;
}



void lcd_putc( char c )		// zobrazeni jednoho znaku
{
    if (c=='\n')
		lcd_newline() ;
	else
	{
        if(( x == LCD_DISP_LENGTH ) && ( y == 0 ))
			lcd_secondline() ;
		else if(( x == LCD_DISP_LENGTH ) && ( y == 1 ))
			lcd_thirdline() ;
		else if(( x == LCD_DISP_LENGTH ) && ( y == 2 ))
			lcd_fourthline() ;
		else if(( x == LCD_DISP_LENGTH ) && ( y == 3 ))
			lcd_firstline() ;
	    lcd_data( c ) ;
		x++ ;
	}
}



void lcd_puts( const char *s )		// zobrazeni retezce znaku
{
    register char c ;

    while(( c = *s++ ))
        lcd_putc( c ) ;
}



void lcd_newline( void )		// skok na dalsi radek
{
	if( y == 3 && x == 0)
		lcd_firstline() ;
	else if( y == 0 && x == 0)
		lcd_secondline() ;
	else if( y == 1 && x == 0)
		lcd_thirdline() ;
	else if( y == 2 && x == 0)
		lcd_fourthline() ;
}



/* ======= vymazani radku displeje ======= */

void lcd_firstline( void )
{
    lcd_command(( 1<<LCD_DDRAM ) + LCD_START_LINE1 ) ;
	x = 0 ;
	y = 0 ;
	lcd_puts( "                " ) ;
    lcd_command(( 1<<LCD_DDRAM ) + LCD_START_LINE1 ) ;
	x = 0 ;
	y = 0 ;
}

void lcd_secondline( void )
{
    lcd_command(( 1<<LCD_DDRAM ) + LCD_START_LINE2 ) ;
	x = 0 ;
	y = 1 ;
	lcd_puts( "                " ) ;
    lcd_command(( 1<<LCD_DDRAM ) + LCD_START_LINE2 ) ;
	x = 0 ;
	y = 1 ;
}

void lcd_thirdline( void )
{
    lcd_command(( 1<<LCD_DDRAM ) + LCD_START_LINE3 ) ;
	x = 0 ;
	y = 2 ;
	lcd_puts( "                " ) ;
    lcd_command(( 1<<LCD_DDRAM ) + LCD_START_LINE3 ) ;
	x = 0 ;
	y = 2 ;
}

void lcd_fourthline( void )
{
    lcd_command(( 1<<LCD_DDRAM ) + LCD_START_LINE4 ) ;
	x = 0 ;
	y = 3 ;
	lcd_puts( "                " ) ;
    lcd_command(( 1<<LCD_DDRAM ) + LCD_START_LINE4 ) ;
	x = 0 ;
	y = 3 ;
}




void lcd_init( void )	// inicializace 
{
	DDRA = 0xfe ;				
    _delay_ms( 16 ) ;			
	lcd_command( 0b00100000 ) ; 
	lcd_command( 0b00101000 ) ;
	lcd_command( 0b00000100 ) ;
	lcd_command( 0b00001100 ) ;
	lcd_clrscr() ;
}
