Skip to content

Commit

Permalink
Apply DRY to fan speed 'should scale zero to one' functionality.
Browse files Browse the repository at this point in the history
Some printers use 0.00-1.00 others use 0.0-256.0 as scale for their fans. This was (nearly) duplicated earlier in the PR.

done as part of CURA-6410
  • Loading branch information
rburema committed Jul 4, 2024
1 parent a3262e9 commit 3435b6d
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/gcodeExport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1434,6 +1434,8 @@ void GCodeExport::writeFanCommand(double speed, std::optional<size_t> extruder)

void GCodeExport::writeSpecificFanCommand(double speed, size_t fan_number)
{


const auto iterator = current_fans_speeds_.find(fan_number);
const std::optional<double> current_fan_speed = (iterator != current_fans_speeds_.end()) ? std::optional<double>(iterator->second) : std::nullopt;

Expand Down Expand Up @@ -1464,16 +1466,17 @@ void GCodeExport::writeSpecificFanCommand(double speed, size_t fan_number)
else
{
const bool should_scale_zero_to_one = Application::getInstance().current_slice_->scene.settings.get<bool>("machine_scale_fan_speed_zero_to_one");
const auto scale_zero_to_one_optional = [should_scale_zero_to_one](double value) -> PrecisionedDouble {
return { (should_scale_zero_to_one ? static_cast<uint8_t>(2) : static_cast<uint8_t>(1)), (should_scale_zero_to_one ? value : value * 255.0) / 100.0 };
};
bool write_value = true;
std::ostringstream new_value;
new_value << PrecisionedDouble{ (should_scale_zero_to_one ? static_cast<uint8_t>(2) : static_cast<uint8_t>(1)), (should_scale_zero_to_one ? speed : speed * 255) / 100 };
new_value << scale_zero_to_one_optional(speed);
const std::string new_value_str = new_value.str();
if (current_fan_speed.has_value())
{
std::ostringstream old_value;
old_value << PrecisionedDouble{ (should_scale_zero_to_one ? static_cast<uint8_t>(2) : static_cast<uint8_t>(1)),
(should_scale_zero_to_one ? current_fan_speed.value() : current_fan_speed.value() * 255) / 100 };

old_value << scale_zero_to_one_optional(current_fan_speed.value());
write_value = new_value_str != old_value.str();
}

Expand Down

0 comments on commit 3435b6d

Please sign in to comment.