eleni@0: #include eleni@0: #include eleni@0: #include eleni@1: eleni@0: #include "kinect.h" eleni@1: #include "frame.h" eleni@0: eleni@5: KinectParams::KinectParams() eleni@5: { eleni@7: angle = 10; eleni@5: eleni@7: video_width = KINECT_VIDEO_WIDTH; eleni@7: video_height = KINECT_VIDEO_HEIGHT; eleni@7: depth_width = KINECT_DEPTH_WIDTH; eleni@7: depth_height = KINECT_DEPTH_HEIGHT; eleni@5: eleni@7: led_color = LED_RED; eleni@7: video_format = FREENECT_VIDEO_RGB; eleni@7: depth_format = FREENECT_DEPTH_11BIT; eleni@7: video_res = FREENECT_RESOLUTION_MEDIUM; eleni@7: depth_res = FREENECT_RESOLUTION_MEDIUM; eleni@5: } eleni@5: eleni@0: bool init_kinect(freenect_context **kin_ctx, freenect_device **kin_dev, KinectParams *kin_params) eleni@0: { eleni@0: if(freenect_init(kin_ctx, NULL) < 0) { eleni@0: fprintf(stderr, "Failed to initialize kinect context.\n"); eleni@0: return false; eleni@0: } eleni@0: eleni@0: freenect_set_log_level(*kin_ctx, FREENECT_LOG_INFO); eleni@0: eleni@0: int num_dev = freenect_num_devices(*kin_ctx); eleni@0: if(num_dev < 1) { eleni@0: fprintf(stderr, "Kinect device not found.\n"); eleni@0: return false; eleni@0: } eleni@0: printf("%d devices found, will use the first.\n", num_dev); eleni@0: eleni@0: if(freenect_open_device(*kin_ctx, kin_dev, 0) < 0) { eleni@0: fprintf(stderr, "Couldn't open kinect device.\n"); eleni@0: return false; eleni@0: } eleni@0: eleni@13: /* if(freenect_set_tilt_degs(*kin_dev, kin_params->angle) < 0) { eleni@0: stop_kinect(*kin_ctx, *kin_dev); eleni@0: fprintf(stderr, "Failed to set kinect angle\n"); eleni@0: return false; eleni@0: } eleni@13: */ eleni@0: if(freenect_set_led(*kin_dev, kin_params->led_color) < 0) { eleni@2: fprintf(stderr, "Failed to set kinect LED\n"); eleni@0: stop_kinect(*kin_ctx, *kin_dev); eleni@0: return false; eleni@0: } eleni@0: eleni@2: return true; eleni@2: } eleni@2: eleni@7: bool init_kinect_frames(freenect_context *kin_ctx, freenect_device *kin_dev, eleni@2: KinectParams *kin_params) { eleni@0: eleni@7: freenect_set_video_callback(kin_dev, video_cb); eleni@7: freenect_set_depth_callback(kin_dev, depth_cb); eleni@0: eleni@7: if(freenect_set_video_mode(kin_dev, eleni@0: freenect_find_video_mode(kin_params->video_res, eleni@0: kin_params->video_format)) < 0) { eleni@0: fprintf(stderr, "Failed to set kinect video mode.\n"); eleni@0: return false; eleni@0: } eleni@0: eleni@7: if(freenect_set_depth_mode(kin_dev, eleni@0: freenect_find_depth_mode(kin_params->depth_res, eleni@0: kin_params->depth_format)) < 0) { eleni@0: fprintf(stderr, "Failed to set kinect depth mode.\n"); eleni@0: return false; eleni@0: } eleni@0: eleni@7: if(freenect_start_video(kin_dev) < 0) { eleni@0: fprintf(stderr, "Failed to start kinect video.\n"); eleni@0: return false; eleni@0: } eleni@0: eleni@7: if(freenect_start_depth(kin_dev) < 0) { eleni@0: fprintf(stderr, "Failed to start kinect depth video.\n"); eleni@0: return false; eleni@0: } eleni@0: eleni@0: return true; eleni@0: } eleni@0: eleni@2: void stop_kinect_video_frames(freenect_device *kin_dev) eleni@2: { eleni@2: freenect_stop_video(kin_dev); eleni@2: } eleni@2: eleni@2: void stop_kinect_depth_frames(freenect_device *kin_dev) eleni@2: { eleni@2: freenect_stop_depth(kin_dev); eleni@2: } eleni@2: eleni@2: void stop_kinect_frames(freenect_device *kin_dev) eleni@2: { eleni@2: stop_kinect_video_frames(kin_dev); eleni@2: stop_kinect_depth_frames(kin_dev); eleni@2: } eleni@2: eleni@0: void stop_kinect(freenect_context *kin_ctx, freenect_device *kin_dev) eleni@0: { eleni@0: freenect_set_led(kin_dev, LED_OFF); eleni@0: freenect_close_device(kin_dev); eleni@0: freenect_shutdown(kin_ctx); eleni@0: }