From 9eaf29caa032334bcc7d602a00d5062554cc9183 Mon Sep 17 00:00:00 2001 From: David Crocker Date: Wed, 16 Feb 2022 20:34:07 +0000 Subject: Corrected adjustment to number of decimal places when printing floats --- src/GCodes/GCodes.cpp | 4 ++-- src/Movement/Move.cpp | 2 +- src/ObjectModel/ObjectModel.cpp | 14 -------------- src/ObjectModel/ObjectModel.h | 4 ++-- src/Platform/Platform.cpp | 2 +- src/Platform/RepRap.cpp | 7 ++++--- src/RepRapFirmware.cpp | 17 +++++++++++++---- src/RepRapFirmware.h | 4 ++-- 8 files changed, 25 insertions(+), 29 deletions(-) (limited to 'src') diff --git a/src/GCodes/GCodes.cpp b/src/GCodes/GCodes.cpp index 0acee9af..dbc2401f 100644 --- a/src/GCodes/GCodes.cpp +++ b/src/GCodes/GCodes.cpp @@ -3072,7 +3072,7 @@ void GCodes::GetCurrentCoordinates(const StringRef& s) const noexcept for (size_t axis = 0; axis < numVisibleAxes; ++axis) { // Don't put a space after the colon in the response, it confuses Pronterface - s.catf("%c:%.3f ", axisLetters[axis], HideNan(GetUserCoordinate(axis))); + s.catf("%c:%.3f ", axisLetters[axis], (double)HideNan(GetUserCoordinate(axis))); } // Now the virtual extruder position, for Octoprint @@ -3098,7 +3098,7 @@ void GCodes::GetCurrentCoordinates(const StringRef& s) const noexcept ToolOffsetTransform(moveState.currentUserPosition, machineCoordinates); for (size_t axis = 0; axis < numVisibleAxes; ++axis) { - s.catf(" %.3f", HideNan(machineCoordinates[axis])); + s.catf(" %.3f", (double)HideNan(machineCoordinates[axis])); } // Add the bed compensation diff --git a/src/Movement/Move.cpp b/src/Movement/Move.cpp index b0148e35..f32f6cc2 100644 --- a/src/Movement/Move.cpp +++ b/src/Movement/Move.cpp @@ -111,7 +111,7 @@ constexpr ObjectModelTableEntry Move::objectModelTable[] = { "rotation", OBJECT_MODEL_FUNC(self, 44), ObjectModelEntryFlags::none }, #endif { "shaping", OBJECT_MODEL_FUNC(&self->axisShaper, 0), ObjectModelEntryFlags::none }, - { "speedFactor", OBJECT_MODEL_FUNC_NOSELF(reprap.GetGCodes().GetSpeedFactor(), 3), ObjectModelEntryFlags::none }, + { "speedFactor", OBJECT_MODEL_FUNC_NOSELF(reprap.GetGCodes().GetSpeedFactor(), 2), ObjectModelEntryFlags::none }, { "travelAcceleration", OBJECT_MODEL_FUNC(InverseConvertAcceleration(self->maxTravelAcceleration), 1), ObjectModelEntryFlags::none }, { "virtualEPos", OBJECT_MODEL_FUNC_NOSELF(reprap.GetGCodes().GetVirtualExtruderPosition(), 5), ObjectModelEntryFlags::live }, { "workplaceNumber", OBJECT_MODEL_FUNC_NOSELF((int32_t)reprap.GetGCodes().GetWorkplaceCoordinateSystemNumber() - 1), ObjectModelEntryFlags::none }, diff --git a/src/ObjectModel/ObjectModel.cpp b/src/ObjectModel/ObjectModel.cpp index b8e2c9ea..714f4470 100644 --- a/src/ObjectModel/ObjectModel.cpp +++ b/src/ObjectModel/ObjectModel.cpp @@ -193,20 +193,6 @@ void ExpressionValue::Release() noexcept } } -// Get the format string to use assuming this is a floating point number -const char *_ecv_array ExpressionValue::GetFloatFormatString() const noexcept -{ - float f = 1.0; - unsigned int digitsAfterPoint = param; - while (digitsAfterPoint > 1 && fVal > f) - { - f *= 10.0; - --digitsAfterPoint; - } - - return ::GetFloatFormatString(digitsAfterPoint); -} - #if SUPPORT_CAN_EXPANSION // Given that this is a CanExpansionBoardDetails value, extract the part requested according to the parameter and append it to the string diff --git a/src/ObjectModel/ObjectModel.h b/src/ObjectModel/ObjectModel.h index bcf77791..5a9227f2 100644 --- a/src/ObjectModel/ObjectModel.h +++ b/src/ObjectModel/ObjectModel.h @@ -71,7 +71,7 @@ class UniqueId; // Encapsulated time_t, used to facilitate overloading the ExpressionValue constructor struct DateTime { - explicit DateTime(time_t t) : tim(t) { } + explicit DateTime(time_t t) noexcept : tim(t) { } time_t tim; }; @@ -192,7 +192,7 @@ struct ExpressionValue #endif // Get the format string to use assuming this is a floating point number - const char *_ecv_array GetFloatFormatString() const noexcept; + const char *_ecv_array GetFloatFormatString() const noexcept { return ::GetFloatFormatString(fVal, param); } // Append a string representation of this value to a string void AppendAsString(const StringRef& str) const noexcept; diff --git a/src/Platform/Platform.cpp b/src/Platform/Platform.cpp index 88abeb58..f89bc9f0 100644 --- a/src/Platform/Platform.cpp +++ b/src/Platform/Platform.cpp @@ -296,7 +296,7 @@ constexpr ObjectModelTableEntry Platform::objectModelTable[] = { "acceleration", OBJECT_MODEL_FUNC(InverseConvertAcceleration(self->Acceleration(ExtruderToLogicalDrive(context.GetLastIndex()))), 1), ObjectModelEntryFlags::none }, { "current", OBJECT_MODEL_FUNC((int32_t)(self->GetMotorCurrent(ExtruderToLogicalDrive(context.GetLastIndex()), 906))), ObjectModelEntryFlags::none }, { "driver", OBJECT_MODEL_FUNC(self->extruderDrivers[context.GetLastIndex()]), ObjectModelEntryFlags::none }, - { "factor", OBJECT_MODEL_FUNC_NOSELF(reprap.GetGCodes().GetExtrusionFactor(context.GetLastIndex()), 3), ObjectModelEntryFlags::none }, + { "factor", OBJECT_MODEL_FUNC_NOSELF(reprap.GetGCodes().GetExtrusionFactor(context.GetLastIndex()), 2), ObjectModelEntryFlags::none }, { "filament", OBJECT_MODEL_FUNC_NOSELF(GetFilamentName(context.GetLastIndex())), ObjectModelEntryFlags::none }, { "jerk", OBJECT_MODEL_FUNC(InverseConvertSpeedToMmPerMin(self->GetInstantDv(ExtruderToLogicalDrive(context.GetLastIndex()))), 1), ObjectModelEntryFlags::none }, { "microstepping", OBJECT_MODEL_FUNC(self, 8), ObjectModelEntryFlags::none }, diff --git a/src/Platform/RepRap.cpp b/src/Platform/RepRap.cpp index e393f08e..0a90d067 100644 --- a/src/Platform/RepRap.cpp +++ b/src/Platform/RepRap.cpp @@ -1582,7 +1582,7 @@ OutputBuffer *RepRap::GetStatusResponse(uint8_t type, ResponseSource source) con first = false; float temp; (void)sensor->GetLatestTemperature(temp); - response->catf("{\"name\":\"%.s\",\"temp\":%.1f}", nm, HideNan(temp)); + response->catf("{\"name\":\"%.s\",\"temp\":%.1f}", nm, (double)HideNan(temp)); } nextSensorNumber = sensor->GetSensorNumber() + 1; } @@ -2469,7 +2469,7 @@ GCodeResult RepRap::GetFileInfoResponse(const char *filename, OutputBuffer *&res } // Helper functions to write JSON arrays -// Append float array using 1 decimal place +// Append float array using the specified number of decimal places void RepRap::AppendFloatArray(OutputBuffer *buf, const char *name, size_t numValues, function_ref func, unsigned int numDecimalDigits) noexcept { if (name != nullptr) @@ -2483,7 +2483,8 @@ void RepRap::AppendFloatArray(OutputBuffer *buf, const char *name, size_t numVal { buf->cat(','); } - buf->catf(GetFloatFormatString(numDecimalDigits), HideNan(func(i))); + const float fVal = HideNan(func(i)); + buf->catf(GetFloatFormatString(fVal, numDecimalDigits), (double)fVal); } buf->cat(']'); } diff --git a/src/RepRapFirmware.cpp b/src/RepRapFirmware.cpp index 57379926..478bebaa 100644 --- a/src/RepRapFirmware.cpp +++ b/src/RepRapFirmware.cpp @@ -172,11 +172,20 @@ Licence: GPL RepRap reprap; // Get the format string to use for printing a floating point number to the specified number of decimal digits. Zero means the maximum sensible number. -const char *_ecv_array GetFloatFormatString(unsigned int numDigitsAfterPoint) noexcept +const char *_ecv_array GetFloatFormatString(float val, unsigned int numDigitsAfterPoint) noexcept { static constexpr const char *_ecv_array FormatStrings[] = { "%.7f", "%.1f", "%.2f", "%.3f", "%.4f", "%.5f", "%.6f", "%.7f" }; static_assert(ARRAY_SIZE(FormatStrings) == MaxFloatDigitsDisplayedAfterPoint + 1); - return FormatStrings[min(numDigitsAfterPoint, MaxFloatDigitsDisplayedAfterPoint)]; + + float f = 1.0; + unsigned int maxDigitsAfterPoint = MaxFloatDigitsDisplayedAfterPoint; + while (maxDigitsAfterPoint > 1 && val >= f) + { + f *= 10.0; + --maxDigitsAfterPoint; + } + + return FormatStrings[min(numDigitsAfterPoint, maxDigitsAfterPoint)]; } static const char *_ecv_array const moduleName[] = @@ -254,9 +263,9 @@ void debugPrintf(const char *_ecv_array fmt, ...) noexcept } // Convert a float to double for passing to printf etc. If it is a NaN or infinity, convert it to 9999.9 to avoid getting JSON parse errors. -double HideNan(float val) noexcept +float HideNan(float val) noexcept { - return (double)((std::isnan(val) || std::isinf(val)) ? 9999.9 : val); + return (std::isnan(val) || std::isinf(val)) ? 9999.9 : val; } // Append a list of driver numbers to a string, with a space before each one diff --git a/src/RepRapFirmware.h b/src/RepRapFirmware.h index e956dd50..b47a86d6 100644 --- a/src/RepRapFirmware.h +++ b/src/RepRapFirmware.h @@ -380,7 +380,7 @@ extern "C" void debugPrintf(const char* fmt, ...) noexcept __attribute__ ((forma // Functions and globals not part of any class -double HideNan(float val) noexcept; +float HideNan(float val) noexcept; void ListDrivers(const StringRef& str, DriversBitmap drivers) noexcept; @@ -544,7 +544,7 @@ static inline constexpr float InverseConvertAcceleration(float accel) noexcept } constexpr unsigned int MaxFloatDigitsDisplayedAfterPoint = 7; -const char *_ecv_array GetFloatFormatString(unsigned int numDigitsAfterPoint) noexcept; +const char *_ecv_array GetFloatFormatString(float val, unsigned int numDigitsAfterPoint) noexcept; #if SUPPORT_WORKPLACE_COORDINATES constexpr size_t NumCoordinateSystems = 9; // G54 up to G59.3 -- cgit v1.2.3