| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "fancurveprofilepart.h" |
| 5 | |
| 6 | #include "core/components/commonutils.h" |
| 7 | #include "core/profilepartprovider.h" |
| 8 | #include <algorithm> |
| 9 | #include <memory> |
| 10 | |
| 11 | class AMD::FanCurveProfilePart::Initializer final : public AMD::FanCurve::Exporter |
| 12 | { |
| 13 | public: |
| 14 | Initializer(AMD::FanCurveProfilePart &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 |
| 27 | takeFanCurvePoints(std::vector<AMD::FanCurve::Point> const &points) override; |
| 28 | void takeFanCurveFanStop(bool enabled) override; |
| 29 | void takeFanCurveFanStartValue(units::concentration::percent_t value) override; |
| 30 | void takeFanCurveTemperatureRange(units::temperature::celsius_t min, |
| 31 | units::temperature::celsius_t max) override; |
| 32 | |
| 33 | private: |
| 34 | AMD::FanCurveProfilePart &outer_; |
| 35 | }; |
| 36 | |
| 37 | void AMD::FanCurveProfilePart::Initializer::takeActive(bool active) |
| 38 | { |
| 39 | outer_.activate(active); |
| 40 | } |
| 41 | |
| 42 | void AMD::FanCurveProfilePart::Initializer::takeFanCurvePoints( |
| 43 | std::vector<AMD::FanCurve::Point> const &points) |
| 44 | { |
| 45 | outer_.points_ = points; |
| 46 | } |
| 47 | |
| 48 | void AMD::FanCurveProfilePart::Initializer::takeFanCurveFanStop(bool enabled) |
| 49 | { |
| 50 | outer_.fanStop_ = enabled; |
| 51 | } |
| 52 | |
| 53 | void AMD::FanCurveProfilePart::Initializer::takeFanCurveFanStartValue( |
| 54 | units::concentration::percent_t value) |
| 55 | { |
| 56 | outer_.fanStartValue_ = value; |
| 57 | } |
| 58 | |
| 59 | void AMD::FanCurveProfilePart::Initializer::takeFanCurveTemperatureRange( |
| 60 | units::temperature::celsius_t min, units::temperature::celsius_t max) |
| 61 | { |
| 62 | outer_.tempRange_ = std::make_pair(min, max); |
| 63 | } |
| 64 | |
| 65 | AMD::FanCurveProfilePart::FanCurveProfilePart() noexcept |
| 66 | : id_(AMD::FanCurve::ItemID) |
| 67 | { |
| 68 | } |
| 69 | |
| 70 | std::unique_ptr<Exportable::Exporter> |
| 71 | AMD::FanCurveProfilePart::factory(IProfilePartProvider const &) |
| 72 | { |
| 73 | return nullptr; |
| 74 | } |
| 75 | |
| 76 | std::unique_ptr<Exportable::Exporter> AMD::FanCurveProfilePart::initializer() |
| 77 | { |
| 78 | return std::make_unique<AMD::FanCurveProfilePart::Initializer>(*this); |
| 79 | } |
| 80 | |
| 81 | std::string const &AMD::FanCurveProfilePart::ID() const |
| 82 | { |
| 83 | return id_; |
| 84 | } |
| 85 | |
| 86 | std::optional<std::reference_wrapper<Importable::Importer>> |
| 87 | AMD::FanCurveProfilePart::provideImporter(Item const &) |
| 88 | { |
| 89 | return {}; |
| 90 | } |
| 91 | |
| 92 | bool AMD::FanCurveProfilePart::provideActive() const |
| 93 | { |
| 94 | return active(); |
| 95 | } |
| 96 | |
| 97 | std::vector<AMD::FanCurve::Point> const & |
| 98 | AMD::FanCurveProfilePart::provideFanCurvePoints() const |
| 99 | { |
| 100 | return points_; |
| 101 | } |
| 102 | |
| 103 | bool AMD::FanCurveProfilePart::provideFanCurveFanStop() const |
| 104 | { |
| 105 | return fanStop_; |
| 106 | } |
| 107 | |
| 108 | units::concentration::percent_t |
| 109 | AMD::FanCurveProfilePart::provideFanCurveFanStartValue() const |
| 110 | { |
| 111 | return fanStartValue_; |
| 112 | } |
| 113 | |
| 114 | void AMD::FanCurveProfilePart::importProfilePart(IProfilePart::Importer &i) |
| 115 | { |
| 116 | auto &pmfImporter = dynamic_cast<AMD::FanCurveProfilePart::Importer &>(i); |
| 117 | points(pmfImporter.provideFanCurvePoints()); |
| 118 | fanStop_ = pmfImporter.provideFanCurveFanStop(); |
| 119 | startValue(pmfImporter.provideFanCurveFanStartValue()); |
| 120 | } |
| 121 | |
| 122 | void AMD::FanCurveProfilePart::exportProfilePart(IProfilePart::Exporter &e) const |
| 123 | { |
| 124 | auto &pmfExporter = dynamic_cast<AMD::FanCurveProfilePart::Exporter &>(e); |
| 125 | pmfExporter.takeFanCurvePoints(points_); |
| 126 | pmfExporter.takeFanCurveFanStop(fanStop_); |
| 127 | pmfExporter.takeFanCurveFanStartValue(fanStartValue_); |
| 128 | } |
| 129 | |
| 130 | std::unique_ptr<IProfilePart> AMD::FanCurveProfilePart::cloneProfilePart() const |
| 131 | { |
| 132 | auto clone = std::make_unique<AMD::FanCurveProfilePart>(); |
| 133 | clone->tempRange_ = tempRange_; |
| 134 | clone->points_ = points_; |
| 135 | clone->fanStop_ = fanStop_; |
| 136 | clone->fanStartValue_ = fanStartValue_; |
| 137 | |
| 138 | return std::move(clone); |
| 139 | } |
| 140 | |
| 141 | void AMD::FanCurveProfilePart::points(std::vector<FanCurve::Point> const &points) |
| 142 | { |
| 143 | points_ = points; |
| 144 | Utils::Common::normalizePoints(points_, tempRange_); |
| 145 | } |
| 146 | |
| 147 | void AMD::FanCurveProfilePart::startValue(units::concentration::percent_t value) |
| 148 | { |
| 149 | fanStartValue_ = std::clamp(value, units::concentration::percent_t(0), |
| 150 | units::concentration::percent_t(100)); |
| 151 | } |
| 152 | |
| 153 | bool const AMD::FanCurveProfilePart::registered_ = |
| 154 | ProfilePartProvider::registerProvider(AMD::FanCurve::ItemID, []() { |
| 155 | return std::make_unique<AMD::FanCurveProfilePart>(); |
| 156 | }); |