我正在尝试根据屏幕上某些像素的颜色制作一个小程序来控制RGB LED的颜色。 由于这是在运行Raspbmc的Raspberry Pi上,我不能使用XLib,因为所有内容都是从帧缓冲区中提取的(不确定这是否属实,但是从我在FAQ上看到的情况看来就是这种情况)。我尝试使用XLib,但无法检测到显示器(理解为什么它现在不起作用)。
这是我在网上找到的一个例子。问题是,它编译得很好,但在运行时会出现第二条错误消息:"读取固定信息时出错"。 我尝试显示fbfd的内容,但这会导致分段错误。所以似乎没有写入fbfd?
我无法理解它,因为我发现关于帧缓冲区的所有内容基本上都是相同的代码,因此我不知道它为什么不起作用。
希望有人可以提供帮助!
#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;
}