?? firewire.c
字號:
FW_camera_init(FWdev * fw, int guid, int format, int framerate){ nodeid_t *cameras; int nodeCount, cameraCount; dc1394bool_t isCamera; FWcamera *c; static char *func = "FW_camera_init"; c = (FWcamera *) calloc(1, sizeof(FWcamera)); if (c == NULL) return FW_error_null("FW_init_camera: no mem"); c->verbose = fw->verbose; c->handle = fw->handle; c->colorFormat = format; c->frameRate = framerate; c->verbose = 0; /*----------------------------------------------------------------------- * Find the specified camera, or just the first camera on the bus * if no camera ID is specified *-----------------------------------------------------------------------*/ nodeCount = raw1394_get_nodecount(fw->handle); if (c->verbose) fprintf(stderr, "Nodes on 1394 interface: %d\n", nodeCount); if (guid != 0) { /* * If the ID is specified, try to get that camera first... */ if (c->verbose) fprintf(stderr, "Looking for camera ID: %x\n", guid); cameras = dc1394_get_sorted_camera_nodes(fw->handle, 1, &guid, &cameraCount, 1); if (!cameras || cameraCount < 1) return FW_error_null ("FW_init_camera: camera not found on bus"); } else { /* * If no camera ID is specified, just grab the first camera */ if (c->verbose) fprintf(stderr, "FW_init_camera: Looking for first camera on bus...\n"); cameras = dc1394_get_camera_nodes(fw->handle, &cameraCount, 1); // 1 = print if (!cameras || cameraCount < 1) return FW_error_null ("FW_init_camera: no cameras on bus"); } /* * Get the node number of the desired camera */ c->cameraNode = cameras[0]; if (c->verbose) fprintf(stderr, "%s: found %d cameras. Requested camera is node %d\n", func, cameraCount, c->cameraNode); /*----------------------------------------------------------------------- * Make sure the camera is valid *-----------------------------------------------------------------------*/ /* * Make sure the node is a camera */ if (dc1394_is_camera(fw->handle, c->cameraNode, &isCamera) != DC1394_SUCCESS) return FW_error_null("FW_init_camera: camera check failed"); if (!isCamera) return FW_error_null("FW_init_camera: node is not a camera"); /* * Make sure the camera is not the root node! */ if (cameras[c->cameraNode] == nodeCount - 1) { fprintf(stderr, "\n*********************************************************\n" "Sorry, your camera is the highest numbered node\n" "of the bus, and has therefore become the root node.\n" "The root node is responsible for maintaining \n" "the timing of isochronous transactions on the IEEE \n" "1394 bus. However, if the root node is not cycle master \n" "capable (it doesn't have to be), then isochronous \n" "transactions will not work. The host controller card is \n" "cycle master capable, however, most cameras are not.\n\n" "The quick solution is to add the parameter \n" "attempt_root=1 when loading the OHCI driver as a \n" "module. So please do (as root):\n\n" " rmmod ohci1394\n" " insmod ohci1394 attempt_root=1\n\n" "for more information see the FAQ at \n" "http://linux1394.sourceforge.net/faq.html#DCbusmgmt\n\n" "\n*********************************************************\n"); return NULL; } /*----------------------------------------------------------------------- * Setup the camera to grab images *-----------------------------------------------------------------------*/ /* * Turn the camera on */ if (dc1394_camera_on(fw->handle, c->cameraNode) != DC1394_SUCCESS) return FW_error_null ("FW_init_camera: could not power up camera"); if (fw->verbose) fprintf(stderr, "Turned camera power on\n"); if (dc1394_stop_iso_transmission(fw->handle, c->cam.node) != DC1394_SUCCESS) { fprintf(stderr, "WARN: could not stop ISO transmission\n"); } /* * Setup the capture format */ if (FW_init_capture(c) < 0) return FW_error_null ("FW_init_camera: camera failed to initalize"); /*----------------------------------------------------------------------- * have the camera start sending us data *-----------------------------------------------------------------------*/ if (dc1394_start_iso_transmission(fw->handle, c->cam.node) != DC1394_SUCCESS) return FW_error_null ("FW_init_camera: unable to start iso transmission\n"); /*----------------------------------------------------------------------- * Get feature set, etc. from this camera *-----------------------------------------------------------------------*/ if ((dc1394_get_camera_feature_set(fw->handle, c->cam.node, &c->camFeatureSet) != DC1394_SUCCESS) || (dc1394_get_camera_info(fw->handle, c->cam.node, &c->camInfo) != DC1394_SUCCESS) || (dc1394_get_camera_misc_info(fw->handle, c->cam.node, &c->camMiscInfo) != DC1394_SUCCESS)) { return FW_error_null ("FW_init_camera: could not get info from camera"); } /*----------------------------------------------------------------------- * Print camera info and feature set *-----------------------------------------------------------------------*/ if (c->verbose) { dc1394_print_camera_info(&c->camInfo); dc1394_print_feature_set(&c->camFeatureSet); FW_info_print(c); } /* * Reset frame and error counts */ c->frameCount = c->errCount = 0; /* * Initialized successfully! */ c->status = 0; return c;}// Initialize the capturingintFW_init_capture(FWcamera * c){ /* * NOTE: We use camera node for iso channel currently */ /* * if (dc1394_setup_capture( all args the same, but remove DMA buffer len */ if (c->verbose) fprintf(stderr, "Setting up DMA capture...\n"); if (c->verbose) fprintf(stderr, "Mode, Frame rate, speed: %d %d %d\n", c->colorFormat, c->frameRate, SPEED_400); /* * Set the default device filename (if only one card and not using devfs c->cam.dma_device_file = DMA_DEVICE_NAME; */ /* * Call the setup routine */#ifdef notdef if (dc1394_dma_setup_capture(c->handle, c->cameraNode, /* Node number */ 0, /* isoc channel */ FORMAT_VGA_NONCOMPRESSED, c->colorFormat, SPEED_400, c->frameRate, DMA_BUFFERS, /* DMA BUFFER LENGTH */ 1, /* Drop frames ? */ "/dev/video1394/0", /* Device file name */ &c->cam) != DC1394_SUCCESS) { // check permission on firewire device and parameters are // achievable by the camera return FW_error("FW_init_capture: Could not setup DMA buffer"); }#else if (dc1394_setup_capture(c->handle, c->cameraNode, /* Node number */ 0, /* isoc channel */ FORMAT_VGA_NONCOMPRESSED, c->colorFormat, SPEED_400, c->frameRate, &c->cam) != DC1394_SUCCESS) { // check permission on firewire device and parameters are // achievable by the camera return FW_error("FW_init_capture: Could not setup DMA buffer"); }#endif#ifdef notdef { quadlet_t val; dc1394_query_supported_modes(c->handle, c->cameraNode, FORMAT_VGA_NONCOMPRESSED, &val); printf("mode query %d\n", val); }#endif /* * Set internal width and height values */ c->width = c->cam.frame_width; c->height = c->cam.frame_height; if (c->colorFormat == MODE_640x480_MONO) c->depth = 1; else if (MODE_640x480_RGB == c->colorFormat || MODE_640x480_YUV422 == c->colorFormat) c->depth = 3; else return FW_error ("FW_init_capture: invalid color format (not mono or rgb)"); if (c->verbose) { fprintf(stderr, "Image parameters (w,h,d): (%d,%d,%d)\n", c->width, c->height, c->depth); fprintf(stderr, "Image server buffer ptr: %p\n", (void *) c->cam.capture_buffer); } /* * Allocate buffer */ //c->buffer = new unsigned char[c->width * c->height * c->depth]; return 0;}intFW_camera_close(FWcamera * c){ /* * Release the camera */ /* * Release memory and handle dc1394_dma_release_camera(c->handle, &c->cam); */ /* * Tell video1394 to stop iso reception dc1394_dma_unlisten(c->handle, &c->cam); */ /* * Stop ISO transmission */ dc1394_stop_iso_transmission(c->handle, c->cam.node); /* * dc1394_release_camera(w->handle, &c->cam); For non-dma version */ return 0;}intFW_close(FWdev * fw){ /* * Release the camera and handle */ if (fw->handle) { raw1394_destroy_handle(fw->handle); } return 0;}//////////////////////////////////////////////////////////// Get the next image and return it//////////////////////////////////////////////////////////unsigned char *FW_grab(FWcamera * c){ dc1394bool_t isoOn = DC1394_FALSE; /* * Make sure we are transmitting */ if (dc1394_get_iso_status(c->handle, c->cam.node, &isoOn) != DC1394_SUCCESS) return FW_error_null("FW_grab: error checking iso status"); if (DC1394_TRUE != isoOn) return FW_error_null("FW_grab: not iso transmitting"); /* * Release the previous DMA buffer if (dc1394_dma_done_with_buffer(&c->cam) != DC1394_SUCCESS) return FW_error_null("FW_grab: Could not release DMA buffer"); */ /* * Get an image if (dc1394_dma_single_capture(&c->cam) != DC1394_SUCCESS) return FW_error_null("FW_grab: getImage capture failed"); */ if (dc1394_single_capture(c->handle, &c->cam) != DC1394_SUCCESS) return FW_error_null("FW_grab: getImage capture failed"); /* * Increment the frame count */ c->frameCount++; /* * Return the buffer */ return (unsigned char *) c->cam.capture_buffer;}//////////////////////////////////////////////////////////// Print miscellaneous information//////////////////////////////////////////////////////////voidFW_info_print(FWcamera * c){ return; fprintf(stderr, "\tformat: %i \tmode: %i \tframerate: %i\n", c->camMiscInfo.format, c->camMiscInfo.mode, c->camMiscInfo.framerate); fprintf(stderr, "\tis_iso_on: %i \tiso_channel: %i \tiso_speed: %i\n", c->camMiscInfo.is_iso_on, c->camMiscInfo.iso_channel, c->camMiscInfo.iso_speed);}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -