LCD 應用程序

#include <unistd.h>
#include <stdlib.h>

#include <fcntl.h>
#include <linux/fb.h>
#include <sys/mman.h>
int main()
{
    int fbfd = 0;
    struct fb_var_screeninfo vinfo;
    struct fb_fix_screeninfo finfo;
    long int screensize = 0;
    char *fbp = 0;
    int x = 0, y = 0;
    long int location = 0;

   
// Open the file for reading and writing

     fbfd = open("/dev/fb0", O_RDWR);
    if (!fbfd) {
        printf("Error: cannot open framebuffer device.\n");
        exit(1);
    }
    printf("The framebuffer device was opened successfully.\n");

   
// Get fixed screen information

   
//FBIOGET_FSCREENINFO獲得固定的屏幕參數設置

    if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo)) {
        printf("Error reading fixed information.\n");
        exit(2);
    }

    printf("%d, %d, %d, %d\n", finfo.smem_start, finfo.smem_len, finfo.mmio_start,finfo.mmio_len);
   
//869007360, 153600, 0, 0

   
// Get variable screen information

   
//FBIOGET_VSCREENINFO 獲得可變的屏幕參數

    if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo)) {
        printf("Error reading variable information.\n");
        exit(3);
    }

    printf("%dx%d, %dbpp\n", vinfo.xres, vinfo.yres, vinfo.bits_per_pixel);
   
//320X240,16bpp bpp 每像素位數 每個點用多少個字節表示


   
// Figure out the size of the screen in bytes

     screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;
   
//2^16=64K TFT 320*240*16/8=153600byte


   
// Map the device to memory

   
/*void *mmap(void *start, size_t length, int prot, int flags,
  int fd, off_t offset);mmap函數是unix/linux下的系統調用
     fbmem.c是內核的,應用程序調用了mmap進入內核空間就是執行的fb_mmap了,
     應用程序是統一的接口,但是在內核空間,不同的驅動使用的實現函數是不一樣的*/

   
//#include <unistd.h> and #include <sys/mman.h> /usr/include/sys/mman.h

     fbp = (char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED,
                        fbfd, 0);
    if ((int)fbp == -1) {
        printf("Error: failed to map framebuffer device to memory.\n");
        exit(4);
    }
    printf("The framebuffer device was mapped to memory successfully.\n");
#if 1
     x = 10; y = 10;
// Where we are going to put the pixel


   
// Figure out where in memory to put the pixel

    for (y = 10; y < 20; y++)
        for (x = 10; x < 30; x++)
        {
             location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) +
                       (y+vinfo.yoffset) * finfo.line_length;

            if (vinfo.bits_per_pixel == 32)
            {
                *(fbp + location) = 10;
// Some blue

                *(fbp + location + 1) = 15+(x-10)/2;
// A little green

                *(fbp + location + 2) = 20-(y-10)/5;
// A lot of red

                *(fbp + location + 3) = 0;
// No transparency

            }
            else
            {
//assume 16bpp

                unsigned short b = 10;
                unsigned short g = (x-10)/6;
// A little green

                unsigned short r = 31-(y-10)/16;
// A lot of red

                unsigned short t = r<<11 | g << 5 | b;
                *((unsigned short *)(fbp + location)) = t;
   
//     printf("x=%d,%d\n",x,y);

            }
        }
#endif
     munmap(fbp, screensize);
//刪除特定地址區域的對象映射 int munmap(void *start, size_t length);

    printf("The framebuffer device was munmapped to memory successfully.\n");
    close(fbfd);
    printf("The framebuffer device was closed successfully.\n");
    return 0;
}

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