嵌入式学习---Linux中的画面显示

framebuffer:帧缓冲、帧缓存

Linux内核为显示提供的一套应用程序接口(驱动内核支持)

**分辨率:**像素点的总和

像素点:

显示屏:800*600(横向有800个像素点,纵向有600个像素点)

显卡(显存(保存像素点的值))

RGB颜色模式:(8个bitR,8个bitG,8个bitB)

常用的有两种:

1.RGB888(PC,4412)

2.RGB565(S3C2440)

原理:

通过内存映射技术,向显存空间写入RGB值

操作步骤:

1.打开显示设备(/dev/fb0)

2.获取显示设备相关参数(分辨率、位深度)

3.建立内存映射

4.写入RGB颜色值

5.解除映射

6.关闭显示设备

代码实现:

cs 复制代码
#include<stdio.h>
#include "framebuffer.h"
#include <linux/fb.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include<math.h>
void *pmem;
struct fb_var_screeninfo vinf;

int init_fb(char *devname)
{
    int fd = open(devname,O_RDWR);
    if(-1 == fd)
    {
        perror("fail open ");
        return -1;
    }

    int ret = ioctl(fd,FBIOGET_VSCREENINFO,&vinf);
    if(-1 == ret)
    {
        perror("fail ioctl");
        return -1;
    }
    printf("xres = %d ,yres = %d\n",vinf.xres,vinf.yres);
    printf("xres_virtual = %d, yres_virtual = %d\n",vinf.xres_virtual,vinf.yres_virtual);
    printf("bits_per_pixel : %d\n",vinf.bits_per_pixel);

    size_t len = vinf.xres_virtual *vinf.yres_virtual *vinf.bits_per_pixel/8;
    pmem = mmap(NULL,len,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
    if((void *)-1 == pmem)
    {
        perror("fail mmap");
        return -1;
    }

    return fd;

}

void draw_point(int x,int y,unsigned int col)
{
    if(x >= vinf.xres || y >= vinf.yres)
    {
        return;
    }
    if(vinf.bits_per_pixel == RGB888_FMT)
    {
        unsigned int *p = pmem;
        *(p + y* vinf.xres_virtual + x)= col;
    }
    else if(vinf.bits_per_pixel == RGB565_FMT)
    {
        unsigned short *p = pmem;
        *(p + y *vinf.xres_virtual + x)= col;
    }
    return;
}
int main(int argc,char*argv[])
{
    int fd_fb = init_fb("/dev/fb0");
    if(fd_fb == -1)
    {
        return -1;
    }
    draw_point(400,300,0x00ff0000);
    uninit_fb(fd_fb);

    return 0;
}
相关推荐
铁匠匠匠1 小时前
从零开始学数据结构系列之第六章《排序简介》
c语言·数据结构·经验分享·笔记·学习·开源·课程设计
架构文摘JGWZ2 小时前
Java 23 的12 个新特性!!
java·开发语言·学习
小齿轮lsl2 小时前
PFC理论基础与Matlab仿真模型学习笔记(1)--PFC电路概述
笔记·学习·matlab
Aic山鱼3 小时前
【如何高效学习数据结构:构建编程的坚实基石】
数据结构·学习·算法
qq11561487073 小时前
Java学习第八天
学习
天玑y3 小时前
算法设计与分析(背包问题
c++·经验分享·笔记·学习·算法·leetcode·蓝桥杯
2301_789985943 小时前
Java语言程序设计基础篇_编程练习题*18.29(某个目录下的文件数目)
java·开发语言·学习
橄榄熊3 小时前
Windows电脑A远程连接电脑B
学习·kind
web_learning_3215 小时前
source insight学习笔记
笔记·学习
Lossya5 小时前
【机器学习】参数学习的基本概念以及贝叶斯网络的参数学习和马尔可夫随机场的参数学习
人工智能·学习·机器学习·贝叶斯网络·马尔科夫随机场·参数学习