显示头文件如下:
#ifndef _DISPLAYFB_H_ #define _DISPLAYFB_H_ extern "C" { #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <errno.h> #include <sys/ioctl.h> #include <sys/stat.h> #include <sys/mman.h> #include <sys/time.h> #include <fcntl.h> #include <string.h> #include <stdlib.h> #include <linux/fb.h> #include <linux/ti81xxfb.h> #include <linux/ti81xxvin.h> #include<linux/videodev2.h> } #define FB_DEV "/dev/fb0" #define __fnc__ __FUNCTION__ class CDisplayFB { public: CDisplayFB(){printf("display class \n");} ~CDisplayFB(); public: int initFB(char * fb_device,const int width,const int height); int initFB(char * fb_device); int exitFB(); int copyImageToFB(unsigned char * source, int width,int height); unsigned short RGB888toRGB565(unsigned char red,unsigned char green, unsigned char blue); int fb_open(char *fb_device); int fb_close(int fd); int fb_stat(int fd, unsigned int *width, unsigned int *height, unsigned int * depth); void *fb_mmap(int fd, unsigned int screensize); void *fd_mmap(int fd, unsigned int filesize); int fb_munmap(void *start, size_t length); int fb_pixel(void *fbmem, int width, int height, int x, int y, unsigned short color); public: int fd; unsigned char *buffer; struct stat st; int fbdev; char *fb_device; unsigned char *fbmem; unsigned char *fdmem; unsigned int screensize; unsigned int fb_width; unsigned int fb_height; unsigned int fb_depth; unsigned int x; unsigned int y; struct fb_fix_screeninfo fb_finfo; struct fb_var_screeninfo fb_vinfo; }; #endif
显示源文件如下:
#include "displayFb.h" int CDisplayFB::fb_open(char *fb_device) { int fd; if ((fd = open(fb_device, O_RDWR)) < 0) { perror(__fnc__); return -1; } return fd; } int CDisplayFB::fb_close(int fd) { return (close(fd)); } /* get framebuffer's width, height, and depth. * return 0 if success, else return -1. */ int CDisplayFB::fb_stat(int fd, unsigned int *width, unsigned int *height, unsigned int * depth) { struct fb_fix_screeninfo fb_finfo; struct fb_var_screeninfo fb_vinfo; if (ioctl(fd, FBIOGET_FSCREENINFO, &fb_finfo)) { perror(__fnc__); return -1; } if (ioctl(fd, FBIOGET_VSCREENINFO, &fb_vinfo)) { perror(__fnc__); return -1; } *width = fb_vinfo.xres; *height = fb_vinfo.yres; *depth = fb_vinfo.bits_per_pixel; return 0; } /* map shared memory to framebuffer device. * return maped memory if success * else return -1, as mmap dose */ void * CDisplayFB::fb_mmap(int fd, unsigned int screensize) { caddr_t fbmem; if ((fbmem = ( char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED) { perror(__func__); return (void *) (-1); } return fbmem; } /* map shared memmory to a opened file */ void *CDisplayFB::fd_mmap(int fd, unsigned int filesize) { caddr_t fdmem; if ((fdmem = ( char *)mmap(0, filesize, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED) { perror(__func__); return (void *) (-1); } return fdmem; } /* unmap map memory for framebuffer device */ int CDisplayFB::fb_munmap(void *start, size_t length) { return (munmap(start, length)); } /* convert 24bit RGB888 to 16bit RGB565 color format */ unsigned short CDisplayFB::RGB888toRGB565(unsigned char red, unsigned char green, unsigned char blue) { unsigned short B = (blue >> 3) & 0x001F; unsigned short G = ((green >> 2) << 5) & 0x07E0; unsigned short R = ((red >> 3) << 11) & 0xF800; return (unsigned short) (R | G | B); } /* display a pixel on the framebuffer device. * fbmem is the starting memory of framebuffer, * width and height are dimension of framebuffer, * width and height are dimension of framebuffer, * x and y are the coordinates to display, * color is the pixel's color value. * return 0 if success, otherwise return -1. */ int CDisplayFB::fb_pixel(void *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; } int CDisplayFB::initFB(char * fb_device,const int width,const int height) { /* open framebuffer device */ // if ((fb_device = getenv("FRAMEBUFFER")) == NULL) // fb_device = FB_DEV; fbdev = fb_open(fb_device); /* get status of framebuffer device */ fb_stat(fbdev, &fb_width, &fb_height, &fb_depth); if(fb_width < width) { fb_width=width; printf("fb_width= %d \n", fb_width); } if(fb_height < height) { fb_height = height; printf("fb_height= %d \n",fb_height); } printf("fb_width= %d \n", fb_width); printf("fb_height= %d \n",fb_height); /* map framebuffer device to shared memory */ screensize = fb_width * fb_height * fb_depth / 8; fbmem = (unsigned char *)fb_mmap(fbdev, screensize); if (ioctl(fbdev, FBIOGET_FSCREENINFO, &fb_finfo)) { perror(__fnc__); return -1; } printf("ioctl FBIOGET_FSCREENINFO ok\n"); if (ioctl(fbdev, FBIOGET_VSCREENINFO, &fb_vinfo)) { perror(__fnc__); return -1; } printf("ioctl FBIOGET_VSCREENINFO ok\n"); return 0; } int CDisplayFB::initFB(char * fb_device) { /* open framebuffer device */ // if ((fb_device = getenv("FRAMEBUFFER")) == NULL) // fb_device = FB_DEV; fbdev = fb_open(fb_device); /* get status of framebuffer device */ fb_stat(fbdev, &fb_width, &fb_height, &fb_depth); //fb_width=width; //fb_height = height; /* map framebuffer device to shared memory */ screensize = fb_width * fb_height * fb_depth / 8; fbmem = (unsigned char *)fb_mmap(fbdev, screensize); if (ioctl(fbdev, FBIOGET_FSCREENINFO, &fb_finfo)) { perror(__fnc__); return -1; } printf("ioctl FBIOGET_FSCREENINFO ok\n"); if (ioctl(fbdev, FBIOGET_VSCREENINFO, &fb_vinfo)) { perror(__fnc__); return -1; } printf("ioctl FBIOGET_VSCREENINFO ok\n"); return 0; } int CDisplayFB::copyImageToFB(unsigned char * source, int width,int height) { int x,y; for(y = 0; y < height; y++) for (x = 0; x < width; x++) { * (fbmem + y * fb_width * 4 + x * 4) = (unsigned char) source[y * width *3 + x * 3 + 2]; * (fbmem + y * fb_width * 4 + x * 4 + 1) = (unsigned char) source[y * width *3 + x * 3 + 1]; * (fbmem + y * fb_width * 4 + x * 4 + 2) = (unsigned char) source[y * width *3 + x * 3 + 0]; * (fbmem + y * fb_width * 4 + x * 4 + 3) = (unsigned char) 0; } return 0; } int CDisplayFB::exitFB() { /* unmap framebuffer's shared memory */ fb_munmap(fbmem, screensize); /* close framebuffer device */ fb_close(fbdev); return 0; }