From fff1c471b4408d1fa64fbf0fea28a6a698a59857 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gu=C3=B0ni=20M=C3=A1r=20Gilbert?= Date: Sun, 20 Aug 2023 14:44:04 +0000 Subject: [PATCH] Optimisation: make constructors constexpr Cppcheck was complaining some member variables are not initialised in the constructor. Change in memory: Flash: -186 bytes SRAM: -15 bytes --- src/logic/command_base.h | 2 +- src/logic/cut_filament.h | 6 ++++-- src/logic/eject_filament.h | 5 +++-- src/logic/feed_to_bondtech.h | 2 +- src/logic/feed_to_finda.h | 5 +++-- src/logic/home.h | 2 +- src/logic/hw_sanity.h | 4 ++-- src/logic/load_filament.h | 5 +++-- src/logic/move_selector.h | 2 +- src/logic/no_command.h | 2 +- src/logic/retract_from_finda.h | 2 +- src/logic/set_mode.h | 2 +- src/logic/start_up.h | 2 +- src/logic/tool_change.h | 5 +++-- src/logic/unload_filament.h | 2 +- src/logic/unload_to_finda.h | 6 ++++-- 16 files changed, 31 insertions(+), 23 deletions(-) diff --git a/src/logic/command_base.h b/src/logic/command_base.h index 5bca87b..4b6f334 100644 --- a/src/logic/command_base.h +++ b/src/logic/command_base.h @@ -28,7 +28,7 @@ ErrorCode AddErrorAxisBit(ErrorCode ec, uint8_t axis); /// These tasks report their progress and only one of these tasks is allowed to run at once. class CommandBase { public: - inline CommandBase() + inline constexpr CommandBase() : state(ProgressCode::OK) , error(ErrorCode::OK) , deferredErrorCode(ErrorCode::OK) diff --git a/src/logic/cut_filament.h b/src/logic/cut_filament.h index a1f332d..449ed51 100644 --- a/src/logic/cut_filament.h +++ b/src/logic/cut_filament.h @@ -11,8 +11,10 @@ namespace logic { /// @brief A high-level command state machine - handles the complex logic of cutting filament class CutFilament : public CommandBase { public: - inline CutFilament() - : CommandBase() {} + inline constexpr CutFilament() + : CommandBase() + , cutSlot(0) + , savedSelectorFeedRate_mm_s(0) {} /// Restart the automaton /// @param param index of filament slot to perform cut onto diff --git a/src/logic/eject_filament.h b/src/logic/eject_filament.h index b77bde9..27acc65 100644 --- a/src/logic/eject_filament.h +++ b/src/logic/eject_filament.h @@ -26,8 +26,9 @@ static constexpr modules::motion::P_speed_t ejectSpeed = 1000.0_P_mm_s; //@@TODO /// The Eject message is not an error, but we'll leverage existing infrastructure of error screens + user input to model a nice UI dialog. class EjectFilament : public CommandBase { public: - inline EjectFilament() - : CommandBase() {} + inline constexpr EjectFilament() + : CommandBase() + , slot(0) {} /// Restart the automaton /// @param param index of filament slot to eject diff --git a/src/logic/feed_to_bondtech.h b/src/logic/feed_to_bondtech.h index 056005c..2953fc1 100644 --- a/src/logic/feed_to_bondtech.h +++ b/src/logic/feed_to_bondtech.h @@ -30,7 +30,7 @@ struct FeedToBondtech { // PulleyStalled }; - inline FeedToBondtech() + inline constexpr FeedToBondtech() : state(OK) , maxRetries(1) {} diff --git a/src/logic/feed_to_finda.h b/src/logic/feed_to_finda.h index 72ca682..c0e00bf 100644 --- a/src/logic/feed_to_finda.h +++ b/src/logic/feed_to_finda.h @@ -23,9 +23,10 @@ struct FeedToFinda { Stopped }; - inline FeedToFinda() + inline constexpr FeedToFinda() : state(OK) - , feedPhaseLimited(true) {} + , feedPhaseLimited(true) + , haltAtEnd(0) {} /// Restart the automaton /// @param feedPhaseLimited diff --git a/src/logic/home.h b/src/logic/home.h index c0e05de..ba70b7d 100644 --- a/src/logic/home.h +++ b/src/logic/home.h @@ -20,7 +20,7 @@ namespace logic { /// This high-level command is just a way to invoke re-homing from the printer while all safety measures are kept. class Home : public CommandBase { public: - inline Home() + inline constexpr Home() : CommandBase() {} /// Restart the automaton diff --git a/src/logic/hw_sanity.h b/src/logic/hw_sanity.h index a97a71e..62b3127 100644 --- a/src/logic/hw_sanity.h +++ b/src/logic/hw_sanity.h @@ -14,7 +14,7 @@ namespace logic { class HWSanity : public CommandBase { public: - inline HWSanity() + inline constexpr HWSanity() : CommandBase() {} /// Restart the automaton @@ -44,7 +44,7 @@ private: static void PrepareAxis(config::Axis axis); uint8_t test_step = 0; - config::Axis axis; + config::Axis axis = config::Axis::Pulley; uint8_t fault_masks[3] = { 0 }; ProgressCode next_state = ProgressCode::HWTestBegin; uint16_t wait_start = 0; diff --git a/src/logic/load_filament.h b/src/logic/load_filament.h index 3cca0cd..a56ba84 100644 --- a/src/logic/load_filament.h +++ b/src/logic/load_filament.h @@ -10,9 +10,10 @@ namespace logic { /// @brief A high-level command state machine - handles the complex logic of loading filament into a filament slot. class LoadFilament : public CommandBase { public: - inline LoadFilament() + inline constexpr LoadFilament() : CommandBase() - , verifyLoadedFilament(0) {} + , verifyLoadedFilament(0) + , result(ResultCode::OK) {} /// Restart the automaton - performs unlimited rotation of the Pulley /// @param param index of filament slot to load diff --git a/src/logic/move_selector.h b/src/logic/move_selector.h index f0a0a37..295b489 100644 --- a/src/logic/move_selector.h +++ b/src/logic/move_selector.h @@ -15,7 +15,7 @@ namespace logic { /// and/or from the MMU's buttons while all safety measures are kept. class MoveSelector : public CommandBase { public: - inline MoveSelector() + inline constexpr MoveSelector() : CommandBase() {} /// Restart the automaton diff --git a/src/logic/no_command.h b/src/logic/no_command.h index c65ef17..f3871f6 100644 --- a/src/logic/no_command.h +++ b/src/logic/no_command.h @@ -8,7 +8,7 @@ namespace logic { /// @brief A dummy No-command operation just to make the init of the firmware consistent (and cleaner code during processing). class NoCommand : public CommandBase { public: - inline NoCommand() + inline constexpr NoCommand() : CommandBase() {} /// Restart the automaton diff --git a/src/logic/retract_from_finda.h b/src/logic/retract_from_finda.h index eb464a3..01c52e2 100644 --- a/src/logic/retract_from_finda.h +++ b/src/logic/retract_from_finda.h @@ -20,7 +20,7 @@ struct RetractFromFinda { Failed }; - inline RetractFromFinda() + inline constexpr RetractFromFinda() : state(OK) {} /// Restart the automaton diff --git a/src/logic/set_mode.h b/src/logic/set_mode.h index 7e25a37..2300ef6 100644 --- a/src/logic/set_mode.h +++ b/src/logic/set_mode.h @@ -13,7 +13,7 @@ namespace logic { /// (regardless of how long it takes it to finish) - that implies no motor moves are being performed while M0/M1 is being applied. class SetMode : public CommandBase { public: - inline SetMode() + inline constexpr SetMode() : CommandBase() {} /// Restart the automaton diff --git a/src/logic/start_up.h b/src/logic/start_up.h index f031979..b941129 100644 --- a/src/logic/start_up.h +++ b/src/logic/start_up.h @@ -8,7 +8,7 @@ namespace logic { /// @brief Firmware start up sequence with error handling & reporting class StartUp : public CommandBase { public: - inline StartUp() + inline constexpr StartUp() : CommandBase() {} /// Restart the automaton diff --git a/src/logic/tool_change.h b/src/logic/tool_change.h index 5e9b14a..5ac7025 100644 --- a/src/logic/tool_change.h +++ b/src/logic/tool_change.h @@ -11,8 +11,9 @@ namespace logic { /// @brief A high-level command state machine - handles the complex logic of tool change - which is basically a chain of an Unload and a Load operation. class ToolChange : public CommandBase { public: - inline ToolChange() - : CommandBase() {} + inline constexpr ToolChange() + : CommandBase() + , plannedSlot(-1) {} /// Restart the automaton /// @param param index of filament slot to change to - i.e. to load diff --git a/src/logic/unload_filament.h b/src/logic/unload_filament.h index c22bedd..2637e1a 100644 --- a/src/logic/unload_filament.h +++ b/src/logic/unload_filament.h @@ -11,7 +11,7 @@ namespace logic { /// @brief A high-level command state machine - handles the complex logic of unloading filament class UnloadFilament : public CommandBase { public: - inline UnloadFilament() + inline constexpr UnloadFilament() : CommandBase() {} /// Restart the automaton diff --git a/src/logic/unload_to_finda.h b/src/logic/unload_to_finda.h index b84e516..7ca73eb 100644 --- a/src/logic/unload_to_finda.h +++ b/src/logic/unload_to_finda.h @@ -21,8 +21,10 @@ struct UnloadToFinda { FailedFINDA, FailedFSensor }; - inline UnloadToFinda() - : maxTries(3) {} + inline constexpr UnloadToFinda() + : state(OK) + , maxTries(3) + , unloadStart_mm(0) {} /// Restart the automaton /// @param maxTries maximum number of retried attempts before reporting a fail