本次实验使用的开发板是正点原子的ATK-DL6Y2C_V2.4.
1 在开发板上显示圆形代码
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <linux/fb.h>
int main(int argc, char const *argv[])
{
struct fb_fix_screeninfo fb_fix;
struct fb_var_screeninfo fb_var;
unsigned int size;
int *screen = NULL;
//圆心的位置和半径的大小
int midlex = 512;
int midley = 300;
int R = 150;
//打开屏幕
int fd = open("/dev/fb0", O_RDWR);
if(fd < 0)
{
perror("open fd error");
exit(-1);
}
//获取显示屏信息并存储
ioctl(fd, FBIOGET_VSCREENINFO, &fb_var);
ioctl(fd, FBIOGET_FSCREENINFO, &fb_fix);
size = fb_fix.line_length * fb_var.yres;
int width = fb_var.xres;
int height = fb_var.yres;
int i = 0;
int x=0;
int y=0;
//内存映射
screen=(unsigned int *)mmap(NULL, size, PROT_WRITE|PROT_READ, MAP_SHARED, fd, 0);
for(y=0;y<height;y++)
{
for(x=0;x<width;x++)
{
i=((x-midlex)*(x-midlex)+((y-midley)*(y-midley)));
if(i<=R*R)
{
*(screen+1024*y+x) = 0x0067f0;
}
else
{
*(screen+1024*y+x) = 0x00FFFFFF;
}
}
}
munmap(screen, size); // 取消映射
close(fd); // 关闭文件
return 0;
}
2在开发板四个边显示矩形
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <linux/fb.h>
int main(int argc, char const *argv[])
{
struct fb_fix_screeninfo fb_fix;
struct fb_var_screeninfo fb_var;
unsigned int size;
int *screen = NULL;
//圆心的位置和半径的大小
int midlex = 512;
int midley = 300;
int R = 150;
//打开屏幕
int fd = open("/dev/fb0", O_RDWR);
if(fd < 0)
{
perror("open fd error");
exit(-1);
}
//获取显示屏信息并存储
ioctl(fd, FBIOGET_VSCREENINFO, &fb_var);
ioctl(fd, FBIOGET_FSCREENINFO, &fb_fix);
size = fb_fix.line_length * fb_var.yres;
int width = fb_var.xres;
int height = fb_var.yres;
int i = 0;
int x=0;
int y=0;
//内存映射
screen=(unsigned int *)mmap(NULL, size, PROT_WRITE|PROT_READ, MAP_SHARED, fd, 0);
for(y=0;y<height;y++)
{
for(x=0;x<width;x++)
{
if(x<200 && y<100)
{
screen[i]=0xff0000;
}
else if(x>1024-200 && y<100)
{
screen[i]=0xff0000;
}
else if(x<200 && y>600-100)
{
screen[i]=0xff0000;
}
else if(x>1024-200 && y>600-100)
{
screen[i]=0xff0000;
}
else
{
screen[i]=0xffffff;
}
i++;
}
}
munmap(screen, size); // 取消映射
close(fd); // 关闭文件
return 0;
}
3 在开发板上显示图片
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <linux/fb.h>
int main(int argc, char const *argv[])
{
struct fb_fix_screeninfo fb_fix;
struct fb_var_screeninfo fb_var;
unsigned int size;
//获取显示屏信息并存储
unsigned char bmpbuf[1024*600*3];
//step1.打开图片
int bmpfd = open("./test.bmp",O_RDWR);
if(bmpfd == -1)
{
perror("open bmp error");
exit(-1);
}
//step2.读图片的信息
lseek(bmpfd,54,SEEK_SET);
read(bmpfd,bmpbuf,1024*600*3);
//step3.打开LCD
int lcdfd = open("/dev/fb0",O_RDWR);
if(lcdfd < 0)
{
perror("open lcd error");
exit(-1);
}
ioctl(lcdfd, FBIOGET_VSCREENINFO, &fb_var);
ioctl(lcdfd, FBIOGET_FSCREENINFO, &fb_fix);
size = fb_fix.line_length * fb_var.yres;
int width = fb_var.xres;
int height = fb_var.yres;
int i = 0;
//step4.数据处理转化为RGB存储格式
unsigned int lcdbuf[1024*600];
for(i=0;i<1024*600;i++)
{
lcdbuf[i] = bmpbuf[3*i] | bmpbuf[3*i+1]<<8 | bmpbuf[3*i+2]<<16 | 0<<24;
}
//step5. 内存映射
int *screen = mmap(NULL, size, PROT_WRITE | PROT_READ, MAP_SHARED, lcdfd, 0);
if(screen == NULL)
{
perror("mamp error");
exit(-1);
}
//step6. 数据写入
int x,y;
for(y=0;y<600;y++)
{
for(x=0;x<1024;x++)
{
*(screen+x+y*1024) = lcdbuf[(599-y)*1024+x];
}
}
munmap(screen, size); // 取消映射
close(bmpfd); // 关闭文件
close(lcdfd);
return 0;
}