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

Fan.cpp « Fans « src - github.com/Duet3D/RepRapFirmware.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c45f602c181c68ab43656c301e11ae870f5def04 (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
/*
 * Fan.cpp
 *
 *  Created on: 29 Jun 2016
 *      Author: David
 */

#include "Fan.h"
#include "GCodes/GCodeBuffer/GCodeBuffer.h"

Fan::Fan(unsigned int fanNum) noexcept
	: fanNumber(fanNum),
	  val(0.0), lastVal(0.0),
	  minVal(DefaultMinFanPwm),
	  maxVal(1.0),										// 100% maximum fan speed
	  blipTime(DefaultFanBlipTime),
	  sensorsMonitored(0),
	  isConfigured(false)
{
	triggerTemperatures[0] = triggerTemperatures[1] = DefaultHotEndFanTemperature;
}

// Set or report the parameters for this fan
// If 'mcode' is an M-code used to set parameters for the f (which should only ever be 106)
// 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.
// Exceptions:
// 1. Only process the S parameter if other values were processed.
// 2. Don't process the R parameter, but if it is present don't print the existing configuration.
bool Fan::Configure(unsigned int mcode, size_t fanNum, GCodeBuffer& gb, const StringRef& reply, bool& error) noexcept
{
	bool seen = false;
	if (mcode == 106)
	{
		// Check that the fan is enabled
		if (!IsEnabled())
		{
			reply.printf("Fan %u is disabled", fanNum);
			error = true;
			return true;											// say we have processed it
		}

		if (gb.Seen('T'))
		{
			seen = true;
			size_t numTemps = 2;
			gb.GetFloatArray(triggerTemperatures, numTemps, true);
		}

		if (gb.Seen('B'))										// Set blip time
		{
			seen = true;
			blipTime = (uint32_t)(max<float>(gb.GetFValue(), 0.0) * SecondsToMillis);
		}

		if (gb.Seen('L'))		// Set minimum speed
		{
			seen = true;
			float speed = gb.GetFValue();
			if (speed > 1.0)
			{
				speed /= 255.0;
			}
			minVal = constrain<float>(speed, 0.0, maxVal);
		}

		if (gb.Seen('X'))		// Set maximum speed
		{
			seen = true;
			float speed = gb.GetFValue();
			if (speed > 1.0)
			{
				speed /= 255.0;
			}
			maxVal = constrain<float>(speed, minVal, 1.0);
		}

		if (gb.Seen('H'))		// Set thermostatically-controlled sensors
		{
			seen = true;
			int32_t sensors[MaxSensors];			// signed because we use H-1 to disable thermostatic mode
			size_t numH = ARRAY_SIZE(sensors);
			gb.GetIntArray(sensors, numH, false);

			// Note that M106 H-1 disables thermostatic mode. The following code implements that automatically.
			sensorsMonitored = 0;
			for (size_t h = 0; h < numH; ++h)
			{
				const int hnum = sensors[h];
				if (hnum >= 0)
				{
					if (hnum < (int)MaxSensors)
					{
						SetBit(sensorsMonitored, (unsigned int)hnum);
					}
					else
					{
						reply.copy("Sensor number out of range");
						error = true;
					}
				}
			}
			if (sensorsMonitored != 0)
			{
				val = 1.0;					// default the fan speed to full for safety
			}
		}

		if (gb.Seen('C') && gb.GetQuotedString(name.GetRef()))
		{
			seen = true;
		}

		if (seen)
		{
			isConfigured = true;

			// We only act on the 'S' parameter here if we have processed other parameters
			if (seen && gb.Seen('S'))		// Set new fan value - process this after processing 'H' or it may not be acted on
			{
				val = gb.GetPwmValue();
			}

			if (!UpdateFanConfiguration(reply))
			{
				error = true;
			}
		}
		else if (!gb.Seen('R') && !gb.Seen('S'))
		{
			// Report the configuration of the specified fan
			reply.printf("Fan %u", fanNum);
			if (name.strlen() != 0)
			{
				reply.catf(" (%s)", name.c_str());
			}
			reply.catf(", speed: %d%%, min: %d%%, max: %d%%, blip: %.2f",
						(int)(val * 100.0),
						(int)(minVal * 100.0),
						(int)(maxVal * 100.0),
						(double)(blipTime * MillisToSeconds)
					  );
			if (sensorsMonitored != 0)
			{
				reply.catf(", temperature: %.1f:%.1fC, sensors:", (double)triggerTemperatures[0], (double)triggerTemperatures[1]);
				for (unsigned int i = 0; i < MaxSensors; ++i)
				{
					if (IsBitSet(sensorsMonitored, i))
					{
						reply.catf(" %u", i);
					}
				}
				reply.catf(", current speed: %d%%:", (int)(lastVal * 100.0));
			}
		}
	}

	return seen;
}

// Set the PWM. 'speed' is in the interval 0.0..1.0.
GCodeResult Fan::SetPwm(float speed, const StringRef& reply) noexcept
{
	val = speed;
	return Refresh(reply);
}

#if HAS_MASS_STORAGE

// Save the settings of this fan if it isn't thermostatic
bool Fan::WriteSettings(FileStore *f, size_t fanNum) const noexcept
{
	if (sensorsMonitored == 0)
	{
		String<StringLength20> fanCommand;
		fanCommand.printf("M106 P%u S%.2f\n", fanNum, (double)val);
		return f->Write(fanCommand.c_str());
	}
	return true;
}

#endif

// End