在ARM板子上显示圆形,矩形和图像

本次实验使用的开发板是正点原子的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;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值