Skip to content

Commit

Permalink
SRV_Channel: use enumeration for servo input type
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbarker committed Aug 16, 2022
1 parent b6186db commit 094e932
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
24 changes: 16 additions & 8 deletions libraries/SRV_Channel/SRV_Channel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,15 @@ void SRV_Channel::calc_pwm(float output_scaled)
return;
}

if (type_angle) {
switch (type) {
case Type::AUX:
return;
case Type::ANGLE:
output_pwm = pwm_from_angle(output_scaled);
} else {
break;
case Type::RANGE:
output_pwm = pwm_from_range(output_scaled);
break;
}
}

Expand All @@ -149,27 +154,30 @@ void SRV_Channel::set_output_pwm(uint16_t pwm, bool force)
void SRV_Channel::set_output_norm(float value)
{
// convert normalised value to pwm
if (type_angle) {
switch (type) {
case Type::AUX:
return;
case Type::ANGLE:
set_output_pwm(pwm_from_angle(value * high_out));
} else {
return;
case Type::RANGE:
set_output_pwm(pwm_from_range(value * high_out));
return;
}
}

// set angular range of scaled output
void SRV_Channel::set_angle(int16_t angle)
{
type_angle = true;
type = Type::ANGLE;
high_out = angle;
type_setup = true;
}

// set range of scaled output
void SRV_Channel::set_range(uint16_t high)
{
type_angle = false;
type = Type::RANGE;
high_out = high;
type_setup = true;
}

/*
Expand Down
12 changes: 7 additions & 5 deletions libraries/SRV_Channel/SRV_Channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ class SRV_Channel {
public:
friend class SRV_Channels;

enum class Type {
AUX = 16,
ANGLE = 17,
RANGE = 18,
};

// constructor
SRV_Channel(void);

Expand Down Expand Up @@ -290,11 +296,7 @@ class SRV_Channel {
// a pending output value as PWM
uint16_t output_pwm;

// true for angle output type
bool type_angle:1;

// set_range() or set_angle() has been called
bool type_setup:1;
Type type = Type::RANGE;

// the hal channel number
uint8_t ch_num;
Expand Down
8 changes: 3 additions & 5 deletions libraries/SRV_Channel/SRV_Channel_aux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,11 @@ SRV_Channel::Aux_servo_function_t SRV_Channels::channel_function(uint8_t channel
}

/*
setup a channels aux servo function
setup a channel's function. Despite the name this is called for
non-aux functions (e.g. yaw out) too.
*/
void SRV_Channel::aux_servo_function_setup(void)
{
if (type_setup) {
return;
}
switch (function.get()) {
case k_flap:
case k_flap_auto:
Expand Down Expand Up @@ -517,7 +515,7 @@ bool SRV_Channels::set_aux_channel_default(SRV_Channel::Aux_servo_function_t fun
(unsigned)channels[channel].function.get());
return false;
}
channels[channel].type_setup = false;
channels[channel].type = SRV_Channel::Type::AUX;
channels[channel].function.set_and_default(function);
channels[channel].aux_servo_function_setup();
function_mask.set((uint16_t)function);
Expand Down

0 comments on commit 094e932

Please sign in to comment.