简单例子操作tq210,/dev/fb0,显示颜色。内核中结构体fb_var_screeninfo,用来描述有关屏幕信息。
arm-linux-gcc 编译后,串口rz下载或者tftp下载到板子上运行。
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <unistd.h>
#include <signal.h>
#include <linux/fb.h>
#include <stdbool.h>
/**
struct fb_var_screeninfo {
__u32 xres; // visible resolution
__u32 yres;
__u32 xres_virtual; // virtual resolution
__u32 yres_virtual;
__u32 xoffset; // offset from virtual to visible
__u32 yoffset; // resolution
__u32 bits_per_pixel; //guess what
__u32 grayscale; / 0 = color, 1 = grayscale,
/>1 = FOURCC
struct fb_bitfield red; / bitfield in fb mem if true color,
struct fb_bitfield green; /else only length is significant
struct fb_bitfield blue;
struct fb_bitfield transp; transparency
__u32 nonstd; != 0 Non standard pixel format
__u32 activate; /see FB_ACTIVATE_
__u32 height; / height of picture in mm
__u32 width; / width of picture in mm
__u32 accel_flags; / (OBSOLETE) see fb_info.flags
/* Timing: All values in pixclocks, except pixclock (of course)
__u32 pixclock; /* pixel clock in ps (pico seconds)
__u32 left_margin; /* time from sync to picture
__u32 right_margin; /* time from picture to sync
__u32 upper_margin; /* time from sync to picture
__u32 lower_margin;
__u32 hsync_len; /* length of horizontal sync
__u32 vsync_len; /* length of vertical sync
__u32 sync; /* see FB_SYNC_*
__u32 vmode; /* see FB_VMODE_*
__u32 rotate; /* angle we rotate counter clockwise
__u32 colorspace; /* colorspace for FOURCC-based modes
__u32 reserved[4]; /* Reserved for future compatibility
};
*/
char* plcd = NULL;
bool terminated = false;
//typedef void (*sighandler_t)(int);
void sighandler(int sig){
switch(sig){
case SIGINT:
terminated = true;
break;
}
}
void LCD_DrawPoint(unsigned long x , unsigned long y, int color)
{
int *p =(int *) plcd;
*(p + y * 800 + x) = color;
}
//draw screen with color
void LCD_ClearScr(int color)
{
unsigned long x, y;
for (y = 0; y < 480; y++)
{
for (x = 0; x < 800; x++)
{
LCD_DrawPoint(x, y, color) ;
}
}
}
int main(int argc,char** argv){
printf("hello tq210\n");
signal(SIGINT,sighandler);//sighandler_t signal(int signum, sighandler_t handler);
int fd = open("/dev/fb0",O_RDWR);
if(fd == -1){
perror("open device error");
exit(-1);
}
printf("open device %d\n",fd);
/*
2. get screen infomations
*/
struct fb_var_screeninfo fbinfo;
int r = ioctl(fd,FBIOGET_VSCREENINFO , &fbinfo);
if (r == -1)
{
perror("ioctl error:");
return -1;
}
printf("resuation: %d x %d\n", fbinfo.xres, fbinfo.yres);
printf("bits_per_pixel: %d bits\n", fbinfo.bits_per_pixel);
printf("A: offset %d length %d msb_rigth: %d\n ", fbinfo.transp.offset, fbinfo.transp.length, fbinfo.transp.msb_right);
printf("R: offset %d length %d msb_rigth: %d\n ", fbinfo.red.offset, fbinfo.red.length, fbinfo.red.msb_right);
printf("G: offset %d length %d msb_rigth: %d\n ", fbinfo.green.offset, fbinfo.green.length, fbinfo.green.msb_right);
printf("B: offset %d length %d msb_rigth: %d\n ", fbinfo.blue.offset, fbinfo.blue.length, fbinfo.blue.msb_right);
/*
3. mmap
*/
plcd = mmap(NULL,
fbinfo.xres * fbinfo.yres * (fbinfo.bits_per_pixel / 8),
PROT_WRITE,
MAP_SHARED,
fd,
0
);
while(!terminated){
LCD_ClearScr(0xff);//blue
sleep(2);
LCD_ClearScr(0xff00);//green
sleep(2);
LCD_ClearScr(0xff0000);//red
sleep(2);
}
munmap(plcd,fbinfo.xres * fbinfo.yres * (fbinfo.bits_per_pixel / 8));
close(fd);
return 0;
}
串口中显示的信息:
[root@klein /]# ./a.out
hello tq210
open device 3
resuation: 800 x 480
bits_per_pixel: 32 bits
A: offset 24 length 8 msb_rigth: 0
R: offset 16 length 8 msb_rigth: 0
G: offset 8 length 8 msb_rigth: 0
B: offset 0 length 8 msb_rigth: 0