Framebuffer principles, Environment setup, and example programs -- Conclusion

Source: Internet
Author: User

First, what can framebuffer do?

A: framebuffer can output something to the screen on a system without starting the graphic interface (for your own understanding). The simplest example is that on the Character interface, you can also use framebuffer to display beautiful images and neat text on the screen.

I. Principles

I do not know much about the official content. The following is my understanding of framebuffer during development. If you have any questions, please give me some advice.

Framebuffer is like a canvas. You Need To Do It Yourself to use a paint brush or paint a painting.

In my opinion, framebuffer is actually a device file, which usually exists in the/dev/directory. The name of the first frambbuffer device is fb0, that is,/dev/fb0, but someone asked, why isn't this file available in my dev directory? Don't worry. Here we will discuss how to set up the environment. After the environment is set up, we will have it.

In the eyes of programmers, framebuffer is a piece of memory, and a linear memory. We have a pointer pointing to this piece of memory, where we want to write data to where we want to write data. When the framebuffer device is turned on, we get an integer one-dimensional array. Each element corresponds to a pixel point on the screen. If our screen is 1024*768, then we should get a one-dimensional array with a length of 1024*768 = 786432. In this array, the first 1024 elements correspond to the first line of pixels on the screen, the next 1024 elements correspond to the second line of pixels on the screen, and so on .... well, if you understand the above principle, you should know how to use framebuffer. I want to display the pixel on the screen, so that I can display the pixel, we want to display Chinese characters and images without any problems. The basic principle is nothing different from the LED luminous characters on the street.

II. Environment Construction

Here, we only need four steps. I have summarized the meaning of each step by checking the information on the Internet. I will not talk about it here. Under normal circumstances, as long as you follow the steps, there will be no problems! (In ubuntu environment)

1. Open the/etc/initramfs-tools/modules file and add fbconvesafb to the end.

2. Open/etc/modprobe. d/blacklist-framebuffer.conf, find the "blacklist vesafb" line, add "#" in front of this line to comment it out.

3. Open/boot/GRUB/menu. lst, find a line of the corresponding kernel, and add the VGA parameter.

For example, if I set the resolution to 1024x768, the corresponding VGA value is 0x317. This file does not exist on my machine. It is in/boot/GRUB/grub. linux/boot/vmlinuz-2.6.22-14-generic root = UUID = 3c51a0d7-d373-473b-830e-225b6d7aafdf Ro quiet splash VGA = 0x317 modified in cfg file

4. Run the following command: sudo Update-initramfs-u update and restart. If you disable graphical interface login, you can see some beautiful character interfaces. If not, press CTRL + ALT + F1 ~ In the desktop environment ~ F6. Press CTRL + ALT + F7 to return to the desktop environment. At this time, the fb0 device should be in your/dev directory, indicating that the configuration is successful!

For more information about VGA parameter settings, see the following table:

4bit 8bit 15bit 16bit 24bit 32bit

640x400x0x300 x

640x480X0x301 0x310 0x311 0x312 x

800x600 0x302 0x303 0x313 0x314 0x315 x

1024x768x0x305 0x316 0x317 0x318 x

1280x1024x0x307 0x319 0x31a 0x31b x

1600x1200 x 0x31c 0x31d 0x31e 0x31f x

Iii. Example

The following is a program that uses framebuffer to display Chinese characters. It is not written by me and can be run after testing.

Hz. c file:

#include <stdio.h>#include <stdlib.h>#include <fcntl.h>#include <string.h>#include <linux/fb.h>#include <sys/types.h>#include <sys/stat.h>#include <sys/mman.h>#include "ziku.h"#define FB_DEV "/dev/fb0"int fb_open(char *fb_device);int fb_close(int fd);void *fb_mmap(int fd,unsigned int screensize);int fb_munmap(void *start,size_t length);int fb_16pixel(unsigned char *fbmem,int width,int height,int x,int y,unsigned short color);void char_hz(char ch,unsigned char *fbmem,unsigned int width,unsigned int height,unsigned int x1,unsigned y1);int main(int argc,char *argv[]){int fbdev;char *fb_device;unsigned char *fbmem;unsigned int screensize;unsigned int fb_width;unsigned int fb_height;unsigned int fb_depth;unsigned int x;unsigned int y;unsigned int ii;if((fb_device = getenv("FRAMEBUFFER")) == NULL){fb_device = FB_DEV;fbdev = fb_open(fb_device);}else printf("%s",fb_device);fb_width = 1024;fb_height = 768;fb_depth = 16;screensize = fb_width * fb_height *(fb_depth / 8);//fbmem = fb_mmap(fbdev,screensize);//memset (fbmem, 0, screensize);//for(ii=0;ii<7;ii++)//{char_hz(ii,fbmem,fb_width,fb_height,30+ii*16,30);}fb_munmap(fbmem,screensize);fb_close(fbdev);return (0);}void char_hz(char ch,unsigned char *fbmem,unsigned int width,unsigned int height,unsigned int x1,unsigned int y1){int i,j,k,n;unsigned char list;n = ch * 32;for(i = 0;i < 16;i++,n++){list = fontdata_16x16[n];for(j=0;j<8;j++){         if(list&(1<<(7-j))){fb_16pixel(fbmem,width,height,y1+j,x1+i, 0x1C3B);}      }n++;list = fontdata_16x16[n];for(j=0;j<8;j++){         if(list&(1<<(7-j))){fb_16pixel(fbmem,width,height,y1+8+j,x1+i,0x1C3B);}      }}}int fb_open(char *fb_device){int fd;if((fd = open(fb_device,O_RDWR)) < 0){perror(__func__);return (-1);}return (fd);}void *fb_mmap(int fd,unsigned int screensize){void*  fbmem;if((fbmem = mmap(0,screensize,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0)) == MAP_FAILED){perror(__func__);return (void *)(-1);}return (fbmem);}int fb_munmap(void *start,size_t length){return (munmap(start,length));}int fb_close(int fd){return (close(fd));}//int fb_16pixel(unsigned char *fbmem, int width,int height,int x,int y,unsigned short color){if((x > width)||(y > height))return (-1);unsigned short *dst = ((unsigned short *)fbmem + y * width + x);*dst = color;return (0);}

Ziku. h file:

# Define fontdatamax 224 const unsigned char fontdata_16x16 [fontdatamax] = {/* me */0x04,0x80, 0x0e, 0xa0, 0x78,0x90,0x08,0x90,0x08,0x84, 0xff, 0xfe, 0x08,0x80, 0x08,0x90, 0x0a, 0x90, 0x0c, 0x60,0x18,0x40,0x68, 0xa0, 0x09,0x20, 0x0a, 0x14,0x28,0x14,0x10, 0x0c,/* is */0x0f, 0xe0, 0x08,0x20, 0x08,0x20, 0x0f, 0xe0, 0x08,0x20,0x08,0x20, 0x0f, 0xe0, 0x00,0x04, 0xff, 0xfe, 0x01,0x00,0x09,0x20,0x09, 0xf0, 0x09,0x00,0x15,0x00,0x23,0x06,0x40, 0xfc, /* large */0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x04, 0xff, 0xfe, 0x0, 0 x, 0x80, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0x10, 0 0 0, 0 0 0, 0x60, 0 x, 0x00,/* Cong */0x04, 0xfe, 0xcc, 0x24, 0x50, 0x24, 0x04, 0x3d, 0xfe, 0 x, 0 x, 0 x, 0 x, 0 x, 0x04, 0x3d, 0xfc, 0x25, 0 x, 0x24, 0x20, 0x24, 0xa4, 0xfd, 0x8a, 0x06, 0x8a, 0x04,0x78,0x04,0x00, // 0x01,0x00,0x01,0x00,0x01,0x04, 0x7f, 0xfe, 0x41,0x04,0x41,0x04,0x41,0x04,0x41,0x04, 0x7f, 0xfc, 0x41,0x04,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00, // 0x00,0x04, 0x7f, 0xfe, 0x40, 0x24, 0x5f, 0xf4, 0x41,0x04,0x41,0x04,0x41,0x44, 0x4f, 0xe4, 0 x, 0x04, 0x5f, 0xf4, 0x40, 0x04, 0x7f, 0xfc, 0x40, 0x04, // 0x0, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x x 80, 0x40, 0 x, 0 x, 0 x, 0x20, 0 x, 0 x, 0x20, 0x0e, 0x40, 0x04 };

Running the above program will display the words "I am dacong Chinese" on the screen

Well, this framebuffer has been summarized. After such a long time, I hope to help new users who are studying framebuffer ~~~, You can leave a message if you do not understand it. Let's discuss it and make progress together ~~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.