四平市建设局网站,遵义市建设局网站,企业战略管理咨询公司,wordpress 多语言网站之前获取LCD屏幕参数信息时了解到#xff0c;LCD屏是 FrameBuffer 设备#xff0c;操作 FrameBuffer 设备 其实就是在读写 /dev/fb0 文件。除此之外#xff0c;LCD屏上包含多个像素点#xff0c;绘制点、线、矩形框本质是在修改这些像素点的颜色。 目录
1、定义 lcd_color…之前获取LCD屏幕参数信息时了解到LCD屏是 FrameBuffer 设备操作 FrameBuffer 设备 其实就是在读写 /dev/fb0 文件。除此之外LCD屏上包含多个像素点绘制点、线、矩形框本质是在修改这些像素点的颜色。 目录
1、定义 lcd_color 结构体
2、获取LCD设备参数信息
3、mmap 建立显存映射
(1) 为什么要建立显存映射
(2) 代码实现
3、定义绘制函数
4、完整代码 1、定义 lcd_color 结构体
为了后续方便传递颜色这里定义一个 lcd_color 结构体结构体声明如下。
struct lcd_color
{u_int8_t red;u_int8_t green;u_int8_t blue;
};
我们要如何将颜色传递给LCD屏呢根据我们之前获取到的屏幕信息LCD屏颜色格式是RGB565。我们改变像素点颜色实际上就是传递一个值。
这个值低5位表示蓝色中间6位表示绿色高5位表示红色因此我们拿到R、G、B对应的数值后需要将他们按下面的方式拼接。 #define RGB565(color) (color-blue 0x05) | \((color-green 0x06) 5) | \((color-red 0x05) 11) 2、获取LCD设备参数信息
这里就不再赘述请参考获取LCD屏幕参数信息
struct fb_fix_screeninfo fb_fix; // 固定参数信息
struct fb_var_screeninfo fb_var; // 可变参数信息int fd open(/dev/fb0, O_RDWR);
if (fd 0)
{perror(open fb failed);exit(-1);
}ioctl(fd, FBIOGET_FSCREENINFO, fb_fix); // 获取固定参数信息
ioctl(fd, FBIOGET_VSCREENINFO, fb_var); // 获取可变参数信息 3、mmap 建立显存映射
(1) 为什么要建立显存映射
建立显存映射的目的是提升IO效率。实际上使用read/write操作显存也是可以的然而数据量较大的时候普通IO的方式效率较低。一般显示的图像是动态变化的图像数据需要不断被更新这种情况下的数据量较大建议采用存储映射IO方式。
mmap 函数的详细使用可以参考mmap函数详解 (2) 代码实现
值得一提的是虽然最开始画的LCD示意图是一个矩形但是在内存中还是线性存储的显存的起始地址为 screen_base 类型必须为 u_int16_t* 或者为 unsigned short*因为指针类型决定了移动一次的步长所以每次 screen_base 1 的时候实际上移动了16bit。如果是 int即32bitscreen_base 1 时将会移动 32 bit u_int16_t *screen_base NULL; // 映射基地址(全局变量)int main(int args, char **argv)
{struct fb_fix_screeninfo fb_fix; // 固定参数信息struct fb_var_screeninfo fb_var; // 可变参数信息// ... ... 获取可变参数信息size_t len fb_fix.line_length * fb_var.yres;screen_base (u_int16_t *)mmap(NULL, len, PROT_WRITE, MAP_SHARED, fd, 0);if ((void*)screen_base MAP_FAILED){perror(mmap failed);exit(-2);}
}3、定义绘制函数 绘制点 绘制点的关键在于确定像素点的位置然后赋予颜色。 /********************************************************************* 函数名称 lcd_draw_point* 功能描述 在LCD屏上绘制一个点* 输入参数 x, y, color* 返 回 值 无********************************************************************/
void lcd_draw_point(uint x, uint y, struct lcd_color* color)
{screen_base[y*width x] RGB565(color); // 这里的 width 是全局变量对应LCD屏的每行的像素点个数
} 绘制线 /********************************************************************* 函数名称 lcd_draw_line* 功能描述 在LCD屏上绘制一根线* 输入参数 其实就是绘制一个实心的矩形start_x: 起始横坐标 start_y: 起始纵坐标line_width: 线的宽度 line_height: 线的高度color: 线的颜色* 返 回 值 无********************************************************************/
void lcd_draw_line(uint start_x, uint start_y, uint line_width, uint line_height, struct lcd_color* color)
{for (size_t i start_y; i start_y line_height; i){for (size_t j start_x; j start_x line_width; j){lcd_draw_point(j, i, color);}}
} 绘制矩形框 /********************************************************************* 函数名称 lcd_draw_rect* 功能描述 在LCD屏上绘制一个矩形框* 输入参数 其实就是绘制一个实心的矩形start_x: 起始横坐标 start_y: 起始纵坐标rect_width: 矩形框的宽度 rect_height: 矩形框的高度line_width线的粗细程度color: 线的颜色* 返 回 值 无********************************************************************/
void lcd_draw_rect(uint start_x, uint start_y, uint rect_width, uint rect_height, uint line_width, struct lcd_color* color)
{lcd_draw_line(start_x, start_y, rect_width, line_width, color); // 上lcd_draw_line(start_x, start_y rect_height - line_width - 1, rect_width, line_width, color); // 下lcd_draw_line(start_x, start_y line_width, line_width, rect_height - line_width, color); // 左lcd_draw_line(start_x rect_width - line_width - 1, start_y line_width, line_width, rect_height - line_width, color); // 右
} 4、完整代码
由于只分配了 5 bit来表示红色所以显示出来的红色可能偏暗。
#include stdio.h
#include sys/types.h
#include sys/stat.h
#include fcntl.h
#include sys/ioctl.h
#include linux/fb.h
#include sys/mman.h
#include stdlib.hu_int16_t width 0;
u_int16_t height 0;u_int16_t *screen_base NULL; // 映射基地址typedef unsigned int uint;
struct lcd_color
{u_int8_t red;u_int8_t green;u_int8_t blue;
};
#define RGB565(color) (color-blue 0x05) | \((color-green 0x06) 5) | \((color-red 0x05) 11)/********************************************************************* 函数名称 lcd_draw_point* 功能描述 在LCD屏上绘制一个点* 输入参数 x, y, color* 返 回 值 无********************************************************************/
void lcd_draw_point(uint x, uint y, struct lcd_color* color)
{screen_base[y*width x] RGB565(color);
}/********************************************************************* 函数名称 lcd_draw_line* 功能描述 在LCD屏上绘制一根线* 输入参数 start_x, start_y, line_width, line_height, color* 返 回 值 无********************************************************************/
void lcd_draw_line(uint start_x, uint start_y, uint line_width, uint line_height, struct lcd_color* color)
{for (size_t i start_y; i start_y line_height; i){for (size_t j start_x; j start_x line_width; j){lcd_draw_point(j, i, color);}}
}/********************************************************************* 函数名称 lcd_draw_rect* 功能描述 在LCD屏上绘制一个矩形框* 输入参数 start_x, start_y, rect_width, rect_height, line_width, color* 返 回 值 无********************************************************************/
void lcd_draw_rect(uint start_x, uint start_y, uint rect_width, uint rect_height, uint line_width, struct lcd_color* color)
{lcd_draw_line(start_x, start_y, rect_width, line_width, color); // 上lcd_draw_line(start_x, start_y rect_height - line_width - 1, rect_width, line_width, color); // 下lcd_draw_line(start_x, start_y line_width, line_width, rect_height - line_width, color); // 左lcd_draw_line(start_x rect_width - line_width - 1, start_y line_width, line_width, rect_height - line_width, color); // 右
}int main(int args, char **argv)
{struct fb_fix_screeninfo fb_fix; // 固定参数信息struct fb_var_screeninfo fb_var; // 可变参数信息int fd open(/dev/fb0, O_RDWR);if (fd 0){perror(open fb failed);exit(-1);}ioctl(fd, FBIOGET_FSCREENINFO, fb_fix);ioctl(fd, FBIOGET_VSCREENINFO, fb_var);width fb_var.xres;height fb_var.yres;size_t len fb_fix.line_length * fb_var.yres;screen_base (u_int16_t *)mmap(NULL, len, PROT_WRITE, MAP_SHARED, fd, 0);if ((void*)screen_base MAP_FAILED){perror(mmap failed);exit(-2);}// 将整个屏幕填充成红色for (size_t i 0; i height; i){for (size_t j 0; j width; j){struct lcd_color color {31, 0, 0};lcd_draw_point(j, i, color);}}// 绘制一根长为width宽为 height/2 的线struct lcd_color line_color {0, 31, 0};lcd_draw_line(0, 0, width, height/2, line_color);// 绘制一个长为width宽为height线粗为10的矩形框struct lcd_color rect_color {31, 31, 0};lcd_draw_rect(0, 0, width, height, 10, rect_color);return 0;
} 文章转载自: http://www.morning.qznkn.cn.gov.cn.qznkn.cn http://www.morning.rttp.cn.gov.cn.rttp.cn http://www.morning.ksgjn.cn.gov.cn.ksgjn.cn http://www.morning.skqfx.cn.gov.cn.skqfx.cn http://www.morning.xqffq.cn.gov.cn.xqffq.cn http://www.morning.sxjmz.cn.gov.cn.sxjmz.cn http://www.morning.dytqf.cn.gov.cn.dytqf.cn http://www.morning.qnwyf.cn.gov.cn.qnwyf.cn http://www.morning.playmi.cn.gov.cn.playmi.cn http://www.morning.hjlsll.com.gov.cn.hjlsll.com http://www.morning.kstgt.cn.gov.cn.kstgt.cn http://www.morning.xbmwh.cn.gov.cn.xbmwh.cn http://www.morning.tgyzk.cn.gov.cn.tgyzk.cn http://www.morning.pxbky.cn.gov.cn.pxbky.cn http://www.morning.srtw.cn.gov.cn.srtw.cn http://www.morning.hgfxg.cn.gov.cn.hgfxg.cn http://www.morning.kqlrl.cn.gov.cn.kqlrl.cn http://www.morning.kwfnt.cn.gov.cn.kwfnt.cn http://www.morning.lthpr.cn.gov.cn.lthpr.cn http://www.morning.hfyll.cn.gov.cn.hfyll.cn http://www.morning.dcdhj.cn.gov.cn.dcdhj.cn http://www.morning.yhdqq.cn.gov.cn.yhdqq.cn http://www.morning.xfxqj.cn.gov.cn.xfxqj.cn http://www.morning.knswz.cn.gov.cn.knswz.cn http://www.morning.xyhql.cn.gov.cn.xyhql.cn http://www.morning.pdmc.cn.gov.cn.pdmc.cn http://www.morning.yrctp.cn.gov.cn.yrctp.cn http://www.morning.lyldhg.cn.gov.cn.lyldhg.cn http://www.morning.lxdbn.cn.gov.cn.lxdbn.cn http://www.morning.ypwlb.cn.gov.cn.ypwlb.cn http://www.morning.wjpsn.cn.gov.cn.wjpsn.cn http://www.morning.rbzd.cn.gov.cn.rbzd.cn http://www.morning.rbbyd.cn.gov.cn.rbbyd.cn http://www.morning.rkmsm.cn.gov.cn.rkmsm.cn http://www.morning.mjwnc.cn.gov.cn.mjwnc.cn http://www.morning.xbmwh.cn.gov.cn.xbmwh.cn http://www.morning.cpctr.cn.gov.cn.cpctr.cn http://www.morning.jtfsd.cn.gov.cn.jtfsd.cn http://www.morning.ssjee.cn.gov.cn.ssjee.cn http://www.morning.tbcfj.cn.gov.cn.tbcfj.cn http://www.morning.qclmz.cn.gov.cn.qclmz.cn http://www.morning.gnjkn.cn.gov.cn.gnjkn.cn http://www.morning.hytqt.cn.gov.cn.hytqt.cn http://www.morning.qxkjy.cn.gov.cn.qxkjy.cn http://www.morning.rkzb.cn.gov.cn.rkzb.cn http://www.morning.jwefry.cn.gov.cn.jwefry.cn http://www.morning.pjxw.cn.gov.cn.pjxw.cn http://www.morning.tpqzs.cn.gov.cn.tpqzs.cn http://www.morning.srgbr.cn.gov.cn.srgbr.cn http://www.morning.wjhqd.cn.gov.cn.wjhqd.cn http://www.morning.frsbf.cn.gov.cn.frsbf.cn http://www.morning.xqndf.cn.gov.cn.xqndf.cn http://www.morning.yzdth.cn.gov.cn.yzdth.cn http://www.morning.wkpfm.cn.gov.cn.wkpfm.cn http://www.morning.wqpsf.cn.gov.cn.wqpsf.cn http://www.morning.qnqt.cn.gov.cn.qnqt.cn http://www.morning.ksbmx.cn.gov.cn.ksbmx.cn http://www.morning.crhd.cn.gov.cn.crhd.cn http://www.morning.yhwmg.cn.gov.cn.yhwmg.cn http://www.morning.qcrhb.cn.gov.cn.qcrhb.cn http://www.morning.cpfx.cn.gov.cn.cpfx.cn http://www.morning.qnsmk.cn.gov.cn.qnsmk.cn http://www.morning.lkmks.cn.gov.cn.lkmks.cn http://www.morning.dzgyr.cn.gov.cn.dzgyr.cn http://www.morning.ssglh.cn.gov.cn.ssglh.cn http://www.morning.nwpnj.cn.gov.cn.nwpnj.cn http://www.morning.mjdbd.cn.gov.cn.mjdbd.cn http://www.morning.ymqfx.cn.gov.cn.ymqfx.cn http://www.morning.qsbcg.cn.gov.cn.qsbcg.cn http://www.morning.kqxwm.cn.gov.cn.kqxwm.cn http://www.morning.znmwb.cn.gov.cn.znmwb.cn http://www.morning.nrfqd.cn.gov.cn.nrfqd.cn http://www.morning.yrddl.cn.gov.cn.yrddl.cn http://www.morning.lpcpb.cn.gov.cn.lpcpb.cn http://www.morning.gkdqt.cn.gov.cn.gkdqt.cn http://www.morning.rwjh.cn.gov.cn.rwjh.cn http://www.morning.zrpys.cn.gov.cn.zrpys.cn http://www.morning.kwqt.cn.gov.cn.kwqt.cn http://www.morning.wbqt.cn.gov.cn.wbqt.cn http://www.morning.gqwbl.cn.gov.cn.gqwbl.cn