Skip to content

Commit

Permalink
Replace string manipulation with numeric calculation.
Browse files Browse the repository at this point in the history
done as part of CURA-6410
  • Loading branch information
rburema committed Jul 4, 2024
1 parent 3435b6d commit c750ec6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
6 changes: 6 additions & 0 deletions include/utils/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#ifndef UTILS_STRING_H
#define UTILS_STRING_H

#include <cmath>
#include <cstdio> // sprintf
#include <ctype.h>
#include <sstream> // ostringstream
Expand Down Expand Up @@ -169,6 +170,11 @@ struct PrecisionedDouble
uint8_t precision; //!< Number of digits after the decimal mark with which to convert to string
double value; //!< The double value

bool wouldWriteZero() const
{
return (std::abs(value) * std::pow(10.0, precision)) < 1.0;
}

friend inline std::ostream& operator<<(std::ostream& out, const PrecisionedDouble precision_and_input)
{
writeDoubleToStream(precision_and_input.precision, precision_and_input.value, out);
Expand Down
12 changes: 4 additions & 8 deletions src/gcodeExport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1471,7 +1471,8 @@ void GCodeExport::writeSpecificFanCommand(double speed, size_t fan_number)
};
bool write_value = true;
std::ostringstream new_value;
new_value << scale_zero_to_one_optional(speed);
const auto num_new_val = scale_zero_to_one_optional(speed);
new_value << num_new_val;
const std::string new_value_str = new_value.str();
if (current_fan_speed.has_value())
{
Expand All @@ -1482,14 +1483,9 @@ void GCodeExport::writeSpecificFanCommand(double speed, size_t fan_number)

if (write_value)
{
// Check if the value to be written is only made with zeroes, in which case it is actually a turn off
std::string new_value_str_reduced = new_value_str;
std::erase(new_value_str_reduced, '0');
std::erase(new_value_str_reduced, '.');
const bool turn_off = new_value_str_reduced.empty();

if (turn_off)
if (num_new_val.wouldWriteZero())
{
// Turn off when the fan value is zero.
*output_stream_ << "M107";
}
else
Expand Down

0 comments on commit c750ec6

Please sign in to comment.