linux(虚拟机、Ubuntu):LCD模拟器显示图片
.c文件代码:
#include <stdio.h> //printf scanf
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h> //open read lseek write close
#include <sys/mman.h> //mmap
int main(int argc, char **argv)
{
int fd_lcd;
unsigned int *pfb=NULL;
unsigned int x,y;
unsigned char bmp_buf[480*800*3];
//打开虚拟lcd设备
fd_lcd = open("/dev/ubuntu_lcd", O_RDWR);
if(fd_lcd < 0)
{
perror("open lcd");
return -1;
}
//进行内存映
pfb=(unsigned int *)mmap(NULL, //映射区的开始地址,设置为NULL时表示由系统决定映射区的起始地址
800*480*4, //映射区的长度
PROT_READ|PROT_WRITE, //内容可以被读取和写入
MAP_SHARED, //对共享区的写入,相当于输出到文件
fd_lcd, //有效的文件描述符
0 //被映射对象内容的起点
);
if(pfb == NULL)
{
perror("mmap");
return -1;
}
//打开资源图片
int fd_bmp = open("./1.bmp", O_RDONLY);
if(fd_bmp <= 0)
{
perror("open fial");
return -1;
}
lseek(fd_bmp, 54, SEEK_SET);
read(fd_bmp, bmp_buf, 480*800*3);
close(fd_bmp);
int i = 0;
for(y=0; y<480; y++)
{
for(x=0; x<800; x++)
{
*(pfb+(479-y)*800+ x) = (bmp_buf[i]<<0) | (bmp_buf[i+1]<<8) | (bmp_buf[i+2]<<16);
i += 3;
}
}
/* 取消内存映射 */
munmap(pfb, 800*480*4);
/* 关闭LCD设备 */
close(fd_lcd);
/* 没有错误代表正确返回 */
return 0;
}
终端编译:
图片(名称为1.png,配合代码):
