Fix handling of statistics before data is available and fix normalization factor

This commit is contained in:
BlackMark 2020-04-08 13:44:54 +02:00
parent 1694e3bbab
commit ed2fddc427

View File

@ -243,27 +243,50 @@ class Terminal {
static void printStatistics()
{
m_serial << F("Minimum temperature .: ") << Statistics::getMinTemperature() << F(" C") << detail::ENDL;
m_serial << F("Maximum temperature .: ") << Statistics::getMaxTemperature() << F(" C") << detail::ENDL;
const auto minTemp = Statistics::getMinTemperature();
const auto maxTemp = Statistics::getMaxTemperature();
m_serial << F("Minimum temperature .: ");
if (minTemp != Statistics::TEMPERATURE_RANGE) {
m_serial << minTemp << F(" C");
} else {
m_serial << F("Not available");
}
m_serial << detail::ENDL << F("Maximum temperature .: ");
if (maxTemp != Statistics::TEMPERATURE_RANGE) {
m_serial << maxTemp << F(" C");
} else {
m_serial << F("Not available");
}
m_serial << detail::ENDL;
}
static void printHistogram()
{
const auto totalSamples = Statistics::getTotalHistogramSamples();
const auto maximumSamples = Statistics::getHighestHistogramSamples();
const auto normalizationFactor = (maximumSamples / 100 > 1) ? (maximumSamples / 100) : 1;
for (uint8_t t = Statistics::getMinTemperature(); t <= Statistics::getMaxTemperature(); ++t) {
const auto histogramSamples = Statistics::getHistogram(t);
m_serial.template txNumber<uint8_t, 10, 2>(t);
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 << '#';
if (totalSamples > 0) {
const auto maximumSamples = Statistics::getHighestHistogramSamples();
auto normalizationFactor = (maximumSamples / 100 > 1) ? (maximumSamples / 100) : 1;
while (maximumSamples / normalizationFactor > 100)
++normalizationFactor;
for (uint8_t t = Statistics::getMinTemperature(); t <= Statistics::getMaxTemperature(); ++t) {
const auto histogramSamples = Statistics::getHistogram(t);
m_serial.template txNumber<uint8_t, 10, 2>(t);
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;
}
m_serial << detail::ENDL;
} else {
m_serial << "There is no data yet!" << detail::ENDL;
}
}