|
| 1 | +/* |
| 2 | + * This file is part of the OpenKinect Project. http://www.openkinect.org |
| 3 | + * |
| 4 | + * Copyright (c) 2010 individual OpenKinect contributors. See the CONTRIB file |
| 5 | + * for details. |
| 6 | + * |
| 7 | + * This code is licensed to you under the terms of the Apache License, version |
| 8 | + * 2.0, or, at your option, the terms of the GNU General Public License, |
| 9 | + * version 2.0. See the APACHE20 and GPL2 files for the text of the licenses, |
| 10 | + * or the following URLs: |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * http://www.gnu.org/licenses/gpl-2.0.txt |
| 13 | + * |
| 14 | + * If you redistribute this file in source form, modified or unmodified, you |
| 15 | + * may: |
| 16 | + * 1) Leave this header intact and distribute it under the same terms, |
| 17 | + * accompanying it with the APACHE20 and GPL20 files, or |
| 18 | + * 2) Delete the Apache 2.0 clause and accompany it with the GPL2 file, or |
| 19 | + * 3) Delete the GPL v2 clause and accompany it with the APACHE20 file |
| 20 | + * In all cases you must keep the copyright notice intact and include a copy |
| 21 | + * of the CONTRIB file. |
| 22 | + * |
| 23 | + * Binary distributions must follow the binary distribution requirements of |
| 24 | + * either License. |
| 25 | + */ |
| 26 | +#include <signal.h> |
| 27 | +#include <stdbool.h> |
| 28 | +#include <stdio.h> |
| 29 | +#include <string.h> |
| 30 | +#include "libfreenect.h" |
| 31 | + |
| 32 | + |
| 33 | +void depth_cb(freenect_device* dev, void* data, uint32_t timestamp) |
| 34 | +{ |
| 35 | + printf("Received depth frame at %d\n", timestamp); |
| 36 | +} |
| 37 | + |
| 38 | +void video_cb(freenect_device* dev, void* data, uint32_t timestamp) |
| 39 | +{ |
| 40 | + printf("Received video frame at %d\n", timestamp); |
| 41 | +} |
| 42 | + |
| 43 | +volatile bool running = true; |
| 44 | +void signalHandler(int signal) |
| 45 | +{ |
| 46 | + if (signal == SIGINT |
| 47 | + || signal == SIGTERM |
| 48 | + || signal == SIGQUIT) |
| 49 | + { |
| 50 | + printf(" %s; shutting down\n", sys_siglist[signal]); |
| 51 | + running = false; |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +int main(int argc, char** argv) |
| 56 | +{ |
| 57 | + // Handle signals gracefully. |
| 58 | + signal(SIGINT, signalHandler); |
| 59 | + signal(SIGTERM, signalHandler); |
| 60 | + signal(SIGQUIT, signalHandler); |
| 61 | + |
| 62 | + // Initialize libfreenect. |
| 63 | + freenect_context* fn_ctx; |
| 64 | + int ret = freenect_init(&fn_ctx, NULL); |
| 65 | + if (ret < 0) |
| 66 | + return ret; |
| 67 | + |
| 68 | + // Show debug messages and use camera only. |
| 69 | + freenect_set_log_level(fn_ctx, FREENECT_LOG_DEBUG); |
| 70 | + freenect_select_subdevices(fn_ctx, FREENECT_DEVICE_CAMERA); |
| 71 | + |
| 72 | + // Find out how many devices are connected. |
| 73 | + int num_devices = ret = freenect_num_devices(fn_ctx); |
| 74 | + if (ret < 0) |
| 75 | + return ret; |
| 76 | + if (num_devices == 0) |
| 77 | + { |
| 78 | + printf("No devices found!\n"); |
| 79 | + freenect_shutdown(fn_ctx); |
| 80 | + return 1; |
| 81 | + } |
| 82 | + |
| 83 | + // Open the first device. |
| 84 | + freenect_device* fn_dev; |
| 85 | + ret = freenect_open_device(fn_ctx, &fn_dev, 0); |
| 86 | + if (ret < 0) |
| 87 | + { |
| 88 | + freenect_shutdown(fn_ctx); |
| 89 | + return ret; |
| 90 | + } |
| 91 | + |
| 92 | + // Set depth and video modes. |
| 93 | + ret = freenect_set_depth_mode(fn_dev, freenect_find_depth_mode(FREENECT_RESOLUTION_MEDIUM, FREENECT_DEPTH_MM)); |
| 94 | + if (ret < 0) |
| 95 | + { |
| 96 | + freenect_shutdown(fn_ctx); |
| 97 | + return ret; |
| 98 | + } |
| 99 | + ret = freenect_set_video_mode(fn_dev, freenect_find_video_mode(FREENECT_RESOLUTION_MEDIUM, FREENECT_VIDEO_RGB)); |
| 100 | + if (ret < 0) |
| 101 | + { |
| 102 | + freenect_shutdown(fn_ctx); |
| 103 | + return ret; |
| 104 | + } |
| 105 | + |
| 106 | + // Set frame callbacks. |
| 107 | + freenect_set_depth_callback(fn_dev, depth_cb); |
| 108 | + freenect_set_video_callback(fn_dev, video_cb); |
| 109 | + |
| 110 | + // Start depth and video. |
| 111 | + ret = freenect_start_depth(fn_dev); |
| 112 | + if (ret < 0) |
| 113 | + { |
| 114 | + freenect_shutdown(fn_ctx); |
| 115 | + return ret; |
| 116 | + } |
| 117 | + ret = freenect_start_video(fn_dev); |
| 118 | + if (ret < 0) |
| 119 | + { |
| 120 | + freenect_shutdown(fn_ctx); |
| 121 | + return ret; |
| 122 | + } |
| 123 | + |
| 124 | + // Run until interruption or failure. |
| 125 | + while (running && freenect_process_events(fn_ctx) >= 0) |
| 126 | + { |
| 127 | + |
| 128 | + } |
| 129 | + |
| 130 | + // Stop everything and shutdown. |
| 131 | + freenect_stop_depth(fn_dev); |
| 132 | + freenect_stop_video(fn_dev); |
| 133 | + freenect_close_device(fn_dev); |
| 134 | + freenect_shutdown(fn_ctx); |
| 135 | + |
| 136 | + return 0; |
| 137 | +} |
0 commit comments