Commandline parsing fixes

This commit is contained in:
Lorenz Meier 2013-10-31 09:03:37 +01:00
parent 3c8c091e76
commit 7d443eb332
1 changed files with 28 additions and 16 deletions

View File

@ -77,7 +77,7 @@ usage(const char *reason)
errx(1, errx(1,
"usage:\n" "usage:\n"
"esc_calib [-d <device>] <channels>\n" "esc_calib [-l <low pwm>] [-h <high pwm>] [-d <device>] <channels>\n"
" <device> PWM output device (defaults to " PWM_OUTPUT_DEVICE_PATH ")\n" " <device> PWM output device (defaults to " PWM_OUTPUT_DEVICE_PATH ")\n"
" <channels> Provide channels (e.g.: 1 2 3 4)\n" " <channels> Provide channels (e.g.: 1 2 3 4)\n"
); );
@ -101,25 +101,32 @@ esc_calib_main(int argc, char *argv[])
fds.fd = 0; /* stdin */ fds.fd = 0; /* stdin */
fds.events = POLLIN; fds.events = POLLIN;
if (argc < 2) if (argc < 2) {
usage(NULL); usage("no channels provided");
}
while ((ch = getopt(argc - 1, argv, "d:")) != EOF) { int arg_consumed = 0;
while ((ch = getopt(argc, &argv[0], "l:h:d:")) != -1) {
switch (ch) { switch (ch) {
case 'd': case 'd':
dev = optarg; dev = optarg;
argc -= 2; arg_consumed += 2;
break; break;
case 'l': case 'l':
low = strtol(optarg, NULL, 0); low = strtoul(optarg, &ep, 0);
argc -= 2; if (*ep != '\0')
usage("bad low pwm value");
arg_consumed += 2;
break; break;
case 'h': case 'h':
high = strtol(optarg, NULL, 0); high = strtoul(optarg, &ep, 0);
argc -= 2; if (*ep != '\0')
usage("bad high pwm value");
arg_consumed += 2;
break; break;
default: default:
@ -127,14 +134,12 @@ esc_calib_main(int argc, char *argv[])
} }
} }
if (argc < 2) { while ((--argc - arg_consumed) > 0) {
usage("no channels provided");
}
while (--argc) {
const char *arg = argv[argc]; const char *arg = argv[argc];
unsigned channel_number = strtol(arg, &ep, 0); unsigned channel_number = strtol(arg, &ep, 0);
warnx("adding channel #%d", channel_number);
if (*ep == '\0') { if (*ep == '\0') {
if (channel_number > MAX_CHANNELS || channel_number <= 0) { if (channel_number > MAX_CHANNELS || channel_number <= 0) {
err(1, "invalid channel number: %d", channel_number); err(1, "invalid channel number: %d", channel_number);
@ -257,11 +262,11 @@ esc_calib_main(int argc, char *argv[])
if (pwm_disarmed < 800) if (pwm_disarmed < 800)
pwm_disarmed = 800; pwm_disarmed = 800;
/* tell IO that its ok to disable its safety with the switch */ /* tell IO/FMU that its ok to disable its safety with the switch */
ret = ioctl(fd, PWM_SERVO_SET_ARM_OK, 0); ret = ioctl(fd, PWM_SERVO_SET_ARM_OK, 0);
if (ret != OK) if (ret != OK)
err(1, "PWM_SERVO_SET_ARM_OK"); err(1, "PWM_SERVO_SET_ARM_OK");
/* tell IO that the system is armed (it will output values if safety is off) */ /* tell IO/FMU that the system is armed (it will output values if safety is off) */
ret = ioctl(fd, PWM_SERVO_ARM, 0); ret = ioctl(fd, PWM_SERVO_ARM, 0);
if (ret != OK) if (ret != OK)
err(1, "PWM_SERVO_ARM"); err(1, "PWM_SERVO_ARM");
@ -343,6 +348,13 @@ esc_calib_main(int argc, char *argv[])
usleep(50000); usleep(50000);
} }
/* disarm */
ret = ioctl(fd, PWM_SERVO_DISARM, 0);
if (ret != OK)
err(1, "PWM_SERVO_DISARM");
warnx("Outputs disarmed");
printf("ESC calibration finished\n"); printf("ESC calibration finished\n");
exit(0); exit(0);