change the nested if structure which tries all i2c busses to a loop.

This commit is contained in:
Simon Laube
2015-07-04 18:26:52 +02:00
committed by Lorenz Meier
parent 4372701dab
commit dac74db104
+45 -36
View File
@@ -616,65 +616,74 @@ start()
errx(1, "already started"); errx(1, "already started");
} }
/* create the driver */ const int busses_to_try[] = {
g_dev = new PX4FLOW(PX4_I2C_BUS_EXPANSION); PX4_I2C_BUS_EXPANSION,
#ifdef PX4_I2C_BUS_ESC
PX4_I2C_BUS_ESC,
#endif
PX4_I2C_BUS_ONBOARD,
-1
};
if (g_dev == nullptr) { const int *cur_bus = busses_to_try;
goto fail; while(*cur_bus != -1) {
/* create the driver */
//warnx("trying bus %d", *cur_bus);
g_dev = new PX4FLOW(*cur_bus);
if (g_dev == nullptr) {
/* this is a fatal error */
break;
}
/* init the driver: */
if (OK == g_dev->init()) {
/* success! */
break;
}
/* destroy it again because it failed. */
delete g_dev;
g_dev = nullptr;
/* try next! */
cur_bus++;
} }
if (OK != g_dev->init()) { /* check whether we found it: */
if (*cur_bus == -1) {
goto not_found;
}
#ifdef PX4_I2C_BUS_ESC /* check for failure: */
delete g_dev; if (g_dev == nullptr) {
/* try 2nd bus */ goto fatal_fail;
g_dev = new PX4FLOW(PX4_I2C_BUS_ESC);
if (g_dev == nullptr) {
goto fail;
}
if (OK != g_dev->init()) {
#endif
delete g_dev;
/* try 3rd bus */
g_dev = new PX4FLOW(PX4_I2C_BUS_ONBOARD);
if (g_dev == nullptr) {
goto fail;
}
if (OK != g_dev->init()) {
goto fail;
}
#ifdef PX4_I2C_BUS_ESC
}
#endif
} }
/* set the poll rate to default, starts automatic data collection */ /* set the poll rate to default, starts automatic data collection */
fd = open(PX4FLOW0_DEVICE_PATH, O_RDONLY); fd = open(PX4FLOW0_DEVICE_PATH, O_RDONLY);
if (fd < 0) { if (fd < 0) {
goto fail; goto fatal_fail;
} }
if (ioctl(fd, SENSORIOCSPOLLRATE, SENSOR_POLLRATE_MAX) < 0) { if (ioctl(fd, SENSORIOCSPOLLRATE, SENSOR_POLLRATE_MAX) < 0) {
goto fail; goto fatal_fail;
} }
exit(0); exit(0);
fail: not_found:
/* for now we do the same as if there was a fatal failure. */
warnx("PX4FLOW not found on I2C busses");
fatal_fail:
if (g_dev != nullptr) { if (g_dev != nullptr) {
delete g_dev; delete g_dev;
g_dev = nullptr; g_dev = nullptr;
} }
errx(1, "no PX4FLOW connected over I2C"); errx(1, "PX4FLOW could not be started over I2C");
} }
/** /**