| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "fanfixedprofilepart.h" |
| 5 | |
| 6 | #include "core/profilepartprovider.h" |
| 7 | #include <algorithm> |
| 8 | #include <memory> |
| 9 | #include <utility> |
| 10 | |
| 11 | class AMD::FanFixedProfilePart::Initializer final : public AMD::FanFixed::Exporter |
| 12 | { |
| 13 | public: |
| 14 | Initializer(AMD::FanFixedProfilePart &outer) noexcept |
| 15 | : outer_(outer) |
| 16 | { |
| 17 | } |
| 18 | |
| 19 | std::optional<std::reference_wrapper<Exportable::Exporter>> |
| 20 | provideExporter(Item const &) override |
| 21 | { |
| 22 | return {}; |
| 23 | } |
| 24 | |
| 25 | void takeActive(bool active) override; |
| 26 | void takeFanFixedValue(units::concentration::percent_t value) override; |
| 27 | void takeFanFixedFanStop(bool enabled) override; |
| 28 | void takeFanFixedFanStartValue(units::concentration::percent_t value) override; |
| 29 | |
| 30 | private: |
| 31 | AMD::FanFixedProfilePart &outer_; |
| 32 | }; |
| 33 | |
| 34 | void AMD::FanFixedProfilePart::Initializer::takeActive(bool active) |
| 35 | { |
| 36 | outer_.activate(active); |
| 37 | } |
| 38 | |
| 39 | void AMD::FanFixedProfilePart::Initializer::takeFanFixedValue( |
| 40 | units::concentration::percent_t value) |
| 41 | { |
| 42 | outer_.value_ = value; |
| 43 | } |
| 44 | |
| 45 | void AMD::FanFixedProfilePart::Initializer::takeFanFixedFanStop(bool enabled) |
| 46 | { |
| 47 | outer_.fanStop_ = enabled; |
| 48 | } |
| 49 | |
| 50 | void AMD::FanFixedProfilePart::Initializer::takeFanFixedFanStartValue( |
| 51 | units::concentration::percent_t value) |
| 52 | { |
| 53 | outer_.fanStartValue_ = value; |
| 54 | } |
| 55 | |
| 56 | AMD::FanFixedProfilePart::FanFixedProfilePart() noexcept |
| 57 | : id_(AMD::FanFixed::ItemID) |
| 58 | { |
| 59 | } |
| 60 | |
| 61 | std::unique_ptr<Exportable::Exporter> |
| 62 | AMD::FanFixedProfilePart::factory(IProfilePartProvider const &) |
| 63 | { |
| 64 | return nullptr; |
| 65 | } |
| 66 | |
| 67 | std::unique_ptr<Exportable::Exporter> AMD::FanFixedProfilePart::initializer() |
| 68 | { |
| 69 | return std::make_unique<AMD::FanFixedProfilePart::Initializer>(*this); |
| 70 | } |
| 71 | |
| 72 | std::string const &AMD::FanFixedProfilePart::ID() const |
| 73 | { |
| 74 | return id_; |
| 75 | } |
| 76 | |
| 77 | std::optional<std::reference_wrapper<Importable::Importer>> |
| 78 | AMD::FanFixedProfilePart::provideImporter(Item const &) |
| 79 | { |
| 80 | return {}; |
| 81 | } |
| 82 | |
| 83 | bool AMD::FanFixedProfilePart::provideActive() const |
| 84 | { |
| 85 | return active(); |
| 86 | } |
| 87 | |
| 88 | units::concentration::percent_t AMD::FanFixedProfilePart::provideFanFixedValue() const |
| 89 | { |
| 90 | return value_; |
| 91 | } |
| 92 | |
| 93 | bool AMD::FanFixedProfilePart::provideFanFixedFanStop() const |
| 94 | { |
| 95 | return fanStop_; |
| 96 | } |
| 97 | |
| 98 | units::concentration::percent_t |
| 99 | AMD::FanFixedProfilePart::provideFanFixedFanStartValue() const |
| 100 | { |
| 101 | return fanStartValue_; |
| 102 | } |
| 103 | |
| 104 | void AMD::FanFixedProfilePart::importProfilePart(IProfilePart::Importer &i) |
| 105 | { |
| 106 | auto &pmfImporter = dynamic_cast<AMD::FanFixedProfilePart::Importer &>(i); |
| 107 | |
| 108 | value(pmfImporter.provideFanFixedValue()); |
| 109 | fanStop_ = pmfImporter.provideFanFixedFanStop(); |
| 110 | startValue(pmfImporter.provideFanFixedFanStartValue()); |
| 111 | } |
| 112 | |
| 113 | void AMD::FanFixedProfilePart::exportProfilePart(IProfilePart::Exporter &e) const |
| 114 | { |
| 115 | auto &pmfExporter = dynamic_cast<AMD::FanFixedProfilePart::Exporter &>(e); |
| 116 | pmfExporter.takeFanFixedValue(value_); |
| 117 | pmfExporter.takeFanFixedFanStop(fanStop_); |
| 118 | pmfExporter.takeFanFixedFanStartValue(fanStartValue_); |
| 119 | } |
| 120 | |
| 121 | std::unique_ptr<IProfilePart> AMD::FanFixedProfilePart::cloneProfilePart() const |
| 122 | { |
| 123 | auto clone = std::make_unique<AMD::FanFixedProfilePart>(); |
| 124 | clone->value_ = value_; |
| 125 | clone->fanStop_ = fanStop_; |
| 126 | clone->fanStartValue_ = fanStartValue_; |
| 127 | |
| 128 | return std::move(clone); |
| 129 | } |
| 130 | |
| 131 | void AMD::FanFixedProfilePart::value(units::concentration::percent_t value) |
| 132 | { |
| 133 | value_ = std::clamp(value, units::concentration::percent_t(0), |
| 134 | units::concentration::percent_t(100)); |
| 135 | } |
| 136 | |
| 137 | void AMD::FanFixedProfilePart::startValue(units::concentration::percent_t value) |
| 138 | { |
| 139 | fanStartValue_ = std::clamp(value, units::concentration::percent_t(0), |
| 140 | units::concentration::percent_t(100)); |
| 141 | } |
| 142 | |
| 143 | bool const AMD::FanFixedProfilePart::registered_ = |
| 144 | ProfilePartProvider::registerProvider(AMD::FanFixed::ItemID, []() { |
| 145 | return std::make_unique<AMD::FanFixedProfilePart>(); |
| 146 | }); |