Wei Dongshan Embedded Introductory Notes-Application Development Fundamentals (6)

Chapter 6 Text Display

6.2 Display of dot matrix characters

6.2.1 Display of ASCII characters

1. The principle of dot matrix

To display an ASCII character in the LCD, that is, English letters, the first step is to find the dot matrix corresponding to the character. There is this file in the Linux kernel source code: lib\fonts\font_8x16.c, which stores the dot matrix of each character in the form of an array, such as:

The array fontdata_8x16 records these dots.
Insert picture description here
Each character of the dot matrix occupies 16 bytes, corresponding to 16 rows of the dot matrix. Each bit in each row of the dot matrix corresponds to the pixel at the corresponding position in the LCD, and "0" means Off, "1" means lit, for example, the figure below is a dot matrix of'A':
Insert picture description here
As shown in the figure, the bit4 of the third row is 1, the pixels there are lit, and all the pixels lit on the LCD Combine them to show the appearance of the characters.

So when you want to display a character, find its dot matrix in the fontdata_8x16 array according to its ASCII code, and then take out these 16 bytes to draw 16 rows of pixels.

Two, source code analysis

The core function is void lcd_put_ascii(int x, int y, unsigned char c) , which displays the character c at the bottom right of the (x, y) position of the LCD. The code is as shown in the figure below:
Insert picture description here
1. Get the dot matrix.
For character c, it The method of obtaining the dot matrix is ​​as follows:

unsigned char *dots = (unsigned char *)&fontdata_8x16[c*16];

In the fontdata_8x16 array, a character occupies 16 bytes, so fontdata_8x16[c*16] is the starting byte of this character, and then the starting address is assigned to the pointer dots to facilitate subsequent calls.

2. Draw points

for (i = 0; i < 16; i++)
{
    
    
	byte = dots[i];
	for (b = 7; b >= 0; b--)
	{
    
    
		if (byte & (1<<b))
		{
    
    
			/* show */
			lcd_put_pixel(x+7-b, y+i, 0xffffff); /* 白 */
		}
		else
		{
    
    
			/* hide */
			lcd_put_pixel(x+7-b, y+i, 0); /* 黑 */
		}
	}
}

(1) Because there are sixteen rows, there must be a large loop that loops 16 times first, and then there are 8 bits in each row, so each large loop also needs a small loop that loops 8 times.

(2) In the small loop, if the bit is 1, it is filled with white, and if the bit is 0, it is filled with black. In this way, English letters with black background and white outlines can be displayed.

(3) To judge whether a certain bit is 0 or 1, you need to extract the information of that bit through phase and. The following sentences are used in the source code to judge:

if (byte & (1<<b))

For example, b=2, then extract bit2: and the dot matrix with 00000100

(4) To display the color, use the previously obtained drawing point function lcd_put_pixel, the upper left corner of a dot matrix is ​​(x, y), then the position of the b position in the i-th row is (x+7-b, y+i)

3. Main function In the
main function, first open the LCD device, obtain the Framebuffer parameters, and implement the lcd_put_pixel function; then call lcd_put_ascii to draw characters. code show as below:

int main(int argc, char **argv)
{
    
    
	fd_fb = open("/dev/fb0", O_RDWR); 
	if (fd_fb < 0)
	{
    
    
		printf("can't open /dev/fb0\n"); 
		return -1;
	}
	if (ioctl(fd_fb, FBIOGET_VSCREENINFO, &var))
	{
    
    
		printf("can't get var\n"); 
		return -1;
	}

	line_width = var.xres * var.bits_per_pixel / 8; 
	pixel_width = var.bits_per_pixel / 8;
	screen_size = var.xres * var.yres * var.bits_per_pixel / 8;
	fbmem = (unsigned char *)mmap(NULL , screen_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd_fb, 0); 
	if (fbmem == (unsigned char *)-1)
	{
    
    
		printf("can't mmap\n"); 
		return -1;
	}

	/* 清 屏 : 全 部 设 为 黑 色 */ 
	memset(fbmem, 0, screen_size);
	lcd_put_ascii(var.xres/2, var.yres/2, 'A'); /*在屏幕中间显示8*16的字母A*/ 
	munmap(fbmem , screen_size);
	close(fd_fb);
	return 0;
}

4. Compile the c file show_ascii.c
compilation command: arm-linux-gnueabihf-gcc -o show_ascii show_ascii.c
Note: The prefix of the compilation tool may be different for different boards.

5. Experiment on the computer
Put the show_ascii program on the board and execute the command: ./show_ascii
If the experiment is successful, we will see a white letter'A' displayed in the middle of the screen.

6. Homework after class
Modify the lcd_put_ascii function to specify the character color.
Realize the lcd_put_str function, output the string, you can wrap.
Realize the display of Chinese characters on the basis of show_ascii.c: find the Chinese character library, understand the pixel arrangement order, and get the Chinese character code.

Guess you like

Origin blog.csdn.net/San_a_fish_of_dream/article/details/113829587