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

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

#include "RepRapFirmware.h"

void Fan::Init(Pin p_pin, bool hwInverted)
{
	val = 0.0;
	minVal = 0.1;				// 10% minimum fan speed
	blipTime = 100;				// 100ms fan blip
	blipStartTime = 0;
	freq = DefaultFanPwmFreq;
	pin = p_pin;
	hardwareInverted = hwInverted;
	inverted = false;
	heatersMonitored = 0;
	triggerTemperature = HOT_END_FAN_TEMPERATURE;
	Refresh();
}

void Fan::SetValue(float speed)
{
	if (speed > 1.0)
	{
		speed /= 255.0;
	}
	const float newVal = (speed > 0.0) ? constrain<float>(speed, minVal, 1.0) : 0.0;
	if (val == 0.0 && newVal < 1.0 && blipTime != 0)
	{
		// Starting the fan from standstill, so blip the fan
		blipStartTime = millis();
	}
	val = newVal;
	Refresh();
}

void Fan::SetMinValue(float speed)
{
	if (speed > 1.0)
	{
		speed /= 255.0;
	}
	minVal = constrain<float>(speed, 0.0, 1.0);
	Refresh();
}

void Fan::SetBlipTime(float t)
{
	blipTime = (uint32_t)(max<float>(t, 0.0) * SecondsToMillis);
}

void Fan::SetInverted(bool inv)
{
	inverted = inv;
	Refresh();
}

void Fan::SetHardwarePwm(float pwmVal)
{
	if (pin >= 0)
	{
		bool invert = hardwareInverted;
		if (inverted)
		{
			invert = !invert;
		}
		AnalogOut(pin, (invert) ? (1.0 - pwmVal) : pwmVal, freq);
	}
}

void Fan::SetPwmFrequency(float p_freq)
{
	freq = (uint16_t)constrain<float>(p_freq, 1.0, 65535.0);
	Refresh();
}

void Fan::SetHeatersMonitored(uint16_t h)
{
	heatersMonitored = h;
	Refresh();
}

void Fan::Refresh()
{
	float reqVal = (heatersMonitored == 0)
					? val
					: (reprap.GetPlatform()->AnyHeaterHot(heatersMonitored, triggerTemperature))
						? max<float>(0.5, val)			// make sure that thermostatic fans always run at 50% speed or more
						: 0.0;
	if (reqVal > 0.0 && millis() - blipStartTime < blipTime)
	{
		SetHardwarePwm(1.0);
	}
	else if (reqVal > 0.0 && reqVal < minVal)
	{
		SetHardwarePwm(minVal);
	}
	else
	{
		SetHardwarePwm(reqVal);
	}
}

void Fan::Check()
{
	if (heatersMonitored != 0 || blipTime != 0)
	{
		Refresh();
	}
}

// End