From 02565c93962da84603bb727d9b6fc4f6b7566c5e Mon Sep 17 00:00:00 2001 From: BlackMark Date: Wed, 8 Apr 2020 12:41:53 +0200 Subject: [PATCH] Add percentages to histogram --- fantemp/statistics.cpp | 17 ++++++++++++++--- fantemp/statistics.hpp | 5 +++-- fantemp/terminal.hpp | 13 +++++++++---- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/fantemp/statistics.cpp b/fantemp/statistics.cpp index 187f25e..722df05 100644 --- a/fantemp/statistics.cpp +++ b/fantemp/statistics.cpp @@ -69,7 +69,18 @@ uint8_t Statistics::getMaxTemp() return TEMPERATURE_RANGE; } -uint32_t Statistics::getMaximumHistogramSamples() +uint64_t Statistics::getTotalHistogramSamples() +{ + uint64_t totalSamples = 0; + + for (uint8_t i = 0; i < TEMPERATURE_RANGE; ++i) { + totalSamples += m_temperatureHistogram[i]; + } + + return totalSamples; +} + +uint32_t Statistics::getHighestHistogramSamples() { uint32_t max = 1; // Avoid division by zero for (uint8_t i = 0; i < TEMPERATURE_RANGE; ++i) { @@ -80,9 +91,9 @@ uint32_t Statistics::getMaximumHistogramSamples() return max; } -uint8_t Statistics::getHistogram(const int8_t &temperature, const uint32_t &normalizationFactor) +uint32_t Statistics::getHistogram(const int8_t &temperature) { - return static_cast(m_temperatureHistogram[clampTemperature(temperature)] / normalizationFactor); + return m_temperatureHistogram[clampTemperature(temperature)]; } uint8_t Statistics::clampTemperature(const int8_t &temperature) diff --git a/fantemp/statistics.hpp b/fantemp/statistics.hpp index 2a10f6a..3405b4f 100644 --- a/fantemp/statistics.hpp +++ b/fantemp/statistics.hpp @@ -10,8 +10,9 @@ class Statistics { static uint8_t getMinTemp(); static uint8_t getMaxTemp(); - static uint32_t getMaximumHistogramSamples(); - static uint8_t getHistogram(const int8_t &temperature, const uint32_t &normalizationFactor); + static uint64_t getTotalHistogramSamples(); + static uint32_t getHighestHistogramSamples(); + static uint32_t getHistogram(const int8_t &temperature); private: static constexpr auto TEMPERATURE_WRITEBACK_DELAY = 1'800'000; diff --git a/fantemp/terminal.hpp b/fantemp/terminal.hpp index adf34a2..abdfc9d 100644 --- a/fantemp/terminal.hpp +++ b/fantemp/terminal.hpp @@ -249,13 +249,18 @@ class Terminal { static void printHistogram() { - auto normalizationFactor = Statistics::getMaximumHistogramSamples(); - normalizationFactor = (normalizationFactor / 100 > 1) ? (normalizationFactor / 100) : 1; + const auto totalSamples = Statistics::getTotalHistogramSamples(); + const auto maximumSamples = Statistics::getHighestHistogramSamples(); + const auto normalizationFactor = (maximumSamples / 100 > 1) ? (maximumSamples / 100) : 1; for (uint8_t t = 10; t <= 60; ++t) { + const auto histogramSamples = Statistics::getHistogram(t); m_serial.template txNumber(t); - m_serial << F(" C\t"); - for (uint8_t i = 0; i < Statistics::getHistogram(t, normalizationFactor); ++i) { + m_serial << F(" C = "); + m_serial.template txNumber((histogramSamples * 100) / totalSamples); + m_serial << F("%\t"); + const auto normalizedSamples = static_cast(histogramSamples / normalizationFactor); + for (uint8_t i = 0; i < normalizedSamples; ++i) { m_serial << '#'; } m_serial << detail::ENDL;