Advertisement
Guest User

Untitled

a guest
Apr 20th, 2013
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.05 KB | None | 0 0
  1. /*
  2.  *  This program can be used and distributed without restrictions.
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <signal.h>
  9.  
  10. #include <fcntl.h>        /* low-level i/o */
  11. #include <unistd.h>
  12. #include <errno.h>
  13. #include <malloc.h>
  14. #include <sys/types.h>
  15. #include <sys/mman.h>
  16. #include <sys/ioctl.h>
  17.  
  18. #include <linux/fb.h>
  19.  
  20.  
  21. #define TPASS 0
  22. #define TFAIL -1
  23.  
  24. int fd_fb = 0;
  25. unsigned short * fb;
  26. int g_fb_size;
  27. unsigned char *megabuff = NULL;
  28.  
  29. //-----------------------------------------------------------------------------
  30.  
  31. void showFrame(const void *p, size_t length)
  32. {
  33.    
  34.     //printf (".");
  35.  
  36.     int screenwidth=1024;
  37.     int screenheight = 768;
  38.  
  39.     memcpy ( fb, p, screenwidth*screenheight*3 );
  40.  
  41.     return ;
  42.    
  43. }
  44.  
  45.  
  46. //-----------------------------------------------------------------------------
  47.  
  48. int InitFrameBuffer(struct fb_var_screeninfo *screen_info, u_int32_t *screensize)
  49. {
  50.     u_int32_t retval;
  51.    
  52.     *screensize = 0;
  53.  
  54.     if ((fd_fb = open("/dev/fb0", O_RDWR, 0)) < 0)
  55.     {
  56.         printf("Unable to open /dev/fb0\n");
  57.         return TFAIL;
  58.     }
  59.  
  60.     retval = ioctl(fd_fb, FBIOGET_VSCREENINFO, screen_info);
  61.     if (retval < 0)
  62.     {
  63.         return TFAIL;
  64.     }
  65.     printf("Set the background to 16-bpp\n");
  66.     screen_info->bits_per_pixel = 16;
  67.     screen_info->yoffset = 0;
  68.     retval = ioctl(fd_fb, FBIOPUT_VSCREENINFO, screen_info);
  69.     if (retval < 0)
  70.     {
  71.         return TFAIL;
  72.     }
  73.     printf ("screen info xres=%d, yres=%d, bits per pixel =%d\n", screen_info->xres , screen_info->yres , screen_info->bits_per_pixel );
  74.     *screensize = screen_info->xres * screen_info->yres * screen_info->bits_per_pixel / 8;
  75.     g_fb_size = screen_info->xres * screen_info->yres_virtual * screen_info->bits_per_pixel / 8;
  76.  
  77.     //printf("\n Screen size = %u \n", *screensize);
  78.  
  79.     /* Map the device to memory*/
  80.     fb = (unsigned short *)mmap(0, g_fb_size,PROT_READ | PROT_WRITE, MAP_SHARED, fd_fb, 0);
  81.     if ((int)fb <= 0)
  82.     {
  83.         printf("\nError: failed to map framebuffer device 0 to memory.\n");
  84.         return TFAIL;
  85.     }
  86.  
  87.     retval = ioctl(fd_fb, FBIOGET_VSCREENINFO, screen_info);
  88.     if (retval < 0)
  89.     {
  90.         printf ("FBIOGET_VSCREENINFO failed\n");
  91.         return TFAIL;
  92.     }
  93.  
  94.     printf("Set the BG to 24-bpp\n");
  95.     screen_info->bits_per_pixel = 24;
  96.     retval = ioctl(fd_fb, FBIOPUT_VSCREENINFO, screen_info);
  97.     if (retval < 0)
  98.     {
  99.         return TFAIL;
  100.     }
  101.     printf("fb_test: xres_virtual = %d\n", screen_info->xres_virtual);
  102.     *screensize = screen_info->xres * screen_info->yres * screen_info->bits_per_pixel / 8;
  103.    
  104.    
  105.     return TPASS;
  106. }
  107.  
  108. //-----------------------------------------------------------------------------
  109.  
  110. void my_handler(int s){
  111.     if (megabuff) free(megabuff);
  112.   munmap(fb,g_fb_size);
  113.   close(fd_fb);
  114.     printf("exit\n");
  115.   exit(EXIT_SUCCESS);      
  116. }
  117.  
  118. //-----------------------------------------------------------------------------
  119.  
  120. int main(int argc, char **argv) {
  121.  
  122.     struct fb_var_screeninfo screen_info;
  123.     u_int32_t screensize;
  124.     struct sigaction sigIntHandler;
  125.  
  126.   if (InitFrameBuffer(&screen_info,&screensize))
  127.     {
  128.         printf ("Error. Failed\n");
  129.         return;
  130.     }
  131.     printf("\n At last screen size = %u \n", screensize);
  132.  
  133.   megabuff = malloc ( screensize );
  134.  
  135.   sigIntHandler.sa_handler = my_handler;
  136.   sigemptyset(&sigIntHandler.sa_mask);
  137.   sigIntHandler.sa_flags = 0;
  138.  
  139.   sigaction(SIGINT, &sigIntHandler, NULL);
  140.  
  141.  
  142.   int i=0,val;
  143.  
  144.   u_int32_t hAct_high,vAct_high = 0;
  145.   u_int32_t vcounter=0,retval;
  146.  
  147.     #if 0
  148.     while (vAct_high == 0)
  149.     {  
  150.         retval = ioctl(fd_fb, FBIOGET_VSCREENINFO, &screen_info);
  151.         if (retval < 0)
  152.         {
  153.             printf ("FBIOGET_VSCREENINFO failed\n");
  154.             break;
  155.         }
  156.         hAct_high = (screen_info.sync & FB_SYNC_HOR_HIGH_ACT) != 0;
  157.         vAct_high = (screen_info.sync & FB_SYNC_VERT_HIGH_ACT) != 0;
  158.         vcounter++;
  159.  
  160.     }
  161.   printf("hsync %d vsync %d\n",hAct_high,vAct_high);    
  162.   #endif
  163.          
  164.     while (1)
  165.     {
  166.    
  167.         if ( i == 0 ) val = 0x00;
  168.         if ( i == 1 ) val = 0x80;
  169.         if ( i == 2 ) val = 0xff;
  170.         memset(megabuff,val,screensize);       
  171.         printf("val %d\n",val);
  172.         showFrame (megabuff, screensize);
  173.         usleep(40);
  174.  
  175.         i++;
  176.         if ( i>2 ) i = 0;
  177.     }
  178.    
  179.   return 0;
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement