opt_nm33_camera.cpp
Go to the documentation of this file.
00001 /*                                                            */
00002 /* This code is based on luvcview_wr_100823 from Opt corp.    */
00003 /*                                                            */
00004 /*                  2010/09/15    k-okada@jsk.t.u-tokyo.ac.jp */
00005 
00006 #include "opt_nm33_camera.h"
00007 
00008 #include <opencv/cv.h>
00009 #include <opencv/highgui.h>
00010 
00011 //
00012 #include <fcntl.h>
00013 #include <sys/mman.h>
00014 #define CLEAR(x) memset (&(x), 0, sizeof (x))
00015 
00016 /* convert from mjpeg to rgb24 */
00017 static bool
00018 mjpeg_to_rgb24 (int width, int height,
00019                 unsigned char *src, int length,
00020                 unsigned char *dst)
00021 {
00022   cv::Mat temp=cv::imdecode(cv::Mat(std::vector<uchar>(src, src + length)), 1);
00023   if( !temp.data || temp.cols != width || temp.rows != height )
00024     return false;
00025   memcpy(dst, temp.data, width*height*3);
00026   return true;
00027 }
00028 
00029 void OptNM3xCamera::device_open(int camera_index)
00030 {
00031   // open_device
00032   char dev_name[128];
00033   sprintf(dev_name, "/dev/video%1d", camera_index);
00034   fprintf(stderr, "Opening device '%s'\n", dev_name);
00035 
00036   width  = 640;
00037   height = 480;
00038 
00039  try_again:
00040   fd = open(dev_name, O_RDWR, 0);
00041 
00042   if (fd == -1) {
00043     fprintf(stderr, "Cannot open '%s': %d, %s\n",
00044             dev_name, errno, strerror(errno));
00045     exit(EXIT_FAILURE);
00046   }
00047 
00048   // init_device
00049   struct v4l2_capability cap;
00050   struct v4l2_format fmt;
00051 
00052   if (ioctl(fd, VIDIOC_QUERYCAP, &cap) == -1) {
00053     if (EINVAL == errno) {
00054       fprintf(stderr, "%s is no V4L2 device\n", dev_name);
00055     }
00056     perror("VIDIOC_QUERYCAP");
00057     exit(EXIT_FAILURE);
00058   }
00059 
00060   if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
00061     fprintf(stderr, "%s is no video capture device\n", dev_name);
00062     exit(EXIT_FAILURE);
00063   }
00064 
00065   if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
00066     fprintf(stderr, "%s does not support streaming i/o\n", dev_name);
00067     exit(EXIT_FAILURE);
00068   }
00069 
00070   CLEAR(fmt);
00071 
00072   fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
00073   fmt.fmt.pix.width = width;
00074   fmt.fmt.pix.height = height;
00075   fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_MJPEG;
00076   fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
00077 
00078   if (ioctl(fd, VIDIOC_S_FMT, &fmt) == -1) {
00079     perror("VIDIOC_S_FMT");
00080     exit(EXIT_FAILURE);
00081   }
00082 
00083   // init mmap
00084   struct v4l2_requestbuffers req;
00085 
00086   CLEAR(req);
00087 
00088   req.count = 4;
00089   req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
00090   req.memory = V4L2_MEMORY_MMAP;
00091 
00092   if (ioctl(fd, VIDIOC_REQBUFS, &req) == -1) {
00093     perror("VIDIOC_REQBUFS");
00094     exit(EXIT_FAILURE);
00095   }
00096 
00097   if (req.count < 2) {
00098     fprintf(stderr, "Insufficient buffer memory on %s\n", dev_name);
00099     exit(EXIT_FAILURE);
00100   }
00101 
00102   buffers = (buffer *)calloc(req.count, sizeof(*buffers));
00103 
00104   if (!buffers) {
00105     fprintf(stderr, "Out of memory\n");
00106     exit(EXIT_FAILURE);
00107   }
00108 
00109   for (n_buffers = 0; n_buffers < req.count; ++n_buffers) {
00110     struct v4l2_buffer buf;
00111 
00112     CLEAR(buf);
00113 
00114     buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
00115     buf.memory = V4L2_MEMORY_MMAP;
00116     buf.index = n_buffers;
00117 
00118     if (ioctl(fd, VIDIOC_QUERYBUF, &buf) == -1) {
00119       perror("VIDIOC_QUERYBUF");
00120       exit(EXIT_FAILURE);
00121     }
00122 
00123     buffers[n_buffers].length = buf.length;
00124     buffers[n_buffers].start = mmap(NULL /* start anywhere */ ,
00125                                     buf.length, PROT_READ | PROT_WRITE
00126                                     /* required */ ,
00127                                     MAP_SHARED /* recommended */ ,
00128                                     fd, buf.m.offset);
00129 
00130     if (buffers[n_buffers].start == MAP_FAILED) {
00131       perror("mmap");
00132       exit(EXIT_FAILURE);
00133     }
00134   }
00135 
00136   // start capturing
00137   unsigned int i;
00138   enum v4l2_buf_type type;
00139 
00140   for (i = 0; i < n_buffers; ++i) {
00141     struct v4l2_buffer buf;
00142 
00143     CLEAR(buf);
00144 
00145     buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
00146     buf.memory = V4L2_MEMORY_MMAP;
00147     buf.index = i;
00148 
00149     if (ioctl(fd, VIDIOC_QBUF, &buf) == -1) {
00150       if ( width == 640 && height == 480 ) {
00151         device_close();
00152         width = 320; height = 240;
00153         goto try_again;
00154       }
00155       perror("VIDIOC_QBUF");
00156       exit(EXIT_FAILURE);
00157     }
00158   }
00159 
00160   type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
00161 
00162   if (ioctl(fd, VIDIOC_STREAMON, &type) == -1) {
00163     if ( width == 640 && height == 480 ) {
00164       device_close();
00165       width = 320; height = 240;
00166       goto try_again;
00167     }
00168     perror("VIDIOC_STREAMON");
00169     exit(EXIT_FAILURE);
00170   }
00171   //
00172   fprintf(stderr, "video capabilities\n");
00173   fprintf(stderr, "cap.driver        =  %s\n", cap.driver);
00174   fprintf(stderr, "cap.card          =  %s\n", cap.card);
00175   fprintf(stderr, "cap.buf_info      =  %s\n", cap.bus_info);
00176   fprintf(stderr, "cap.version       =  %d\n", cap.version);
00177   fprintf(stderr, "cap.capabilities  =  0x%08x ", cap.capabilities);
00178   if (cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)
00179     fprintf(stderr, " VIDEO_CAPTURE");
00180   if (cap.capabilities & V4L2_CAP_VIDEO_OUTPUT)
00181     fprintf(stderr, " VIDEO_OUTPUT");
00182   if (cap.capabilities & V4L2_CAP_VIDEO_OVERLAY)
00183     fprintf(stderr, " VIDEO_OVERLAY");
00184   if (cap.capabilities & V4L2_CAP_VBI_CAPTURE)
00185     fprintf(stderr, " VBI_CAPTURE");
00186   if (cap.capabilities & V4L2_CAP_VBI_OUTPUT)
00187     fprintf(stderr, " VBI_OUTPUT");
00188 #ifdef V4L2_CAP_SLICED_VBI_CAPTURE
00189   if (cap.capabilities & V4L2_CAP_SLICED_VBI_CAPTURE)
00190     fprintf(stderr, " SLICED_VBI_CAPTURE");
00191 #endif
00192 #ifdef V4L2_CAP_SLICED_VBI_OUTPUT
00193   if (cap.capabilities & V4L2_CAP_SLICED_VBI_OUTPUT)
00194     fprintf(stderr, " VBI_SLICED_OUTPUT");
00195 #endif
00196   if (cap.capabilities & V4L2_CAP_RDS_CAPTURE)
00197     fprintf(stderr, " RDS_CAPTURE");
00198 #if V4L2_CAP_VIDEO_OUTPUT_OVERLAY
00199   if (cap.capabilities & V4L2_CAP_VIDEO_OUTPUT_OVERLAY)
00200     fprintf(stderr, " VIDEO_OUTPUT_OVERLAY");
00201 #endif
00202   if (cap.capabilities & V4L2_CAP_TUNER)
00203     fprintf(stderr, " TUNER");
00204   if (cap.capabilities & V4L2_CAP_AUDIO)
00205     fprintf(stderr, " AUDIO");
00206   if (cap.capabilities & V4L2_CAP_RADIO)
00207     fprintf(stderr, " RADIO");
00208   if (cap.capabilities & V4L2_CAP_READWRITE)
00209     fprintf(stderr, " READWRITE");
00210   if (cap.capabilities & V4L2_CAP_ASYNCIO)
00211     fprintf(stderr, " ASYNCIO");
00212   if (cap.capabilities & V4L2_CAP_STREAMING)
00213     fprintf(stderr, " STREAMING");
00214   fprintf(stderr, "\n");
00215   fprintf(stderr, "cap.width         =  %d\n", width);
00216   fprintf(stderr, "cap.height        =  %d\n", height);
00217 
00218   /* Set up Image data */
00219   frame = (IplImage *)malloc(sizeof(IplImage));
00220   cvInitImageHeader( frame,
00221                      cvSize( width, height ),
00222                      IPL_DEPTH_8U, 3, IPL_ORIGIN_TL, 4 );
00223   /* Allocate space for RGBA data */
00224   frame->imageData = (char *)cvAlloc(frame->imageSize);
00225 }
00226 
00227 void OptNM3xCamera::device_close()
00228 {
00229   // stop capturing
00230   enum v4l2_buf_type type;
00231 
00232   type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
00233 
00234   if (ioctl(fd, VIDIOC_STREAMOFF, &type) == -1) {
00235     perror("VIDIOC_STREAMOFF");
00236     exit(EXIT_FAILURE);
00237   }
00238 
00239   // uninit_mmap
00240   unsigned int i;
00241 
00242   for (i = 0; i < n_buffers; ++i) {
00243     if (munmap(buffers[i].start, buffers[i].length) == -1) {
00244       perror("munmap");
00245       exit(EXIT_FAILURE);
00246     }
00247   }
00248 
00249   // uninit_device
00250   free(buffers);
00251 
00252   // close_device
00253   if (close(fd) == -1) {
00254     perror("close");
00255     exit(EXIT_FAILURE);
00256   }
00257   fd = -1;
00258 }
00259 
00260 IplImage *OptNM3xCamera::read_frame ()
00261 {
00262   // read frame
00263   struct v4l2_buffer buf;
00264 
00265   CLEAR(buf);
00266 
00267   buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
00268   buf.memory = V4L2_MEMORY_MMAP;
00269 
00270   if (ioctl(fd, VIDIOC_DQBUF, &buf) == -1) {
00271     perror("VIDIOC_DQBUF");
00272     return NULL;
00273   }
00274   assert(buf.index < n_buffers);
00275 
00276   //process_image(buffers[buf.index].start);
00277   if (!mjpeg_to_rgb24(width, height,
00278                       (unsigned char*)(buffers[buf.index].start),
00279                       buffers[buf.index].length,
00280                       (unsigned char*)frame->imageData)) {
00281     perror("mjpeg_to_rgb24");
00282     return NULL;
00283   }
00284 
00285   if (ioctl(fd, VIDIOC_QBUF, &buf) == -1) {
00286     perror("VIDIOC_QBUF");
00287     return NULL;
00288   }
00289 
00290   return frame;
00291 }
00292 
00293 OptNM3xCamera::OptNM3xCamera(int camera_index)
00294 {
00295   device_open(camera_index);
00296 
00297   setMode(4);
00298   if ( strcmp(getFirmwareVersion().c_str(), "") == 0 ) {
00299     fprintf(stderr, "ERROR : try sudo -E ./bin/init_xu_register\n");
00300     exit(1);
00301   }
00302   fprintf(stderr, "firmwareVersion -> %s\n", getFirmwareVersion().c_str());
00303   fprintf(stderr, "serialId -> %s\n", getSerialID().c_str());
00304 
00305   frame_omni = frame_wide = frame_middle = frame_narrow = NULL;
00306 }
00307 
00308 OptNM3xCamera::~OptNM3xCamera()
00309 {
00310   device_close();
00311 }
00312 
00313 // obtaining images
00314 IplImage *OptNM3xCamera::queryFrame ()
00315 {
00316   if ( read_frame()==NULL ) {
00317     fprintf(stderr, "ERROR : read_frame returns NULL\n");
00318     return NULL;
00319   }
00320 
00321 
00322   int height = frame->height, width = frame->width;
00323   if ( ! ( frame_omni && (frame_omni->width  == height/2 &&
00324                           frame_omni->height == height/2 ) ) )
00325     frame_omni = cvCreateImage(cvSize(height/2, height/2), frame->depth, frame->nChannels);
00326   if ( ! ( frame_wide && (frame_wide->width  == (width-height/2) &&
00327                           frame_wide->height == height/2 ) ) )
00328     frame_wide = cvCreateImage(cvSize(width-height/2, height/2), frame->depth, frame->nChannels);
00329   if ( ! ( frame_middle && (frame_middle->width  == width/2 &&
00330                             frame_middle->height == height/2 ) ) )
00331     frame_middle = cvCreateImage(cvSize(width/2, height/2), frame->depth, frame->nChannels);
00332   if ( ! ( frame_narrow && (frame_narrow->width  == width/2 &&
00333                             frame_narrow->height == height/2 ) ) )
00334     frame_narrow = cvCreateImage(cvSize(width/2, height/2), frame->depth, frame->nChannels);
00335   return frame;
00336 }
00337 
00338 void OptNM3xCamera::getOmniImage (IplImage *frame, CvMat &subframe) {
00339   int height = frame->height;
00340   cvGetSubRect(frame, &subframe, cvRect(0, 0, height/2, height/2));
00341 }
00342 
00343 void OptNM3xCamera::getWideImage (IplImage *frame, CvMat &subframe) {
00344   int width = frame->width, height = frame->height;
00345   cvGetSubRect(frame, &subframe, cvRect(height/2, 0, width-height/2, height/2));
00346 }
00347 
00348 void OptNM3xCamera::getMiddleImage (IplImage *frame, CvMat &subframe) {
00349   int width = frame->width, height = frame->height;
00350   cvGetSubRect(frame, &subframe, cvRect(0, height/2, width/2, height/2));
00351 }
00352 
00353 void OptNM3xCamera::getNarrowImage (IplImage *frame, CvMat &subframe) {
00354   int width = frame->width, height = frame->height;
00355   cvGetSubRect(frame, &subframe, cvRect(width/2, height/2, width/2, height/2));
00356 }
00357 
00358 
00359 IplImage *OptNM3xCamera::queryOmniFrame () {
00360   int height = frame->height;
00361   cvSetImageROI(frame, cvRect(0, 0, height/2, height/2));
00362   cvCopy(frame, frame_omni);
00363   cvResetImageROI(frame);
00364   return frame_omni;
00365 }
00366 
00367 IplImage *OptNM3xCamera::queryWideFrame () {
00368   int width = frame->width, height = frame->height;
00369   cvSetImageROI(frame, cvRect(height/2, 0, width-height/2, height/2));
00370   cvCopy(frame, frame_wide);
00371   cvResetImageROI(frame);
00372   return frame_wide;
00373 }
00374 
00375 IplImage *OptNM3xCamera::queryMiddleFrame () {
00376   int width = frame->width, height = frame->height;
00377   cvSetImageROI(frame, cvRect(0, height/2, width/2, height/2));
00378   cvCopy(frame, frame_middle);
00379   cvResetImageROI(frame);
00380   return frame_middle;
00381 }
00382 
00383 IplImage *OptNM3xCamera::queryNarrowFrame () {
00384   int width = frame->width, height = frame->height;
00385   cvSetImageROI(frame, cvRect(width/2, height/2, width/2, height/2));
00386   cvCopy(frame, frame_narrow);
00387   cvResetImageROI(frame);
00388   return frame_narrow;
00389 }
00390 
00391 // set commands
00392 bool OptNM3xCamera::setMode (int mode) {
00393   return (ioctl(fd, VIDIOC_S_INPUT, &mode)==0);
00394 }
00395 
00396 // ct command
00397 bool OptNM3xCamera::setAutoExposure(bool mode) {
00398   v4l2_set_ioctl(V4L2_CID_EXPOSURE_AUTO, mode?1:2);
00399   return true;
00400 }
00401 bool OptNM3xCamera::setExposure (int value) {
00402   v4l2_set_ioctl(V4L2_CID_EXPOSURE_ABSOLUTE, value);
00403   return true;
00404 }
00405 bool OptNM3xCamera::setIris(int value) {
00406   v4l2_set_ioctl(V4L2_CID_IRIS_ABSOLUTE, value);
00407   return true;
00408 }
00409 bool OptNM3xCamera::setBrightness(int value) {
00410   v4l2_set_ioctl(V4L2_CID_BRIGHTNESS, value);
00411   return true;
00412 }
00413 bool OptNM3xCamera::setSharpness(int value) {
00414   v4l2_set_ioctl(V4L2_CID_SHARPNESS, value);
00415   return true;
00416 }
00417 bool OptNM3xCamera::setWhitebalance(int value) {
00418   v4l2_set_ioctl(V4L2_CID_WHITE_BALANCE_TEMPERATURE, value);
00419   return true;
00420 }
00421 bool OptNM3xCamera::setAutoWhitebalance(bool mode) {
00422   v4l2_set_ioctl(V4L2_CID_AUTO_WHITE_BALANCE, mode?1:0);
00423   return true;
00424 }
00425 bool OptNM3xCamera::setPanAbsolute(double value) {
00426   return v4l2_set_ioctl(V4L2_CID_PAN_ABSOLUTE,  (int)(value*3600));
00427 }
00428 bool OptNM3xCamera::setTiltAbsolute(double value) {
00429   return v4l2_set_ioctl(V4L2_CID_TILT_ABSOLUTE, (int)(value*3600));
00430 }
00431 bool OptNM3xCamera::setRollAbsolute(double value) {
00432   return v4l2_set_ioctl(V4L2_CID_ROLL_ABSOLUTE, (int)(value*1));
00433 }
00434 bool OptNM3xCamera::setZoomAbsolute(double value) {
00435   return v4l2_set_ioctl(V4L2_CID_ZOOM_ABSOLUTE, (int)value);
00436 }
00437 bool OptNM3xCamera::setPanAbsolute(int value) {
00438   return v4l2_set_ioctl(V4L2_CID_PAN_ABSOLUTE,  value);
00439 }
00440 bool OptNM3xCamera::setTiltAbsolute(int value) {
00441   return v4l2_set_ioctl(V4L2_CID_TILT_ABSOLUTE, value);
00442 }
00443 bool OptNM3xCamera::setRollAbsolute(int value) {
00444   return v4l2_set_ioctl(V4L2_CID_ROLL_ABSOLUTE, value);
00445 }
00446 bool OptNM3xCamera::setZoomAbsolute(int value) {
00447   return v4l2_set_ioctl(V4L2_CID_ZOOM_ABSOLUTE, value);
00448 }
00449 
00450 bool OptNM3xCamera::v4l2_set_ioctl(int selector, int value) {
00451   v4l2_queryctrl q;
00452   q.id = selector;
00453   if ( ioctl(fd, VIDIOC_QUERYCTRL, &q) < 0 )
00454     {
00455       perror("unable to query control");
00456       return false;
00457     }
00458 
00459   v4l2_control  c;
00460   c.id = selector;
00461   c.value = value;
00462   fprintf(stderr, "VIDIOC_S_CTRL: sel = 0x%08x, data =0x%08x\n",
00463           selector, value);
00464   if (ioctl(fd, VIDIOC_S_CTRL, &c) < 0)
00465     {
00466       perror("unable to set control");
00467       return false;
00468     }
00469   return true;
00470 }
00471 
00472 int OptNM3xCamera::v4l2_get_ioctl(int selector) {
00473   v4l2_control c;
00474   c.id = selector;
00475   fprintf(stderr, "VIDIOC_S_CTRL: sel = 0x%02x\n", selector);
00476   if (ioctl(fd, VIDIOC_G_CTRL, &c) == 0)
00477     {
00478       perror("unable to get control");
00479       return -1;
00480     }
00481   return c.value;
00482 }
00483 
00484 // xu command
00485 std::string OptNM3xCamera::getFirmwareVersion() {
00486 #ifdef NON_STD_UVC
00487   std::string str(255,0);
00488 #else
00489   std::string str(32,0);
00490 #endif
00491   getXuValue(XU_FIRMWARE_VERSION_CONTROL, str.c_str());
00492   return str.substr(0,str.find_first_of('\0'));
00493 }
00494 bool OptNM3xCamera::setFlipScreen(char value) {
00495   return setXuValue(XU_FLIP_SCREEN_CONTROL,value);
00496 }
00497 bool OptNM3xCamera::setSmallHemisphere(char value) {
00498   return setXuValue(XU_SMALL_HEMISPHERE_CONTROL,value);
00499 }
00500 bool OptNM3xCamera::setMedianFilter(bool mode) {
00501   return setXuValue(XU_MEDIAN_FILTER_CONTROL, (char)(mode?1:0));
00502 }
00503 bool OptNM3xCamera::setJpegQuality(char value) {
00504   return setXuValue(XU_JPEG_QUALITY_CONTROL,value);
00505 }
00506 std::string OptNM3xCamera::getSerialID() {
00507   std::string str(16,0);
00508   getXuValue(XU_SERIAL_ID_CONTROL, str.c_str());
00509   return str.substr(0,str.find_first_of('\0'));
00510 }
00511 bool OptNM3xCamera::setInfoDisplay(bool mode) {
00512   return setXuValue(XU_INFO_DISPLAY_CONTROL, mode?1:0);
00513 }
00514 bool OptNM3xCamera::setCaptureFPS(short value) {
00515   return setXuValue(XU_CAPTURE_FPS_CONTROL,value);
00516 }
00517 short OptNM3xCamera::getActualFPS() {
00518   short value;
00519   getXuValue(XU_CAPTURE_FPS_CONTROL, &value);
00520   return value;
00521 }
00522 bool OptNM3xCamera::setLensType(char value) {
00523   return setXuValue(XU_LENS_TYPE_CONTROL,value);
00524 }
00525 bool OptNM3xCamera::setPanAbsolute(int no, int value) {
00526   return setXuValue(XU_PAN_ABSOLUTE_CONTROL,  no, value);
00527 }
00528 bool OptNM3xCamera::setTiltAbsolute(int no, int value) {
00529   return setXuValue(XU_TILT_ABSOLUTE_CONTROL,  no, value);
00530 }
00531 bool OptNM3xCamera::setRollAbsolute(int no, int value) {
00532   return setXuValue(XU_ROLL_ABSOLUTE_CONTROL,  no, value);
00533 }
00534 bool OptNM3xCamera::setZoomAbsolute(int no, int value) {
00535   return setXuValue(XU_ZOOM_ABSOLUTE_CONTROL,  no, value);
00536 }
00537 bool OptNM3xCamera::setLocationAbsolute(int no, int pan, int tilt, int roll, int zoom) {
00538   return setXuValue(XU_LOCATION_ABSOLUTE_CONTROL, no, pan, tilt, roll, zoom);
00539 }
00540 //
00541 bool OptNM3xCamera::getXuValue(int selector, const char *str) {
00542   return xu_ioctl(selector, UVCIOC_CTRL_GET, (void *)str);
00543 }
00544 bool OptNM3xCamera::getXuValue(int selector, short *value) {
00545   return xu_ioctl(selector, UVCIOC_CTRL_GET, (void *)value);
00546 }
00547 bool OptNM3xCamera::setXuValue(int selector, char value) {
00548   return xu_ioctl(selector, UVCIOC_CTRL_SET, (void *)&value);
00549 }
00550 bool OptNM3xCamera::setXuValue(int selector, short v1, short v2) {
00551   struct { short v1, v2;} value;
00552   value.v1 = v1;
00553   value.v2 = v2;
00554   return xu_ioctl(selector, UVCIOC_CTRL_SET, (void *)&value);
00555 }
00556 bool OptNM3xCamera::setXuValue(int selector, short v1, short v2, short v3, short v4, short v5) {
00557   struct { short v1, v2, v3, v4, v5;} value;
00558   value.v1 = v1; value.v2 = v2; value.v3 = v3; value.v4 = v4; value.v5 = v5;
00559   return xu_ioctl(selector, UVCIOC_CTRL_SET, (void *)&value);
00560 }
00561 bool OptNM3xCamera::xu_ioctl(int selector, int ctrl, void* value) {
00562   for (int i = 0; i < uvc_xu_tbl_cnt; i++){
00563     if ( xu_control_tbl[i].selector == selector ) {
00564       struct uvc_xu_control xctrl;
00565       xctrl.unit      = 0x60;
00566       xctrl.selector = xu_control_tbl[i].selector;
00567       xctrl.size     = xu_control_tbl[i].size;
00568       xctrl.data     = (__u8*)value;
00569       fprintf(stderr, "%s: name = %s, sel = 0x%02x, size = %d, data =",
00570               (ctrl==UVCIOC_CTRL_SET)?"UVCIOC_CTRL_SET":"UVCIOC_CTRL_GET",
00571               xu_control_tbl[i].name,
00572               xctrl.selector,
00573               xctrl.size);
00574       if (  ioctl(fd, ctrl, &xctrl) != 0 ) {
00575         xctrl.data[0]=0;
00576         fprintf(stderr, "\nioctl error %s\n", strerror(errno));
00577         return false;
00578       } else {
00579         for ( int j = 0; j  < xctrl.size; j++ )
00580           fprintf(stderr, " 0x%02x", *(xctrl.data + j));
00581         fprintf(stderr, "\n");
00582         if ( xu_control_tbl[i].size==255 ) xctrl.data[xctrl.size] = 0;
00583         return true;
00584       }
00585     }
00586   }
00587   return false;
00588 }
00589 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Friends Defines


opt_camera
Author(s): Kei Okada
autogenerated on Sat Mar 23 2013 21:00:14