hifb.c示例代码
// hifb.c示例代码
#include
#include
#include
#define FRAMEBUFFER_SIZE (1920*1080*4)
int main(int argc, char *argv[])
{
int fbfd = 0;
char *fbp = 0;
int x = 0, y = 0;
long int screensize = 0;
// 打开设备文件
fbfd = open("/dev/fb0", O_RDWR);
if (fbfd == -1) {
perror("Error: cannot open framebuffer device");
exit(1);
}
// 获取设备信息
struct fb_var_screeninfo vinfo;
struct fb_fix_screeninfo finfo;
if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo) == -1) {
perror("Error reading fixed information");
exit(2);
}
if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo) == -1) {
perror("Error reading variable information");
exit(3);
}
// 计算屏幕的大小
screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;
// 映射到内存中
fbp = (char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, fbfd, 0);
if ((int)fbp == -1) {
perror("Error: failed to map framebuffer device to memory");
exit(4);
}
// 将屏幕清空为黑色
memset(fbp, 0, screensize);
// 在屏幕上绘制一个红色的矩形
for (y = 100; y < 300; y++) {
for (x = 100; x < 300; x++) {
long int location = (x + vinfo.xoffset) * (vinfo.bits_per_pixel / 8) +
(y + vinfo.yoffset) * finfo.line_length;
*(fbp + location) = 0; // 蓝色
*(fbp + location + 1) = 0; // 绿色
*(fbp + location + 2) = 255; // 红色
*(fbp + location + 3) = 0; // 透明度
}
}
// 等待用户按下任意键后退出
printf("Press any key to exit...\n");
getchar();
// 取消映射并关闭设备文件
munmap(fbp, screensize);
close(fbfd);
return 0;
}
下载地址
用户评论