Skip to content

Commit a325695

Browse files
committed
examples: Show how to pause
1 parent db37864 commit a325695

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

examples/Protonect.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,27 @@ void sigint_handler(int s)
4848
protonect_shutdown = true;
4949
}
5050

51+
bool protonect_paused = false;
52+
libfreenect2::Freenect2Device *devtopause;
53+
54+
//Doing non-trivial things in signal handler is bad. If you want to pause,
55+
//do it in another thread.
56+
//Though libusb operations are generally thread safe, I cannot guarantee
57+
//everything above is thread safe when calling start()/stop() while
58+
//waitForNewFrame().
59+
void sigusr1_handler(int s)
60+
{
61+
if (devtopause == 0)
62+
return;
63+
/// [pause]
64+
if (protonect_paused)
65+
devtopause->start();
66+
else
67+
devtopause->stop();
68+
protonect_paused = !protonect_paused;
69+
/// [pause]
70+
}
71+
5172
//The following demostrates how to create a custom logger
5273
/// [logger]
5374
#include <fstream>
@@ -91,6 +112,7 @@ int main(int argc, char *argv[])
91112
std::string program_path(argv[0]);
92113
std::cerr << "Environment variables: LOGFILE=<protonect.log>" << std::endl;
93114
std::cerr << "Usage: " << program_path << " [gl | cl | cpu] [<device serial>] [-noviewer]" << std::endl;
115+
std::cerr << "To pause and unpause: pkill -USR1 Protonect" << std::endl;
94116
size_t executable_name_idx = program_path.rfind("Protonect");
95117

96118
std::string binpath = "/";
@@ -193,7 +215,12 @@ int main(int argc, char *argv[])
193215
return -1;
194216
}
195217

218+
devtopause = dev;
219+
196220
signal(SIGINT,sigint_handler);
221+
#ifdef SIGUSR1
222+
signal(SIGUSR1, sigusr1_handler);
223+
#endif
197224
protonect_shutdown = false;
198225

199226
/// [listeners]

include/libfreenect2/libfreenect2.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,20 @@ If you are finished and no longer need to receive more frames, you can stop
224224
the device and exit.
225225
226226
@snippet Protonect.cpp stop
227+
228+
Pause the Device
229+
----------------
230+
231+
You can also temporarily pause the device with
232+
[stop()](@ref libfreenect2::Freenect2Device::stop) and
233+
[start()](@ref libfreenect2::Freenect2Device::start).
234+
235+
@snippet Protonect.cpp pause
236+
237+
Doing this during `waitForNewFrame()` should be thread safe, and tests also
238+
show well. But a guarantee of thread safety has not been checked yet.
239+
240+
THE END.
227241
*/
228242

229243
namespace libfreenect2

0 commit comments

Comments
 (0)