How to get pixel colour from framebuffer on linux (Raspberry Pi)

杀马特。学长 韩版系。学妹 提交于 2020-01-05 08:22:19

问题


I am trying to make a small program to control the colour of an RGB LED according to the colour of certain pixels on the screen. Since this is on Raspberry Pi running Raspbmc, I cant use XLib because everything is drawn from the frame buffer(not sure if this is true, but from what I read on the FAQ this appears to be the case). I tried using XLib but couldn't get the display to be detected (makes sense why it doesn't work now).

This is an example I found online. The problem is, it compiles fine, but when run, the second error message appears: "Error reading fixed information". I tried displaying the content of fbfd but that results in a Segmentation Error. So it appears nothing is written to fbfd?

I cant make sense of it because everything I find about the frame buffer is basicly this same code, so I don't know why it doesn't work.

Hope someone can help!

#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;
}

来源:https://stackoverflow.com/questions/19542029/how-to-get-pixel-colour-from-framebuffer-on-linux-raspberry-pi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!