From bdfc7b9f69ec8a9495e2efecdf7bbe3c627c03b8 Mon Sep 17 00:00:00 2001 From: Julian Oes Date: Sun, 22 Sep 2013 17:07:02 +0200 Subject: [PATCH] Listen to all consoles plus some more small fixes --- src/systemcmds/esc_calib/esc_calib.c | 39 +++++++++++++++------------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/src/systemcmds/esc_calib/esc_calib.c b/src/systemcmds/esc_calib/esc_calib.c index 0d74218422..608c9fff17 100644 --- a/src/systemcmds/esc_calib/esc_calib.c +++ b/src/systemcmds/esc_calib/esc_calib.c @@ -46,6 +46,7 @@ #include #include #include +#include #include #include #include @@ -83,22 +84,26 @@ usage(const char *reason) int esc_calib_main(int argc, char *argv[]) { - const char *dev = PWM_OUTPUT_DEVICE_PATH; + char *dev = PWM_OUTPUT_DEVICE_PATH; char *ep; bool channels_selected[MAX_CHANNELS] = {false}; int ch; int ret; char c; + struct pollfd fds; + fds.fd = 0; /* stdin */ + fds.events = POLLIN; + if (argc < 2) usage(NULL); - while ((ch = getopt(argc, argv, "d:")) != EOF) { + while ((ch = getopt(argc-1, argv, "d:")) != EOF) { switch (ch) { case 'd': dev = optarg; - argc=-2; + argc-=2; break; default: @@ -106,7 +111,7 @@ esc_calib_main(int argc, char *argv[]) } } - if(argc < 1) { + if(argc < 2) { usage("no channels provided"); } @@ -124,11 +129,6 @@ esc_calib_main(int argc, char *argv[]) } } - /* Wait for confirmation */ - int console = open("/dev/ttyACM0", O_NONBLOCK | O_RDONLY | O_NOCTTY); - if (!console) - err(1, "failed opening console"); - printf("\nATTENTION, please remove or fix propellers before starting calibration!\n" "\n" "Make sure\n" @@ -141,21 +141,21 @@ esc_calib_main(int argc, char *argv[]) /* wait for user input */ while (1) { - if (read(console, &c, 1) == 1) { + ret = poll(&fds, 1, 0); + if (ret > 0) { + + read(0, &c, 1); if (c == 'y' || c == 'Y') { break; } else if (c == 0x03 || c == 0x63 || c == 'q') { printf("ESC calibration exited\n"); - close(console); exit(0); } else if (c == 'n' || c == 'N') { printf("ESC calibration aborted\n"); - close(console); exit(0); } else { printf("Unknown input, ESC calibration aborted\n"); - close(console); exit(0); } } @@ -187,13 +187,15 @@ esc_calib_main(int argc, char *argv[]) } } - if (read(console, &c, 1) == 1) { + ret = poll(&fds, 1, 0); + if (ret > 0) { + + read(0, &c, 1); if (c == 13) { break; } else if (c == 0x03 || c == 0x63 || c == 'q') { warnx("ESC calibration exited"); - close(console); exit(0); } } @@ -218,13 +220,15 @@ esc_calib_main(int argc, char *argv[]) } } - if (read(console, &c, 1) == 1) { + ret = poll(&fds, 1, 0); + if (ret > 0) { + + read(0, &c, 1); if (c == 13) { break; } else if (c == 0x03 || c == 0x63 || c == 'q') { printf("ESC calibration exited\n"); - close(console); exit(0); } } @@ -234,7 +238,6 @@ esc_calib_main(int argc, char *argv[]) printf("ESC calibration finished\n"); - close(console); exit(0); }