|
3 | 3 | static_assert(__cplusplus >= 202302L, "C++23 standard or later required."); |
4 | 4 |
|
5 | 5 | // Imports |
| 6 | +#include <cstddef> |
6 | 7 | #include <cstdint> |
7 | 8 | #include <etl/array.h> |
8 | 9 | #include <etl/combinations.h> |
9 | 10 | #include <etl/vector.h> |
| 11 | +#include <utility> |
10 | 12 |
|
11 | 13 | // CommonConfig.hpp |
12 | 14 |
|
@@ -54,12 +56,62 @@ namespace LOAD { |
54 | 56 | etl::combinations<NUM_PINS, 4>::value + |
55 | 57 | etl::combinations<NUM_PINS, 5>::value + |
56 | 58 | etl::combinations<NUM_PINS, 6>::value; |
| 59 | + |
| 60 | + // According to GitHub Copilot, GPT-5.1, Ask mode |
| 61 | + static_assert(NUM_COMBINATIONS == (1u << NUM_PINS), |
| 62 | + "NUM_COMBINATIONS must be 2^NUM_PINS"); |
| 63 | + |
57 | 64 | constexpr etl::array<uint_fast16_t, NUM_PINS> PIN_VALUES_mOhms = { |
58 | 65 | 500, 1000, 2000, 3000, 5000, 10000}; // in mOhms, in series // TODO |
59 | 66 | static_assert(PIN_VALUES_mOhms.size() == NUM_PINS, |
60 | 67 | "PIN_VALUES size mismatch"); |
61 | 68 | static_assert(PIN_VALUES_mOhms.back() < UINT16_MAX, |
62 | 69 | "PIN_VALUES value too large"); |
| 70 | + |
| 71 | + /** |
| 72 | + * @brief Detail namespace for combination sum calculations |
| 73 | + * @author GitHub Copilot, GPT-5.1, Ask mode |
| 74 | + * @author BobSaidHi |
| 75 | + * @details Prompt: Using C++23 consteval functions and templates, I would |
| 76 | + * like to create an array of all possible combinations of values in |
| 77 | + * PIN_VALUES_mOhms. (included some relevant lines of my own attempts as |
| 78 | + * context) |
| 79 | + */ |
| 80 | + namespace detail { |
| 81 | + // Sum of selected pins for a given bitmask |
| 82 | + consteval uint_fast16_t |
| 83 | + sum_for_mask(uint_fast8_t mask, |
| 84 | + const etl::array<uint_fast16_t, NUM_PINS> &values) { |
| 85 | + |
| 86 | + uint_fast16_t sum = 0; |
| 87 | + for (std::size_t i = 0; i < NUM_PINS; ++i) { |
| 88 | + if (mask & (1u << i)) { |
| 89 | + sum += values[i]; |
| 90 | + } |
| 91 | + } |
| 92 | + return sum; |
| 93 | + } |
| 94 | + |
| 95 | + template <std::size_t... Is> |
| 96 | + consteval etl::array<uint_fast16_t, NUM_COMBINATIONS> |
| 97 | + make_all_combinations(const etl::array<uint_fast16_t, NUM_PINS> &values, |
| 98 | + std::index_sequence<Is...>) { |
| 99 | + |
| 100 | + return {sum_for_mask(static_cast<uint_fast8_t>(Is), values)...}; |
| 101 | + } |
| 102 | + } // namespace detail |
| 103 | + |
| 104 | + /** |
| 105 | + * @brief get all combinations of values |
| 106 | + * @author GitHub Copilot, GPT-5.1, Ask mode |
| 107 | + * @author BobSaidHi |
| 108 | + */ |
| 109 | + consteval etl::array<uint_fast16_t, NUM_COMBINATIONS> |
| 110 | + getAllCombinations(const etl::array<uint_fast16_t, NUM_PINS> &values) { |
| 111 | + return detail::make_all_combinations( |
| 112 | + values, std::make_index_sequence<NUM_COMBINATIONS>{}); |
| 113 | + } |
| 114 | + |
63 | 115 | constexpr etl::array<uint_fast16_t, NUM_COMBINATIONS> |
64 | 116 | ALL_COMBINATIONS_mOhms = getAllCombinations(PIN_VALUES_mOhms); |
65 | 117 |
|
|
0 commit comments