From 6cb17839ee0cb2e9bd7f652c7d0251a236ff172e Mon Sep 17 00:00:00 2001 From: DanielePettenuzzo Date: Tue, 3 Jul 2018 13:10:15 +0200 Subject: [PATCH] distance sensors: remove bus_option array and add it to i2c.h header --- src/drivers/distance_sensor/mb12xx/mb12xx.cpp | 22 ++----------------- src/drivers/distance_sensor/sf1xx/sf1xx.cpp | 22 ++----------------- src/drivers/distance_sensor/srf02/srf02.cpp | 22 ++----------------- .../distance_sensor/teraranger/teraranger.cpp | 22 ++----------------- .../distance_sensor/vl53lxx/vl53lxx.cpp | 22 ++----------------- src/lib/drivers/device/i2c.h | 18 +++++++++++++++ 6 files changed, 28 insertions(+), 100 deletions(-) diff --git a/src/drivers/distance_sensor/mb12xx/mb12xx.cpp b/src/drivers/distance_sensor/mb12xx/mb12xx.cpp index 0959a5203c..c9e599f83f 100644 --- a/src/drivers/distance_sensor/mb12xx/mb12xx.cpp +++ b/src/drivers/distance_sensor/mb12xx/mb12xx.cpp @@ -683,23 +683,6 @@ namespace mb12xx MB12XX *g_dev; -int bus_options[] = { -#ifdef PX4_I2C_BUS_EXPANSION - PX4_I2C_BUS_EXPANSION, -#endif -#ifdef PX4_I2C_BUS_EXPANSION1 - PX4_I2C_BUS_EXPANSION1, -#endif -#ifdef PX4_I2C_BUS_EXPANSION2 - PX4_I2C_BUS_EXPANSION2, -#endif -#ifdef PX4_I2C_BUS_ONBOARD - PX4_I2C_BUS_ONBOARD, -#endif -}; - -#define NUM_BUS_OPTIONS (sizeof(bus_options)/sizeof(bus_options[0])) - int start(uint8_t rotation); int start_bus(uint8_t rotation, int i2c_bus); int stop(); @@ -724,8 +707,8 @@ start(uint8_t rotation) return PX4_ERROR; } - for (unsigned i = 0; i < NUM_BUS_OPTIONS; i++) { - if (start_bus(rotation, bus_options[i]) == PX4_OK) { + for (unsigned i = 0; i < NUM_I2C_BUS_OPTIONS; i++) { + if (start_bus(rotation, i2c_bus_options[i]) == PX4_OK) { return PX4_OK; } } @@ -785,7 +768,6 @@ fail: g_dev = nullptr; } - PX4_ERR("not started on bus %d", i2c_bus); return PX4_ERROR; } diff --git a/src/drivers/distance_sensor/sf1xx/sf1xx.cpp b/src/drivers/distance_sensor/sf1xx/sf1xx.cpp index 687d87f5d9..540dcd63b0 100644 --- a/src/drivers/distance_sensor/sf1xx/sf1xx.cpp +++ b/src/drivers/distance_sensor/sf1xx/sf1xx.cpp @@ -635,23 +635,6 @@ namespace sf1xx SF1XX *g_dev; -int bus_options[] = { -#ifdef PX4_I2C_BUS_EXPANSION - PX4_I2C_BUS_EXPANSION, -#endif -#ifdef PX4_I2C_BUS_EXPANSION1 - PX4_I2C_BUS_EXPANSION1, -#endif -#ifdef PX4_I2C_BUS_EXPANSION2 - PX4_I2C_BUS_EXPANSION2, -#endif -#ifdef PX4_I2C_BUS_ONBOARD - PX4_I2C_BUS_ONBOARD, -#endif -}; - -#define NUM_BUS_OPTIONS (sizeof(bus_options)/sizeof(bus_options[0])) - int start(uint8_t rotation); int start_bus(uint8_t rotation, int i2c_bus); int stop(); @@ -676,8 +659,8 @@ start(uint8_t rotation) return PX4_ERROR; } - for (unsigned i = 0; i < NUM_BUS_OPTIONS; i++) { - if (start_bus(rotation, bus_options[i]) == PX4_OK) { + for (unsigned i = 0; i < NUM_I2C_BUS_OPTIONS; i++) { + if (start_bus(rotation, i2c_bus_options[i]) == PX4_OK) { return PX4_OK; } } @@ -734,7 +717,6 @@ fail: g_dev = nullptr; } - PX4_ERR("not started on bus %d", i2c_bus); return PX4_ERROR; } diff --git a/src/drivers/distance_sensor/srf02/srf02.cpp b/src/drivers/distance_sensor/srf02/srf02.cpp index 1fd0da6ac6..105769d6ee 100644 --- a/src/drivers/distance_sensor/srf02/srf02.cpp +++ b/src/drivers/distance_sensor/srf02/srf02.cpp @@ -686,23 +686,6 @@ namespace srf02 SRF02 *g_dev; -int bus_options[] = { -#ifdef PX4_I2C_BUS_EXPANSION - PX4_I2C_BUS_EXPANSION, -#endif -#ifdef PX4_I2C_BUS_EXPANSION1 - PX4_I2C_BUS_EXPANSION1, -#endif -#ifdef PX4_I2C_BUS_EXPANSION2 - PX4_I2C_BUS_EXPANSION2, -#endif -#ifdef PX4_I2C_BUS_ONBOARD - PX4_I2C_BUS_ONBOARD, -#endif -}; - -#define NUM_BUS_OPTIONS (sizeof(bus_options)/sizeof(bus_options[0])) - int start(uint8_t rotation); int start_bus(uint8_t rotation, int i2c_bus); int stop(); @@ -727,8 +710,8 @@ start(uint8_t rotation) return PX4_ERROR; } - for (unsigned i = 0; i < NUM_BUS_OPTIONS; i++) { - if (start_bus(rotation, bus_options[i]) == PX4_OK) { + for (unsigned i = 0; i < NUM_I2C_BUS_OPTIONS; i++) { + if (start_bus(rotation, i2c_bus_options[i]) == PX4_OK) { return PX4_OK; } } @@ -788,7 +771,6 @@ fail: g_dev = nullptr; } - PX4_ERR("not started on bus %d", i2c_bus); return PX4_ERROR; } diff --git a/src/drivers/distance_sensor/teraranger/teraranger.cpp b/src/drivers/distance_sensor/teraranger/teraranger.cpp index 6bc34fdfb1..c526b16e93 100644 --- a/src/drivers/distance_sensor/teraranger/teraranger.cpp +++ b/src/drivers/distance_sensor/teraranger/teraranger.cpp @@ -746,23 +746,6 @@ namespace teraranger TERARANGER *g_dev; -int bus_options[] = { -#ifdef PX4_I2C_BUS_EXPANSION - PX4_I2C_BUS_EXPANSION, -#endif -#ifdef PX4_I2C_BUS_EXPANSION1 - PX4_I2C_BUS_EXPANSION1, -#endif -#ifdef PX4_I2C_BUS_EXPANSION2 - PX4_I2C_BUS_EXPANSION2, -#endif -#ifdef PX4_I2C_BUS_ONBOARD - PX4_I2C_BUS_ONBOARD, -#endif -}; - -#define NUM_BUS_OPTIONS (sizeof(bus_options)/sizeof(bus_options[0])) - int start(uint8_t rotation); int start_bus(uint8_t rotation, int i2c_bus); int stop(); @@ -787,8 +770,8 @@ start(uint8_t rotation) return PX4_ERROR; } - for (unsigned i = 0; i < NUM_BUS_OPTIONS; i++) { - if (start_bus(rotation, bus_options[i]) == PX4_OK) { + for (unsigned i = 0; i < NUM_I2C_BUS_OPTIONS; i++) { + if (start_bus(rotation, i2c_bus_options[i]) == PX4_OK) { return PX4_OK; } } @@ -849,7 +832,6 @@ fail: g_dev = nullptr; } - PX4_ERR("not started on bus %d", i2c_bus); return PX4_ERROR; } diff --git a/src/drivers/distance_sensor/vl53lxx/vl53lxx.cpp b/src/drivers/distance_sensor/vl53lxx/vl53lxx.cpp index d722aadb4d..f8c53ccbb5 100644 --- a/src/drivers/distance_sensor/vl53lxx/vl53lxx.cpp +++ b/src/drivers/distance_sensor/vl53lxx/vl53lxx.cpp @@ -967,23 +967,6 @@ namespace vl53lxx VL53LXX *g_dev; -int bus_options[] = { -#ifdef PX4_I2C_BUS_EXPANSION - PX4_I2C_BUS_EXPANSION, -#endif -#ifdef PX4_I2C_BUS_EXPANSION1 - PX4_I2C_BUS_EXPANSION1, -#endif -#ifdef PX4_I2C_BUS_EXPANSION2 - PX4_I2C_BUS_EXPANSION2, -#endif -#ifdef PX4_I2C_BUS_ONBOARD - PX4_I2C_BUS_ONBOARD, -#endif -}; - -#define NUM_BUS_OPTIONS (sizeof(bus_options)/sizeof(bus_options[0])) - int start(uint8_t rotation); int start_bus(uint8_t rotation, int i2c_bus); int stop(); @@ -1007,8 +990,8 @@ start(uint8_t rotation) return PX4_ERROR; } - for (unsigned i = 0; i < NUM_BUS_OPTIONS; i++) { - if (start_bus(rotation, bus_options[i]) == PX4_OK) { + for (unsigned i = 0; i < NUM_I2C_BUS_OPTIONS; i++) { + if (start_bus(rotation, i2c_bus_options[i]) == PX4_OK) { return PX4_OK; } } @@ -1068,7 +1051,6 @@ fail: g_dev = nullptr; } - PX4_ERR("not started on bus %d", i2c_bus); return PX4_ERROR; } diff --git a/src/lib/drivers/device/i2c.h b/src/lib/drivers/device/i2c.h index 822c35801e..8e4de55839 100644 --- a/src/lib/drivers/device/i2c.h +++ b/src/lib/drivers/device/i2c.h @@ -37,3 +37,21 @@ #else #include "posix/I2C.hpp" #endif + + +static const int i2c_bus_options[] = { +#ifdef PX4_I2C_BUS_EXPANSION + PX4_I2C_BUS_EXPANSION, +#endif +#ifdef PX4_I2C_BUS_EXPANSION1 + PX4_I2C_BUS_EXPANSION1, +#endif +#ifdef PX4_I2C_BUS_EXPANSION2 + PX4_I2C_BUS_EXPANSION2, +#endif +#ifdef PX4_I2C_BUS_ONBOARD + PX4_I2C_BUS_ONBOARD, +#endif +}; + +#define NUM_I2C_BUS_OPTIONS (sizeof(i2c_bus_options)/sizeof(i2c_bus_options[0])) \ No newline at end of file