Skip to content

Commit a77faaf

Browse files
IamPete1peterbarker
authored andcommitted
AP_Relay: move from using AP_RELAY_NUM_RELAYS to ARRAY_SIZE(_params)
1 parent c917480 commit a77faaf

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

libraries/AP_Relay/AP_Relay.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ void AP_Relay::convert_params()
164164
const bool have_default = AP_Param::get_param_by_index(this, 4, AP_PARAM_INT8, &default_state);
165165

166166
// grab the old values if they were set
167-
for (uint8_t i = 0; i < MIN(AP_RELAY_NUM_RELAYS, 6); i++) {
167+
for (uint8_t i = 0; i < MIN(ARRAY_SIZE(_params), 6U); i++) {
168168
if (_params[i].function.configured()) {
169169
// Conversion already done, or user has configured manually
170170
continue;
@@ -235,7 +235,7 @@ void AP_Relay::set_defaults() {
235235
RELAY5_PIN_DEFAULT,
236236
RELAY6_PIN_DEFAULT };
237237

238-
for (uint8_t i = 0; i < MIN(uint8_t(AP_RELAY_NUM_RELAYS), ARRAY_SIZE(pins)); i++) {
238+
for (uint8_t i = 0; i < MIN(ARRAY_SIZE(_params), ARRAY_SIZE(pins)); i++) {
239239
// set the default
240240
if (pins[i] != -1) {
241241
_params[i].pin.set_default(pins[i]);
@@ -250,7 +250,7 @@ void AP_Relay::init()
250250
convert_params();
251251

252252
// setup the actual default values of all the pins
253-
for (uint8_t instance = 0; instance < AP_RELAY_NUM_RELAYS; instance++) {
253+
for (uint8_t instance = 0; instance < ARRAY_SIZE(_params); instance++) {
254254
const int8_t pin = _params[instance].pin;
255255
if (pin == -1) {
256256
// no valid pin to set it on, skip it
@@ -285,7 +285,7 @@ void AP_Relay::set(const AP_Relay_Params::FUNCTION function, const bool value) {
285285
return;
286286
}
287287

288-
for (uint8_t instance = 0; instance < AP_RELAY_NUM_RELAYS; instance++) {
288+
for (uint8_t instance = 0; instance < ARRAY_SIZE(_params); instance++) {
289289
if (function != _params[instance].function) {
290290
continue;
291291
}
@@ -324,7 +324,7 @@ void AP_Relay::set_pin_by_instance(uint8_t instance, bool value)
324324

325325
void AP_Relay::set(const uint8_t instance, const bool value)
326326
{
327-
if (instance >= AP_RELAY_NUM_RELAYS) {
327+
if (instance >= ARRAY_SIZE(_params)) {
328328
return;
329329
}
330330

@@ -337,15 +337,15 @@ void AP_Relay::set(const uint8_t instance, const bool value)
337337

338338
void AP_Relay::toggle(uint8_t instance)
339339
{
340-
if (instance < AP_RELAY_NUM_RELAYS) {
340+
if (instance < ARRAY_SIZE(_params)) {
341341
set(instance, !get(instance));
342342
}
343343
}
344344

345345
// check settings are valid
346346
bool AP_Relay::arming_checks(size_t buflen, char *buffer) const
347347
{
348-
for (uint8_t i=0; i<AP_RELAY_NUM_RELAYS; i++) {
348+
for (uint8_t i=0; i<ARRAY_SIZE(_params); i++) {
349349
const int8_t pin = _params[i].pin.get();
350350
if (pin != -1 && !hal.gpio->valid_pin(pin)) {
351351
char param_name_buf[14];
@@ -364,7 +364,7 @@ bool AP_Relay::arming_checks(size_t buflen, char *buffer) const
364364

365365
bool AP_Relay::get(uint8_t instance) const
366366
{
367-
if (instance >= AP_RELAY_NUM_RELAYS) {
367+
if (instance >= ARRAY_SIZE(_params)) {
368368
// invalid instance
369369
return false;
370370
}
@@ -383,13 +383,13 @@ bool AP_Relay::get(uint8_t instance) const
383383
bool AP_Relay::enabled(uint8_t instance) const
384384
{
385385
// Must be a valid instance with function relay and pin set
386-
return (instance < AP_RELAY_NUM_RELAYS) && (_params[instance].pin != -1) && (_params[instance].function == AP_Relay_Params::FUNCTION::RELAY);
386+
return (instance < ARRAY_SIZE(_params)) && (_params[instance].pin != -1) && (_params[instance].function == AP_Relay_Params::FUNCTION::RELAY);
387387
}
388388

389389
// see if the relay is enabled
390390
bool AP_Relay::enabled(AP_Relay_Params::FUNCTION function) const
391391
{
392-
for (uint8_t instance = 0; instance < AP_RELAY_NUM_RELAYS; instance++) {
392+
for (uint8_t instance = 0; instance < ARRAY_SIZE(_params); instance++) {
393393
if ((_params[instance].function == function) && (_params[instance].pin != -1)) {
394394
return true;
395395
}
@@ -408,7 +408,7 @@ bool AP_Relay::send_relay_status(const GCS_MAVLINK &link) const
408408

409409
uint16_t present_mask = 0;
410410
uint16_t on_mask = 0;
411-
for (auto i=0; i<AP_RELAY_NUM_RELAYS; i++) {
411+
for (uint8_t i=0; i<ARRAY_SIZE(_params); i++) {
412412
if (!enabled(i)) {
413413
continue;
414414
}

0 commit comments

Comments
 (0)