Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * This program can be used and distributed without restrictions.
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <signal.h>
- #include <fcntl.h> /* low-level i/o */
- #include <unistd.h>
- #include <errno.h>
- #include <malloc.h>
- #include <sys/types.h>
- #include <sys/mman.h>
- #include <sys/ioctl.h>
- #include <linux/fb.h>
- #define TPASS 0
- #define TFAIL -1
- int fd_fb = 0;
- unsigned short * fb;
- int g_fb_size;
- unsigned char *megabuff = NULL;
- //-----------------------------------------------------------------------------
- void showFrame(const void *p, size_t length)
- {
- //printf (".");
- int screenwidth=1024;
- int screenheight = 768;
- memcpy ( fb, p, screenwidth*screenheight*3 );
- return ;
- }
- //-----------------------------------------------------------------------------
- int InitFrameBuffer(struct fb_var_screeninfo *screen_info, u_int32_t *screensize)
- {
- u_int32_t retval;
- *screensize = 0;
- if ((fd_fb = open("/dev/fb0", O_RDWR, 0)) < 0)
- {
- printf("Unable to open /dev/fb0\n");
- return TFAIL;
- }
- retval = ioctl(fd_fb, FBIOGET_VSCREENINFO, screen_info);
- if (retval < 0)
- {
- return TFAIL;
- }
- printf("Set the background to 16-bpp\n");
- screen_info->bits_per_pixel = 16;
- screen_info->yoffset = 0;
- retval = ioctl(fd_fb, FBIOPUT_VSCREENINFO, screen_info);
- if (retval < 0)
- {
- return TFAIL;
- }
- printf ("screen info xres=%d, yres=%d, bits per pixel =%d\n", screen_info->xres , screen_info->yres , screen_info->bits_per_pixel );
- *screensize = screen_info->xres * screen_info->yres * screen_info->bits_per_pixel / 8;
- g_fb_size = screen_info->xres * screen_info->yres_virtual * screen_info->bits_per_pixel / 8;
- //printf("\n Screen size = %u \n", *screensize);
- /* Map the device to memory*/
- fb = (unsigned short *)mmap(0, g_fb_size,PROT_READ | PROT_WRITE, MAP_SHARED, fd_fb, 0);
- if ((int)fb <= 0)
- {
- printf("\nError: failed to map framebuffer device 0 to memory.\n");
- return TFAIL;
- }
- retval = ioctl(fd_fb, FBIOGET_VSCREENINFO, screen_info);
- if (retval < 0)
- {
- printf ("FBIOGET_VSCREENINFO failed\n");
- return TFAIL;
- }
- printf("Set the BG to 24-bpp\n");
- screen_info->bits_per_pixel = 24;
- retval = ioctl(fd_fb, FBIOPUT_VSCREENINFO, screen_info);
- if (retval < 0)
- {
- return TFAIL;
- }
- printf("fb_test: xres_virtual = %d\n", screen_info->xres_virtual);
- *screensize = screen_info->xres * screen_info->yres * screen_info->bits_per_pixel / 8;
- return TPASS;
- }
- //-----------------------------------------------------------------------------
- void my_handler(int s){
- if (megabuff) free(megabuff);
- munmap(fb,g_fb_size);
- close(fd_fb);
- printf("exit\n");
- exit(EXIT_SUCCESS);
- }
- //-----------------------------------------------------------------------------
- int main(int argc, char **argv) {
- struct fb_var_screeninfo screen_info;
- u_int32_t screensize;
- struct sigaction sigIntHandler;
- if (InitFrameBuffer(&screen_info,&screensize))
- {
- printf ("Error. Failed\n");
- return;
- }
- printf("\n At last screen size = %u \n", screensize);
- megabuff = malloc ( screensize );
- sigIntHandler.sa_handler = my_handler;
- sigemptyset(&sigIntHandler.sa_mask);
- sigIntHandler.sa_flags = 0;
- sigaction(SIGINT, &sigIntHandler, NULL);
- int i=0,val;
- u_int32_t hAct_high,vAct_high = 0;
- u_int32_t vcounter=0,retval;
- #if 0
- while (vAct_high == 0)
- {
- retval = ioctl(fd_fb, FBIOGET_VSCREENINFO, &screen_info);
- if (retval < 0)
- {
- printf ("FBIOGET_VSCREENINFO failed\n");
- break;
- }
- hAct_high = (screen_info.sync & FB_SYNC_HOR_HIGH_ACT) != 0;
- vAct_high = (screen_info.sync & FB_SYNC_VERT_HIGH_ACT) != 0;
- vcounter++;
- }
- printf("hsync %d vsync %d\n",hAct_high,vAct_high);
- #endif
- while (1)
- {
- if ( i == 0 ) val = 0x00;
- if ( i == 1 ) val = 0x80;
- if ( i == 2 ) val = 0xff;
- memset(megabuff,val,screensize);
- printf("val %d\n",val);
- showFrame (megabuff, screensize);
- usleep(40);
- i++;
- if ( i>2 ) i = 0;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement