From ccb7d31920be43555d68428e334e2a4d5a3c0b15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gu=C3=B0ni=20M=C3=A1r=20Gilbert?= Date: Mon, 29 Jul 2024 22:22:55 +0000 Subject: [PATCH] optimisation: set enum types explictly to uint8_t This commit produces the same savings as the compiler options -fshort-enums. Except by setting the types manually we save also 2 bytes of SRAM. By default, the enum type is 2 bytes, with we can explictly set it to one byte when applicable to reduce code size. Almost all the savings from from 'enum Mode' in leds.h. Change in memory: Flash: -116 bytes SRAM: -2 bytes --- src/logic/feed_to_bondtech.h | 2 +- src/logic/feed_to_finda.h | 2 +- src/logic/hw_sanity.cpp | 2 +- src/logic/result_codes.h | 2 +- src/logic/retract_from_finda.h | 2 +- src/logic/unload_to_finda.h | 2 +- src/modules/buttons.h | 2 +- src/modules/debouncer.h | 2 +- src/modules/leds.h | 4 ++-- src/modules/movable_base.h | 2 +- src/modules/permanent_storage.h | 2 +- 11 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/logic/feed_to_bondtech.h b/src/logic/feed_to_bondtech.h index 2953fc1..7b32606 100644 --- a/src/logic/feed_to_bondtech.h +++ b/src/logic/feed_to_bondtech.h @@ -17,7 +17,7 @@ namespace logic { /// To prevent constant EEPROM updates only significant changes are recorded. struct FeedToBondtech { /// internal states of the state machine - enum { + enum : uint8_t { EngagingIdler, PushingFilamentFast, PushingFilamentToFSensor, diff --git a/src/logic/feed_to_finda.h b/src/logic/feed_to_finda.h index c0e00bf..119c18b 100644 --- a/src/logic/feed_to_finda.h +++ b/src/logic/feed_to_finda.h @@ -12,7 +12,7 @@ namespace logic { /// Leaves the Pulley axis enabled for chaining potential next operations struct FeedToFinda { /// internal states of the state machine - enum { + enum : uint8_t { EngagingIdler, PushingFilament, PushingFilamentUnlimited, diff --git a/src/logic/hw_sanity.cpp b/src/logic/hw_sanity.cpp index 0b396fc..341b92b 100644 --- a/src/logic/hw_sanity.cpp +++ b/src/logic/hw_sanity.cpp @@ -32,7 +32,7 @@ bool HWSanity::Reset(uint8_t param) { return true; } -enum pin_bits { +enum pin_bits : uint8_t { BIT_STEP = 0b001, BIT_DIR = 0b010, BIT_ENA = 0b100, diff --git a/src/logic/result_codes.h b/src/logic/result_codes.h index c92ff68..f87c552 100644 --- a/src/logic/result_codes.h +++ b/src/logic/result_codes.h @@ -7,7 +7,7 @@ /// therefore the error codes have been extracted to one place. /// /// Please note that currently only LoadFilament can return something else than "OK" -enum class ResultCode : uint_fast16_t { +enum class ResultCode : uint_fast8_t { OK = 0, Cancelled = 1 }; diff --git a/src/logic/retract_from_finda.h b/src/logic/retract_from_finda.h index 01c52e2..96aed72 100644 --- a/src/logic/retract_from_finda.h +++ b/src/logic/retract_from_finda.h @@ -13,7 +13,7 @@ namespace logic { /// - leaves idler engaged for chaining operations struct RetractFromFinda { /// internal states of the state machine - enum { + enum : uint8_t { EngagingIdler, UnloadBackToPTFE, OK, diff --git a/src/logic/unload_to_finda.h b/src/logic/unload_to_finda.h index 7ca73eb..e6b31fd 100644 --- a/src/logic/unload_to_finda.h +++ b/src/logic/unload_to_finda.h @@ -13,7 +13,7 @@ namespace logic { /// - load/unload to finda struct UnloadToFinda { /// internal states of the state machine - enum { + enum : uint8_t { EngagingIdler, UnloadingToFinda, WaitingForFINDA, diff --git a/src/modules/buttons.h b/src/modules/buttons.h index a460a6c..2a8c4b4 100644 --- a/src/modules/buttons.h +++ b/src/modules/buttons.h @@ -20,7 +20,7 @@ private: }; /// Enum of buttons - used also as indices in an array of buttons to keep the code size tight. -enum { +enum : uint8_t { Right = 0, Middle, Left diff --git a/src/modules/debouncer.h b/src/modules/debouncer.h index c30e4e5..67b74b9 100644 --- a/src/modules/debouncer.h +++ b/src/modules/debouncer.h @@ -28,7 +28,7 @@ private: /// Intentionally not modeled as an enum class /// as it would impose additional casts which do not play well with the struct Flags /// and would make the code less readable - enum State { + enum State : uint8_t { Waiting = 0, Detected, WaitForRelease, diff --git a/src/modules/leds.h b/src/modules/leds.h index e5f1152..0f12f0c 100644 --- a/src/modules/leds.h +++ b/src/modules/leds.h @@ -20,7 +20,7 @@ namespace leds { /// Enum of LED modes /// blink0 and blink1 allow for interlaced blinking of LEDs (one is on and the other off) -enum Mode { +enum Mode : uint8_t { off, on, blink0, ///< start blinking at even periods @@ -28,7 +28,7 @@ enum Mode { }; /// Enum of LEDs color - green or red -enum Color { +enum Color : uint8_t { red = 0, green = 1 }; diff --git a/src/modules/movable_base.h b/src/modules/movable_base.h index bca6cdd..d00b3f4 100644 --- a/src/modules/movable_base.h +++ b/src/modules/movable_base.h @@ -11,7 +11,7 @@ namespace motion { class MovableBase { public: /// Internal states of the state machine - enum { + enum : uint8_t { Ready = 0, // intentionally set as zero in order to allow zeroing the Idler structure upon startup -> avoid explicit initialization code Moving = 1, PlannedHome = 2, diff --git a/src/modules/permanent_storage.h b/src/modules/permanent_storage.h index 8ec947d..a0af601 100644 --- a/src/modules/permanent_storage.h +++ b/src/modules/permanent_storage.h @@ -68,7 +68,7 @@ public: static bool set(uint8_t filament); private: - enum Key { + enum Key : uint8_t { KeyFront1, KeyReverse1, KeyFront2,