首页 > 代码库 > E-BOOK-TINY6410-LCD的使用

E-BOOK-TINY6410-LCD的使用

电子书需要通过屏幕显示出来,首先写了LCD模块。代码上传到了 github https://github.com/qq2216691777/E-book

本次完善了lcd模块的程序。可以适用在其他地方。

代码:fb.c  fb.h

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


#include "fb.h"
#include "main.h"

static int Device_FB_Init(void);
static void lcd_put_pixel(int x, int y, unsigned int color );
static void lcd_clean( unsigned int color );
static void Device_FB_Destory(void);

fb_t g_fb={
            0,
            0,
            0,
            0,
            0,
            0,
            NULL,
            &Device_FB_Init,
            &lcd_put_pixel,
            &lcd_clean,
            &Device_FB_Destory,
};

static void lcd_put_pixel(int x, int y, unsigned int color )
{
    unsigned char *pen_8 = g_fb.fbmem + y*g_fb.xres*g_fb.pixel_chars+x*g_fb.pixel_chars;
    unsigned short *pen_16 = (unsigned short *)pen_8;
    unsigned short *pen_32 = (unsigned short *)pen_8;
    unsigned char red,blue,green;
    switch(g_fb.pixel_bits)
    {
        case 8:
            *pen_8 = color;
            break;
        case 16:
            /* 565 */
            red = (color>>16) & 0xff;
            green = (color>>8) & 0xff;
            blue = (color>>0) & 0xff;
            color = ((red>>3)<<11) | ((green>>2)<<5) |((blue>>3));
            *pen_16 = color;
               break;
           case 32:
               *pen_32 = color;
               break;
           default:
               DEBUG_PRINT("ERROR: can‘t support %ddpp\n",g_fb.pixel_bits);
               break;
    }
}

static void lcd_clean( unsigned int color )
{
    unsigned char *pen_8 = g_fb.fbmem;
    unsigned short *pen_16 = (unsigned short *)pen_8;
    unsigned short *pen_32 = (unsigned short *)pen_8;
    unsigned char red,blue,green;
    int i = g_fb.screen_size;
    switch(g_fb.pixel_bits)
    {
        case 8:
            break;
        case 16:
            /* 565 */
            red = (color>>16) & 0xff;
            green = (color>>8) & 0xff;
            blue = (color>>0) & 0xff;
            color = ((red>>3)<<11) | ((green>>2)<<5) |((blue>>3));
               break;
           case 32:
               break;
           default:
               DEBUG_PRINT("ERROR: can‘t support %ddpp\n",g_fb.pixel_bits);
               return;
    }
    while(i)
    {
        switch(g_fb.pixel_bits)
        {
            case 8:
                *pen_8 = color;
                pen_8++;
                i--;
                break;
            case 16:
                *pen_16 = color;
                pen_16++;
                i-=2;
                break;
            case 32:
                *pen_32 = color;
                pen_32++;
                i-=4;
                break;

        }

    }


}

static int Device_FB_Init(void)
{
    int fb_fd;
    struct fb_fix_screeninfo fix;
    struct fb_var_screeninfo var;


    fb_fd = open(DEVICE_NAME, O_RDWR);
    if (fb_fd<0)
    {
        DEBUG_PRINT("open device fb failed\n");
        return -1;
    }
    if( ioctl( fb_fd,  FBIOGET_VSCREENINFO, &var ) )
    {
        DEBUG_PRINT("can‘t get var\n");
        return -1;
    }

    if( ioctl( fb_fd,  FBIOGET_FSCREENINFO, &fix ) )
    {
        DEBUG_PRINT("can‘t get fix\n");
        return -1;
    }

    g_fb.fd = fb_fd;
    g_fb.xres = var.xres;
    g_fb.yres = var.yres;
    g_fb.pixel_bits = var.bits_per_pixel;
    g_fb.pixel_chars = var.bits_per_pixel/8;
    g_fb.screen_size = var.xres*var.yres*var.bits_per_pixel/8;
    g_fb.fbmem = (unsigned char *)mmap( NULL, g_fb.screen_size,  PROT_READ | PROT_WRITE, MAP_SHARED,fb_fd,0 );

    printf("xres:%d\n", g_fb.xres);
    printf("yres:%d\n", g_fb.yres);
    printf("pixel bit:%d\n", g_fb.pixel_bits);
    printf("pixel char:%d\n", g_fb.pixel_chars);
    return 0;


}

static void Device_FB_Destory(void)
{
    free(g_fb.fbmem);
}
#ifndef _FB_H__
#define _FB_H__

#define DEVICE_NAME    "/dev/fb0"

typedef struct
{
    int fd;
    int xres;
    int yres;
    int pixel_bits;
    int pixel_chars;
    int screen_size;
    unsigned char *fbmem;
    void (*Devie_Init)(void);
    void (*put_pixel)(int x, int y, unsigned int color );
    void (*cleanscreen)( unsigned int color );
    void (*Devie_Destory)(void);

}fb_t;

extern fb_t g_fb;


#endif

 

E-BOOK-TINY6410-LCD的使用