-->

如何从framebuffer来实现像素颜色在Linux(树莓派)(How to get pixel

2019-10-18 19:23发布

我试图做一个小程序来控制RGB按照屏幕上的某些像素的彩色LED的颜色。 由于这是在树莓派运行Raspbmc,我不能使用XLIB,因为一切都从帧缓冲器绘制(不知道这是真实的,但是从我的FAQ阅读这似乎是如此)。 我试着用XLIB但却无法被检测到显示(是有道理的,为什么它现在无法正常工作)。

这是一个例子,我在网上找到。 问题是,它编译正常,但在运行时,将出现第二错误消息:“错误读取固定信息”。 我试图显示fbfd的内容,但导致分割错误。 所以会出现没有数据被写入到fbfd?

我不能做它的意义,因为一切我找到帧缓冲区basicly相同的代码,所以我不知道为什么它不工作。

希望有人能帮助!

#include <unistd.h>
#include <fcntl.h>        /* for fcntl */
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>        /* for mmap */
#include <sys/ioctl.h>
#include <linux/fb.h>

#include <stdio.h>
#include <stdlib.h>

int main() {
    long int screensize = 0;    
    int fbfd =0;                    /* frame buffer file descriptor */
    struct fb_var_screeninfo vinfo;
    struct fb_fix_screeninfo finfo;
    char* fbp;                    /* pointer to framebuffer */
    int location;                    /* iterate to location */

    int x, y;                    /* x and y location */

    /* open the file for reading and writing */
    fbfd = open("/dev/fb0", O_RDWR);
    //printf("%s\n",fbfd);
    if (!fbfd) {
        printf("Error: cannot open framebuffer device.\n");
        exit(1);
    }
    printf ("The framebuffer device was opened successfully.\n");

    /* get the fixed screen information */
    if (ioctl (fbfd, FBIOGET_FSCREENINFO, &finfo)) {
        printf("Error reading fixed information.\n");
        exit(2);
    }

    /* get variable screen information */
    if (ioctl (fbfd, FBIOGET_VSCREENINFO, &vinfo)) {
        printf("Error reading variable information.\n");
        exit(3);
    }

    /* figure out the size of the screen in bytes */
    //screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;

    /* map the device to memory */
    fbp = (char*)mmap(0, finfo.smem_len, 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 ("Framebuffer device was mapped to memory successfully.\n");

    // Figure out where in memory to put the pixel
    for ( y = 0; y < (vinfo.yres/2); y++ )
        for ( x = 0; x < vinfo.xres; x++ ) { 
            location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) + (y+vinfo.yoffset) * finfo.line_length;
            if ( vinfo.bits_per_pixel == 32 ) { 
                *(fbp + location) = 100; // Some blue 
                *(fbp + location + 1) = 15+(x-100)/2; // A little green 
                *(fbp + location + 2) = 200-(y-100)/5; // A lot of red
                *(fbp + location + 3) = 0; // No transparency 
            } else { //assume 16bpp 
                int b = 10; int g = (x-100)/6; // A little green 
                int r = 31-(y-100)/16; // A lot of red 
                unsigned short int t = r<<11 | g << 5 | b; 
                *((unsigned short int*)(fbp + location)) = t; 
            }
        }
    munmap(fbp, screensize);
    close(fbfd);

    return 0;
}
文章来源: How to get pixel colour from framebuffer on linux (Raspberry Pi)