linux驅動開發:用戶空間操作LCD顯示簡單的圖片

上一章我們簡單介紹了LCD的一些基本原理。當然更深奧的還有,比如gamma,dither,HUE,satuation.OSD等等.

我們知道我們是用framebuffer來實現顯示的.

顯存:framebuffer.由DDRAM中劃去一部分內存供顯存使用.
從而操作lcd相當於操作顯存.
lcd控制器(s5pv210裏面有lcd控制器)會週期的獲取framebuffer中的數據。經過處理丟給 顯示屏的lcd 驅動器
lcd驅動器分析,解碼,將數據顯示到lcd上.

lcd驅動開發的工作:
配置lcd 控制器,讓lcd 控制器週期性的讀取顯存中的數據
然後按照一定的時序和格式講數據發送給lcd驅動器.驅動器會驅動lcd屏進行顯示.
1)申請顯存

framebuffer:導出lcd物理緩衝區(顯存)導入到用戶空間(0-3G)
用戶空間要顯示一幅圖片到lcd,在用戶空間直接操作顯存,將要顯示的圖片copy到
顯存中的相應位置上就可以了


這邊就先不多講了,因爲時間太晚,太累。我們畫一個這樣的圖片:
這裏寫圖片描述

用戶空間代碼:

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <linux/fb.h>
#include <sys/mman.h>


#define _COLOR_RED      0x00ff0000
#define _COLOR_GREEN    0x0000ff00
#define _COLOR_BLUE     0x000000ff

static struct fb_fix_screeninfo fb_fix ={0};
static struct fb_var_screeninfo fb_var ={0};

long screen_size=0;

int *fb32 =NULL;
int main()
{
    int fd = -1;
    int x,y;
    fd =open("/dev/fb0",O_RDWR);
    if(fd < 0)
    {
        printf("open dev fb0 fail.\n");
        return -1;
    }
    //get  lcd param
    ioctl(fd,FBIOGET_FSCREENINFO,&fb_fix);

    ioctl(fd,FBIOGET_VSCREENINFO,&fb_var);

    screen_size = fb_var.xres*fb_var.yres*(fb_var.bits_per_pixel/8);

    fb32 =mmap(0,screen_size,PROT_READ |PROT_WRITE,MAP_SHARED,fd,0);

    if(fb32 == NULL)
    {
        printf("mmap framebuffer fail.\n");
        return -1;
    }

    if(fb_var.bits_per_pixel == 8)
    {
        printf("8bpp framebuffer test.\n");
    }
    else if(fb_var.bits_per_pixel == 16)
    {
        printf("16bpp framebuffer test.\n");
    }
    else if(fb_var.bits_per_pixel == 24)
    {
        printf("24bpp framebuffer test.\n");
    }
    else if(fb_var.bits_per_pixel == 32)
    {
        printf("32bpp framebuffer test.\n");
    }
    for(y=0;y< fb_var.yres/3;y++)
    {
        for(x=0;x< fb_var.xres;x++)
        {
            *(fb32 +y*fb_var.xres + x) = _COLOR_RED;
        }
    }

    for(;y< fb_var.yres*2/3;y++)
    {
        for(x=0;x< fb_var.xres;x++)
        {
            *(fb32 +y*fb_var.xres + x) = _COLOR_GREEN;
        }
    }

    for(;y< fb_var.yres;y++)
    {
        for(x=0;x< fb_var.xres;x++)
        {
            *(fb32 +y*fb_var.xres + x) = _COLOR_BLUE;
        }
    }
    munmap(fb32,screen_size);
    close(fd);
    return 0;
}

實驗效果:
這裏寫圖片描述

可以看到,和我們預期的一樣。其中那邊有一個小黑框那是光標,我們不管。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章