From 21c791e9597118ec950c7ae4330d57b967ff2032 Mon Sep 17 00:00:00 2001 From: Daniel Agar Date: Wed, 29 May 2019 21:20:36 -0400 Subject: [PATCH] listener exit with ctrl-c, escape, or q --- .../topic_listener/listener_main.cpp | 33 +++++++++++++++++-- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/src/systemcmds/topic_listener/listener_main.cpp b/src/systemcmds/topic_listener/listener_main.cpp index 17d4b40c47..a08b522d82 100644 --- a/src/systemcmds/topic_listener/listener_main.cpp +++ b/src/systemcmds/topic_listener/listener_main.cpp @@ -40,6 +40,8 @@ #include #include +#include + #include "topic_listener.hpp" #include "topic_listener_generated.hpp" @@ -63,13 +65,38 @@ void listener(listener_print_topic_cb cb, const orb_id_t &id, unsigned num_msgs, hrt_abstime start_time = hrt_absolute_time(); while (i < num_msgs) { + + // check for user input to quit + int user_input_timeout = 1; + orb_check(sub, &updated); if (i == 0) { updated = true; + user_input_timeout = 0; // don't wait + } - } else { - px4_usleep(500); + // check for user input + struct pollfd fds {}; + fds.fd = 0; /* stdin */ + fds.events = POLLIN; + + if (poll(&fds, 1, 0) > 0) { + + char c = 0; + int ret = read(0, &c, user_input_timeout); + + if (ret) { + return; + } + + switch (c) { + case 0x03: // ctrl-c + case 0x1b: // esc + case 'q': + return; + /* not reached */ + } } if (updated) { @@ -136,7 +163,7 @@ int listener_main(int argc, char *argv[]) if (num_msgs == 0) { if (topic_rate != 0) { - num_msgs = 10 * topic_rate; // arbitrary limit (10 seconds at max rate) + num_msgs = 30 * topic_rate; // arbitrary limit (30 seconds at max rate) } else { num_msgs = 1;