/**************************************************************************************************
---------------------------------------------------------------------------------------------------
	Copyright (c) 2004, Jonathan Bagg
	All rights reserved.

	 Redistribution and use in source and binary forms, with or without modification, are permitted 
	 provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this list of 
	  conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice, this list of 
	  conditions and the following disclaimer in the documentation and/or other materials provided 
	  with the distribution.
    * Neither the name of Jonathan Bagg nor the names of its contributors may be used to 
	  endorse or promote products derived from this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR 
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
  AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
  POSSIBILITY OF SUCH DAMAGE.
---------------------------------------------------------------------------------------------------
   Project name : Infidigm AVR Drivers
   Processor	: ATMega16
   File name    : lcd.c
---------------------------------------------------------------------------------------------------
   Modifications: 

   Revision 1.0  2004/12/24 	Bagg
   - Cleaned up for release
   
---------------------------------------------------------------------------------------------------
   Created      : 17 November 2004                Author(s) : Jonathan Bagg
---------------------------------------------------------------------------------------------------
   16x2 LCD Drisplay Driver 
---------------------------------------------------------------------------------------------------
**************************************************************************************************/

#include <avr/io.h>
#include "lcd.h"
#include "tim.h"

/**************************************************************************************************
*   lcd[]; - LCD Display Buffer
**************************************************************************************************/
static unsigned char lcd[32], refresh_lcd;

/**************************************************************************************************
*   INTERNAL Function declaration - See 'lcd.h' Header file for Descriptions
**************************************************************************************************/
#define lcdata(data) 	lcdwrt(1, data) 
#define lcdcmd(cmd) 	lcdwrt(0, cmd)
static unsigned char lcdwrt (char dt, char data);		// Writes Byte to LCD dt=0 Control dt=1 data

/**************************************************************************************************
*   lcd_start(); - See 'lcd.h' Header file for Description
**************************************************************************************************/
void lcd_start(void)
{
 while(lcdcmd (0x01));			//Clear LCD, move home
 while(lcdcmd (0x02));			//Home Cursor
 while(lcdcmd (0x38));			//Set 8-bit interface
 while(lcdcmd (0x0C));			//No cursor, display visable
 while(lcdcmd (0x06));			//Incement display address
}

/**************************************************************************************************
*   lcdwrt(); - See 'lcd.h' Header file for Description
**************************************************************************************************/
unsigned char lcdwrt(char dt, char data)		// Writes Byte to LCD dt=0 Control dt=1 data
{
 static char write_timer, write_state;
 
 if (!write_state) {
	DDRC = 0xFF;
	if (dt == 1) 
		sbi(PORTB,5);  				//Data PB5=1 or Control PB5=0
	else 
		cbi(PORTB,5);
	PORTC = data;						//Send data byte (PORTB)
	write_timer = timer_get(2);		//wait 2mS before next state
	write_state++;
 }
 else if (write_state == 1 && timer_expired(write_timer)) {
	sbi(PORTB,6);	   					//Set Strobe 
	timer_reset(write_timer, 2);		//wait 2mS before next state
	write_state++;
 }
 else if (write_state == 2 && timer_expired(write_timer)) {
	cbi(PORTB,6);	   					//Clear Strobe 
	timer_reset(write_timer, 2);		//wait 2mS before next state
	write_state++;
 }
 else if (write_state == 3 && timer_expired(write_timer)) {
	DDRC = 0x00;						//Set PORTC as Input
	PORTC = 0x00;
	timer_release(write_timer);
	write_state = 0;
 }
 return(write_state);
}

/**************************************************************************************************
*   lcd_update(); - See 'lcd.h' Header file for Description
**************************************************************************************************/
void lcd_update(void)
{
 static unsigned char pos, lcdstate = IDLE;

 if (lcdstate != IDLE) {
	if (lcdstate == START) {
		if (!lcdcmd(0x80)) {						//LCD Start Position on 1st Row
			pos = 0;
			lcdstate = FIRST;
		}
	}
	else if (lcdstate == FIRST && pos == 16) {
		if (!lcdcmd(0xC0))							//LCD Start Position on 2st Row
			lcdstate = SECOND;
	}
	else if (!lcdata(lcd[pos])) {
		pos++;
		if (pos == 32)
			lcdstate = IDLE;
		if (refresh_lcd) {
			refresh_lcd = 0;
			lcdstate = START;
		}
	}
 }
 else if (refresh_lcd) {
	refresh_lcd = 0;
	lcdstate = START;
 }
}

/**************************************************************************************************
*   LCD_PRINT(); - See 'lcd.h' Header file for Description
**************************************************************************************************/
void LCD_PRINT(unsigned const char* pBuf, char pos) 
{ 
 unsigned char i=0;
 while (PRG_RDB(pBuf)!=0) { 		//Go through string until end(null)
	lcd[pos+i] = PRG_RDB(pBuf);		//Move string byte to LCD array
   	pBuf++;							//Point to next byte in string
  	i++;							//point to next LCD position
 }
 refresh_lcd = 1;
} 

/**************************************************************************************************
*   lcd_blank_buff(); - See 'lcd.h' Header file for Description
**************************************************************************************************/
void lcd_blank_buff(void)
{
 unsigned char i;
 
 for (i=0;i<32;i++)
	lcd[i] = ' ';
}

/**************************************************************************************************
*   lcd_u16(); - See 'lcd.h' Header file for Description
**************************************************************************************************/
void lcd_u16(unsigned int x, unsigned char pos)
{
 lcd[pos] = (x / 10000 + 0x30);					//Add ASCI offset for digits (0-9)-> 0x30
 x=x % 10000;
 lcd[pos+1] = (x / 1000 + 0x30);
 x=x % 1000;
 lcd[pos+2] = (x / 100 + 0x30);
 x=x % 100;
 lcd[pos+3] = (x / 10 + 0x30);
 x=x%10;
 lcd[pos+4] = (x + 0x30);
 refresh_lcd = 1;
}

