• 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
    ",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
    ",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
    ");
            return -1;
        }
        if( ioctl( fb_fd,  FBIOGET_VSCREENINFO, &var ) )
        {
            DEBUG_PRINT("can't get var
    ");
            return -1;
        }
    
        if( ioctl( fb_fd,  FBIOGET_FSCREENINFO, &fix ) )
        {
            DEBUG_PRINT("can't get fix
    ");
            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
    ", g_fb.xres);
        printf("yres:%d
    ", g_fb.yres);
        printf("pixel bit:%d
    ", g_fb.pixel_bits);
        printf("pixel char:%d
    ", 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
  • 相关阅读:
    变量定义和声明的差别(整理)
    堆栈指针理解
    HDU 4349 Xiao Ming&#39;s Hope
    iOS 8中CLLocationManager及MKMapView showUserLocation失败的解决的方法
    Ant命令行操作
    linux awk命令详细使用方法
    mysql 修改[取消]timestamp的自动更新
    cocos2d-x 3.0游戏实例学习笔记《卡牌塔防》第六步---炮台&amp;点击炮台加入英雄&amp;英雄升级
    SendMessage、PostMessage原理
    poj 2104 K-th Number 主席树+超级详细解释
  • 原文地址:https://www.cnblogs.com/ynxf/p/6344659.html
Copyright © 2020-2023  润新知