본문 바로가기

프로젝트/TFT LCD 한글 출력 (아두이노)

Hangul_SD_Selected.H 를 이용한 한글 출력

Hangul_SD_Selected.H
 
//        Hangul_SD_Selected.H    ver 1.0     아두이노용 한글 출력 클래스
//
//         제작자  :  신 옥 진
//          
//          재배포는  금지, 그냥 링크를 사용 하세요.  
//
//           영리목적(  유료 강의 자료, 판매 시스템에 포함 등 )의 사용은 사전 허락을 받아야 합니다.
////  
//   < 소스 코드 순서 >
//   #define   _TFTLCD_      myLcd              // myLcd는 원하는 이름으로 하면 됨
//   #include "c:\\Arduino\\Hangul_SD_Selected.H"       // 헤더파일 전체 경로                                        
//   HanGul_TFT  ht;                            // 한글출력 클래스
//
   
                           

#ifndef _Hangul_SD_Selected_          
#define _Hangul_SD_Selected_      
                           
#include <SPI.h>              
#include <SD.h>                
#include <Adafruit_GFX.h>                                          
#include <UTFTGLUE.h>

#ifndef _MY_COLOR_               //  Color 정의
#define _MY_COLOR_
#define cBlack        0x0000
#define cWhite        0xFFFF
#define cRed          0xF800      
#define cGreen        0x0400
#define cBlue         0x001F
#define cSilver       0xC618
#define cGray         0x8410
#define cMaroon       0x8000
#define cYellow       0xFFE0
#define cOlive        0x8400
#define cLime         0x07E0
#define cAqua         0x07FF
#define cTeal         0x0410
#define cNavy         0x0010
#define cFuchsia      0xF81F
#define cPurple       0x8010
#define cTransparent  0xFFFFFFFF
#endif

UTFTGLUE  _TFTLCD_(0x9488,A2,A1,A3,A4,A0);  
 
struct Box               // 문자열의 사각형 좌표
{
   int x,y,x2,y2;  
} ;

class HanGul_TFT
{
  private:
      unsigned short _HANGUL_FONT[16];               // 폰트 저장변수    
      File  _fFile;  
      String _fname;
      int  _color=cWhite;                  // 폰트색상
      int  _bkColor = cBlack;              // 폰트배경색      

  public:  
    Box  box;    
 
    HanGul_TFT( String fname,int pin )
    {
      SD.begin(pin);    //  uno 10 mega 53
     
      pinMode(A0, OUTPUT);                  
      digitalWrite(A0, HIGH);               //  TFT LCD 시작  
      _fname=fname;    
    }
    HanGul_TFT( String fname)
    {
      SD.begin(10);    //  uno 10 mega 53
     
      pinMode(A0, OUTPUT);                  
      digitalWrite(A0, HIGH);               //  TFT LCD 시작  
      _fname=fname;  
     }
   
    void end(){_fFile.close();   }
    void color(int _c ) ;           // 글자색
    void color(int _c ,int _bk);    // 글자색,  배경색
    void fill(int _c);              // 바로 앞에 출력한 문자열을 _c 색으로 채우기
    void fill() ;                   // 배경색으로 채우기
    void rect(int _c);              // 바로 앞에 출력한 문자열에 _c 색으로 테두리 그리기
    void rect();                    // 문자색으로 테두리 그리기
    void under(int _c);             // 바로 앞에 출력한 문자열에 문자색으로 밑줄 그리기
    void under();                   // 문자색으로 밑줄 그리기
    void print( String s, int x, int y , int mx, int my );  // x,y좌표에 가로 mx배 ,세로 my배로 문장 출력
    void print( String s,int  x, int y  );                  //  1배
    void print( String s,int  x, int y ,int m  );           //  m배
   
  private:
    void _setDot_Func( int col, int row, int x, int y,int mx,int my ,int c );  //폰트의 1bit에 해당하는 점 그리기
    void _drawFont_Func( int x, int y,  int mx , int my);    
    bool _getFont( long code );
    #ifdef _INDEX_2_
    void getFont( long code );
    #endif    
};



void HanGul_TFT::color(int _c )
{
  _color=_c;    
}

void HanGul_TFT::color(int _c ,int _bk)
{
  _color=_c;
  _bkColor=_bk;

}

void HanGul_TFT::fill(int _c)                 // 문자열을  _c색상으로 채우기
{
    _TFTLCD_.setColor( _c  );    
    _TFTLCD_.fillRect(box.x,box.y,box.x2,box.y2);    

}

void HanGul_TFT::fill() { fill(_bkColor); }   // 문자열을  배경색상으로 채우기  // 문자열 지우기에 이용

void HanGul_TFT::rect(int _c)                 // _c색상으로 문자열의 외곽선 그리기
{
    _TFTLCD_.setColor( _c );
    _TFTLCD_.drawRect(box.x,box.y,box.x2,box.y2);    
}

void HanGul_TFT::rect() { rect(_color); }       // 문자색상으로 문자열의 외곽선 그리기

void HanGul_TFT::under(int _c)                  // 문자열에 _c색상으로 밑줄
{
    _TFTLCD_.setColor( _c );
    _TFTLCD_.fillRect(box.x, box.y2-2,box.x2,box.y2+1);    
}
void HanGul_TFT::under(){ under(_color); }                               // 문자열에 문자색상으로 밑줄

void HanGul_TFT::print( String s,int  x, int y  ) {  print( s, x, y , 1 ,1 ); }

void HanGul_TFT::print( String s,int  x, int y ,int m  ) { print( s, x, y , m, m  );  }



void HanGul_TFT::_setDot_Func( int col, int row, int x, int y,int mx,int my ,int c )
{                      
    _TFTLCD_.setColor( c );  
    for(int dx=0 ; dx< mx ;dx++)      
       for( int dy=0;dy<my;dy++)  
          _TFTLCD_.drawPixel(   x+col*mx+dx   ,y+row*my+dy  );  
}

void HanGul_TFT::_drawFont_Func( int x, int y,  int mx , int my)
{                                   // 문자(1자) 그리기
   
    for( int row=0; row< 16 ; row++)    // 폰트 bit 세로방향
    {
          for(int col=0;col<16;col++)      // 폰트 bit 가로방향
          {                                    
                if( _HANGUL_FONT[row] & 0b1000000000000000 )   _setDot_Func(  col,  row, x, y, mx, my, _color);
                else      _setDot_Func(  col,  row, x, y, mx, my, _bkColor  );        
                _HANGUL_FONT[row] <<= 1;                    // bit가 1이면 점그리기 함수 호출
          }
    }
}

bool HanGul_TFT::_getFont( long code )
{    
  int idx=0;
  long hCode;

  while( _fFile.available() )
  {  
      _fFile.seek(idx);  
      _fFile.read(&hCode,4);
     
      if( hCode == code )
      {          
          _fFile.seek(idx+4);
          _fFile.read(_HANGUL_FONT,32);
          return true;
      }
      idx+=36;
  }
  return (false);
}

void HanGul_TFT::print( String s, int x, int y , int mx, int my )
{  
    box.x=x;
    box.y=y;
    box.y2=y+16*my-1;    

    _TFTLCD_.setTextSize(my*2);            
      _fFile = SD.open( _fname );
    for( int p=0; p <s.length() ;p++)
    {                                                
        if(  isAscii(s[p])  )
        {      
            if( s[p]==0x0d )break;                                        
            _TFTLCD_.setCursor(x,y);  
            _TFTLCD_.setTextColor(_color,_bkColor);                                                                                                    
            _TFTLCD_.println(s[p]);          
            if( s[p]==0x20 ) x+=8*my;
            else x+=12*my;        
        }
        else
        {    
            _TFTLCD_.setColor( _color)  ;
           
                  long code =byte(s[p]);              
                  code = code << 8;
                  code+=byte(s[p+1]);              
                  code = code << 8;
                  code+=byte(s[p+2]);  
                  _getFont( code);                  
                  _drawFont_Func( x,  y, mx, my) ;
                  x+=16*mx;
                  p+=2;
                                 
         } // isASCII else
    }// p 스트링 길이 반복  
    box.x2=x-1;
 _fFile.close();
}

#endif

 

아두이노 소스
  #define   _TFTLCD_      myLcd              // TFT LCD 이름 정의/          
#include  "D:\\myHeader\\StreamReader.H"        
#include  "D:\\myHeader\\Hangul_SD_Selected.H"    
                                    
HanGul_TFT  ht("myfont.utf");     // 아두이노 한글 폰트 Code 자동생성 프로그램 ver 2.5  추출한 폰트
                                                           파일 이름 넘겨준다.


void setup()
{
  int y=0;     
       
  StreamReader txt("my.txt");    
                            
  myLcd.InitLCD();            
  myLcd.setRotation(3);   //3 가로 480x320  //2 세로   320x480
  myLcd.clrScr();      
   
   
   while( ! txt.endf())
   {     
     ht.print( txt.readLine() ,0,y,1,2);   
   
     y=y+32;
     if(y>300 ){
        y=0;    
        myLcd.clrScr();      
     }
  }
  txt.close();
  ht.end();
}

void loop()
{
 
}