Refactor code from main to controller class
This commit is contained in:
parent
a33a764b1d
commit
226d1ba6c9
36
fantemp/controller.cpp
Normal file
36
fantemp/controller.cpp
Normal file
@ -0,0 +1,36 @@
|
||||
#include "controller.hpp"
|
||||
|
||||
#define ADC_INT_VECTOR
|
||||
#include "adc/adc.hpp"
|
||||
|
||||
double Controller::m_adcSample;
|
||||
double Controller::m_resistance;
|
||||
double Controller::m_temperature;
|
||||
uint8_t Controller::m_fanSpeed;
|
||||
|
||||
void Controller::init()
|
||||
{
|
||||
m_adcPin.init();
|
||||
pwm::init();
|
||||
pwm::setDuty(100);
|
||||
}
|
||||
|
||||
void Controller::callback()
|
||||
{
|
||||
sample();
|
||||
mapTemperature();
|
||||
pwm::setDuty(m_fanSpeed);
|
||||
}
|
||||
|
||||
void Controller::sample()
|
||||
{
|
||||
m_adcSample = m_thermistor.sampleAdc(m_adcPin, 1000);
|
||||
m_resistance = m_thermistor.getResistance(m_adcSample);
|
||||
m_temperature = m_thermistor.getTemperature(m_resistance);
|
||||
}
|
||||
|
||||
void Controller::mapTemperature()
|
||||
{
|
||||
double fanSpeed = (10 * m_temperature - 200) / 3;
|
||||
m_fanSpeed = clamp<uint8_t>(fanSpeed, 0, 100);
|
||||
}
|
39
fantemp/controller.hpp
Normal file
39
fantemp/controller.hpp
Normal file
@ -0,0 +1,39 @@
|
||||
#pragma once
|
||||
|
||||
#include "adc/adc.hpp"
|
||||
#include "io/io.hpp"
|
||||
|
||||
#include "pwm.hpp"
|
||||
#include "thermistor.hpp"
|
||||
|
||||
class Controller {
|
||||
public:
|
||||
static double m_adcSample;
|
||||
static double m_resistance;
|
||||
static double m_temperature;
|
||||
static uint8_t m_fanSpeed;
|
||||
|
||||
static void init();
|
||||
|
||||
static void callback();
|
||||
|
||||
private:
|
||||
using adc_conf = adc::Config<adc::SingleMode>;
|
||||
static adc::Adc<adc_conf, io::P, io::P::C0> m_adcPin;
|
||||
|
||||
static Thermistor m_thermistor;
|
||||
|
||||
static void sample();
|
||||
|
||||
template <typename T>
|
||||
static T clamp(double value, T lower, T upper)
|
||||
{
|
||||
if (value < lower)
|
||||
return lower;
|
||||
if (value > upper)
|
||||
return upper;
|
||||
return static_cast<T>(value);
|
||||
}
|
||||
|
||||
static void mapTemperature();
|
||||
};
|
@ -203,6 +203,12 @@
|
||||
<Compile Include="clock.hpp">
|
||||
<SubType>compile</SubType>
|
||||
</Compile>
|
||||
<Compile Include="controller.cpp">
|
||||
<SubType>compile</SubType>
|
||||
</Compile>
|
||||
<Compile Include="controller.hpp">
|
||||
<SubType>compile</SubType>
|
||||
</Compile>
|
||||
<Compile Include="flash\flash.hpp">
|
||||
<SubType>compile</SubType>
|
||||
</Compile>
|
||||
|
@ -1,75 +1,26 @@
|
||||
#include "clock.hpp"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "flash/flash.hpp"
|
||||
#include "io/io.hpp"
|
||||
#include "uart/uart.hpp"
|
||||
|
||||
#define ADC_INT_VECTOR
|
||||
#include "adc/adc.hpp"
|
||||
|
||||
#define UART0_INT_VECTORS
|
||||
#include "uart/hardware0.hpp"
|
||||
|
||||
#include "pwm.hpp"
|
||||
#include "controller.hpp"
|
||||
#include "terminal.hpp"
|
||||
#include "thermistor.hpp"
|
||||
|
||||
template <typename T>
|
||||
T clamp(double value, T lower, T upper)
|
||||
{
|
||||
if (value < lower)
|
||||
return lower;
|
||||
if (value > upper)
|
||||
return upper;
|
||||
return static_cast<T>(value);
|
||||
}
|
||||
|
||||
uint8_t temperatureCurve(double temperature)
|
||||
{
|
||||
double fanSpeed = (10 * temperature - 200) / 3;
|
||||
return clamp<uint8_t>(fanSpeed, 0, 100);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
uart::Uart0<uart::Config<115200>> serial;
|
||||
Terminal<decltype(serial)> terminal;
|
||||
using serial = uart::Uart0<uart::Config<115200>>;
|
||||
Terminal<serial> terminal;
|
||||
terminal.init();
|
||||
|
||||
using adc_conf = adc::Config<adc::SingleMode>;
|
||||
|
||||
adc::Adc<adc_conf, io::P, io::P::C0> adcPin;
|
||||
adcPin.init();
|
||||
|
||||
Thermistor temp;
|
||||
|
||||
pwm::init();
|
||||
pwm::setDuty(100);
|
||||
Controller controller;
|
||||
controller.init();
|
||||
|
||||
while (true) {
|
||||
const auto adcSample = temp.sampleAdc(adcPin, 1000);
|
||||
const auto fantempResistance = temp.getResistance(adcSample);
|
||||
const auto temperature = temp.getTemperature(fantempResistance);
|
||||
const auto fanSpeed = temperatureCurve(temperature);
|
||||
|
||||
/*char floatBuffer[16];
|
||||
|
||||
sprintf(floatBuffer, "%.2f", adcSample);
|
||||
serial << F("Read ADC: ") << floatBuffer << F("\r\n");
|
||||
sprintf(floatBuffer, "%.2f", fantempResistance);
|
||||
serial << F("Resistance: ") << floatBuffer << F("\r\n");
|
||||
sprintf(floatBuffer, "%.2f", temperature);
|
||||
serial << F("Temperature: ") << floatBuffer << F(" C \r\n");
|
||||
serial << F("Fan speed: ") << fanSpeed << F("%\r\n");*/
|
||||
|
||||
pwm::setDuty(fanSpeed);
|
||||
|
||||
controller.callback();
|
||||
terminal.callback();
|
||||
}
|
||||
|
||||
serial.flushTx();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -2,12 +2,17 @@
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include <avr/pgmspace.h>
|
||||
|
||||
#include "flash/flash.hpp"
|
||||
|
||||
#include "controller.hpp"
|
||||
|
||||
#define ENDL F("\r\n")
|
||||
#define HELP_CMD F("help")
|
||||
#define SHOW_CMD F("show")
|
||||
|
||||
template <class Uart>
|
||||
class Terminal {
|
||||
@ -79,15 +84,33 @@ class Terminal {
|
||||
|
||||
static void parseInput()
|
||||
{
|
||||
if (m_inputSize && strncmp_P(m_inputBuffer, reinterpret_cast<const char *>(F("help")), m_inputSize) == 0) {
|
||||
if (m_inputSize) {
|
||||
if (strncmp_P(m_inputBuffer, reinterpret_cast<const char *>(HELP_CMD), m_inputSize) == 0) {
|
||||
printHelp();
|
||||
} else if (strncmp_P(m_inputBuffer, reinterpret_cast<const char *>(SHOW_CMD), m_inputSize) == 0) {
|
||||
showState();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void printHelp()
|
||||
{
|
||||
m_serial << F("Help: ") << ENDL;
|
||||
m_serial << F("help .: print this help message") << ENDL;
|
||||
m_serial << F("FanTemp command overview: ") << ENDL;
|
||||
m_serial << HELP_CMD << F(" .: print this help message") << ENDL;
|
||||
m_serial << SHOW_CMD << F(" .: shows current temperature and fan speed") << ENDL;
|
||||
}
|
||||
|
||||
static void showState()
|
||||
{
|
||||
char floatBuffer[16];
|
||||
|
||||
sprintf(floatBuffer, "%.2f", Controller::m_adcSample);
|
||||
m_serial << F("Read ADC: ") << floatBuffer << F("\r\n");
|
||||
sprintf(floatBuffer, "%.2f", Controller::m_resistance);
|
||||
m_serial << F("Resistance: ") << floatBuffer << F("\r\n");
|
||||
sprintf(floatBuffer, "%.2f", Controller::m_temperature);
|
||||
m_serial << F("Temperature: ") << floatBuffer << F(" C \r\n");
|
||||
m_serial << F("Fan speed: ") << Controller::m_fanSpeed << F("%\r\n");
|
||||
}
|
||||
};
|
||||
|
||||
@ -98,3 +121,5 @@ template <class Uart>
|
||||
uint16_t Terminal<Uart>::m_inputSize = 0;
|
||||
|
||||
#undef ENDL
|
||||
#undef HELP_CMD
|
||||
#undef SHOW_CMD
|
||||
|
Loading…
Reference in New Issue
Block a user