Skip to content

Commit

Permalink
AP_Relay: move from using AP_RELAY_NUM_RELAYS to ARRAY_SIZE(_params)
Browse files Browse the repository at this point in the history
  • Loading branch information
IamPete1 committed Dec 18, 2023
1 parent b177be2 commit ce2f4ff
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions libraries/AP_Relay/AP_Relay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ void AP_Relay::convert_params()
const bool have_default = AP_Param::get_param_by_index(this, 4, AP_PARAM_INT8, &default_state);

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

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

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

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

void AP_Relay::set(const uint8_t instance, const bool value)
{
if (instance >= AP_RELAY_NUM_RELAYS) {
if (instance >= ARRAY_SIZE(_params)) {
return;
}

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

void AP_Relay::toggle(uint8_t instance)
{
if (instance < AP_RELAY_NUM_RELAYS) {
if (instance < ARRAY_SIZE(_params)) {
set(instance, !get(instance));
}
}

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

bool AP_Relay::get(uint8_t instance) const
{
if (instance >= AP_RELAY_NUM_RELAYS) {
if (instance >= ARRAY_SIZE(_params)) {
// invalid instance
return false;
}
Expand All @@ -383,13 +383,13 @@ bool AP_Relay::get(uint8_t instance) const
bool AP_Relay::enabled(uint8_t instance) const
{
// Must be a valid instance with function relay and pin set
return (instance < AP_RELAY_NUM_RELAYS) && (_params[instance].pin != -1) && (_params[instance].function == AP_Relay_Params::FUNCTION::RELAY);
return (instance < ARRAY_SIZE(_params)) && (_params[instance].pin != -1) && (_params[instance].function == AP_Relay_Params::FUNCTION::RELAY);
}

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

uint16_t present_mask = 0;
uint16_t on_mask = 0;
for (auto i=0; i<AP_RELAY_NUM_RELAYS; i++) {
for (uint8_t i=0; i<ARRAY_SIZE(_params); i++) {
if (!enabled(i)) {
continue;
}
Expand Down

0 comments on commit ce2f4ff

Please sign in to comment.