Welcome to mirror list, hosted at ThFree Co, Russian Federation.

FansManager.cpp « Fans « src - github.com/Duet3D/RepRapFirmware.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7aa484b3e831fd17d5899dfb69376c4072c42b48 (plain)
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
 * FansManager.cpp
 *
 *  Created on: 3 Sep 2019
 *      Author: David
 */

#include "FansManager.h"

#include "LocalFan.h"
#include "RemoteFan.h"
#include "GCodes/GCodeBuffer/GCodeBuffer.h"

#if SUPPORT_CAN_EXPANSION
# include <CanMessageFormats.h>
#endif

#include <utility>

FansManager::FansManager() noexcept
{
	for (Fan*& f : fans)
	{
		f = nullptr;
	}
}

// Retrieve the pointer to a fan, or nullptr if it doesn't exist.
// Lock the fan system before calling this, so that the fan can't be deleted while we are accessing it.
ReadLockedPointer<Fan> FansManager::FindFan(size_t fanNum) const noexcept
{
	ReadLocker locker(fansLock);
	return ReadLockedPointer<Fan>(locker, (fanNum < ARRAY_SIZE(fans)) ? fans[fanNum] : nullptr);
}

// Create and return a local fan. if it fails, return nullptr with the error message in 'reply'.
LocalFan *FansManager::CreateLocalFan(uint32_t fanNum, const char *pinNames, PwmFrequency freq, const StringRef& reply) noexcept
{
	LocalFan *newFan = new LocalFan(fanNum);
	if (!newFan->AssignPorts(pinNames, reply))
	{
		delete newFan;
		return nullptr;
	}
	(void)newFan->SetPwmFrequency(freq, reply);
	return newFan;
}

// Check and if necessary update all fans. Return true if a thermostatic fan is running.
bool FansManager::CheckFans() noexcept
{
	ReadLocker lock(fansLock);
	bool thermostaticFanRunning = false;
	for (Fan* fan : fans)
	{
		if (fan != nullptr && fan->Check())
		{
			thermostaticFanRunning = true;
		}
	}
	return thermostaticFanRunning;
}

// Return the highest used fan number. Used by RepRap.cpp to shorten responses by omitting unused trailing fan numbers. If no fans are configured, return 0.
size_t FansManager::GetHighestUsedFanNumber() const noexcept
{
	size_t highestFan = ARRAY_SIZE(fans);
	do
	{
		--highestFan;
	} while (fans[highestFan] == nullptr && highestFan != 0);
	return highestFan;
}

#if HAS_MASS_STORAGE

bool FansManager::WriteFanSettings(FileStore *f) const noexcept
{
	ReadLocker lock(fansLock);
	bool ok = true;
	for (size_t fanNum = 0; ok && fanNum < MaxFans; ++fanNum)
	{
		ok = fans[fanNum] == nullptr || fans[fanNum]->WriteSettings(f, fanNum);
	}
	return ok;
}

#endif

// This is called by M950 to create a fan or change its PWM frequency
GCodeResult FansManager::ConfigureFanPort(uint32_t fanNum, GCodeBuffer& gb, const StringRef& reply) noexcept
{
	if (fanNum < MaxFans)
	{
		const bool seenPin = gb.Seen('C');
		if (seenPin)
		{
			String<StringLength50> pinName;
			if (!gb.GetReducedString(pinName.GetRef()))
			{
				reply.copy("Missing pin name");
				return GCodeResult::error;
			}

			WriteLocker lock(fansLock);

			Fan *oldFan = nullptr;
			std::swap(oldFan, fans[fanNum]);
			delete oldFan;

			const PwmFrequency freq = (gb.Seen('Q')) ? gb.GetPwmFrequency() : DefaultFanPwmFreq;

#if SUPPORT_CAN_EXPANSION
			const CanAddress board = IoPort::RemoveBoardAddress(pinName.GetRef());
			if (board != CanId::MasterAddress)
			{
				auto *newFan = new RemoteFan(fanNum, board);
				const GCodeResult rslt = newFan->ConfigurePort(pinName.c_str(), freq, reply);
				if (rslt == GCodeResult::ok)
				{
					fans[fanNum] = newFan;
				}
				else
				{
					delete newFan;
				}
				return rslt;
			}
#endif
			fans[fanNum] = CreateLocalFan(fanNum, pinName.c_str(), freq, reply);
			return (fans[fanNum] == nullptr) ? GCodeResult::error : GCodeResult::ok;
		}

		const auto fan = FindFan(fanNum);
		if (fan.IsNull())
		{
			reply.printf("Fan %u does not exist", (unsigned int)fanNum);
			return GCodeResult::error;
		}

		if (gb.Seen('Q'))
		{
			return fan->SetPwmFrequency(gb.GetPwmFrequency(), reply);
		}

		return fan->ReportPortDetails(reply);
	}

	reply.copy("Fan number out of range");
	return GCodeResult::error;
}

// Set or report the parameters for the specified fan
// If 'mCode' is an M-code used to set parameters for the current kinematics (which should only ever be 106 or 107)
// then search for parameters used to configure the fan. If any are found, perform appropriate actions and return true.
// If errors were discovered while processing parameters, put an appropriate error message in 'reply' and set 'error' to true.
// If no relevant parameters are found, print the existing ones to 'reply' and return false.
bool FansManager::ConfigureFan(unsigned int mcode, size_t fanNum, GCodeBuffer& gb, const StringRef& reply, bool& error) noexcept
{
	auto fan = FindFan(fanNum);
	if (fan.IsNull())
	{
		reply.printf("Fan number %u not found", fanNum);
		error = true;
		return false;
	}

	return fan->Configure(mcode, fanNum, gb, reply, error);
}

float FansManager::GetFanValue(size_t fanNum) const noexcept
{
	auto fan = FindFan(fanNum);
	return (fan.IsNull()) ? -1 : fan->GetConfiguredPwm();
}

GCodeResult FansManager::SetFanValue(size_t fanNum, float speed, const StringRef& reply) noexcept
{
	auto fan = FindFan(fanNum);
	if (fan.IsNotNull())
	{
		return fan->SetPwm(speed, reply);
	}
	reply.printf("Fan number %u not found", fanNum);
	return GCodeResult::error;
}

void FansManager::SetFanValue(size_t fanNum, float speed) noexcept
{
	String<1> dummy;
	(void)SetFanValue(fanNum, speed, dummy.GetRef());
}

// Check if the given fan can be controlled manually so that DWC can decide whether or not to show the corresponding fan
// controls. This is the case if no thermostatic control is enabled and if the fan was configured at least once before.
bool FansManager::IsFanControllable(size_t fanNum) const noexcept
{
	auto fan = FindFan(fanNum);
	return fan.IsNotNull() && !fan->HasMonitoredSensors() && fan->IsConfigured();
}

// Return the fan's name
const char *FansManager::GetFanName(size_t fanNum) const noexcept
{
	auto fan = FindFan(fanNum);
	return (fan.IsNull()) ? "" : fan->GetName();
}

// Get current fan RPM, or -1 if the fan is invalid or doesn't have a tacho pin
int32_t FansManager::GetFanRPM(size_t fanNum) const noexcept
{
	auto fan = FindFan(fanNum);
	return (fan.IsNull()) ? -1 : fan->GetRPM();
}

// Initialise fans. Call this only once, and only during initialisation.
void FansManager::Init() noexcept
{
#if ALLOCATE_DEFAULT_PORTS
	for (size_t i = 0; i < ARRAY_SIZE(DefaultFanPinNames); ++i)
	{
		String<1> dummy;
		fans[i] = CreateLocalFan(i, DefaultFanPinNames[i], i < ARRAY_SIZE(DefaultFanPwmFrequencies) ? DefaultFanPwmFrequencies[i] : DefaultFanPwmFreq, dummy.GetRef());
	}

# if defined(PCCB)
	// Fan 3 needs to be set explicitly to zero PWM, otherwise it turns on because the MCU output pin isn't set low
	if (fans[3] != nullptr)
	{
		String<1> dummy;
		(void)fans[3]->SetPwm(0.0, dummy.GetRef());
	}
# endif
#endif
}

#if SUPPORT_CAN_EXPANSION

void FansManager::ProcessRemoteFanRpms(CanAddress src, const CanMessageFanRpms& msg) noexcept
{
	size_t numFansProcessed = 0;
	uint64_t whichFans = msg.whichFans;
	while (whichFans != 0)
	{
		const unsigned int fanNum = LowestSetBit(whichFans);
		auto fan = FindFan(fanNum);
		if (fan.IsNotNull())
		{
			fan->UpdateRpmFromRemote(src, msg.fanRpms[numFansProcessed]);
		}
		++numFansProcessed;
		whichFans &= ~((uint64_t)1 << fanNum);
	}
}

#endif

// End