From 3895ace12a7634ce4d2bc27e92dd0dfa43610803 Mon Sep 17 00:00:00 2001 From: Quan Date: Fri, 9 Oct 2020 19:05:50 +0700 Subject: [PATCH] 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 --- include/drawtypes/ramp.hpp | 1 + src/drawtypes/ramp.cpp | 13 +++++++++++++ src/modules/temperature.cpp | 4 ++-- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/include/drawtypes/ramp.hpp b/include/drawtypes/ramp.hpp index 0b8b266e..fb94c190 100644 --- a/include/drawtypes/ramp.hpp +++ b/include/drawtypes/ramp.hpp @@ -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: diff --git a/src/drawtypes/ramp.cpp b/src/drawtypes/ramp.cpp index 096f2be5..45a9cfba 100644 --- a/src/drawtypes/ramp.cpp +++ b/src/drawtypes/ramp.cpp @@ -18,6 +18,19 @@ namespace drawtypes { return m_icons[math_util::cap(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(index, 0, m_icons.size() - 1); + } + return m_icons[index]; + } + ramp::operator bool() { return !m_icons.empty(); } diff --git a/src/modules/temperature.cpp b/src/modules/temperature.cpp index c38e7bc7..8d2a50ca 100644 --- a/src/modules/temperature.cpp +++ b/src/modules/temperature.cpp @@ -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; }