framebuffer:帧缓冲、帧缓存
Linux内核为显示提供的一套应用程序接口(驱动内核支持)
分辨率:像素点的总和
像素点:
显示屏:800*600(横向有800个像素点,纵向有600个像素点)
显卡(显存(保存像素点的值))
RGB:(8个bitR,8个bitG,8个bitB)
RGB888(PC,4412)
RGB565(S3C2440)
原理:
通过内存映射技术,向显存空间写入RGB值
1.打开显示设备(/dev/fb0)
2.获取显示设备相关参数(分辨率、位深度)
3.建立内存映射
4.写入RGB颜色值
5.解除映射
6.关闭显
#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 <stdio.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("open fail");return -1;}int ret = ioctl(fd, FBIOGET_VSCREENINFO, &vinf);if(-1 == ret){perror("ioctl fail");return -1;}size_t len = vinf.xres_virtual * vinf.xres_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 = (unsigned int*)pmem;*(p + y * vinf.xres_virtual + x) = col;}else if (vinf.bits_per_pixel == RGB565_FMT){unsigned short *p = (unsigned short*)pmem;*(p + y * vinf.xres_virtual + x) = col;}return ;
}
//横线
void draw_xline(int x, int y, int xlen, unsigned int col)
{if (x >= vinf.xres || y >= vinf.yres){return ;}int i = 0;for(i = 0; i < xlen; ++i){if (vinf.bits_per_pixel == RGB888_FMT){unsigned int *p = (unsigned int*)pmem;*(p + y * vinf.xres_virtual + (x + i)) = col;}else if (vinf.bits_per_pixel == RGB565_FMT){unsigned short *p = (unsigned short*)pmem;*(p + y * vinf.xres_virtual + (x + i)) = col;}}return ;}
//竖线
void draw_yline(int x, int y, int ylen, unsigned int col)
{if (x >= vinf.xres || y >= vinf.yres){return ;}int i = 0;for(i = 0; i < ylen; ++i){if (vinf.bits_per_pixel == RGB888_FMT){unsigned int *p = (unsigned int*)pmem;*(p + (y + i) * vinf.xres_virtual + x) = col;}else if (vinf.bits_per_pixel == RGB565_FMT){unsigned short *p = (unsigned short*)pmem;*(p + (y + i) * vinf.xres_virtual + x) = col;}}return ;}
//矩形
void draw_rectangle(int x, int y, int xlen, int ylen, unsigned int col)
{if (x >= vinf.xres || y >= vinf.yres){return ;}draw_xline(x,y,xlen,col);draw_xline(x,y + ylen, xlen, col);draw_yline(x,y,ylen,col);draw_yline(x + xlen, y, ylen, col);return ;
}
//清屏
void clear_scream_col(unsigned int col)
{int i = 0, j = 0;for(i = 0; i < vinf.xres; ++i){for(j = 0; j < vinf.yres; ++j){draw_point(i, j, col);}}}
//斜线
void draw_xyline(double x1, double y1, double x2, double y2, unsigned int col)
{double k = (y2 - y1) / (x2 - x1);double b = y1 - (k * x1);int i = 0;for(i = 0; i < (x2 - x1); ++i){draw_point((int)x1 + i, (int)(k * (x1 + i) + b), col);}return ;
}
//圆
void draw_circle(double x, double y, double r, unsigned int col)
{double a = x, b = y;int i = 0;for (i = 0; i < 360; i++) { float theta = 2 * 3.14 * i / 360; float x1 = r * cos(theta) + x; float y1 = r * sin(theta) + y;draw_point(x1,y1,col);}return ;
}void uninit_fb(int fd)
{size_t len = vinf.xres_virtual * vinf.yres_virtual * vinf.bits_per_pixel/8;munmap(pmem, len);close(fd);
}
示设备