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
259
260
261
262
263
264
265
266
267
268
|
/*
* GpioPorts.cpp
*
* Created on: 11 Feb 2020
* Author: David
*/
#include "GpioPorts.h"
#include <GCodes/GCodeBuffer/GCodeBuffer.h>
#include <RepRap.h>
#include <Platform.h>
#if SUPPORT_CAN_EXPANSION
# include <CAN/CanInterface.h>
# include <CAN/CanMessageGenericConstructor.h>
#endif
#if SUPPORT_OBJECT_MODEL
// Object model table and functions
// Note: if using GCC version 7.3.1 20180622 and lambda functions are used in this table, you must compile this file with option -std=gnu++17.
// Otherwise the table will be allocated in RAM instead of flash, which wastes too much RAM.
// Macro to build a standard lambda function that includes the necessary type conversions
#define OBJECT_MODEL_FUNC(...) OBJECT_MODEL_FUNC_BODY(GpInputPort, __VA_ARGS__)
#define OBJECT_MODEL_FUNC_IF(_condition,...) OBJECT_MODEL_FUNC_IF_BODY(GpInputPort, _condition,__VA_ARGS__)
constexpr ObjectModelTableEntry GpInputPort::objectModelTable[] =
{
// Within each group, these entries must be in alphabetical order
// 0. sensors members
{ "configured", OBJECT_MODEL_FUNC(!self->IsUnused()), ObjectModelEntryFlags::none },
{ "value", OBJECT_MODEL_FUNC_IF(!self->IsUnused(), self->GetState()), ObjectModelEntryFlags::live },
};
constexpr uint8_t GpInputPort::objectModelTableDescriptor[] = { 1, 2 };
DEFINE_GET_OBJECT_MODEL_TABLE(GpInputPort)
#endif
bool GpInputPort::GetState() const noexcept
{
// Temporary implementation until we use interrupts to track input pin state changes
#if SUPPORT_CAN_EXPANSION
if (boardAddress != CanId::MasterAddress)
{
return currentState;
}
#endif
return port.Read();
}
// Return true if the port is not configured
bool GpInputPort::IsUnused() const noexcept
{
return
#if SUPPORT_CAN_EXPANSION
boardAddress == CanId::MasterAddress &&
#endif
!port.IsValid();
}
GCodeResult GpInputPort::Configure(uint32_t gpinNumber, GCodeBuffer &gb, const StringRef &reply)
{
if (gb.Seen('C'))
{
String<StringLength50> pinName;
gb.GetReducedString(pinName.GetRef());
// Remove any existing assignment
#if SUPPORT_CAN_EXPANSION
if (boardAddress != CanId::MasterAddress)
{
const GCodeResult rslt = CanInterface::DeleteHandle(boardAddress, handle, reply);
if (rslt != GCodeResult::ok)
{
reply.cat('\n');
const MessageType mtype = (rslt == GCodeResult::warning) ? AddWarning(gb.GetResponseMessageType()) : AddError(gb.GetResponseMessageType());
reprap.GetPlatform().Message(mtype, reply.c_str());
reply.Clear();
}
boardAddress = CanId::MasterAddress;
}
#endif
port.Release();
currentState = false;
GCodeResult rslt;
#if SUPPORT_CAN_EXPANSION
const CanAddress newBoard = IoPort::RemoveBoardAddress(pinName.GetRef());
if (newBoard != CanId::MasterAddress)
{
handle.Set(RemoteInputHandle::typeGpIn, gpinNumber, 0);
rslt = CanInterface::CreateHandle(newBoard, handle, pinName.c_str(), 0, MinimumGpinReportInterval, currentState, reply);
if (rslt == GCodeResult::ok)
{
boardAddress = newBoard;
}
else
{
currentState = false;
}
}
else
#endif
{
if (port.AssignPort(pinName.c_str(), reply, PinUsedBy::gpin, PinAccess::read))
{
currentState = port.Read();
rslt = GCodeResult::ok;
}
else
{
rslt = GCodeResult::error;
}
}
reprap.InputsUpdated();
return rslt;
}
else
{
// Report the pin details
#if SUPPORT_CAN_EXPANSION
if (boardAddress != CanId::MasterAddress)
{
const GCodeResult rslt = CanInterface::GetHandlePinName(boardAddress, handle, currentState, reply);
if (rslt != GCodeResult::ok)
{
return rslt;
}
reply.Prepend("Pin ");
}
else
#endif
{
reply.copy("Pin ");
port.AppendPinName(reply);
}
reply.catf(", active: %s", (currentState) ? "true" : "false");
}
return GCodeResult::ok;
}
GCodeResult GpOutputPort::Configure(uint32_t gpioNumber, bool isServo, GCodeBuffer &gb, const StringRef &reply)
{
PwmFrequency freq = 0;
const bool seenFreq = gb.Seen('Q');
if (seenFreq)
{
freq = gb.GetPwmFrequency();
}
if (gb.Seen('C'))
{
String<StringLength50> pinName;
gb.GetReducedString(pinName.GetRef());
// Remove any existing assignment
#if SUPPORT_CAN_EXPANSION
if (boardAddress != CanId::MasterAddress)
{
CanMessageGenericConstructor cons(M950GpioParams);
cons.AddUParam('P', gpioNumber);
cons.AddStringParam('C', NoPinName);
if (cons.SendAndGetResponse(CanMessageType::m950Gpio, boardAddress, reply) != GCodeResult::ok)
{
reprap.GetPlatform().Message(WarningMessage, reply.c_str());
reply.Clear();
}
boardAddress = CanId::MasterAddress;
}
#endif
port.Release();
if (!seenFreq)
{
freq = (isServo) ? ServoRefreshFrequency : DefaultPinWritePwmFreq;
}
GCodeResult rslt;
#if SUPPORT_CAN_EXPANSION
boardAddress = IoPort::RemoveBoardAddress(pinName.GetRef());
if (boardAddress != CanId::MasterAddress)
{
CanMessageGenericConstructor cons(M950GpioParams);
cons.AddUParam('P', gpioNumber);
cons.AddUParam('Q', freq);
cons.AddUParam('S', (isServo) ? 1 : 0);
cons.AddStringParam('C', pinName.c_str());
rslt = cons.SendAndGetResponse(CanMessageType::m950Gpio, boardAddress, reply);
}
else
#endif
{
if (port.AssignPort(pinName.c_str(), reply, PinUsedBy::gpout, (isServo) ? PinAccess::servo : PinAccess::pwm))
{
rslt = GCodeResult::ok;
port.SetFrequency(freq);
}
else
{
rslt = GCodeResult::error;
}
}
// GPOut pins are not yet in the object model
// reprap.OutputsUpdated();
return rslt;
}
else if (seenFreq)
{
#if SUPPORT_CAN_EXPANSION
if (boardAddress != CanId::MasterAddress)
{
CanMessageGenericConstructor cons(M950GpioParams);
cons.AddUParam('P', gpioNumber);
cons.AddUParam('Q', freq);
// reprap.OutputsUpdated();
return cons.SendAndGetResponse(CanMessageType::m950Gpio, boardAddress, reply);
}
#endif
port.SetFrequency(freq);
// reprap.OutputsUpdated();
}
else
{
#if SUPPORT_CAN_EXPANSION
if (boardAddress != CanId::MasterAddress)
{
CanMessageGenericConstructor cons(M950GpioParams);
cons.AddUParam('P', gpioNumber);
return cons.SendAndGetResponse(CanMessageType::m950Gpio, boardAddress, reply);
}
#endif
reply.printf("GPIO/servo port %" PRIu32, gpioNumber);
port.AppendDetails(reply);
}
return GCodeResult::ok;
}
GCodeResult GpOutputPort::WriteAnalog(uint32_t gpioPortNumber, bool isServo, float pwm, const GCodeBuffer& gb, const StringRef& reply) const noexcept
{
#if SUPPORT_CAN_EXPANSION
if (boardAddress != CanId::MasterAddress)
{
return CanInterface::WriteGpio(boardAddress, gpioPortNumber, pwm, isServo, gb, reply);
}
#endif
port.WriteAnalog(pwm);
return GCodeResult::ok;
}
#ifdef PCCB
// Function used to assign default GPOUT devices
void GpOutputPort::Assign(const char *pinName) noexcept
{
String<1> dummy;
port.AssignPort(pinName, dummy.GetRef(), PinUsedBy::gpout, PinAccess::pwm);
}
#endif
// End
|