所以,我们就使用ioctl来实现控制的功能 。要记住,用户程序所作的只是通过命令码告诉驱动程序它想做什么,至于怎么解释这些命令和怎么实现这些命令,这都是驱动程序要做的事情。二: ioctl()用法 int ioctl(int fd, ind cmd, …);其中fd就是用户程序打开设备时使用open函数返回的文件标示符,cmd就是用户程序对设备的控制命令,至于后面的省略号,那是一些补充参数,一般最多一个,有或没有是和cmd的意义相关的。下面是一个关于V4L视频采集中用到的用ioctl来配置视频采集设备(USB摄像头)的一些特性参数的例子:1. 定义设备结构体 struct vdIn { int fd ; //设备描述符 char *videodevice ; //设备节点,在linux下,通用的视频采集设备节点为/dev/video0 struct video_mmap vmmap; struct video_capability videocap; int mmapsize; struct video_mbuf videombuf; struct video_picture videopict; struct video_window videowin; struct video_channel videochan; int cameratype ; char *cameraname; char bridge[9]; int sizenative; // available size in jpeg int sizeothers; // others palette int palette; // available palette int norme ; // set spca506 usb video grabber int channel ; // set spca506 usb video grabber int grabMethod ; unsigned char *pFramebuffer; unsigned char *ptframe[4]; int framelock[4]; pthread_mutex_t grabmutex; int framesizeIn ; volatile int frame_cour; int bppIn; int hdrwidth; int hdrheight; int formatIn; int signalquit; };2. 设备节点赋值, "/dev/video0"是真实的物理摄像头设备在linux中的表示 if (videodevice == NULL || *videodevice == 0) { videodevice = "/dev/video0"; }3. 调用 设备 初始化函数 struct vdIn videoIn; //在spcav4l.h中定义 videodevice = "/dev/video0"; //节点 int width = 352; //宽 int height = 288; //高 int format = VIDEO_PALETTE_JPEG; //格式 int grabmethod = 1; memset (&videoIn, 0, sizeof (struct vdIn)); if (init_videoIn(&videoIn, videodevice, width, height, format,grabmethod) != 0)
if(debug) printf (" damned encore rate !!/n");4. 设备初始化函数传值 int init_videoIn (struct vdIn *vd, char *device, int width, int height, int format, int grabmethod) { int err = -1; int i; if (vd == NULL || device == NULL) return -1; if (width == 0 || height == 0) return -1; if(grabmethod < 0 || grabmethod > 1) grabmethod = 1; //read by default; // check format vd->videodevice = NULL; vd->cameraname = NULL; vd->videodevice = NULL; vd->videodevice = (char *) realloc (vd->videodevice, 16); vd->cameraname = (char *) realloc (vd->cameraname, 32); snprintf (vd->videodevice, 12, "%s", device); if(debug) printf("video %s /n",vd->videodevice); memset (vd->cameraname, 0, sizeof (vd->cameraname)); memset(vd->bridge, 0, sizeof(vd->bridge)); vd->signalquit = 1;//信号设置 vd->hdrwidth = width; vd->hdrheight = height; /* compute the max frame size */ vd->formatIn = format; //传进来的 format = VIDEO_PALETTE_JPEG; vd->bppIn = GetDepth (vd->formatIn); vd->grabMethod = grabmethod; //mmap or read vd->pFramebuffer = NULL; /* init and check all setting */ err = init_v4l (vd); // V4L初始化函数 .................................................... }