Advertisement
Guest User

fbTest.c

a guest
Mar 11th, 2013
532
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.52 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. #include <fcntl.h>
  5. #include <linux/fb.h>
  6. #include <sys/mman.h>
  7. #include <sys/ioctl.h>
  8.  
  9. int main()
  10. {
  11.     int fbfd = 0;
  12.     struct fb_var_screeninfo vinfo;
  13.     struct fb_fix_screeninfo finfo;
  14.     long int screensize = 0;
  15.     char *fbp = 0;
  16.     int x = 0, y = 0;
  17.     long int location = 0;
  18.  
  19.     // Open the file for reading and writing
  20.     fbfd = open("/dev/fb1", O_RDWR);
  21.     if (fbfd == -1) {
  22.         perror("Error: cannot open framebuffer device");
  23.         exit(1);
  24.     }
  25.     printf("The framebuffer device was opened successfully.\n");
  26.  
  27.     // Get fixed screen information
  28.     if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo) == -1) {
  29.         perror("Error reading fixed information");
  30.         exit(2);
  31.     }
  32.  
  33.     // Get variable screen information
  34.     if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo) == -1) {
  35.         perror("Error reading variable information");
  36.         exit(3);
  37.     }
  38.  
  39.     printf("%dx%d, %dbpp\n", vinfo.xres, vinfo.yres, vinfo.bits_per_pixel);
  40.  
  41.     // Figure out the size of the screen in bytes
  42.     screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;
  43.  
  44.     // Map the device to memory
  45.     fbp = (char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, fbfd, 0);
  46.     if ((int)fbp == -1) {
  47.         perror("Error: failed to map framebuffer device to memory");
  48.         exit(4);
  49.     }
  50.     printf("The framebuffer device was mapped to memory successfully.\n");
  51.  
  52.     x = 0; y = 0;       // Where we are going to put the pixel
  53.  
  54.     // Figure out where in memory to put the pixel
  55.     for (y = 0; y < 200; y++)
  56.         for (x = 0; x < 200; x++) {
  57.  
  58.             location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) +
  59.                        (y+vinfo.yoffset) * finfo.line_length;
  60.  
  61.             if (vinfo.bits_per_pixel == 32) {
  62.                 *(fbp + location) = 100;        // Some blue
  63.                 *(fbp + location + 1) = 15+(x-100)/2;     // A little green
  64.                 *(fbp + location + 2) = 200-(y-100)/5;    // A lot of red
  65.                 *(fbp + location + 3) = 0;      // No transparency
  66.         //location += 4;
  67.             } else  { //assume 16bpp
  68.                 int b = 10;
  69.                 int g = (x-100)/6;     // A little green
  70.                 int r = 31-(y-100)/16;    // A lot of red
  71.                 unsigned short int t = r<<11 | g << 5 | b;
  72.                 *((unsigned short int*)(fbp + location)) = t;
  73.             }
  74.  
  75.         }
  76.     munmap(fbp, screensize);
  77.     close(fbfd);
  78.     return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement