Skip to content

Commit a8aec6a

Browse files
committed
Improve PWMs parsing
1 parent fe5945f commit a8aec6a

File tree

1 file changed

+37
-35
lines changed

1 file changed

+37
-35
lines changed

src/config.cpp

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -71,52 +71,54 @@ struct PwmNode
7171
std::string name;
7272
std::string type;
7373
std::string bind;
74-
int minpwm{}, maxpwm{};
74+
int minpwm{}, maxpwm{PWM_MAX_VAL};
7575
Pwm::Mode mode{Pwm::Mode::NoChange};
7676
int fanStopHyst{FANSTOP_DISABLE};
7777
};
7878

7979
static void operator>>(const YAML::Node& node, PwmNode& pwmNode)
8080
{
81-
for(auto it = node.begin(); it != node.end(); ++it) {
82-
pwmNode.name = it->first.as<std::string>();
83-
for(auto it2 = it->second.begin(); it2 != it->second.end(); ++it2) {
84-
auto key = it2->first.as<std::string>();
85-
if(key == "type") {
86-
pwmNode.type = it2->second.as<std::string>();
87-
}
88-
else if(key == "bind") {
89-
pwmNode.bind = it2->second.as<std::string>();
90-
}
91-
else if(key == "mode") {
92-
auto rawMode = it2->second.as<std::string>();
93-
if(rawMode == "dc") {
94-
pwmNode.mode = Pwm::Mode::Dc;
95-
}
96-
else if(rawMode == "pwm") {
97-
pwmNode.mode = Pwm::Mode::Pwm;
98-
}
99-
else {
100-
cout << "Incompatible PWM mode, no change: " + rawMode << "\n";
101-
}
102-
}
103-
else if(key == "minpwm") {
104-
pwmNode.minpwm = it2->second.as<int>();
105-
}
106-
else if(key == "maxpwm") {
107-
pwmNode.maxpwm = it2->second.as<int>();
108-
}
109-
else if(key == "fan_stop") {
110-
pwmNode.fanStopHyst = it2->second.as<bool>() ? FANSTOP_DEFAULT_HYSTERESIS : FANSTOP_DISABLE;
81+
auto it = node.begin();
82+
pwmNode.name = it->first.as<std::string>();
83+
for(auto it2 = it->second.begin(); it2 != it->second.end(); ++it2) {
84+
auto key = it2->first.as<std::string>();
85+
if(key == "type") {
86+
pwmNode.type = it2->second.as<std::string>();
87+
}
88+
else if(key == "bind") {
89+
pwmNode.bind = it2->second.as<std::string>();
90+
}
91+
else if(key == "mode") {
92+
auto rawMode = it2->second.as<std::string>();
93+
if(rawMode == "dc") {
94+
pwmNode.mode = Pwm::Mode::Dc;
11195
}
112-
else if(key == "fan_stop_hysteresis") {
113-
pwmNode.fanStopHyst = static_cast<int>(it2->second.as<uint32_t>());
96+
else if(rawMode == "pwm") {
97+
pwmNode.mode = Pwm::Mode::Pwm;
11498
}
115-
11699
else {
117-
cout << "unknown attribute:" << key << "\n";
100+
cout << "Incompatible PWM mode, no change: " + rawMode << "\n";
118101
}
119102
}
103+
else if(key == "minpwm") {
104+
pwmNode.minpwm = it2->second.as<int>();
105+
}
106+
else if(key == "maxpwm") {
107+
pwmNode.maxpwm = it2->second.as<int>();
108+
}
109+
else if(key == "fan_stop") {
110+
pwmNode.fanStopHyst = it2->second.as<bool>() ? FANSTOP_DEFAULT_HYSTERESIS : FANSTOP_DISABLE;
111+
}
112+
else if(key == "fan_stop_hysteresis") {
113+
pwmNode.fanStopHyst = static_cast<int>(it2->second.as<uint32_t>());
114+
}
115+
116+
else {
117+
cout << "unknown attribute:" << key << "\n";
118+
}
119+
}
120+
if(pwmNode.type.empty() || pwmNode.bind.empty()) {
121+
throw std::invalid_argument(R"(PWM config entry is inconsistent, must contain 'bind' and 'type' fields)");
120122
}
121123
}
122124

0 commit comments

Comments
 (0)