-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHardware.h
126 lines (109 loc) · 3.12 KB
/
Hardware.h
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#ifndef HARDWARE_H
#define HARDWARE_H
#define HISTORY_SIZE 120
#include <ArduinoJson.h>
#include "Air.h"
#include "Fan.h"
#include "Heater.h"
#include "History.h"
#include "NTCSensor.h"
#include "Status.h"
const char * sensorNames[] = {"Heater","Air","Fan", "Power"/*, "Resin" */};
class Hardware
{
public:
Hardware()
: mHistory(sensorNames)
{
mStatus.lastTimestamp = 0;
mLastTimestampHistory = 0;
}
void runHistory(Status &status)
{
if (status.currentTimestamp - mLastTimestampHistory >= 1000)
{
HistoryEntry<float, 4> entry;
entry.timestamp = status.currentTimestamp;
entry.values[0] = status.temperatureHeater;
entry.values[1] = status.temperatureAir;
// entry.values[2] = status.temperatureResin;
entry.values[2] = status.fanSpeed;
entry.values[3] = status.powerHeater;
/* Start of critical section */
mHistory.push(entry);
/* End of critical section */
mLastTimestampHistory = status.currentTimestamp;
}
}
void run()
{
// Read the current time
mStatus.currentTimestamp = millis();
// Returns if the time elapsed from the last execution is under 100ms
if (mStatus.currentTimestamp - mStatus.lastTimestamp < 100)
return;
// Calculate the time difference in seconds
mStatus.dt = ((float)(mStatus.currentTimestamp - mStatus.lastTimestamp))/1000.0;
// Read values from the sensors
mStatus.temperatureHeater = mSensorHeater.readValue(mStatus.dt);
mStatus.temperatureAir = mSensorAir.readValue(mStatus.dt);
// mStatus.temperatureResin = mSensorResin.readValue(mStatus.dt);
// Run the various logics
mFan.run(mStatus);
mAir.run(mStatus);
mHeater.run(mStatus);
runHistory(mStatus);
// Store the current timestamp as the timestamp of the last execution
mStatus.lastTimestamp = mStatus.currentTimestamp;
}
void setHeaterOn(bool heater)
{
mStatus.heater = heater;
}
void setFanMode(fan_mode_t mode)
{
mStatus.fanMode = mode;
}
void setFanManualSpeed(float speed)
{
mStatus.fanManualSpeed = speed;
}
void setTemperature(float temperature)
{
mStatus.airSetpoint = temperature;
}
void loadFromJson(JsonDocument & json)
{
mAir.loadFromJson(json["air"]);
mHeater.loadFromJson(json["heater"]);
mFan.loadFromJson(json["fan"]);
mSensorHeater.loadFromJson(json["heater"]["sensor"]);
mSensorAir.loadFromJson(json["air"]["sensor"]);
// mSensorResin.loadFromJson(json["resin"]["sensor"]);
}
void populateHistoryJson(JsonDocument & doc, unsigned long fromTimestamp)
{
/* Start of critical section */
mHistory.populateJson(doc, fromTimestamp);
/* End of critical section */
}
void printHistoryTo(Print &stream, unsigned long fromTimestamp, char separator)
{
/* Start of critical section */
mHistory.printTo(stream, fromTimestamp, separator);
/* End of critical section */
}
const Status & getStatus() const
{
return mStatus;
}
private:
unsigned long mLastTimestampHistory;
Status mStatus;
Fan mFan;
Air mAir;
Heater mHeater;
History<float, 4, HISTORY_SIZE> mHistory;
NTCSensor mSensorHeater, mSensorAir /*, mSensorResin */;
};
#endif /* HARDWARE_H */