safeproofing int types in gcode writer

This commit is contained in:
supermerill 2021-06-18 16:27:55 +02:00 committed by remi durand
parent 8e4e8023c0
commit c8f25eabed
5 changed files with 44 additions and 44 deletions

View File

@ -147,12 +147,12 @@ double Tool::retract_restart_extra_toolchange() const
return 0;
}
int Tool::temp_offset() const
int16_t Tool::temp_offset() const
{
return 0;
}
int Tool::fan_offset() const
int8_t Tool::fan_offset() const
{
return 0;
}
@ -219,12 +219,12 @@ double Extruder::retract_restart_extra_toolchange() const
return m_config->retract_restart_extra_toolchange.get_at(m_id);
}
int Extruder::temp_offset() const
int16_t Extruder::temp_offset() const
{
return m_config->extruder_temperature_offset.get_at(m_id);
}
int Extruder::fan_offset() const
int8_t Extruder::fan_offset() const
{
return m_config->extruder_fan_offset.get_at(m_id);
}

View File

@ -48,8 +48,8 @@ public:
virtual double retract_restart_extra() const;
virtual double retract_length_toolchange() const;
virtual double retract_restart_extra_toolchange() const;
virtual int temp_offset() const;
virtual int fan_offset() const;
virtual int16_t temp_offset() const;
virtual int8_t fan_offset() const;
protected:
// Private constructor to create a key for a search in std::set.
@ -104,8 +104,8 @@ public:
double retract_restart_extra() const override;
double retract_length_toolchange() const override;
double retract_restart_extra_toolchange() const override;
int temp_offset() const override;
int fan_offset() const override;
int16_t temp_offset() const override;
int8_t fan_offset() const override;
protected:
// Private constructor to create a key for a search in std::set.

View File

@ -1411,7 +1411,7 @@ void GCode::_do_export(Print& print, FILE* file, ThumbnailsGeneratorCallback thu
// Disable fan.
if ((initial_extruder_id != (uint16_t)-1) && !this->config().start_gcode_manual && print.config().disable_fan_first_layers.get_at(initial_extruder_id))
_write(file, m_writer.set_fan(0, true, initial_extruder_id));
_write(file, m_writer.set_fan(uint8_t(0), true, initial_extruder_id));
//ensure fan is at the right speed
print.throw_if_canceled();
@ -1613,7 +1613,7 @@ void GCode::_do_export(Print& print, FILE* file, ThumbnailsGeneratorCallback thu
_add_object_change_labels(gcode);
_write(file, gcode);
}
_write(file, m_writer.set_fan(false));
_write(file, m_writer.set_fan(uint8_t(0)));
// adds tag for processor
_write_format(file, ";%s%s\n", GCodeProcessor::Extrusion_Role_Tag.c_str(), ExtrusionEntity::role_to_string(erCustom).c_str());
@ -3767,7 +3767,7 @@ std::string GCode::_before_extrude(const ExtrusionPath &path, const std::string
acceleration = m_config.infill_acceleration.get_abs_value(acceleration);
}
//travel acceleration should be already set at startup via special gcode, and so it's automatically used by G0.
m_writer.set_acceleration((uint16_t)floor(acceleration + 0.5));
m_writer.set_acceleration((uint32_t)floor(acceleration + 0.5));
}

View File

@ -127,19 +127,19 @@ std::string GCodeWriter::postamble() const
return gcode.str();
}
std::string GCodeWriter::set_temperature(const unsigned int temperature, bool wait, int tool)
std::string GCodeWriter::set_temperature(const int16_t temperature, bool wait, int tool)
{
//use m_tool if tool isn't set
if (tool < 0 && m_tool != nullptr)
tool = m_tool->id();
//add offset
int16_t temp_w_offset = int16_t(temperature);
int16_t temp_w_offset = temperature;
temp_w_offset += int16_t(get_tool(tool)->temp_offset());
temp_w_offset = std::max(int16_t(0), std::min(int16_t(2000), temp_w_offset));
// temp_w_offset has an effective minimum value of 0, so this cast is safe.
if (m_last_temperature_with_offset == static_cast<uint16_t>(temp_w_offset) && !wait)
if (m_last_temperature_with_offset == temp_w_offset && !wait)
return "";
if (wait && (FLAVOR_IS(gcfMakerWare) || FLAVOR_IS(gcfSailfish)))
return "";
@ -182,7 +182,7 @@ std::string GCodeWriter::set_temperature(const unsigned int temperature, bool wa
return gcode.str();
}
std::string GCodeWriter::set_bed_temperature(unsigned int temperature, bool wait)
std::string GCodeWriter::set_bed_temperature(uint32_t temperature, bool wait)
{
if (temperature == m_last_bed_temperature && (! wait || m_last_bed_temperature_reached))
return std::string();
@ -218,25 +218,25 @@ std::string GCodeWriter::set_bed_temperature(unsigned int temperature, bool wait
return gcode.str();
}
std::string GCodeWriter::set_fan(const unsigned int speed, bool dont_save, uint16_t default_tool)
std::string GCodeWriter::set_fan(const uint8_t speed, bool dont_save, uint16_t default_tool)
{
std::ostringstream gcode;
const Tool *tool = m_tool == nullptr ? get_tool(default_tool) : m_tool;
//add fan_offset
int16_t fan_speed = int16_t(speed);
int8_t fan_speed = int8_t(std::min(uint8_t(100), speed));
if (tool != nullptr)
fan_speed += int8_t(tool->fan_offset());
fan_speed = std::max(int16_t(0), std::min(int16_t(100), fan_speed));
fan_speed += tool->fan_offset();
fan_speed = std::max(int8_t(0), std::min(int8_t(100), fan_speed));
const auto fan_baseline = (this->config.fan_percentage.value ? 100.0 : 255.0);
// fan_speed has an effective minimum value of 0, so this cast is safe.
//test if it's useful to write it
if (m_last_fan_speed_with_offset != static_cast<uint16_t>(fan_speed) || dont_save) {
if (m_last_fan_speed_with_offset != fan_speed || dont_save) {
//save new current value
if (!dont_save) {
m_last_fan_speed = speed;
m_last_fan_speed_with_offset = fan_speed;
m_last_fan_speed_with_offset = uint8_t(fan_speed);
}
// write it
@ -269,7 +269,7 @@ std::string GCodeWriter::set_fan(const unsigned int speed, bool dont_save, uint1
return gcode.str();
}
void GCodeWriter::set_acceleration(unsigned int acceleration)
void GCodeWriter::set_acceleration(uint32_t acceleration)
{
// Clamp the acceleration to the allowed maximum.
if (m_max_acceleration > 0 && acceleration > m_max_acceleration)
@ -329,16 +329,16 @@ std::string GCodeWriter::reset_e(bool force)
}
}
std::string GCodeWriter::update_progress(unsigned int num, unsigned int tot, bool allow_100) const
std::string GCodeWriter::update_progress(uint32_t num, uint32_t tot, bool allow_100) const
{
if (FLAVOR_IS_NOT(gcfMakerWare) && FLAVOR_IS_NOT(gcfSailfish))
return "";
unsigned int percent = (unsigned int)floor(100.0 * num / tot + 0.5);
if (!allow_100) percent = std::min(percent, (unsigned int)99);
uint8_t percent = (uint32_t)floor(100.0 * num / tot + 0.5);
if (!allow_100) percent = std::min(percent, (uint8_t)99);
std::ostringstream gcode;
gcode << "M73 P" << percent;
gcode << "M73 P" << int(percent);
if (this->config.gcode_comments) gcode << " ; update progress";
gcode << "\n";
return gcode.str();
@ -352,7 +352,7 @@ std::string GCodeWriter::toolchange_prefix() const
"T";
}
std::string GCodeWriter::toolchange(unsigned int tool_id)
std::string GCodeWriter::toolchange(uint16_t tool_id)
{
// set the new extruder
/*auto it_extruder = Slic3r::lower_bound_by_predicate(m_extruders.begin(), m_extruders.end(), [tool_id](const Extruder &e) { return e.id() < tool_id; });

View File

@ -44,24 +44,24 @@ public:
const Tool* get_tool(uint16_t id) const;
std::string preamble();
std::string postamble() const;
std::string set_temperature(unsigned int temperature, bool wait = false, int tool = -1);
std::string set_bed_temperature(unsigned int temperature, bool wait = false);
unsigned int get_fan() { return m_last_fan_speed; }
std::string set_temperature(int16_t temperature, bool wait = false, int tool = -1);
std::string set_bed_temperature(uint32_t temperature, bool wait = false);
uint8_t get_fan() { return m_last_fan_speed; }
/// set fan at speed. Save it as current fan speed if !dont_save, and use tool default_tool if the internal m_tool is null (no toolchange done yet).
std::string set_fan(unsigned int speed, bool dont_save = false, uint16_t default_tool = 0);
void set_acceleration(unsigned int acceleration);
std::string set_fan(uint8_t speed, bool dont_save = false, uint16_t default_tool = 0);
void set_acceleration(uint32_t acceleration);
std::string write_acceleration();
std::string reset_e(bool force = false);
std::string update_progress(unsigned int num, unsigned int tot, bool allow_100 = false) const;
std::string update_progress(uint32_t num, uint32_t tot, bool allow_100 = false) const;
// return false if this extruder was already selected
bool need_toolchange(unsigned int tool_id) const
bool need_toolchange(uint16_t tool_id) const
{ return m_tool == nullptr || m_tool->id() != tool_id; }
std::string set_tool(unsigned int tool_id)
std::string set_tool(uint16_t tool_id)
{ return this->need_toolchange(tool_id) ? this->toolchange(tool_id) : ""; }
// Prefix of the toolchange G-code line, to be used by the CoolingBuffer to separate sections of the G-code
// printed with the same extruder.
std::string toolchange_prefix() const;
std::string toolchange(unsigned int tool_id);
std::string toolchange(uint16_t tool_id);
std::string set_speed(double F, const std::string &comment = std::string(), const std::string &cooling_marker = std::string()) const;
std::string travel_to_xy(const Vec2d &point, const std::string &comment = std::string());
std::string travel_to_xyz(const Vec3d &point, const std::string &comment = std::string());
@ -84,16 +84,16 @@ private:
std::string m_extrusion_axis;
bool m_single_extruder_multi_material;
Tool* m_tool;
unsigned int m_last_acceleration;
unsigned int m_current_acceleration;
uint32_t m_last_acceleration;
uint32_t m_current_acceleration;
// Limit for setting the acceleration, to respect the machine limits set for the Marlin firmware.
// If set to zero, the limit is not in action.
unsigned int m_max_acceleration;
unsigned int m_last_fan_speed;
unsigned int m_last_fan_speed_with_offset;
unsigned int m_last_temperature;
unsigned int m_last_temperature_with_offset;
unsigned int m_last_bed_temperature;
uint32_t m_max_acceleration;
uint8_t m_last_fan_speed;
uint8_t m_last_fan_speed_with_offset;
int16_t m_last_temperature;
int16_t m_last_temperature_with_offset;
int16_t m_last_bed_temperature;
bool m_last_bed_temperature_reached;
double m_lifted;
Vec3d m_pos = Vec3d::Zero();