forked from coders-school/fan_controller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathController.cpp
63 lines (55 loc) · 1.7 KB
/
Controller.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "Controller.hpp"
#include <iostream>
#include <iomanip>
#include <sstream>
Controller::Controller(const Thermometer& _thermometer,
const Fan& _fan,
double target,
double _tolerance,
const std::shared_ptr<LcdDisplay>& display_ptr) :
thermometer(_thermometer.clone()),
fan(_fan.clone()),
targetTemperature(target),
tolerance(_tolerance),
lcdDisplay(display_ptr),
actualTemp(-1)
{}
void Controller::updateRpm()
{
actualTemp = thermometer->getTemperature();
auto differenceTemp = std::abs(actualTemp - targetTemperature);
fan->enable();
if(differenceTemp > tolerance)
{
if(targetTemperature > actualTemp)
{
fan->disable();
}
else
{
fan->setSpeed(1000+differenceTemp * 1000);
}
}
}
std::string Controller::printInfo() const
{
std::ostringstream oss;
oss << std::fixed << std::setprecision(2) << actualTemp;
std::string actualTempSTR = oss.str();
oss.clear();
oss.str("");
oss << std::fixed << std::setprecision(2) << targetTemperature;
std::string targetTemperatureSTR = oss.str();
const std::string output =
"\nActual Temp: " + actualTempSTR +
"\nTarget Temp: " + targetTemperatureSTR +
"\nFan speed: " + std::to_string(fan->getSpeed()) + "\n";
return output;
}
void Controller::displayInfo() const
{
if(lcdDisplay)
{
lcdDisplay->print(printInfo());
}
}