Add eeprom saved statistics

This commit is contained in:
BlackMark 2020-04-07 21:09:27 +02:00
parent ea6a6bd218
commit 6ba4a2ce3d
9 changed files with 127 additions and 6 deletions

6
.gitmodules vendored
View File

@ -10,3 +10,9 @@
[submodule "fantemp/adc"] [submodule "fantemp/adc"]
path = fantemp/adc path = fantemp/adc
url = git@git.blackmark.me:avr/adc.git url = git@git.blackmark.me:avr/adc.git
[submodule "fantemp/type"]
path = fantemp/type
url = git@git.blackmark.me:avr/type.git
[submodule "fantemp/eeprom"]
path = fantemp/eeprom
url = git@git.blackmark.me:avr/eeprom.git

1
fantemp/eeprom Submodule

@ -0,0 +1 @@
Subproject commit 33a4d55f03451f487ace046a6f2328351fda69ee

View File

@ -231,6 +231,9 @@
<Compile Include="controller.hpp"> <Compile Include="controller.hpp">
<SubType>compile</SubType> <SubType>compile</SubType>
</Compile> </Compile>
<Compile Include="eeprom\eeprom.hpp">
<SubType>compile</SubType>
</Compile>
<Compile Include="flash\flash.hpp"> <Compile Include="flash\flash.hpp">
<SubType>compile</SubType> <SubType>compile</SubType>
</Compile> </Compile>
@ -246,6 +249,12 @@
<Compile Include="pwm.hpp"> <Compile Include="pwm.hpp">
<SubType>compile</SubType> <SubType>compile</SubType>
</Compile> </Compile>
<Compile Include="statistics.cpp">
<SubType>compile</SubType>
</Compile>
<Compile Include="statistics.hpp">
<SubType>compile</SubType>
</Compile>
<Compile Include="terminal.hpp"> <Compile Include="terminal.hpp">
<SubType>compile</SubType> <SubType>compile</SubType>
</Compile> </Compile>
@ -255,6 +264,9 @@
<Compile Include="thermistor.hpp"> <Compile Include="thermistor.hpp">
<SubType>compile</SubType> <SubType>compile</SubType>
</Compile> </Compile>
<Compile Include="type\type.hpp">
<SubType>compile</SubType>
</Compile>
<Compile Include="uart\config.hpp"> <Compile Include="uart\config.hpp">
<SubType>compile</SubType> <SubType>compile</SubType>
</Compile> </Compile>
@ -273,14 +285,13 @@
<Compile Include="uart\uart.hpp"> <Compile Include="uart\uart.hpp">
<SubType>compile</SubType> <SubType>compile</SubType>
</Compile> </Compile>
<Compile Include="uart\utils.hpp">
<SubType>compile</SubType>
</Compile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Folder Include="flash" /> <Folder Include="flash" />
<Folder Include="io" /> <Folder Include="io" />
<Folder Include="adc" /> <Folder Include="adc" />
<Folder Include="eeprom" />
<Folder Include="type" />
<Folder Include="uart" /> <Folder Include="uart" />
</ItemGroup> </ItemGroup>
<Import Project="$(AVRSTUDIO_EXE_PATH)\\Vs\\Compiler.targets" /> <Import Project="$(AVRSTUDIO_EXE_PATH)\\Vs\\Compiler.targets" />

View File

@ -7,12 +7,15 @@
#include "bootloader.hpp" #include "bootloader.hpp"
#include "controller.hpp" #include "controller.hpp"
#include "statistics.hpp"
#include "terminal.hpp" #include "terminal.hpp"
int main() int main()
{ {
Bootloader::init([]() {}); Bootloader::init([]() {});
clk::init();
using serial = uart::Uart0<uart::Config<115200>>; using serial = uart::Uart0<uart::Config<115200>>;
Terminal<serial> terminal; Terminal<serial> terminal;
terminal.init(); terminal.init();
@ -20,11 +23,13 @@ int main()
Controller controller; Controller controller;
controller.init(); controller.init();
clk::init(); Statistics statistics;
statistics.init();
while (true) { while (true) {
controller.callback(); controller.callback();
terminal.callback(); terminal.callback();
statistics.callback();
} }
return 0; return 0;

62
fantemp/statistics.cpp Normal file
View File

@ -0,0 +1,62 @@
#include "statistics.hpp"
#include <math.h>
#include "eeprom/eeprom.hpp"
#include "type/type.hpp"
#include "clock.hpp"
#include "controller.hpp"
uint64_t Statistics::m_lastTempSave = 0;
double Statistics::m_minTemp;
double Statistics::m_maxTemp;
EEVAR(double, e_minTemp);
EEVAR(double, e_maxTemp);
void Statistics::init()
{
m_minTemp = getMinTemp();
m_maxTemp = getMaxTemp();
if (isnan(m_minTemp)) {
m_minTemp = type::numeric_limits<double>::max();
}
if (isnan(m_maxTemp)) {
m_maxTemp = type::numeric_limits<double>::lowest();
}
}
void Statistics::callback()
{
Eeprom<e_minTemp, true> eepromMinTemp;
Eeprom<e_maxTemp, true> eepromMaxTemp;
if (Controller::m_dataAvailable) {
if (Controller::m_temperature < m_minTemp) {
m_minTemp = Controller::m_temperature;
}
if (Controller::m_temperature > m_maxTemp) {
m_maxTemp = Controller::m_temperature;
}
if (clk::millis() >= m_lastTempSave + TEMP_SAVE_DELAY) {
eepromMinTemp = m_minTemp;
eepromMaxTemp = m_maxTemp;
m_lastTempSave = clk::millis();
}
}
}
double Statistics::getMinTemp()
{
Eeprom<e_minTemp, true> eepromMinTemp;
return eepromMinTemp;
}
double Statistics::getMaxTemp()
{
Eeprom<e_maxTemp, true> eepromMaxTemp;
return eepromMaxTemp;
}

20
fantemp/statistics.hpp Normal file
View File

@ -0,0 +1,20 @@
#pragma once
#include <stdint.h>
class Statistics {
public:
static void init();
static void callback();
static double getMinTemp();
static double getMaxTemp();
private:
static constexpr auto TEMP_SAVE_DELAY = 60000;
static uint64_t m_lastTempSave;
static double m_minTemp;
static double m_maxTemp;
};

View File

@ -10,6 +10,7 @@
#include "clock.hpp" #include "clock.hpp"
#include "controller.hpp" #include "controller.hpp"
#include "statistics.hpp"
namespace detail { namespace detail {
@ -20,9 +21,10 @@ GF(CURVE_CMD, "curve");
GF(MONITOR_CMD, "monitor"); GF(MONITOR_CMD, "monitor");
GF(BOOTLOADER_CMD, "bootloader"); GF(BOOTLOADER_CMD, "bootloader");
GF(UPTIME_CMD, "uptime"); GF(UPTIME_CMD, "uptime");
GF(STATISTICS_CMD, "statistics");
GF(VERSION_CMD, "version"); GF(VERSION_CMD, "version");
GF(VERSION, "1.3"); GF(VERSION, "1.4");
static inline bool substringEquals(const char *str, const ::detail::FlashString *flashStr, const size_t &size) static inline bool substringEquals(const char *str, const ::detail::FlashString *flashStr, const size_t &size)
{ {
@ -142,6 +144,8 @@ class Terminal {
handleBootloader(); handleBootloader();
} else if (substringEquals(m_inputBuffer, detail::UPTIME_CMD, m_inputSize)) { } else if (substringEquals(m_inputBuffer, detail::UPTIME_CMD, m_inputSize)) {
printUptime(); printUptime();
} else if (substringEquals(m_inputBuffer, detail::STATISTICS_CMD, m_inputSize)) {
printStatistics();
} else if (substringEquals(m_inputBuffer, detail::VERSION_CMD, m_inputSize)) { } else if (substringEquals(m_inputBuffer, detail::VERSION_CMD, m_inputSize)) {
printVersion(); printVersion();
} else { } else {
@ -170,6 +174,7 @@ class Terminal {
m_serial << detail::MONITOR_CMD << F(" ....: loops the show command until Ctrl + C is pressed") << detail::ENDL; m_serial << detail::MONITOR_CMD << F(" ....: loops the show command until Ctrl + C is pressed") << detail::ENDL;
m_serial << detail::BOOTLOADER_CMD << F(" .: enters the bootloader after 3 seconds") << detail::ENDL; m_serial << detail::BOOTLOADER_CMD << F(" .: enters the bootloader after 3 seconds") << detail::ENDL;
m_serial << detail::UPTIME_CMD << F(" .....: show system uptime") << detail::ENDL; m_serial << detail::UPTIME_CMD << F(" .....: show system uptime") << detail::ENDL;
m_serial << detail::STATISTICS_CMD << F(" .: Print overall statistics like min and max temp") << detail::ENDL;
m_serial << detail::VERSION_CMD << F(" ....: displays firmware version") << detail::ENDL; m_serial << detail::VERSION_CMD << F(" ....: displays firmware version") << detail::ENDL;
} }
@ -232,6 +237,16 @@ class Terminal {
m_serial << detail::ENDL; m_serial << detail::ENDL;
} }
static void printStatistics()
{
char floatBuffer[16];
sprintf(floatBuffer, "%.2f", Statistics::getMinTemp());
m_serial << F("Minimum temperature .: ") << floatBuffer << F(" C") << detail::ENDL;
sprintf(floatBuffer, "%.2f", Statistics::getMaxTemp());
m_serial << F("Maximum temperature .: ") << floatBuffer << F(" C") << detail::ENDL;
}
static void printVersion() static void printVersion()
{ {
m_serial << F("FanTemp v") << detail::VERSION << detail::ENDL; m_serial << F("FanTemp v") << detail::VERSION << detail::ENDL;

1
fantemp/type Submodule

@ -0,0 +1 @@
Subproject commit ce31ef017f738baaca6f0cbf99dac9d59b5e6811

@ -1 +1 @@
Subproject commit 6f592dd0987cae4f7f826056e5ff358ad26d3127 Subproject commit ae03c8d43e46dfbd396a052f71670727b293ac76