본문 바로가기
C & C++/MFC 컨트롤

[Tip] LED로(7-segment) 숫자를 표시하는 방법

by izen8 2011. 4. 14.
반응형


1. 어떤걸 할지 한번 보자.

 ① 금강산도 이쁜걸 아니깐 가는 것이다!

 

  이런걸 만들려고 하는 것이다.

  프로그래머라면 이런 생각을 할 수 있다. 단순히 텍스트 박스를 놓고 거기다가 숫자로 표현해도 되지 않느냐고 말이다.

  물론 얼마든지 가능한 일이지만, 이쁘지 않거나 뭔가 남달라 보이지 않으니깐..... 그렇다고 저게 이쁘다고 볼 수는 없겠지만...

  그나마... 단순히 우리네가 간혹 볼 수 있는 완전 없어 보이는 듯한 프로그램보다는 아주 조금은 나아 보인다는 이점이 있다.

 ② 준비물

  공짜지만... MFC 그 자체만으로는 저런 기능이 없다. 그래서 필요한 것이 MatrixStatic.h(그리고 cpp) 파일이다.

  어디에 있느냐면 www.codeproject.com 에 가면 구할 수 있다.

  찾는 번거로움을 해소시키기 위해 사이트는 다음과 같다.

  소스 제공 사이트 주소 : http://www.codeproject.com/KB/static/digidisplay.aspx

  영어에 확고한 의지와 자신감이 있다면, 절대로 아래에 나오는 이쪽 한글 설명은 안 보시길 권합니다.

  이유는 습관을 들여야지 다른 것도 직접 찾아 할 수 있기 때문이죠.

2. 간단한 설명

 ① 이 녀석의 부모는?

  여기서는 원래 사이트에 나와 있는 설명을 직역한게 아니라 그냥 이해하기 쉽게 약간 풀어서 제 맘대로 해석합니다. ㅡ_ㅡㅋ

  MatrixStatic 이라는 클래스 이름에서 유추할 수 있듯이 CStatic에서 파생된 컨트롤입니다.

  사용자는 문자를 LED 도트 매트릭스처럼 화면에 쉽게 이미지화 할 수 있습니다.

  이 컨트롤은 총 3가지의 크기로 나타낼 수 있으며 사이트에서 제공하는 예제에는 몇 가지 예제들도 함께 제공합니다. (작동 잘 됨)

 

 ② 특징은?

  1. 이미지를 기반으로 하여 문자를 쉽게 도트 매트릭스로 표현 가능

  2. 여러 줄도 가능

  3. 왼쪽, 오른쪽, 위, 아래 글씨가 다양한 속도로 움직일 수 있음

  4. CStatic에서 상속 받은 클래스이기 때문에 사용법도 쉬움

  5. 어떤 색깔이든 쉽게 바꿀 수 있음

  6. 3가지 형태로 밖에 사이즈 제공이 되지 않음

  7. CStatic 컨트롤이 가진 자동으로 사이즈 조절하는 기능을 옵션 제공

  8. 문자든 문자열이든 잘 붙음

  9. 기본 제공되는 비트맵을 바꿔도 됨...(이거 은근 귀찮은거임)

3. 사용 방법

 ① 예제 파일과 소스 파일 모두 다운 받자!

  소스 파일만 다운 받는 우를 범해서는 안된다. 왜냐하면 특징에서 이야기한 비트맵을 이용하여 문자를 표시한다.

  자세히 알아보면 문자를 넣으면 그에 해당하는 그림이 특정 비트맵 파일에서 불려져 오는 것이다.

  단순 소스만 받으면 이미지 파일이 없기 때문에 예제 파일도 함께 받아야 한다.

  아마 기본적으로 제공되는 bmp 파일이 3개가 있을 것이다.

 

 ② bmp 파일들과 MatrixStatic.h, MatrixStatic.cpp 파일을 작업중인 프로젝트 폴더에 복사한다.

  bmp 파일은 입맛대로 고쳐 쓰면 되고(그림 그리기... ㅋ), 소스는 이제부터 이걸 쓰기 위해서 필요함

  참고로 bmp 파일은 ResourceView 탭에서 Bitmap에 등록을 하면 된다. 이때 작명 센스는 알아보기 쉽게 한다.

  아래 보면 자꾸 말만해서 궁금해할까봐 친절히도 bmp 파일을 png로 변환해서 올려놓았다.

 

 

 ③ 대화상자에 넣고 싶은 위치에다가 Static 컨트롤을 하나 그려주고 위자드 클래스에서 변수를 Static으로 등록한다.

  MatrixStatic 클래스는 Static을 상속받아서 사용하기 때문에, Static 컨트롤로 일단 대화상자에 그려준다.

  그려주고 나면 작업하려는 특성에 맞는 이름을 작명해 준다. 그리고 Ctrl + W를 눌러 클래스 위자드를 열고

  두번째 탭에서 해당 컨트롤을 사용하기 위한 변수를 Static Ctrotal 형태로 등록해 준다.

  그리고 나서 다음의 절차에 의해서 상속 받은 효과를 나타내어 준다.

 

 ④ 대화상자에 대한 헤더 파일을 열어 MatrixStatic.h 를 인클루드 시켜준다. 

 대화상자가 실행되기 위해서 대화상자에 대한 Header 파일과 Cpp 파일이 존재한다.

 여기서 Header 파일을 열어보면 방금 등록한 Static 컨트롤 변수가 정의되어 있는 것을 알 수 있다.

 우선 Header 파일의 상단에 #include "MatrixStatic.h"를 추가하여 MatrixStatic를 사용할 준비를 한다.

 그리고 아래에 CStatic으로 등록되어 있는 방금 등록한 변수를 찾아서 CMatrixStatic으로 이름을 바꿔준다.

 어차피 CMatrixStatic은 Static 컨트롤을 상속받았기 때문에 헤더 파일을 선언하면 아무 탈 없이 쓸 수 있다.

 

 ⑤ MatrixStatic으로 정의된 변수 사용 준비하기

 대화상자가의 Cpp 파일에 보면 OnInitDialog라는 함수가 있다.

 이 함수는 해당 대화상자가 열릴때 해야할 일을 지정해 주는 역할을 수행한다.

 여기서 MatrixStatic 컨트롤을 어떻게 쓸지를 지정해 주면 된다.

 MatrixStatic을 몇 줄로 사용할 것인지 움직이게 할 것인지 색은 무엇으로 할 것인지 등을 정해주면 된다.

 위에 그림에 보이는 색은 변경이 가능하다는 것!

 

 ⑥ 자~ 이제 대망의 MatrixStatic에 대한 환경 설정

 MatrixStatic Header파일 열어보면 다양한 함수가 존재하는걸 알 수 있는데,

 이 중에서 우리가 알고 싶은건 어떤 크기로 어떤 색으로 몇 줄, 몇 칸으로 어떻게 정렬해서 움직이지 않게 고정해서 쓰겠다~

 뭐 이런 요구 조건을 들어주길 원한다. 그래서 그에 맞는 세팅을 해 주면 된다. 예를 들자면 다음과 같다.

 참고로 위에 있는 그림에 흰색으로 60% 설정된 공간에 적용된 예이다.

  m_rate_static.SetNumberOfLines(1);           // 1줄로 쓸래.

  m_rate_static.SetXCharsPerLine(3);           // 3칸으로 표시할께.
  m_rate_static.SetSize(CMatrixStatic::LARGE);           // 이미지는 큰걸로 쓸 것
  m_rate_static.SetDisplayColors(RGB(0, 0, 0), RGB(255, 255, 255), RGB(53, 53, 53));           // 검은 배경, 흰색글씨, 약한 회색 점들
  m_rate_static.AdjustClientXToSize(3);           // 3칸에 X 크기 맞추고
  m_rate_static.AdjustClientYToSize(1);           // 1칸에 Y 크기 맞추고
  m_rate_static.SetText(_T("  0"));           // 초기 값은 센스 있게 두칸 비워두고 0
  m_rate_static.SetAutoPadding(true);           // 알아서 채워~... ㅡ_ㅡㅋ 뭔 소린지 잘 기억 안남.

 

 ⑦ 값을 바꿔 줄 때는 어떻게?

 간단하다. 위에 마지막에서 2번재 줄인 SetText를 적용하면 된다.

  m_rate_static.SetText(_T(" 60"));           // 60으로 바뀐다~

 문자열 변수가 있다면 문자열 변수를 SetText() 함수 안에 넣어주면 된다.

//////////////////////////////////////////////////////////////////////////////////////////////////////

Introduction

This control is derived from CStatic. It is bitmap-based to make it easy for the user to alter the character set. Three sized character sets are provided as can be seen in the example above. Features are:

  • Bitmapped based for easy altering of character set.
  • Single or multilined displays.
  • Scrolling left, right, up or down at various speeds.
  • Class is derived from CStatic and is easy to use.
  • All colours can be very easily changed.
  • Three character sizes are provided
  • Optional automatic sizing of CStatic control
  • Optional auto padding of strings with a supplied character
  • Support for altering format of supplied bitmap.

Class Members

MatrixStatic::SetAutoPadding

This function pads the internal display string (set with the SetText() function) with a supplied character so that it equals the length of the value supplied in the SetXCharsPerLine() function.

Collapse | Copy Code
void SetAutoPadding(
   bool pad,
   char ch = ' ');

Parameters

pad - A C++ boolean variable. true = Strings will be automatically padded with the given character to equal the value supplied to the class in the SetXCharsPerLine() function. false = No auto padding will occur.

ch - a char variable. This parameter is optional as it defaults to a space character. This is the character used to pad the string if the above parameter is true.

Return Value

None

MatrixStatic::GetText

This function fills a supplied buffer with the text that was set in the SetText() function.

Collapse | Copy Code
int GetText(
	char *string,  
	int size);

Parameters

string - A pointer to a buffer the requested text will be placed in to.

size - An integer containing the length of the supplied buffer.

Return Value

Function returns an integer of the number of characters placed in the buffer.

MatrixStatic::SetDisplayColors

This function sets all three colours of the display characters in a single function call.

Collapse | Copy Code
void SetDisplayColors(
	COLORREF bk,  
	COLORREF on,  
	COLORREF off);

Parameters

bk - A COLORREF variable which supplies the RGB(x, x, x) value of the background colour of the display.

on - A COLORREF variable which supplies the RGB(x, x, x) value of the on colour of the display.

off - A COLORREF variable which supplies the RGB(x, x, x) value of the off colour of the display.

Return Value

None

MatrixStatic::SetBkColor

This function sets the background colour of the display.

Collapse | Copy Code
void SetBkColor(
	COLORREF bk);

Parameters

bk - A COLORREF variable which supplies the RGB(x, x, x) value of the background colour of the display.

Return Value

None

MatrixStatic::SetColor

This function sets the on and off colours of the display.

void SetColor( COLORREF off, COLORREF on);

Parameters

on - A COLORREF variable which supplies the RGB(x, x, x) value of the on colour of the display.

off - A COLORREF variable which supplies the RGB(x, x, x) value of the off colour of the display.

Return Value

None

MatrixStatic::SetBitmapResource

This function is optional. It allows for an alternate bitmap resource to be supplied. (See the supplied resource bitmaps which can be used as a template for new character formats.) There are three bitmap resources supplied that the program requires. The correct one to use is set when you call the SetSize() function. They must have the ID's of IDB_MATRIXLARGE, IDB_MATRIXSMALL and IDB_MATRIXTINY.

If this function is used to supply a new or different bitmap, even if the format is the same, you must also call the MatrixStatic::SetCustomCharSizes function to supply information about this bitmap.

Collapse | Copy Code
void SetBitmapResource(
	UINT bitmap);

Parameters

bitmap - Resource identifier of the bitmap resource.

Return Value

None.

See Also

SetSize which identifies the different size bitmaps supported.

SetCustomCharSizes() which allows different format bitmaps.

MatrixStatic::SetText

This function supplies the text that is to be displayed. Once called your supplied string can be deleted, the text is stored in a member CString variable.

Collapse | Copy Code
void SetText(
	LPCTSTR lpszText);

Parameters

lpszText - A 32-bit pointer to a constant character string

Return Value

None.

MatrixStatic::SetSize

This function identifies which of the three supported sized bitmaps was supplied in the SetBitMapResource() function.

Collapse | Copy Code
void SetSize(
	int size = SMALL);

Parameters

size - an integer variable that identifies which of the three bitmap sizes supported was given in the SetBitMapResource() function. Possible values are LARGE, SMALL & TINY, which a defined within the class as 0, 1 and 2 integers respectively.

Return Value

None.

See Also

See the three supplied bitmap templates as an example of this.

SetCustomCharSizes() which allows custom format bitmaps.

MatrixStatic::StopScroll

This function ceases the scrolling of the display.

Collapse | Copy Code
void StopScroll();

Parameters

None.

Return Value

None.

MatrixStatic::DoScroll

This function starts the display scrolling in the supplied direction. This is not a pixel scroll but a character scroll.

Collapse | Copy Code
void DoScroll(
	int speed, 
	BOOL dir);

Parameters

speed - Speed of the scrolling in milliseconds. This is the same value you would supply to the Windows SetTimer() function.

dir - Direction of scroll. Possible values are:

Single Line Display: (SetNumberOfLines was supplied a value of 1) LEFT or RIGHT, defined within the class as boolean 0 or 1 respectively.

Multiline Display: (SetNumberOfLines was supplied a value greater than 1) UP, DOWN, LEFT or RIGHT, defined within the class as 0, 1, 2 or 3 respectively.

Return Value

None.

MatrixStatic::SetCustomCharSizes

This function allows you to use custom format bitmaps. It sets the pixel sizes of each character and the vertical and horizontal spacing.

These values are automatically filled in for the three internally supported sizes of bitmaps.

Collapse | Copy Code
void SetCustomCharSizes(
	int width, 
	int height, 
	int xspace,  
	int yspace);

Parameters

width - An integer of the width of each character in pixels.

height - An integer of the height of each character in pixels.

xspace - An integer of the space between each character horizontally in pixels.

yspace - An integer of the space between each character vertically in pixels.

Return Value

None.

MatrixStatic::AdjustClientYToSize

This function allows the CStatic object to be automatically sized vertically for you to a given number of characters.

Collapse | Copy Code
void AdjustClientYToSize(
	int size);

Parameters

size - number of vertical display lines you want the object sized to.

Return Value

None.

See Also

SetNumberOfLines()

SetXCharsPerLine()

SetAutoPadding()

AdjustClientXToSize

MatrixStatic::AdjustClientXToSize

This function allows the CStatic object to be automatically sized horizontally for you a given number of characters.

Collapse | Copy Code
void AdjustClientXToSize(
	int size);

Parameters

size - number of characters you want the object sized to.

Return Value

None.

See Also

SetNumberOfLines()

SetXCharsPerLine()

SetAutoPadding()

AdjustClientYToSize()

MatrixStatic::SetNumberOfLines

This function sets the number of vertical display lines you want. It does not have to equal the length of the string divided by the MaxXChars value. This is useful for vertical scrolling. If the string is longer than the number of characters shown in a multiline display and vertical scrolling is enabled, the display will scroll through the string showing all characters. An example of this is in the project example.

Collapse | Copy Code
void SetNumberOfLines(
	int max = 0);

Parameters

max - An integer that supplies the number of lines you want on the display.

Return Value

None.

See Also

SetXCharsPerLine()

SetAutoPadding()

AdjustClientXToSize

AdjustClientYToSize

MatrixStatic::SetXCharsPerLine

This function sets the number of characters horizontally in the display. In a single line display it sets how many characters can be seen in the display at one time. On multiline displays it sets how many horizontal characters are to be used on each line.

For example: If you wanted three lines of ten characters each, a string of 30 characters would be supplied in the SetText() function. This parameter would be set to 10, and the SetNumberOfLines() function would be supplied with 3.

The difference between single and multiline is: Single line displays can have a string longer than the supplied parameter and only the number of characters supplied here will be shown. Multiline displays cannot, The next line displayed starts at the character following the number of characters supplied here.

Collapse | Copy Code
void SetXCharsPerLine(
	int max = 0);

Parameters

None.

max - An integer that supplies the number horizontal characters to display.

Return Value

None.

See Also

SetNumberOfLines()

SetAutoPadding()

AdjustClientXToSize()

AdjustClientYToSize()


반응형

댓글