Skip to content

Commit 4487f12

Browse files
committed
Add freenect-camtest example for headless testing - fixes #428
Signed-off-by: Benn Snyder <[email protected]>
1 parent 45f97b6 commit 4487f12

File tree

2 files changed

+142
-4
lines changed

2 files changed

+142
-4
lines changed

examples/CMakeLists.txt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
######################################################################################
44

55
# todo: use these throughout
6-
file(GLOB SRC_STANDARD chunkview.c glview.c hiview.c regview.c)
6+
file(GLOB SRC_STANDARD chunkview.c glview.c hiview.c regview.c camtest.c)
77
file(GLOB SRC_AUDIO micview.c wavrecord.c)
88
file(GLOB SRC_SYNC glpclview.c regtest.c tiltdemo.c)
99
set (SRC_ALL ${SRC_STANDARD} ${SRC_AUDIO} ${SRC_SYNC})
@@ -21,6 +21,7 @@ add_executable(freenect-hiview hiview.c)
2121
add_executable(freenect-chunkview chunkview.c)
2222
add_executable(freenect-wavrecord wavrecord.c)
2323
add_executable(freenect-micview micview.c)
24+
add_executable(freenect-camtest camtest.c)
2425

2526
if (BUILD_C_SYNC)
2627
add_executable(freenect-glpclview glpclview.c)
@@ -43,12 +44,12 @@ if(APPLE)
4344
target_link_libraries(freenect-chunkview freenect)
4445
target_link_libraries(freenect-wavrecord freenect)
4546
target_link_libraries(freenect-micview freenect)
47+
target_link_libraries(freenect-camtest freenect)
4648
if (BUILD_C_SYNC)
4749
target_link_libraries(freenect-glpclview freenect_sync)
4850
target_link_libraries(freenect-tiltdemo freenect_sync)
4951
target_link_libraries(freenect-regtest freenect_sync)
5052
endif()
51-
# Linux, not so much
5253
else()
5354

5455
find_package(Threads REQUIRED)
@@ -63,9 +64,9 @@ else()
6364
target_link_libraries(freenect-chunkview freenect ${OPENGL_LIBRARIES} ${GLUT_LIBRARY} ${CMAKE_THREAD_LIBS_INIT} ${MATH_LIB})
6465
target_link_libraries(freenect-wavrecord freenect ${OPENGL_LIBRARIES} ${GLUT_LIBRARY} ${CMAKE_THREAD_LIBS_INIT} ${MATH_LIB})
6566
target_link_libraries(freenect-micview freenect ${OPENGL_LIBRARIES} ${GLUT_LIBRARY} ${CMAKE_THREAD_LIBS_INIT} ${MATH_LIB})
67+
target_link_libraries(freenect-camtest freenect)
6668
if (BUILD_C_SYNC)
67-
target_link_libraries(freenect-glpclview freenect_sync ${OPENGL_LIBRARIES} ${GLUT_LIBRARY}
68-
${CMAKE_THREAD_LIBS_INIT} ${MATH_LIB})
69+
target_link_libraries(freenect-glpclview freenect_sync ${OPENGL_LIBRARIES} ${GLUT_LIBRARY} ${CMAKE_THREAD_LIBS_INIT} ${MATH_LIB})
6970
target_link_libraries(freenect-tiltdemo freenect_sync ${CMAKE_THREAD_LIBS_INIT} ${MATH_LIB})
7071
target_link_libraries(freenect-regtest freenect_sync ${CMAKE_THREAD_LIBS_INIT})
7172
endif()

examples/camtest.c

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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

Comments
 (0)