对Framebuffer的操作

转载 2016年05月30日 21:26:54
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <stdint.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <asm/page.h>
#include <linux/fb.h>

#define TRUE    1
#define FALSE   0

#define MIN(x,y)        ((x)>(y)?(y):(x))
#define MAX(x,y)        ((x)>(y)?(x):(y))

typedef struct fbdev{
        int fb;
        unsigned long fb_mem_offset;
        unsigned long fb_mem;
        struct fb_fix_screeninfo fb_fix;
        struct fb_var_screeninfo fb_var;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <stdint.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <asm/page.h>
#include <linux/fb.h>

#define TRUE    1
#define FALSE   0

#define MIN(x,y)        ((x)>(y)?(y):(x))
#define MAX(x,y)        ((x)>(y)?(x):(y))

typedef struct fbdev{
        int fb;
        unsigned long fb_mem_offset;
        unsigned long fb_mem;
        struct fb_fix_screeninfo fb_fix;
        struct fb_var_screeninfo fb_var;
        char dev[20];
} FBDEV, *PFBDEV;

typedef struct point{
        int x;
        int y;
        int z;

} POINT, *PPOINT;

int fb_open(PFBDEV pFbdev)
{
        pFbdev->fb = open(pFbdev->dev, O_RDWR);
        if(pFbdev->fb < 0)
        {
                printf("Error opening %s: %m. Check kernel config\n", pFbdev->dev);
                return FALSE;
        }
        if (-1 == ioctl(pFbdev->fb,FBIOGET_VSCREENINFO,&(pFbdev->fb_var)))
        {
                printf("ioctl FBIOGET_VSCREENINFO\n");
                return FALSE;
        }
        if (-1 == ioctl(pFbdev->fb,FBIOGET_FSCREENINFO,&(pFbdev->fb_fix)))
        {
                printf("ioctl FBIOGET_FSCREENINFO\n");
                return FALSE;
        }

        pFbdev->fb_mem_offset = (unsigned long)(pFbdev->fb_fix.smem_start) & (~PAGE_MASK);
        pFbdev->fb_mem = (unsigned long int)mmap(NULL, pFbdev->fb_fix.smem_len +
                        pFbdev->fb_mem_offset,
                        PROT_READ | PROT_WRITE, MAP_SHARED, pFbdev->fb, 0);
        if (-1L == (long) pFbdev->fb_mem)
        {
                printf("mmap error! mem:%ld offset:%ld\n", pFbdev->fb_mem,
                                pFbdev->fb_mem_offset);
                return FALSE;
        }

        return TRUE;
}

void fb_close(PFBDEV pFbdev)
{
        close(pFbdev->fb);
        pFbdev->fb=-1;
}

void draw_dot (PFBDEV pFbdev, POINT p, uint8_t r, uint8_t g, uint8_t b)
{
        uint32_t offset;
        uint8_t color[4];
        color[0] = b;
        color[1] = g;
        color[2] = r;
        color[3] = 0x0;
        offset = p.y*pFbdev->fb_fix.line_length+4*p.x;
        fb_memcpy((void*)(pFbdev->fb_mem + pFbdev->fb_mem_offset + offset), color, 4);
}


void draw_line(PFBDEV pFbdev, POINT p1, POINT p2, uint8_t r, uint8_t g, uint8_t b)
{
        POINT p;
        int x,y;
        for( x=MIN(p1.x,p2.x); x<=MAX(p1.x,p2.x); x++ )
        {
                y = (p2.y-p1.y)*(x-p1.x)/(p2.x-p1.x) + p1.y;
                p.x = x;
                p.y = y;
                draw_dot(pFbdev, p, r, g, b);
        }
}

void draw_triangle(PFBDEV pFbdev, POINT p1, POINT p2, POINT p3, uint8_t r, uint8_t g, uint8_t b)
{
        POINT p[3];
        p[0] = p1;
        p[1] = p2;
        p[2] = p3;
        draw_polygon(pFbdev, 3, p, r, g, b);
}

void draw_polygon(PFBDEV pFbdev, uint32_t num, PPOINT array, uint8_t r, uint8_t g, uint8_t b)
{
        int i;
        for(i=0; i<num; i++)
        {
                draw_line(pFbdev, array[i], array[(i+1)%num], r, g, b);
        }
}


void draw_circle(PFBDEV pFbdev, POINT center, uint32_t radius, uint8_t r, uint8_t g, uint8_t b)
{
        POINT p;
        int x,y,tmp;
        for(x=center.x-radius; x<=center.x+radius; x++)
        {
                p.x = x;
                tmp = sqrt(radius*radius -(x-center.x)*(x-center.x));
                p.y = center.y + tmp;
                draw_dot(pFbdev, p, r, g, b);
                p.y = center.y - tmp;
                draw_dot(pFbdev, p, r, g, b);
        }
        for(y=center.y-radius; y<=center.y+radius; y++)
        {
                p.y = y;
                tmp = sqrt(radius*radius - (y-center.y)*(y-center.y));
                p.x = center.x + tmp;
                draw_dot(pFbdev, p, r, g, b);
                p.x = center.x - tmp;
                draw_dot(pFbdev, p, r, g, b);
        }
}

void draw_parabola_x(PFBDEV pFbdev, POINT center, int a, uint8_t r, uint8_t g, uint8_t b)
{
        int x;
        POINT p;
        for(x=center.x-100; x<center.x+100; x++)
        {
                p.x = x;
                p.y = (x-center.x)*(x-center.x)/a + center.y;
                draw_dot(pFbdev, p, r, g, b);
        }
}


void draw_parabola_y(PFBDEV pFbdev, POINT center, int a, uint8_t r, uint8_t g, uint8_t b)
{
        int y;
        POINT p;
        for(y=center.y-100; y<center.y+100; y++)
        {
                p.y = y;
                p.x = (y-center.y)*(y-center.y)/a + center.x;
                draw_dot(pFbdev, p, r, g, b);
        }
}

int main(void)
{
        FBDEV fb;
        memset(&fb, 0, sizeof(FBDEV));
        strcpy(fb.dev, "/dev/fb0");
        if(fb_open(&fb)==FALSE)
        {
                printf("open frame buffer error\n");
                return 0;
        }

        POINT p1, p2, p3;

        p1.x = 300;
        p1.y = 450;
        p2.x = 500;
        p2.y = 300;
        draw_line( &fb, p1, p2, 0x0, 0xff, 0x0 );

        p1.x = 200;
        p1.y = 200;
        p2.x = 300;
        p2.y = 400;
        p3.x = 500;
        p3.y = 250;
        draw_triangle(&fb, p1, p2, p3, 0x0, 0xff, 0x0);

        POINT center = { 600, 500, 0 };
        int radius = 100;
        draw_circle(&fb, center, radius, 0x0, 0xff, 0x0);

        center.x = 700;
        center.y = 250;
        int a = -100;
        draw_parabola_x(&fb, center, a, 0x0, 0xff, 0x0);
        draw_parabola_x(&fb, center, -a, 0x0, 0xff, 0x0);
        draw_parabola_y(&fb, center, a, 0x0, 0xff, 0x0);
        draw_parabola_y(&fb, center, -a, 0x0, 0xff, 0x0);

        fb_close(&fb);
        return 0;
}

相关文章推荐

直接操作Framebuffer测试液晶屏

自己写的一个液晶屏测试小程序,直接写frambuffer,总共四幅画面,如下所示: 程序很简单,就是进行内存映射之后写frambuffer。这个程序是在华清远见的一个程序上改的,目前...

android下操作FrameBuffer

可以同时参考另外几篇详细文章: 基于S3C2440的嵌入式Linux驱动——Framebuffer子系统解读  linux LCD驱动(二)--FrameBuffer Lin...

Android操作framebuffer

Android操作framebuffer 操作framebuffer的主要步骤如下: 1、打开一个可用的FrameBuffer设备; 2、通过mmap调用把显卡的物理内存空间映射到用户空间;...

android下操作FrameBuffer

一、framebuffer使用基础: 1. Linux是工作在保护模式下,所以用户态进程是无法象DOS那样使用显卡BIOS里提供的中断调用来实现直接写屏, Linux抽象出FrameBuffer这个...

android framebuffer 操作-进度条显示

一、framebuffer使用基础:    1. Linux是工作在保护模式下,所以用户态进程是无法象DOS那样使用显卡BIOS里提供的中断调用来实现直接写屏, Linux抽象出FrameBuf...

Android操作framebuffer

操作framebuffer的主要步骤如下:1、打开一个可用的FrameBuffer设备;2、通过mmap调用把显卡的物理内存空间映射到用户空间;3、更改内存空间里的像素数据并显示;4、退出时关闭fra...

【Linux】FrameBuffer操作入门

所有的这些操作,都是在控制台界面下,root登录。 一, $ cat /dev/fb0 > sreensnap /获取一屏的数据/ $ clear ...

linux下实现对framebuffer(/dev/fb0)的截屏操作

在linux系统中,使用framebuffer来提供用户态进程直接操作显示屏的功能. 在嵌入式系统开发中,需要对显示屏的内容进行截取,实现一个lcd截屏工具实现对显示屏内容的截取,存储为bmp格式....

linux直接写framebuffer linux 直接 对 Frame Buffer 操作,写画面缓存例子,c语言读写framebuffer

提供的中断调用来实现直接写屏,故Linux抽象出FrameBuffer这个设备来供用户态   进程实现直接写屏。   在继续下面的之前,先说明几个背景知识:   1、FrameBuffer主要是...

LINUX_framebuffer_show_word

  • 2013-04-14 11:39
  • 3.47MB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)