Browse Source

lis3mdl: add argc check and use px4_getopt

sbg
Beat Küng 7 years ago committed by Lorenz Meier
parent
commit
cce3c270c3
  1. 1
      src/drivers/magnetometer/lis3mdl/lis3mdl.h
  2. 36
      src/drivers/magnetometer/lis3mdl/lis3mdl_main.cpp

1
src/drivers/magnetometer/lis3mdl/lis3mdl.h

@ -40,7 +40,6 @@
#pragma once #pragma once
#include <float.h> #include <float.h>
#include <getopt.h>
#include <drivers/device/i2c.h> #include <drivers/device/i2c.h>
#include <drivers/device/ringbuffer.h> #include <drivers/device/ringbuffer.h>

36
src/drivers/magnetometer/lis3mdl/lis3mdl_main.cpp

@ -38,6 +38,7 @@
*/ */
#include "lis3mdl_main.h" #include "lis3mdl_main.h"
#include <px4_getopt.h>
/** /**
* Driver 'main' command. * Driver 'main' command.
@ -295,16 +296,18 @@ lis3mdl::usage()
int int
lis3mdl_main(int argc, char *argv[]) lis3mdl_main(int argc, char *argv[])
{ {
int index = 0; int myoptind = 1;
bool calibrate = false; int ch;
const char *myoptarg = nullptr;
bool calibrate = false;
enum LIS3MDL_BUS bus_id = LIS3MDL_BUS_ALL; enum LIS3MDL_BUS bus_id = LIS3MDL_BUS_ALL;
enum Rotation rotation = ROTATION_NONE; enum Rotation rotation = ROTATION_NONE;
while ((index = getopt(argc, argv, "XISR:CT")) != EOF) { while ((ch = px4_getopt(argc, argv, "XISR:CT", &myoptind, &myoptarg)) != EOF) {
switch (index) { switch (ch) {
case 'R': case 'R':
rotation = (enum Rotation)atoi(optarg); rotation = (enum Rotation)atoi(myoptarg);
break; break;
#if (PX4_I2C_BUS_ONBOARD || PX4_SPIDEV_LIS) #if (PX4_I2C_BUS_ONBOARD || PX4_SPIDEV_LIS)
@ -331,10 +334,15 @@ lis3mdl_main(int argc, char *argv[])
} }
} }
const char *arg = argv[optind]; if (myoptind >= argc) {
lis3mdl::usage();
return -1;
}
const char *verb = argv[myoptind];
// Start/load the driver // Start/load the driver
if (!strcmp(arg, "start")) { if (!strcmp(verb, "start")) {
if (lis3mdl::start(bus_id, rotation)) { if (lis3mdl::start(bus_id, rotation)) {
if (calibrate) { if (calibrate) {
@ -354,28 +362,28 @@ lis3mdl_main(int argc, char *argv[])
} }
// Stop the driver // Stop the driver
if (!strcmp(arg, "stop")) { if (!strcmp(verb, "stop")) {
return lis3mdl::stop(); return lis3mdl::stop();
} }
// Test the driver/device // Test the driver/device
if (!strcmp(arg, "test")) { if (!strcmp(verb, "test")) {
return lis3mdl::test(bus_id); return lis3mdl::test(bus_id);
} }
// Reset the driver // Reset the driver
if (!strcmp(arg, "reset")) { if (!strcmp(verb, "reset")) {
return lis3mdl::reset(bus_id); return lis3mdl::reset(bus_id);
} }
// Print driver information // Print driver information
if (!strcmp(arg, "info") || if (!strcmp(verb, "info") ||
!strcmp(arg, "status")) { !strcmp(verb, "status")) {
return lis3mdl::info(bus_id); return lis3mdl::info(bus_id);
} }
// Autocalibrate the scaling // Autocalibrate the scaling
if (!strcmp(arg, "calibrate")) { if (!strcmp(verb, "calibrate")) {
if (lis3mdl::calibrate(bus_id) == 0) { if (lis3mdl::calibrate(bus_id) == 0) {
PX4_INFO("calibration successful"); PX4_INFO("calibration successful");
return 0; return 0;
@ -402,4 +410,4 @@ lis3mdl::lis3mdl_bus_option &lis3mdl::find_bus(LIS3MDL_BUS bus_id)
} }
errx(1, "bus %u not started", (unsigned)bus_id); errx(1, "bus %u not started", (unsigned)bus_id);
} }

Loading…
Cancel
Save