昨天做的一个极简单的framebuffer的例子,用来学习怎样操作fb设备。
这段代码是在picogl的vesafb backend部分的基础上简出来的,所以变量名还保留着。
流程如下:
1 打开framebuffer设备;
2 通过ioctl取得fixed screen information;
3 通过ioctl取得variable screen information;
4 通过mmap映射设备内存到进程空间;
5 写framebuffer;
6 终止。
- #include
- #include
- #include
- #include
- #include
- #include
- struct fb_fix_screeninfo FixedInfo;
- struct fb_var_screeninfo OrigVarInfo;
- static int FrameBufferFD = -1;
- void *FrameBuffer = ( void *) -1;
- void openFBDEV( void ) {
-
- FrameBufferFD = open( "/dev/fb0" , O_RDWR);
- if (FrameBufferFD < 0) {
- fprintf(stderr, "Error opening /dev/fb0/n" );
- exit(1);
- }
-
- if (ioctl(FrameBufferFD, FBIOGET_FSCREENINFO, &FixedInfo)) {
- fprintf(stderr, "error: ioctl(FBIOGET_FSCREENINFO) failed/n" );
- exit(1);
- }
-
- if (ioctl(FrameBufferFD, FBIOGET_VSCREENINFO, &OrigVarInfo)) {
- fprintf(stderr, "error: ioctl(FBIOGET_VSCREENINFO) failed/n" );
- exit(1);
- }
-
- if (FixedInfo.visual != FB_VISUAL_TRUECOLOR && FixedInfo.visual != FB_VISUAL_DIRECTCOLOR) {
- fprintf(stderr, "non-TRUE/DIRECT-COLOR visuals (0x%x) not supported by this demo./n", FixedInfo.visual);
- exit(1);
- }
-
-
- FrameBuffer = ( void *) mmap(0,
- FixedInfo.smem_len,
- PROT_READ | PROT_WRITE,
- MAP_SHARED,
- FrameBufferFD,
- 0 );
- if (FrameBuffer == ( void *) - 1) {
- fprintf(stderr, "error: unable to mmap framebuffer/n" );
- exit(1);
- }
- }
- void closeFBDEV( void ) {
- munmap(FrameBuffer, FixedInfo.smem_len);
- close(FrameBufferFD);
- }
- int main() {
- openFBDEV();
- fprintf(stderr, "openFBDEV finish/n" );
- memset(FrameBuffer, 128, FixedInfo.smem_len);
- sleep(5);
- closeFBDEV();
- fprintf(stderr, "closeFBDEV finish/n" );
- return 0;
- }
- 补充一点,比如直接写屏操作:
- u_int * screen_memory = (u_int*)mmap(0,
buffer_size,//fixinfo.smem_len,
PROT_READ | PROT_WRITE,
MAP_SHARED,
fbdev,
0); - for(i=0;i *(screen_memory+i) = 0xff; //red
sleep(2);
for(i=0;i *(screen_memory+i) = 0xff00;//green
sleep(2);
for(i=0;i *(screen_memory+i) = 0xff0000;//blue
sleep(2);
阅读(267) | 评论(0) | 转发(0) |