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
127
128
129
130
131
132
133
134
135
|
#include <Heating/Sensors/CurrentLoopTemperatureSensor.h>
#include "TemperatureSensor.h"
#include "Thermistor.h"
#include "ThermocoupleSensor31855.h"
#include "ThermocoupleSensor31856.h"
#include "RtdSensor31865.h"
#include "GCodes/GCodeBuffer.h"
#if HAS_CPU_TEMP_SENSOR
#include "CpuTemperatureSensor.h"
#endif
#if SUPPORT_DHT_SENSOR
#include "DhtSensor.h"
#endif
#if HAS_SMART_DRIVERS
#include "TmcDriverTemperatureSensor.h"
#endif
// Constructor
TemperatureSensor::TemperatureSensor(unsigned int chan, const char *t) : sensorChannel(chan), sensorType(t), heaterName(nullptr) {}
// Virtual destructor
TemperatureSensor::~TemperatureSensor()
{
delete heaterName;
}
// Set the name - normally called only once, so we allow heap memory to be allocated
void TemperatureSensor::SetHeaterName(const char *newName)
{
// Change the heater name in a thread-safe manner
const char *oldName = heaterName;
heaterName = nullptr;
delete oldName;
if (newName != nullptr && strlen(newName) != 0)
{
char * const temp = new char[strlen(newName) + 1];
strcpy(temp, newName);
heaterName = temp;
}
}
// Default implementation of Configure, for sensors that have no configurable parameters
bool TemperatureSensor::Configure(unsigned int mCode, unsigned int heater, GCodeBuffer& gb, StringRef& reply, bool& error)
{
bool seen = false;
if (mCode == 305)
{
TryConfigureHeaterName(gb, seen);
if (!seen && !gb.Seen('X'))
{
// No parameters provided, so report the current configuration
CopyBasicHeaterDetails(heater, reply);
}
}
return seen;
}
void TemperatureSensor::CopyBasicHeaterDetails(unsigned int heater, StringRef& reply) const
{
reply.printf("Heater %u", heater);
if (heaterName != nullptr)
{
reply.catf(" (%s)", heaterName);
}
reply.catf(" uses %s sensor channel %u", sensorType, sensorChannel);
}
// Configure then heater name, if it is provided
void TemperatureSensor::TryConfigureHeaterName(GCodeBuffer& gb, bool& seen)
{
String<MaxHeaterNameLength> buf;
bool localSeen = false;
gb.TryGetQuotedString('S', buf.GetRef(), localSeen);
if (localSeen)
{
SetHeaterName(buf.c_str());
seen = true;
}
}
// Factory method
TemperatureSensor *TemperatureSensor::Create(unsigned int channel)
{
TemperatureSensor *ts = nullptr;
if (channel < Heaters)
{
ts = new Thermistor(channel);
}
else if (FirstMax31855ThermocoupleChannel <= channel && channel < FirstMax31855ThermocoupleChannel + MaxSpiTempSensors)
{
ts = new ThermocoupleSensor31855(channel);
}
else if (FirstMax31856ThermocoupleChannel <= channel && channel < FirstMax31856ThermocoupleChannel + MaxSpiTempSensors)
{
ts = new ThermocoupleSensor31856(channel);
}
else if (FirstRtdChannel <= channel && channel < FirstRtdChannel + MaxSpiTempSensors)
{
ts = new RtdSensor31865(channel);
}
else if (FirstLinearAdcChannel <= channel && channel < FirstLinearAdcChannel + MaxSpiTempSensors)
{
ts = new CurrentLoopTemperatureSensor(channel);
}
#if SUPPORT_DHT_SENSOR
else if (channel == DhtTemperatureChannel || channel == DhtHumidityChannel)
{
ts = new DhtSensor(channel);
}
#endif
#if HAS_CPU_TEMP_SENSOR
else if (channel == CpuTemperatureSenseChannel)
{
ts = new CpuTemperatureSensor(channel);
}
#endif
#if HAS_SMART_DRIVERS
else if (channel >= FirstTmcDriversSenseChannel && channel < FirstTmcDriversSenseChannel + 2)
{
ts = new TmcDriverTemperatureSensor(channel);
}
#endif
if (ts != nullptr)
{
ts->Init();
}
return ts;
}
// End
|