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
|
#include <Heating/Sensors/CurrentLoopTemperatureSensor.h>
#include "TemperatureSensor.h"
#include "Thermistor.h"
#include "ThermocoupleSensor31855.h"
#include "RtdSensor31865.h"
#include "GCodes/GCodeBuffer.h"
#ifndef __RADDS__
#include "CpuTemperatureSensor.h"
#endif
#ifdef DUET_NG
#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
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)
{
char *temp = new char[strlen(newName)];
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)
{
char buf[MaxHeaterNameLength + 1];
bool localSeen = false;
gb.TryGetQuotedString('H', buf, ARRAY_SIZE(buf), localSeen);
if (localSeen)
{
SetHeaterName(buf);
seen = true;
}
}
// Factory method
TemperatureSensor *TemperatureSensor::Create(unsigned int channel)
{
TemperatureSensor *ts = nullptr;
if (channel < Heaters)
{
ts = new Thermistor(channel);
}
else if (FirstThermocoupleChannel <= channel && channel < FirstThermocoupleChannel + MaxSpiTempSensors)
{
ts = new ThermocoupleSensor31855(channel);
}
else if (FirstRtdChannel <= channel && channel < FirstRtdChannel + MaxSpiTempSensors)
{
ts = new RtdSensor31865(channel);
}
else if (FirstLinearAdcChannel <= channel && channel < FirstLinearAdcChannel + MaxSpiTempSensors)
{
ts = new CurrentLoopTemperatureSensor(channel);
}
#ifndef __RADDS__
else if (channel == CpuTemperatureSenseChannel)
{
ts = new CpuTemperatureSensor(channel);
}
#endif
#ifdef DUET_NG
else if (channel >= FirstTmcDriversSenseChannel && channel < FirstTmcDriversSenseChannel + 2)
{
ts = new TmcDriverTemperatureSensor(channel);
}
#endif
if (ts != nullptr)
{
ts->Init();
}
return ts;
}
// End
|