Move selected power source to inst 0 only if nb bricks > 1

When trying to move the selected power source to the first publication instance
on systems where there is only one power source, the compiler issued the warning
```
../src/modules/sensors/sensors.cpp:512:54: error: array subscript is outside array bounds [-Werror=array-bounds]
         tmp_h = _battery_pub[_battery_pub_intance0ndx];
```
because it could not verify that _battery_pub_intance0ndx would always be 0.

Wrap the block between `#if BOARD_NUMBER_BRICKS > 1 [...] #endif`
to ensure no out of bound subscript and to remove the warning.

Remove unused _battery_pub_intance0ndx variable when nb bricks = 1
This commit is contained in:
Julien Lecoeur 2017-07-24 14:01:37 +02:00 committed by David Sidrane
parent 9fb5c4f0e9
commit 2c0539ae9c
1 changed files with 14 additions and 8 deletions

View File

@ -169,7 +169,11 @@ private:
orb_advert_t _sensor_pub; /**< combined sensor data topic */
orb_advert_t _battery_pub[BOARD_NUMBER_BRICKS] = {nullptr}; /**< battery status */
#if BOARD_NUMBER_BRICKS > 1
int _battery_pub_intance0ndx = 0; /**< track the index of instance 0 */
#endif
orb_advert_t _airspeed_pub; /**< airspeed */
orb_advert_t _diff_pres_pub; /**< differential_pressure */
orb_advert_t _sensor_preflight; /**< sensor preflight topic */
@ -457,7 +461,6 @@ Sensors::adc_poll(struct sensor_combined_s &raw)
*/
int selected_source = -1;
orb_advert_t tmp_h = nullptr;
if (ret >= (int)sizeof(buf_adc[0])) {
@ -503,17 +506,20 @@ Sensors::adc_poll(struct sensor_combined_s &raw)
* that is valid as the one that is the selected source for the
* VDD_5V_IN
*/
selected_source = b;
/* Move the selected_source to instance 0 */
if (_battery_pub_intance0ndx != b) {
#if BOARD_NUMBER_BRICKS > 1
tmp_h = _battery_pub[_battery_pub_intance0ndx];
_battery_pub[_battery_pub_intance0ndx] = _battery_pub[b];
_battery_pub[b] = tmp_h;
_battery_pub_intance0ndx = b;
/* Move the selected_source to instance 0 */
if (_battery_pub_intance0ndx != selected_source) {
orb_advert_t tmp_h = _battery_pub[_battery_pub_intance0ndx];
_battery_pub[_battery_pub_intance0ndx] = _battery_pub[selected_source];
_battery_pub[selected_source] = tmp_h;
_battery_pub_intance0ndx = selected_source;
}
#endif
}
// todo:per brick scaling