From 5586f17b38278ba7ff5c2c8fba47ce08c8325275 Mon Sep 17 00:00:00 2001 From: Pascal Brunot Date: Sun, 21 Jul 2024 10:06:35 +0200 Subject: [PATCH] Config: using C++ constexpr sort --- include/PinsConfig.hpp | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/include/PinsConfig.hpp b/include/PinsConfig.hpp index 40adb06d..123c7917 100644 --- a/include/PinsConfig.hpp +++ b/include/PinsConfig.hpp @@ -96,20 +96,28 @@ namespace fabomatic pins.led.blue_pin, pins.buttons.factory_defaults_pin}; - // No constexpr std::sort available - for (auto i = 0; i < pin_nums.size(); ++i) + // C++20 provides constexpr sorting + std::sort(pin_nums.begin(), pin_nums.end()); + + uint8_t previous = NO_PIN; + for (const auto pin : pin_nums) { - if (pin_nums[i] == NO_PIN) + if (pin == NO_PIN) + { continue; + } - for (auto j = i + 1; j < pin_nums.size(); ++j) + if (pin == previous) { - if (pin_nums[i] == pin_nums[j]) - return false; + return false; } + + previous = pin; + // Check pins numbers are convertible to gpio_num_t - static_cast(pin_nums[i]); + [[maybe_unused]] auto test = static_cast(pin); } + return true; }