#include "font_eng.h"

#define X_POS_START 0x40
#define Y_POS_START 0xb8
#define DISP_AREA1 1
#define DISP_AREA2 2
BYTE blink_blink_1;


void busy_check_g(BYTE disp_area) // lcd busy check
{
BYTE b;

b = 0xff; // dummy inst.
// b = 0xff; // dummy inst.
// b = 0xff; // dummy inst.
// b = 0xff; // dummy inst.
// b = 0xff; // dummy inst.
// b = 0xff; // dummy inst.
// b = 0xff; // dummy inst.
// b = 0xff; // dummy inst.
// b = 0xff; // dummy inst.
// b = 0xff; // dummy inst.
}

void lcd_write_inst_g(BYTE disp_area, BYTE i) // lcd instruction write
{
busy_check_g(disp_area);
if ( disp_area == DISP_AREA1 ) { GLCD_IW1 = i; }
else { GLCD_IW2 = i; }
}

void lcd_write_data_g(BYTE disp_area, BYTE i) // lcd data write
{
busy_check_g(disp_area);
if ( disp_area == DISP_AREA1 ) { GLCD_DW1 = i; }
else { GLCD_DW2 = i; }
}

void lcd_clear()
{
BYTE bbb1, bbb2;

for ( bbb1 = 0; bbb1 < 8; bbb1++ )
{
lcd_write_inst_g(DISP_AREA1, Y_POS_START + bbb1);
for ( bbb2 = 0; bbb2 < 64; bbb2++ )
{
lcd_write_inst_g(DISP_AREA1, X_POS_START + bbb2);
lcd_write_data_g(DISP_AREA1, 0x00);
}
}
for ( bbb1 = 0; bbb1 < 8; bbb1++ )
{
lcd_write_inst_g(DISP_AREA2, Y_POS_START + bbb1);
for ( bbb2 = 0; bbb2 < 64; bbb2++ )
{
lcd_write_inst_g(DISP_AREA2, X_POS_START + bbb2);
lcd_write_data_g(DISP_AREA2, 0x00);
}
}
}

void lcd_init_g() // lcd initialize
{
fnd_dig_buf2 = 0x40; // 0100 0000 , reset off, black light on
ET2 = 0;
FND_DIG = fnd_dig_buf2 | fnd_dig_buf1;
ET2 = 1;

// lcd on
lcd_write_inst_g(DISP_AREA1, 0x3f);
lcd_write_inst_g(DISP_AREA2, 0x3f);

// display position init.
lcd_write_inst_g(DISP_AREA1, 0xc0);
lcd_write_inst_g(DISP_AREA2, 0xc0);

// lcd clear
lcd_clear();
}



const BYTE hex_val[8] = { 0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80 };
const BYTE row_pos[4] = { 0, 2, 4, 6 };
const BYTE column_pos[16] = { 0, 8, 16, 24, 32, 40, 48, 56,
0, 8, 16, 24, 32, 40, 48, 56 };
const BYTE font_read_pointer[4] = { 0, 1, 16, 17 };


// 찍는 좌표는 8X16 영문기준
void disp_char(BYTE row, BYTE column, WORD disp_val)
{
BYTE b1,b2,b3,b4;
BYTE h1,h2,h3;
WORD cho,jung,jong;

BYTE font_buf1[32],font_buf2[8];
BYTE disp_font_buf[8];


row--;
column--;

b1 = disp_val;

// 글꼴 읽어오기
for ( b2 = 0; b2 < 16; b2++) { font_buf1[b2] = eng_font[b1][b2]; }

for ( b1 = 0; b1 < 2; b1++)
{
// 글꼴을 불러올 위치
b2 = font_read_pointer[b1];

for ( b3 = 0; b3 < 8; b3++) { disp_font_buf[b3] = 0x00; }

for ( b3 = 0; b3 < 8; b3++)
{
disp_font_buf[b3] = disp_font_buf[b3] | font_buf1[b2];
b2 += 2;
}

// 가로위치가 8글자를 넘으면 다음 표시영역으로 전환
if ( column > 7 ) { b4 = DISP_AREA2; }
else { b4 = DISP_AREA1; }

if ( b1 == 1 ) { b2 = 0; }
else { b2 = 1; }
lcd_write_inst_g(b4, (Y_POS_START+row_pos[row]+b2));

for ( b3 = 0; b3 < 8; b3++ )
{
lcd_write_inst_g(b4, (X_POS_START+column_pos[column]+b3));
lcd_write_data_g(b4, disp_font_buf[b3]);
}
}
}


// 여러글자 찍기
void disp_string(BYTE row, BYTE *disp_str)
{
BYTE b1,b2;
WORD w1;

b2 = 1;
while(*disp_str)
{
b1 = *disp_str++;
if ( b1 & 0x80 )
{
w1 = b1;
w1 = w1 << 8;
b1 = *disp_str++;
w1 = w1 | b1;
disp_char(row, b2, w1);
b2++;
b2++;
}
else
{
w1 = b1;
disp_char(row, b2, w1);
b2++;
}
}
}

// 실제 쓰기
lcd_init_g();
disp_string(1,"AAA");
disp_char(1, 7, 'A');