#include <unistd.h> #include <stdlib.h>
#include <fcntl.h> #include <linux/fb.h> #include <sys/mman.h> int main() { int fbfd = 0; struct fb_var_screeninfo vinfo; struct fb_fix_screeninfo finfo; long int screensize = 0; char *fbp = 0; int x = 0, y = 0; long int location = 0;
// Open the file for reading and writing
fbfd = open("/dev/fb0", O_RDWR); if (!fbfd) { printf("Error: cannot open framebuffer device.\n"); exit(1); } printf("The framebuffer device was opened successfully.\n");
// Get fixed screen information
//FBIOGET_FSCREENINFO获得固定的屏幕参数设置
if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo)) { printf("Error reading fixed information.\n"); exit(2); }
printf("%d, %d, %d, %d\n", finfo.smem_start, finfo.smem_len, finfo.mmio_start,finfo.mmio_len); //869007360, 153600, 0, 0
// Get variable screen information
//FBIOGET_VSCREENINFO 获得可变的屏幕参数
if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo)) { printf("Error reading variable information.\n"); exit(3); }
printf("%dx%d, %dbpp\n", vinfo.xres, vinfo.yres, vinfo.bits_per_pixel); //320X240,16bpp bpp 每像素位数 每个点用多少个字节表示
// Figure out the size of the screen in bytes
screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8; //2^16=64K TFT 320*240*16/8=153600byte
// Map the device to memory
/*void *mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);mmap函数是unix/linux下的系统调用 fbmem.c是内核的,应用程序调用了mmap进入内核空间就是执行的fb_mmap了, 应用程序是统一的接口,但是在内核空间,不同的驱动使用的实现函数是不一样的*/ //#include <unistd.h> and #include <sys/mman.h> /usr/include/sys/mman.h
fbp = (char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, fbfd, 0); if ((int)fbp == -1) { printf("Error: failed to map framebuffer device to memory.\n"); exit(4); } printf("The framebuffer device was mapped to memory successfully.\n"); #if 1 x = 10; y = 10; // Where we are going to put the pixel
// Figure out where in memory to put the pixel
for (y = 10; y < 20; y++) for (x = 10; x < 30; x++) { location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) + (y+vinfo.yoffset) * finfo.line_length;
if (vinfo.bits_per_pixel == 32) { *(fbp + location) = 10; // Some blue
*(fbp + location + 1) = 15+(x-10)/2; // A little green
*(fbp + location + 2) = 20-(y-10)/5; // A lot of red
*(fbp + location + 3) = 0; // No transparency
} else { //assume 16bpp
unsigned short b = 10; unsigned short g = (x-10)/6; // A little green
unsigned short r = 31-(y-10)/16; // A lot of red
unsigned short t = r<<11 | g << 5 | b; *((unsigned short *)(fbp + location)) = t; // printf("x=%d,%d\n",x,y);
} } #endif munmap(fbp, screensize);//删除特定地址区域的对象映射 int munmap(void *start, size_t length);
printf("The framebuffer device was munmapped to memory successfully.\n"); close(fbfd); printf("The framebuffer device was closed successfully.\n"); return 0; }
|