temp: Use first/last ramp only for edge temps (#2197)

ramp-0 is used for everything <= base-temperature and ramp-N is used for everything >= warn-temperature

* [Temperature, Ramp] fix wrong icon for temperatures near base and warn temps

* [Temperature, Ramp] fix wrong icon for temperatures near base and warn temps

* Fix minor error

* explicitly check percentage in get_by_percentage_with_borders

* Fixed silly error
This commit is contained in:
Quan 2020-10-09 19:05:50 +07:00 committed by GitHub
parent 8dbd1740a7
commit 3895ace12a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 2 deletions

View File

@ -16,6 +16,7 @@ namespace drawtypes {
void add(label_t&& icon);
label_t get(size_t index);
label_t get_by_percentage(float percentage);
label_t get_by_percentage_with_borders(float percentage);
operator bool();
protected:

View File

@ -18,6 +18,19 @@ namespace drawtypes {
return m_icons[math_util::cap<size_t>(index, 0, m_icons.size() - 1)];
}
label_t ramp::get_by_percentage_with_borders(float percentage) {
size_t index;
if (percentage <= 0.0f) {
index = 0;
} else if (percentage >= 100.0f) {
index = m_icons.size() - 1;
} else {
index = percentage * (m_icons.size() - 2) / 100.0f + 1;
index = math_util::cap<size_t>(index, 0, m_icons.size() - 1);
}
return m_icons[index];
}
ramp::operator bool() {
return !m_icons.empty();
}

View File

@ -53,7 +53,7 @@ namespace modules {
bool temperature_module::update() {
m_temp = std::strtol(file_util::contents(m_path).c_str(), nullptr, 10) / 1000.0f + 0.5f;
int temp_f = floor(((1.8 * m_temp) + 32) + 0.5);
m_perc = math_util::cap(math_util::percentage(m_temp, m_tempbase, m_tempwarn), 0, 100);
m_perc = math_util::unbounded_percentage(m_temp, m_tempbase, m_tempwarn);
string temp_c_string = to_string(m_temp);
string temp_f_string = to_string(temp_f);
@ -97,7 +97,7 @@ namespace modules {
} else if (tag == TAG_LABEL_WARN) {
builder->node(m_label.at(temp_state::WARN));
} else if (tag == TAG_RAMP) {
builder->node(m_ramp->get_by_percentage(m_perc));
builder->node(m_ramp->get_by_percentage_with_borders(m_perc));
} else {
return false;
}