Fix argument parsing in the rgbled command.

This commit is contained in:
px4dev 2013-07-03 22:23:01 -07:00
parent ea1f61e093
commit 03a15bfdc5
1 changed files with 23 additions and 31 deletions

View File

@ -248,7 +248,7 @@ RGBLED::ioctl(struct file *filp, int cmd, unsigned long arg)
void void
RGBLED::led_trampoline(void *arg) RGBLED::led_trampoline(void *arg)
{ {
RGBLED *rgbl = (RGBLED *)arg; RGBLED *rgbl = reinterpret_cast<RGBLED *>(arg);
rgbl->led(); rgbl->led();
} }
@ -413,35 +413,34 @@ void rgbled_usage();
void rgbled_usage() { void rgbled_usage() {
warnx("missing command: try 'start', 'systemstate', 'test', 'info', 'off'"); warnx("missing command: try 'start', 'systemstate', 'test', 'info', 'off'");
warnx("options:"); warnx("options:");
warnx("\t-b --bus i2cbus (3)"); warnx(" -b i2cbus (%d)", PX4_I2C_BUS_LED);
warnx("\t-a --ddr addr (9)"); errx(0, " -a addr (0x%x)", ADDR);
} }
int int
rgbled_main(int argc, char *argv[]) rgbled_main(int argc, char *argv[])
{ {
int i2cdevice = PX4_I2C_BUS_LED; int i2cdevice = PX4_I2C_BUS_LED;
int rgbledadr = ADDR; /* 7bit */ int rgbledadr = ADDR; /* 7bit */
int x; int ch;
while ((ch = getopt(argc, argv, "a:b:")) != EOF) {
switch (ch) {
case 'a':
rgbledadr = strtol(optarg, NULL, 0);
break;
case 'b':
i2cdevice = strtol(optarg, NULL, 0);
break;
default:
rgbled_usage();
}
}
argc -= optind;
argv += optind;
const char *verb = argv[0];
for (x = 1; x < argc; x++) { if (!strcmp(verb, "start")) {
if (strcmp(argv[x], "-b") == 0 || strcmp(argv[x], "--bus") == 0) {
if (argc > x + 1) {
i2cdevice = atoi(argv[x + 1]);
}
}
if (strcmp(argv[x], "-a") == 0 || strcmp(argv[x], "--addr") == 0) {
if (argc > x + 1) {
rgbledadr = atoi(argv[x + 1]);
}
}
}
if (!strcmp(argv[1], "start")) {
if (g_rgbled != nullptr) if (g_rgbled != nullptr)
errx(1, "already started"); errx(1, "already started");
@ -459,39 +458,32 @@ rgbled_main(int argc, char *argv[])
exit(0); exit(0);
} }
/* need the driver past this point */
if (g_rgbled == nullptr) { if (g_rgbled == nullptr) {
fprintf(stderr, "not started\n"); fprintf(stderr, "not started\n");
rgbled_usage(); rgbled_usage();
exit(0); exit(0);
} }
if (!strcmp(argv[1], "test")) { if (!strcmp(verb, "test")) {
g_rgbled->setMode(LED_MODE_TEST); g_rgbled->setMode(LED_MODE_TEST);
exit(0); exit(0);
} }
if (!strcmp(argv[1], "systemstate")) { if (!strcmp(verb, "systemstate")) {
g_rgbled->setMode(LED_MODE_SYSTEMSTATE); g_rgbled->setMode(LED_MODE_SYSTEMSTATE);
exit(0); exit(0);
} }
if (!strcmp(argv[1], "info")) { if (!strcmp(verb, "info")) {
g_rgbled->info(); g_rgbled->info();
exit(0); exit(0);
} }
if (!strcmp(argv[1], "off")) { if (!strcmp(verb, "off")) {
g_rgbled->setMode(LED_MODE_OFF); g_rgbled->setMode(LED_MODE_OFF);
exit(0); exit(0);
} }
/* things that require access to the device */
int fd = open(RGBLED_DEVICE_PATH, 0);
if (fd < 0)
err(1, "can't open RGBLED device");
rgbled_usage(); rgbled_usage();
exit(0);
} }