unit MyTwiLcd;

// This is the address to change if using a different I2C interface
const 
  LCD_ADDR = 0x7E;

  procedure MyLCD_Init(row_count: byte; column_count: byte);
  procedure MyLCD_Out(row, column: byte; var txt: array[21] of char);
  procedure MyLCD_Out_Cp(var txt: array[21] of char);
  procedure MyLCD_Chr(row, column, out_char: byte);
  procedure MyLCD_Chr_Cp(out_char: byte);
  procedure MyLCD_Cmd(out_char: byte);

  procedure MyLCD_Clear;
  procedure MyLCD_Clearline(row: byte);

implementation

var
  rows: byte;
  columns: byte;
  
// Initialize LCD module
procedure MyLCD_Init(row_count: byte; column_count: byte);
var
  rs: byte;
begin
  rows := row_count;
  columns := column_count;

  TWI_Init(100000);
  Delay_ms(200);

  MyLCD_Cmd(_LCD_CURSOR_OFF);
  MyLCD_Cmd(_LCD_CLEAR);

  Delay_ms(150);

  TWI_Start();
  TWI_Write(LCD_ADDR);

  TWI_Write(0x30 or rs or 0x04 or 0x08);
  Delay_us(50);
  TWI_Write(0x30 or rs or 0x00 or 0x08);
  Delay_ms(30);

  TWI_Write(0x30 or rs or 0x04 or 0x08);
  Delay_us(50);
  TWI_Write(0x30 or rs or 0x00 or 0x08);
  Delay_ms(30);

  TWI_Write(0x30 or rs or 0x04 or 0x08);
  Delay_us(50);
  TWI_Write(0x30 or rs or 0x00 or 0x08);
  Delay_ms(30);

  TWI_Write(0x20 or rs or 0x04 or 0x08);
  Delay_us(50);
  TWI_Write(0x20 or rs or 0x00 or 0x08);
  TWI_Stop();

  Delay_ms(20);

  MyLCD_Cmd(0x28);
  MyLCD_Cmd(0x06);
end;

// Prints text on LCD starting from specified position
procedure MyLCD_Out(row, column: byte; var txt: array[21] of char);
var
  i: byte;
begin
  if row > rows then
    Exit;
  if column > columns then
    Exit;
  i := 0;
  while(txt[i]) do
    begin
      MyLCD_Chr(row, column,txt[i]);
      i := i + 1;
      column := column + 1;
    end;
end;

// Prints text on LCD at current cursor position
procedure MyLCD_Out_Cp(var txt: array[21] of char);
var
  i: byte;
begin
  i := 0;
  while(txt[i]) do
    begin
      MyLCD_Chr_Cp(txt[i]);
      i := i + 1;
    end;
end;

// Prints character on Lcd at specified position
procedure MyLCD_Chr(row, column, out_char: byte);
var
  hi_n, lo_n:byte;
  rs:byte;
begin
  rs := 1;
  column := column - 1;
  case row of
    1: MyLCD_Cmd(0x80 + column);
    2: MyLCD_Cmd(0xC0 + column);
    3: MyLCD_Cmd(0x94 + column);
    4: MyLCD_Cmd(0xD4 + column);
  end;

  hi_n := out_char and 0xF0;
  lo_n := (out_char shl 4) and 0xF0;

  TWI_Start();
  TWI_Write(LCD_ADDR);
  TWI_Write(hi_n or rs or 0x04 or 0x08);
  Delay_us(50);
  TWI_Write(hi_n or rs or 0x00 or 0x08);
  Delay_us(100);
  TWI_Write(lo_n or rs or 0x04 or 0x08);
  Delay_us(50);
  TWI_Write(lo_n or rs or 0x00 or 0x08);
  TWI_stop();
end;

// Print character on LCD at current cursor position
procedure MyLCD_Chr_Cp(out_char: byte);
var
  hi_n, lo_n:byte;
  rs:byte;
begin
  rs := 0x01;

  hi_n := out_char and 0xF0;
  lo_n := (out_char shl 4) and 0xF0;

  TWI_Start();
  TWI_Write(LCD_ADDR);
  TWI_Write(hi_n or rs or 0x04 or 0x08);
  Delay_us(50);
  TWI_Write(hi_n or rs or 0x00 or 0x08);
  Delay_us(100);
  TWI_Write(lo_n or rs or 0x04 or 0x08);
  Delay_us(50);
  TWI_Write(lo_n or rs or 0x00 or 0x08);
  TWI_stop();
end;

// Sends command to LCD
procedure MyLCD_Cmd(out_char: byte);
var
  hi_n, lo_n:byte;
  rs:byte;
begin
  rs := 0;

  hi_n := out_char and 0xF0;
  lo_n := (out_char shl 4) and 0xF0;

  TWI_Start();
  TWI_Write(LCD_ADDR);
  TWI_Write(hi_n or rs or 0x04 or 0x08);
  Delay_us(50);
  TWI_Write(hi_n or rs or 0x00 or 0x08);
  Delay_us(100);
  TWI_Write(lo_n or rs or 0x04 or 0x08);
  Delay_us(50);
  TWI_Write(lo_n or rs or 0x00 or 0x08);
  TWI_stop();

  if(out_char = 0x01) then
    begin
      Delay_ms(2);
    end;
end;

procedure MyLCD_Clear;
begin
  MyLCD_Cmd(_LCD_CLEAR);
end;

procedure MyLCD_Clearline(row: byte);
begin
  MyLCD_Out(1, 1, '                    ');
end;


end.