/*
  Copyright (c) 2000 Karl-Petter Åkesson. All rights reserved.

  This software was developed by Karl-Petter Åkesson, karl-petter@home.se.

  Redistribution and use in source and binary forms, with or without 
  modification, are permitted provided that the following conditions are met:

   1.Redistributions of source code must retain the above copyright notice, 
     this list of conditions and the following disclaimer. 
   2.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. 
   3.The name of the author may not be used to endorse or promote products 
     derived from this software without specific prior written permission. 

   THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
*/

import java.util.*;
import java.io.*;
import java.lang.*;
import java.net.*;
import com.dalsemi.system.*;

/** The LCD class makes it easy to write text to an LCD(HD44780) connected 
    to the TINI, with PCE0, A19 and A3. 

    TiniOS V1.01
*/ 

public class LCD {
    private DataPort cmd=null;
    private DataPort data=null;

    private int cursor_x, cursor_y;
    private boolean scrolling=false;

    private int line[][]= new int[16][2];

    /** Constructor to initialize the LCD and clear it. 
        Current verion only support 16x2 displays, connected with PCE0, 
	A19 and A3.
    */
    public LCD() {
	cmd = new DataPort(0x00880000);
		
	data = new DataPort(0x00880008);

	try {		
	    cmd.write(0x30);
	    TINIOS.sleepProcess(1);
	    
	    cmd.write(0x30);
	    TINIOS.sleepProcess(150);
	    
	    // Do the last init.
	    cmd.write(0x30);
	    
	    // Set the functionality of the display
	    cmd.write(0x38);
	    
	    // Turn on display 
	    cmd.write(0x0c);
	    
	    // Set the mode so the cursor moves when characters written	
	    cmd.write(0x06);
	    scrolling=false;
	} catch(IllegalAddressException iae) {
	    System.exit(0);
	}
	this.clear();
    }

    /** Writes a string on the LCD at the current position. If the string does
	not fit the screen the text scrolls, one line at a time.
	@param txt The text to print on the LCD.
    */
    public void write(String txt) {
	if(data!=null) 
	    for(int i=0;i<txt.length();i++) {
		char c = txt.charAt(i);
		switch(c){
		case '\n': 
		    cursor_x=0;
		    cursor_y++;
		    if(cursor_y>1) cursor_y=0;
		    break;
		case '\b':
		    cursor_x--;
		    if(cursor_x<0) {
			cursor_x=16+cursor_x;
			cursor_y--;
			if(cursor_y<0)
			    cursor_y=1;
		    }
		    break;
		case '\f':
		    this.clear();
		    break;
		case 'å':
		    c='a';
		    break;
		case 'Å':
		    c='A';
		    break;
		case 'ä':
		    c='a';
		    break;
		case 'Ä':
		    c='A';
		    break;
		case 'ö':
		    c='o';
		    break;
		case 'Ö':
		    c='O';
		    break;
		default:
		}
		if((c!='\n')&&(c!='\f')&&(c!='\b')) {
		    if(cursor_x>15) {
			cursor_y++;
			cursor_x=0;
			if(cursor_y>1){
			    cursor_y=1;
			    try {
				cmd.write(0x01);
			    } catch(IllegalAddressException iae) {
				System.exit(0);
			    } 
			    for(int j=0;j<16;j++) {
				line[j][0]=line[j][1];
				line[j][1]=' ';
				try {
				    data.write((int)line[j][0]);
				} catch(IllegalAddressException iae) {
				    System.exit(0);
				}
			    }
			}
		    }
		    this.positionCursor(cursor_x,cursor_y);

		    try {
			data.write((int)c);
		    } catch(IllegalAddressException iae) {
			System.exit(0);
		    }
		    line[cursor_x][cursor_y]=c;
		    cursor_x++;
		}
	    }
    }
    
    /* public void scrolling(boolean scroll) {
       scrolling=scroll;
	if(cmd!=null) { 
	if(scroll)
	cmd.write(0x07);
	else
	cmd.write(0x06);
	}
	}
    */

    /** Place the cursor at the given position.
	@param x  X position
	@param y  Y position3
    */
    public void positionCursor(int x, int y) {
	cursor_x=x;
	cursor_y=y;
	
	if(cmd!=null) {
	    if(y==0) {
		if(x<0x27) {
		    try {
			cmd.write(128+x);
		    } catch(IllegalAddressException iae) {
			System.exit(0);
		    }
		} 
	    } else if(y==1) 
		if(x<0x27) { 
		    try {    
			cmd.write(128+0x40+x);
		    } catch(IllegalAddressException iae) {
			System.exit(0);
		    }
		}
	}
    }
    
    /** Moves the cursor to home postion. */
    public void home() {
	cursor_x=0;
	cursor_y=0;	

	if(cmd!=null) {
	    try {
		cmd.write(0x02);
	    } catch(IllegalAddressException iae) {
		System.exit(0);
	    } 
	}	
    }
    
    /** Clears the LCD */
    public void clear() {
	cursor_x=0;
	cursor_y=0;	
	
	if(cmd!=null) {
	    try {
		cmd.write(0x01);
	    } catch(IllegalAddressException iae) {
		System.exit(0);
	    }
	}
	
	for(int j=0;j<2;j++)
	    for(int i=0;i<16;i++)
		line[i][j]=' ';
    }
}


