[LCD application programming] Obtain LCD screen parameter information (resolution, pixel depth, RGB format)

Directory

1. The basic principle of LCD display

1. Know FrameBuffer

2. Understand the resolution and depth of LCD

2. Interface function ioctl

1. Function declaration

2. Structure introduction

3. Obtain the information of the LCD screen (resolution, depth)


1. The basic principle of LCD display

1. Know FrameBuffer

FrameBuffer is a frame buffer, which can be regarded as a piece of memory. Frame buffer is a display driver interface, which shields the implementation of the hardware level of display devices (such as LCD). From the perspective of the application layer, the display device is a piece of memory, and operating this piece of memory is equivalent to operating the display device.

In the Linux system, the display device is also called FrameBuffer device, LCD is FrameBuffer device, and the device file corresponding to FrameBuffer device is /dev/fdX (X=0, 1, 2 …), generally /dev/fb0 represents LCD display . Application reading and writing /dev/fbX is equivalent to reading and writing the video memory (display buffer) of the display device

2. Understand the resolution and depth of LCD

When it comes to display devices or pictures, we often refer to the concepts of resolution and pixel depth

  • Resolution: how many pixels are there in a row, how many pixels are in a column
  • Pixel depth: how many bits are used to represent each pixel

Suppose the resolution of LCD is 800*480, each pixel is represented by RGB565.

800*480 Each line has 800 Pixels, 480 pixels per column
RGB565

Each pixel is represented by R, G, B three colors

– R occupies 5 bits

– G occupies 6 bits

– B occupies 5 bits

As mentioned earlier, the display device can actually be regarded as a display buffer, and the size of the display buffer = resolution * pixel depth

2. Interface function ioctl

1. Function declaration

The purpose of ioctl will change according to the parameters passed in. The ioctl function is declared as follows:

The first parameter is the file descriptor, which is the device file corresponding to the FrameBuffer device /dev/fdX (X=0, 1, 2 …)

The second parameter needs to pass in a macro, and the second parameter determines the purpose of the ioctl function

The second parameter Meaning
FBIOGET_VSCREENINFO Get the variable parameter information of the FrameBuffer device
FBIOPUT_VSCREENINFO Set the variable parameter information of the FrameBuffer device
FBIOGET_FSCREENINFO Get the inherent parameter information of the FrameBuffer device

The type passed in by the third parameter will change with the second parameter.

The second parameter The third parameter type
FBIOGET_VSCREENINFO struct fb_var_screeninfo *
FBIOPUT_VSCREENINFO struct fb_var_screeninfo *
FBIOGET_FSCREENINFO struct fb_fix_screeninfo *
// Get device variable parameter information
struct fb_var_screeninfo fb_var;
int fd = 0;
if((fd = open("/dev/fb0", O_RDWR) < 0))
{
    perror("open fb failed");
    exit(-1);
}

ioctl(fd, FBIOGET_VSCREENINFO, &fb_var);

2. Structure introduction

The third parameter above involves a variety of structures struct fb_var_screeninfo and struct fb_fix_screeninfo. If we want to obtain information about variable parameters, we must know the member variables contained in the corresponding structure.

struct fb_var_screeninfo

struct fb_var_screeninfo {
     __u32 xres; /* Visual area, how many pixels are there in one line, X resolution */
     __u32 yres; /* Visual area, how many pixels are there in a column, Y resolution */

     __u32 bits_per_pixel; /* How many bits are used to describe each pixel, that is, the pixel depth bpp */
     __u32 grayscale; /* =0 means color, =1 means grayscale, >1 means FOURCC color */
     /* Used to describe how many bits are used to represent the three color components of R, G, and B and their respective offsets */
     struct fb_bitfield red; /* Red color component gamut offset */
     struct fb_bitfield green; /* Green color component gamut offset */
     struct fb_bitfield blue; /* Blue color component gamut offset */
     struct fb_bitfield transp; /* transparency component color gamut offset */
    
    // ... ...
};

The declaration of the struct fb_bitfield structure is as follows. The following “offset” can refer to the result analysis in the last part of this article

struct fb_bitfield {
     __u32 offset; /* offset */
     __u32 length; /* length */
     __u32 msb_right; /* != 0 : Most significant bit is right */
} 

struct fb_fix_screeninfo

struct fb_fix_screeninfo {
     char id[16]; /* identifier in string form */
     unsigned long smem_start; /* The starting address of video memory (physical address) */
     __u32 smem_len; /* length of video memory */
     __u32 type;
     __u32 type_aux;
     __u32 visual;
     __u16 xpanstep;
     __u16 ypanstep;
     __u16 ywrapstep;
     __u32 line_length; /* The number of bytes in a line */
     unsigned long mmio_start; /* Start of Memory Mapped I/O(physical address) */
     __u32 mmio_len; /* Length of Memory Mapped I/O */
     __u32 accel; /* Indicate to driver which specific chip/card we have */
     __u16 capabilities;
     __u16 reserved[2];
};

3. Get LCD screen information (resolution, depth)

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/fb.h>

int main(int args, char **argv)
{
    struct fb_fix_screeninfo fb_fix; // fixed parameter information
    struct fb_var_screeninfo fb_var; // variable parameter information

    int fd = open("/dev/fb0", O_RDWR);
    if (fd < 0)
    {
        perror("open fb failed");
        return -1;
    }

    ioctl(fd, FBIOGET_FSCREENINFO, & amp;fb_fix); // Get fixed parameter information
    ioctl(fd, FBIOGET_VSCREENINFO, & amp;fb_var); // Get variable parameter information
    printf(
        "Resolution: %d * %d \
" \
        "Pixel depth: %d bit \
" \
        "Pixel format: R<%d %d> G<%d %d> B<%d %d>\
", \
        "Size per line: %d bytes \
", \
        fb_var.xres, fb_var.yres, \
        fb_var.bits_per_pixel, \
        fb_var.red.offset, fb_var.red.length, \
        fb_var.green.offset, fb_var.green.length, \
        fb_var.blue.offset, fb_var.blue.length, \
        fb_fix.line_length
    );

    close(fd);
    return 0;
}

Resolution: 800 pixels per row, 480 pixels per column

Pixel depth: use 16bit to represent each pixel

Pixel format: The format used is RGB565, red (R) occupies 5 bits, green (G) occupies 6 bits, and blue (B) occupies 5 bits

Number of bytes per row = number of pixels in a row * pixel depth = 800 * 16 = 12800 bit = 1600 bytes