Skip to content

Commit

Permalink
SRV_Channels: find_channel: use channel mask remove need for search o…
Browse files Browse the repository at this point in the history
…ver all channels
  • Loading branch information
IamPete1 committed Aug 16, 2024
1 parent 61bd5e9 commit e9f2515
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions libraries/SRV_Channel/SRV_Channel_aux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -565,16 +565,25 @@ bool SRV_Channels::set_aux_channel_default(SRV_Channel::Aux_servo_function_t fun
// find first channel that a function is assigned to
bool SRV_Channels::find_channel(SRV_Channel::Aux_servo_function_t function, uint8_t &chan)
{
if (!function_assigned(function)) {
// Must have populated channel masks
if (!initialised) {
update_aux_servo_function();
}

// Make sure function is valid
if (!SRV_Channel::valid_function(function)) {
return false;
}
for (uint8_t i=0; i<NUM_SERVO_CHANNELS; i++) {
if (channels[i].function == function) {
chan = channels[i].ch_num;
return true;
}

// Get the index of the first set bit, returns 0 if no bits are set
const int first_chan = __builtin_ffs(functions[function].channel_mask);
if (first_chan == 0) {
return false;
}
return false;

// Convert to 0 indexed
chan = first_chan - 1;
return true;
}

/*
Expand Down

0 comments on commit e9f2515

Please sign in to comment.