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
|
/*
* RemoteFan.cpp
*
* Created on: 3 Sep 2019
* Author: David
*/
#include "RemoteFan.h"
#if SUPPORT_CAN_EXPANSION
#include "CanMessageBuffer.h"
#include "CAN/CanInterface.h"
#include "CAN/CanMessageGenericConstructor.h"
RemoteFan::RemoteFan(unsigned int fanNum, CanAddress boardNum) noexcept
: Fan(fanNum),
lastRpm(-1), lastPwm(-1.0), whenLastReportReceived(0),
boardNumber(boardNum)
{
}
RemoteFan::~RemoteFan() noexcept
{
CanMessageGenericConstructor cons(M950FanParams);
try
{
cons.AddUParam('F', fanNumber);
cons.AddStringParam('C', "nil");
String<1> dummy;
(void)cons.SendAndGetResponse(CanMessageType::m950Fan, boardNumber, dummy.GetRef());
}
catch (...)
{
}
}
bool RemoteFan::Check(bool checkSensors) noexcept
{
if (millis() - whenLastReportReceived > FanReportTimeout)
{
lastRpm = -1;
lastPwm = -1.0;
}
return sensorsMonitored.IsNonEmpty() && lastPwm > 0.0;
}
bool RemoteFan::IsEnabled() const noexcept
{
return true;
}
GCodeResult RemoteFan::SetPwmFrequency(PwmFrequency freq, const StringRef& reply) noexcept
{
CanMessageGenericConstructor cons(M950FanParams);
cons.AddUParam('F', fanNumber);
cons.AddUParam('Q', freq);
return cons.SendAndGetResponse(CanMessageType::m950Fan, boardNumber, reply);
}
void RemoteFan::UpdateFromRemote(CanAddress src, const FanReport& report) noexcept
{
if (src == boardNumber)
{
lastPwm = (report.actualPwm < 0) ? -1.0 : (float)report.actualPwm * (1.0/65535);
lastRpm = report.rpm;
whenLastReportReceived = millis();
}
}
GCodeResult RemoteFan::ReportPortDetails(const StringRef& str) const noexcept
{
CanMessageGenericConstructor cons(M950FanParams);
cons.AddUParam('F', fanNumber);
return cons.SendAndGetResponse(CanMessageType::m950Fan, boardNumber, str);
}
bool RemoteFan::UpdateFanConfiguration(const StringRef& reply) noexcept
{
CanMessageBuffer *buf = CanMessageBuffer::Allocate();
if (buf == nullptr)
{
reply.copy("No CAN buffer available");
return false;
}
const CanRequestId rid = CanInterface::AllocateRequestId(boardNumber);
auto msg = buf->SetupRequestMessage<CanMessageFanParameters>(rid, CanInterface::GetCanAddress(), boardNumber);
msg->fanNumber = fanNumber;
msg->blipTime = blipTime;
msg->val = val;
msg->minVal = minVal;
msg->maxVal = maxVal;
msg->triggerTemperatures[0] = triggerTemperatures[0];
msg->triggerTemperatures[1] = triggerTemperatures[1];
msg->sensorsMonitored = sensorsMonitored.GetRaw();
return CanInterface::SendRequestAndGetStandardReply(buf, rid, reply) == GCodeResult::ok;
}
// Update the hardware PWM
GCodeResult RemoteFan::Refresh(const StringRef& reply) noexcept
{
CanMessageBuffer *buf = CanMessageBuffer::Allocate();
if (buf == nullptr)
{
reply.copy("No CAN buffer available");
return GCodeResult::error;
}
const CanRequestId rid = CanInterface::AllocateRequestId(boardNumber);
auto msg = buf->SetupRequestMessage<CanMessageSetFanSpeed>(rid, CanInterface::GetCanAddress(), boardNumber);
msg->fanNumber = fanNumber;
msg->pwm = val;
return CanInterface::SendRequestAndGetStandardReply(buf, rid, reply);
}
GCodeResult RemoteFan::ConfigurePort(const char* pinNames, PwmFrequency freq, const StringRef& reply) noexcept
{
CanMessageGenericConstructor cons(M950FanParams);
cons.AddUParam('F', fanNumber);
cons.AddUParam('Q', freq);
cons.AddStringParam('C', pinNames);
return cons.SendAndGetResponse(CanMessageType::m950Fan, boardNumber, reply);
}
#endif
// End
|