Linux Framebuffer 实验

Linux Framebuffer 实验

一、准备

  1. linux虚拟机或ARM开发板
  2. Ubuntu18.04

二、Framebuffer介绍

Linux LCD Frambuffer 基础介绍和使用:https://blog.51cto.com/u_13064014/5079683
Linux应用开发【第一章】Framebuffer应用开发:https://zhuanlan.zhihu.com/p/443120506
Linux Framebuffer 技术:https://zhuanlan.zhihu.com/p/496623603
为了能直观的看明白 Framebuffer 的原理,所以我从他们博客中引用了几张图片,如下所示:

  1. LCD显示原理
    在这里插入图片描述
  2. Framebuffer架构
    在这里插入图片描述
  3. 在这里插入图片描述
    从上面图中很容易看明白Framebuffer是怎么回事,接下来我们进行测试,分别在ubuntu和ARM开发板上进行测试。

三、Framebuffer 测试命令

为了方便测试 Framebuffer 可用,可以快速通过命令进行简单测试,如下所示:

  1. 清屏命令
dd if=/dev/zero of=/dev/fb0
dd if=/dev/zero of=/dev/fb0 bs=1024 count=768
  1. 截屏命令
dd if=/dev/fb0 of=fbfile
cp /dev/fb0 fbfile
# 注意:这里的截屏其实就是拷贝 中的数据,所以只有当framebuffer中有数据存在时才能截屏成功
  1. 将保存的信息显示传回framebuffer
dd if=fbfile of=/dev/fb0
  1. 往屏幕的左上角画一个白色的像素点
echo -en '\xFF\xFF\xFF\x00' > /dev/fb0
  1. 花屏指令
cat /dev/urandom > /dev/fb0

四、Framebuffer 测试程序

display.c

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <sys/ioctl.h>
 
/* 显示屏相关头文件 */
#include <linux/fb.h>
#include <sys/mman.h>
 
typedef struct lcd_color
{
    unsigned char bule;
    unsigned char green;
    unsigned char red;
    unsigned char alpha;
} lcd_color;
 
/**
 * 更新屏幕显示内存块信息,颜色格式为RGB8888
*/
void screen_refresh(char *fbp, lcd_color color_buff, long screen_size)
{
    for(int i=0; i < screen_size; i+=4)
    {
        *((lcd_color*)(fbp + i)) = color_buff;
    }
    usleep(1000*2000);
}
 
int main()
{
    int fp = 0;
    int rgb_type = 0;
    long screen_size = 0; 
    struct fb_var_screeninfo vinfo;
    struct fb_fix_screeninfo finfo;          
    unsigned char *fbp = 0;
 
    fp = open("/dev/fb0", O_RDWR);
 
    if (fp < 0)
    {
        printf("Error : Can not open framebuffer device/n");
        exit(1);
    }
 
    if (ioctl(fp, FBIOGET_FSCREENINFO, &finfo))
    {
        printf("Error reading fixed information/n");
        exit(2);
    }
 
    if (ioctl(fp, FBIOGET_VSCREENINFO, &vinfo))
    {
        printf("Error reading variable information/n");
        exit(3);
    }
 
    /* 打印获取的屏幕信息 */
    printf("The mem is :%d\n", finfo.smem_len);
    printf("The line_length is :%d\n", finfo.line_length);
    printf("The xres is :%d\n", vinfo.xres);
    printf("The yres is :%d\n", vinfo.yres);
    printf("bits_per_pixel is :%d\n", vinfo.bits_per_pixel);
 
    /* 获取RGB的颜色颜色格式,比如RGB8888、RGB656 */
    rgb_type = vinfo.bits_per_pixel / 8;
    /* 屏幕的像素点 */
    screen_size = vinfo.xres * vinfo.yres * rgb_type;
    /* 映射 framebuffer 的缓冲空间,得到一个指向这块空间的指针 */
    fbp =(unsigned char *) mmap (NULL, screen_size, PROT_READ | PROT_WRITE, MAP_SHARED, fp, 0);
    if (fbp == NULL)
    {
       printf ("Error: failed to map framebuffer device to memory./n");
       exit (4);
    }
 
    /* 刷白屏 */
    memset(fbp, 0xff, screen_size);    
    usleep(1000*2000);
 
    /* 我的显示屏是RGDA的,所以县色格式为32为,注意自己的显示屏信息,对应修改 */
    /* 刷红色 */
    screen_refresh(fbp, (lcd_color){0, 0, 255, 255}, screen_size);
 
    /* 刷绿色 */
    screen_refresh(fbp, (lcd_color){0, 255, 0, 255}, screen_size);
 
    /* 刷蓝色 */
    screen_refresh(fbp, (lcd_color){255, 0, 0, 255}, screen_size);
 
    /* 解除映射 */
    munmap (fbp, screen_size); 
 
    close(fp);
    return 0;
}

Makefile

out_file_name = "display"
 
all: display.c
#	gcc $^ -o $(out_file_name)
	arm-linux-gnueabihf-gcc $^ -o $(out_file_name)
 
 
.PHONY: clean
clean:
	rm $(out_file_name)

五、ubuntu测试

  1. 驱动查看
    查看设备的Framebuffer驱动,在设备中可以看到 fbx的驱动
ls /dev/fb* -l
  1. 使能fb显示通道(有些设备默认是关闭的)
echo 0 > /sys/class/graphics/fb0/blank
  1. 运行程序
./display
  1. 结果
    在这里插入图片描述
    在这里插入图片描述

从图中可以看出执行后打印的信息,到此我们测试就算完成了,说明LCD的驱动是没问题的,可以进行GUI的开发。
注意:如果LCD的屏是RGB8888格式的,那么可能出现黑屏不显示的现象,这是需要适当调整一下数据格式,如下图所示:在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Qt is a popular cross-platform framework for developing graphical user interfaces (GUI) and applications. It provides support for Linux framebuffer, which allows you to create GUI applications that can run directly on the Linux framebuffer without the need for an X server. To use Qt with Linux framebuffer, you can follow these general steps: 1. Install the required dependencies: Make sure you have the necessary libraries and development packages installed on your Linux system. This may include framebuffer-related libraries like `libdrm` and `libgbm`. 2. Configure Qt with framebuffer support: When building Qt from source, you can enable framebuffer support by passing the `-qt-libinput` flag to the `configure` script. For example: ``` ./configure -qt-libinput ``` 3. Build your Qt application: Once Qt is configured with framebuffer support, you can build your application using the `qmake` and `make` commands as usual. Make sure to set the appropriate target platform, such as `linuxfb`. 4. Run your Qt application on the Linux framebuffer: After building your application, you can run it directly on the Linux framebuffer by setting the appropriate environment variables. For example: ``` export QT_QPA_PLATFORM=linuxfb export QT_QPA_FB_TTY=/dev/fb0 ./your_application ``` By following these steps, you should be able to develop and run Qt applications using the Linux framebuffer as the target platform. Keep in mind that framebuffer support may vary depending on your specific Linux distribution and hardware setup.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值