Add percentages to histogram

This commit is contained in:
BlackMark 2020-04-08 12:41:53 +02:00
parent 00082617d1
commit 02565c9396
3 changed files with 26 additions and 9 deletions

View File

@ -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<uint8_t>(m_temperatureHistogram[clampTemperature(temperature)] / normalizationFactor);
return m_temperatureHistogram[clampTemperature(temperature)];
}
uint8_t Statistics::clampTemperature(const int8_t &temperature)

View File

@ -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;

View File

@ -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<uint8_t, 10, 2>(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<uint8_t, 10, 3, ' '>((histogramSamples * 100) / totalSamples);
m_serial << F("%\t");
const auto normalizedSamples = static_cast<uint8_t>(histogramSamples / normalizationFactor);
for (uint8_t i = 0; i < normalizedSamples; ++i) {
m_serial << '#';
}
m_serial << detail::ENDL;