/***************************************************************************
 *   Copyright (C) 19.10.2004 by                                           *
 *   Matthias Kranz (matthias.kranz@ifi.lmu.de)                            *
 *   Paul Holleis (paul.holleis@ifi.lmu.de)                                *
 *                                                                         *
 *   particle header file for barton 96040 i2c display.                    *
 *   Last modified: 17.11.2004                                             *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 **************************************************************************/
		#use i2c(master, sda=PIN_DISPLAY_2_I2C_SDA, scl=PIN_DISPLAY_1_I2C_SCL)
/**
 * Writes a long int (length: 4 characters) to the standard display.
 * \param row the row to write to.
 * \param col the column to start writing to.
 * \param long_val the long integer to print.
 */
void bt_write_long4(int row, int col, long long_val)
{
	char str_long[5];
	sprintf(str_long, "%04li", long_val);
	bt_write_string(row, col, str_long);
}	

/**
 * Writes a long int (length: 4 characters) to a named display.
 * \param display_no the display to write to.
 * \param row the row to write to.
 * \param col the column to start writing to.
 * \param long_val the long integer to print.
 */
void bt_write_long4_display(int display_no, int row, int col, long long_val)
{
	char str_long[5];
	sprintf(str_long, "%04li", long_val);
	bt_write_string_display(display_no, row, col, str_long);
}	
 
/**
 * Prints a character array to the standard display
 * \param row the row to write to.
 * \param col the column to start writing to.
 * \param str a pointer to a character array, terminated by <b>'\\0'</b>
 */
void bt_write_string(int row, int col, char * str)
{
	int strpos;
	strpos = 0;
	
	// change commented lines against for loop...
	for (; *str != '\0'; str++)
	//while (*str != '\0')
	{		
		bt_write_char(row, (col + (strpos * BT_CHARACTER_PIXEL_WIDTH)), *str);
		strpos++;
		//str++;
	}	
}

/**
 * Prints a character array to a named display
 * \param display_no the display to write to.
 * \param row the row to write to.
 * \param col the column to start writing to.
 * \param str a pointer to a character array, terminated by <b>'\\0'</b>
 */
void bt_write_string_display(int display_no, int row, int col, char * str)
{
	int strpos;
	strpos = 0;
	
	// change commented lines against for loop...
	for (; *str != '\0'; str++)
	//while (*str != '\0')
	{		
		bt_write_char_display(display_no, row, (col + (strpos * BT_CHARACTER_PIXEL_WIDTH)), *str);
		strpos++;
		//str++;
	}
}

/**
 * Writes the string "HCILAB.ORG" in <em>row</em> at character column 
 * <em>col</em>. Attention: Character position is <b>BT_CHARACTER_PIXEL_WIDTH</b>*col. 
 * So the starting position is defined in characters, not pixels.
 * \param display_no the display to write to.
 * \param row the row to write to.
 * \param col the column to start writing to.
 */
void bt_write_hcilab_display(int display_no, int row, int col)
{
	char hcilab[11] = "HCILAB.ORG";
	bt_write_string_display(display_no, row, col, hcilab);
	/*
	int cur_pos = 0;
	cur_pos = BT_CHARACTER_PIXEL_WIDTH * col;
	bt_write_char_display(display_no, row,cur_pos, 'H'); cur_pos=cur_pos+BT_CHARACTER_PIXEL_WIDTH;
	bt_write_char_display(display_no, row,cur_pos,'C'); cur_pos=cur_pos+BT_CHARACTER_PIXEL_WIDTH;
	bt_write_char_display(display_no, row,cur_pos,'I'); cur_pos=cur_pos+BT_CHARACTER_PIXEL_WIDTH;
	bt_write_char_display(display_no, row,cur_pos,'L'); cur_pos=cur_pos+BT_CHARACTER_PIXEL_WIDTH;
	bt_write_char_display(display_no, row,cur_pos,'A'); cur_pos=cur_pos+BT_CHARACTER_PIXEL_WIDTH;
	bt_write_char_display(display_no, row,cur_pos,'B'); cur_pos=cur_pos+BT_CHARACTER_PIXEL_WIDTH;
	bt_write_char_display(display_no, row,cur_pos,'.'); cur_pos=cur_pos+BT_CHARACTER_PIXEL_WIDTH;
	bt_write_char_display(display_no, row,cur_pos,'O'); cur_pos=cur_pos+BT_CHARACTER_PIXEL_WIDTH;
	bt_write_char_display(display_no, row,cur_pos,'R'); cur_pos=cur_pos+BT_CHARACTER_PIXEL_WIDTH;
	bt_write_char_display(display_no, row,cur_pos,'G');
	*/
}

/**
 * Writes a line of the character ch to a named display.
 * \param display_no the display to write to.
 * \param row the row to write to.
 * \param ch the character to be written.
 */
void bt_write_line_display(int display_no, int row, int ch)
{
	int i;
	if (display_no == 0) {
		#use i2c(master, sda=PIN_DISPLAY_1_I2C_SDA, scl=PIN_DISPLAY_1_I2C_SCL)

		i2c_start();
  		i2c_write(0x7A);			// send address 0x7A
		i2c_write(0b01100000+row);
		i2c_write(0b00000000);
		for(i=0;i<100;i++){
			i2c_write(ch);
		}
		i2c_stop();		// end transfer
	}

	if (display_no == 1) {
		#use i2c(master, sda=PIN_DISPLAY_2_I2C_SDA, scl=PIN_DISPLAY_1_I2C_SCL)
		
		i2c_start();
  		i2c_write(0x7A);			// send address 0x7A
		i2c_write(0b01100000+row);
		i2c_write(0b00000000);
		for(i=0;i<100;i++){
			i2c_write(ch);
		}
		i2c_stop();		// end transfer
	}

	if (display_no == 2) {
		#use i2c(master, sda=PIN_B2, scl=PIN_DISPLAY_1_I2C_SCL)
		
		i2c_start();
  		i2c_write(0x7A);			// send address 0x7A
		i2c_write(0b01100000+row);
		i2c_write(0b00000000);
		for(i=0;i<100;i++){
			i2c_write(ch);
		}
		i2c_stop();		// end transfer
	}

	if (display_no == 3) {
		#use i2c(master, sda=PIN_B3, scl=PIN_DISPLAY_1_I2C_SCL)
		
		i2c_start();
  		i2c_write(0x7A);			// send address 0x7A
		i2c_write(0b01100000+row);
		i2c_write(0b00000000);
		for(i=0;i<100;i++){
			i2c_write(ch);
		}
		i2c_stop();		// end transfer
	}

	if (display_no == 4) {
		#use i2c(master, sda=PIN_B4, scl=PIN_DISPLAY_1_I2C_SCL)
		
		i2c_start();
  		i2c_write(0x7A);			// send address 0x7A
		i2c_write(0b01100000+row);
		i2c_write(0b00000000);
		for(i=0;i<100;i++){
			i2c_write(ch);
		}
		i2c_stop();		// end transfer
	}

	if (display_no == 5) {
		#use i2c(master, sda=PIN_B5, scl=PIN_DISPLAY_1_I2C_SCL)
		
		i2c_start();
  		i2c_write(0x7A);			// send address 0x7A
		i2c_write(0b01100000+row);
		i2c_write(0b00000000);
		for(i=0;i<100;i++){
			i2c_write(ch);
		}
		i2c_stop();		// end transfer
	}

}

/**
 * Writes a char to a named display.
 * \param display_no the display to write to.
 * \param row the row to write to.
 * \param col the column to start writing to.
 * \param ch the character to be written.
 */
// extract ascii from tables & write to LCD
void bt_write_char_display(int display_no, int row, int col, BYTE ch)	
{
	int tab_index;
	BYTE bt_char_bit_set;
	//int ch_pix_set;
	int i;
		
	if (ch<0x20)return;
	if (ch>0x7f)return;

	for (i=0;i<5;i++) {
		if (ch<0x50){
			tab_index=(((ch&0xff)-0x20)*5);
			bt_char_bit_set=table1[(tab_index+i)];
		} else if (ch>0x4f){
			tab_index=(((ch&0xff)-0x50)*5);
			tab_index=(((ch&0xff)-0x50)*5);
			tab_index=(((ch&0xff)-0x50)*5);
			bt_char_bit_set=table2[(tab_index+i)];
		}
		bt_write_pix_set_display(display_no, row, col+i, bt_char_bit_set,1);		
	}	
	bt_write_pix_set_display(display_no, row, col+5, 0x00,1);		
	
}

/**
 * ???
 * \param ch ???
 * \param count ???
 * \param display_no the display to write to.
 * \param row the row to write to.
 * \param col the column to start writing to.
 */
void bt_write_pix_set_display(int display_no, int row, int col, int ch, int count)
{
	int i;

	if (display_no == 0) {
		#use i2c(master, sda=PIN_DISPLAY_1_I2C_SDA, scl=PIN_DISPLAY_1_I2C_SCL)
		
		i2c_start();
  		i2c_write(0x7A);			// send address 0x7A
		i2c_write(0b01100000+row);
		i2c_write(0b00000000+col);
		for(i=0;i<count;i++){
			i2c_write(ch);
		}
		i2c_stop();		// end transfer
	}

	if (display_no == 1) {
		#use i2c(master, sda=PIN_DISPLAY_2_I2C_SDA, scl=PIN_DISPLAY_1_I2C_SCL)
		

		i2c_start();
  		i2c_write(0x7A);			// send address 0x7A
		i2c_write(0b01100000+row);
		i2c_write(0b00000000+col);
		for(i=0;i<count;i++){
			i2c_write(ch);
		}
		i2c_stop();		// end transfer
	}

	if (display_no == 2) {
		#use i2c(master, sda=PIN_DISPLAY_3_I2C_SDA, scl=PIN_DISPLAY_1_I2C_SCL)
		//#use i2c(master, sda=PIN_B2, scl=PIN_DISPLAY_1_I2C_SCL)
		

		i2c_start();
  		i2c_write(0x7A);			// send address 0x7A
		i2c_write(0b01100000+row);
		i2c_write(0b00000000+col);
		for(i=0;i<count;i++){
			i2c_write(ch);
		}
		i2c_stop();		// end transfer
	}

	if (display_no == 3) {
		#use i2c(master, sda=PIN_DISPLAY_4_I2C_SDA, scl=PIN_DISPLAY_1_I2C_SCL)
		//#use i2c(master, sda=PIN_B3, scl=PIN_DISPLAY_1_I2C_SCL)
		

		i2c_start();
  		i2c_write(0x7A);			// send address 0x7A
		i2c_write(0b01100000+row);
		i2c_write(0b00000000+col);
		for(i=0;i<count;i++){
			i2c_write(ch);
		}
		i2c_stop();		// end transfer
	}

	if (display_no == 4) {
		#use i2c(master, sda=PIN_DISPLAY_5_I2C_SDA, scl=PIN_DISPLAY_1_I2C_SCL)
		//#use i2c(master, sda=PIN_B4, scl=PIN_DISPLAY_1_I2C_SCL)
		

		i2c_start();
  		i2c_write(0x7A);			// send address 0x7A
		i2c_write(0b01100000+row);
		i2c_write(0b00000000+col);
		for(i=0;i<count;i++){
			i2c_write(ch);
		}
		i2c_stop();		// end transfer
	}

	if (display_no == 5) {
		#use i2c(master, sda=PIN_DISPLAY_6_I2C_SDA, scl=PIN_DISPLAY_1_I2C_SCL)
		//#use i2c(master, sda=PIN_B5, scl=PIN_DISPLAY_1_I2C_SCL)
		

		i2c_start();
  		i2c_write(0x7A);			// send address 0x7A
		i2c_write(0b01100000+row);
		i2c_write(0b00000000+col);
		for(i=0;i<count;i++){
			i2c_write(ch);
		}
		i2c_stop();		// end transfer
	}


}

/**
 * Clears the display number <em>display_no</em>.
 * \param display_no the display to clear to.
 */
void bt_clear_display(int display_no)
{
	bt_write_line_display(display_no, 0x00,0x00);
	bt_write_line_display(display_no, 0x01,0x00);
	bt_write_line_display(display_no, 0x02,0x00);
	bt_write_line_display(display_no, 0x03,0x00);
	bt_write_line_display(display_no, 0x04,0x00);
}


/**
 * Clears the standard display.
 */
void bt_clear()//int display_no)
{
	bt_write_line(0x00,0x00);
	bt_write_line(0x01,0x00);
	bt_write_line(0x02,0x00);
	bt_write_line(0x03,0x00);
	bt_write_line(0x04,0x00);
}

/**
 * Writes a line of the character ch to the standard display. 
 * The line starts in <em>row</em>
 * and writes <em>ch</em> rows (binary representation of the
 * int (ch), 1 = row 1, 2 = row 2, 3 = row 1 & 2, 4 = row 3, ...)
 * \param row the row to write to.
 * \param ch the caracter to be written.
 */
void bt_write_line(int row, int ch)
{
	int i;
	i2c_start();
	// send address 0x7A
  	i2c_write(0x7A);			
	i2c_write(0b01100000+row);
	i2c_write(0b00000000);
	for(i=0;i<100;i++)
	{
		i2c_write(ch);
	}
	// end transfer
	i2c_stop();		
}

/**
 * ???
 * \param count ???
 * \param ch ???
 * \param row the row to write to.
 * \param col the column to start writing to.
 */
void write_pix_set(int row, int col, int ch, int count)
{
	int i;
	i2c_start();
	// send address 0x7A
  	i2c_write(0x7A);			
	i2c_write(0b01100000+row);
	i2c_write(0b00000000+col);
	for(i=0;i<count;i++)
	{
		i2c_write(ch);
	}
	// end transfer
	i2c_stop();		
}
/**
 * Deprecated. Use <em>bt_write_char</em> instead.
 * \param row the row to write to.
 * \param col the column to start writing to.
 * \param ch character to be written.
 */
void bt_print_char_lcd(int row, int col, BYTE ch)
{
	bt_write_char(row, col, ch);	
}

/**
 * Extracts ascii from tables & write to the standard display.
 * \param row the row to write to.
 * \param col the column to start writing to.
 * \param ch character to be written.
 */
void bt_write_char(int row, int col, BYTE ch)	
{
	int tab_index;
	BYTE bt_char_bit_set;
	//int ch_pix_set;
	int i;
		
	if (ch<0x20)return;
	if (ch>0x7f)return;

	for (i=0;i<5;i++) 
	{
		if (ch<0x50)
		{
			tab_index=(((ch&0xff)-0x20)*5);
			bt_char_bit_set=table1[(tab_index+i)];
		} 
		else if (ch>0x4f)
		{
			tab_index=(((ch&0xff)-0x50)*5);
			tab_index=(((ch&0xff)-0x50)*5);
			tab_index=(((ch&0xff)-0x50)*5);
			bt_char_bit_set=table2[(tab_index+i)];
		}
		write_pix_set(row, col+i, bt_char_bit_set,1);		
	}	
	write_pix_set(row, col+5, 0x00,1);			
}

