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
|
/*
* Fan.cpp
*
* Created on: 29 Jun 2016
* Author: David
*/
#include "Fan.h"
#include "Platform.h"
#include "RepRap.h"
void Fan::Init(Pin p_pin, bool hwInverted)
{
val = 0.0;
minVal = 0.1; // 10% minimum fan speed
blipTime = 100; // 100ms fan blip
freq = DefaultFanPwmFreq;
pin = p_pin;
hardwareInverted = hwInverted;
inverted = blipping = false;
heatersMonitored = 0;
triggerTemperature = HOT_END_FAN_TEMPERATURE;
lastPwm = -1.0;
Refresh();
}
void Fan::SetValue(float speed)
{
if (speed > 1.0)
{
speed /= 255.0;
}
const float newVal = constrain<float>(speed, 0.0, 1.0);
if (val == 0.0 && newVal > 0.0 && newVal < 1.0 && blipTime != 0)
{
// Starting the fan from standstill, so blip the fan
blipping = true;
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 != NoPin)
{
bool invert = hardwareInverted;
if (inverted)
{
invert = !invert;
}
if (invert)
{
pwmVal = 1.0 - pwmVal;
}
// Only set the PWM if it has changed, to avoid a lot of I2C traffic when we have a DueX5 connected
if (pwmVal != lastPwm)
{
lastPwm = pwmVal;
Platform::WriteAnalog(pin, 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)
{
if (reqVal < minVal)
{
reqVal = minVal;
}
if (blipping)
{
if (millis() - blipStartTime < blipTime)
{
reqVal = 1.0;
}
else
{
blipping = false;
}
}
}
SetHardwarePwm(reqVal);
}
void Fan::Check()
{
if (heatersMonitored != 0 || blipping)
{
Refresh();
}
}
void Fan::Disable()
{
if (pin != NoPin)
{
inverted = false;
SetHardwarePwm(0.0);
}
pin = NoPin;
}
// End
|