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

GCodeMachineState.cpp « GCodes « src - github.com/Duet3D/RepRapFirmware.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6c4730f3157458728c440a5526181e9933a78a16 (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
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
/*
 * GCodeMachineState.cpp
 *
 *  Created on: 15 Nov 2016
 *      Author: David
 */

#include "GCodeMachineState.h"
#include <Platform/RepRap.h>

#include <limits>

// Create a default initialised GCodeMachineState
GCodeMachineState::GCodeMachineState() noexcept
	: feedRate(ConvertSpeedFromMmPerMin(DefaultFeedRate)),
#if HAS_LINUX_INTERFACE
	  fileId(NoFileId),
#endif
	  lineNumber(0),
	  selectedPlane(0), drivesRelative(false), axesRelative(false),
	  doingFileMacro(false), waitWhileCooling(false), runningM501(false), runningM502(false),
	  volumetricExtrusion(false), g53Active(false), runningSystemMacro(false), usingInches(false),
	  waitingForAcknowledgement(false), messageAcknowledged(false), localPush(false),
#if HAS_LINUX_INTERFACE
	  lastCodeFromSbc(false), macroStartedByCode(false), fileFinished(false),
#endif
	  compatibility(Compatibility::RepRapFirmware),
	  previous(nullptr), errorMessage(nullptr),
	  blockNesting(0), state(GCodeState::normal), stateMachineResult(GCodeResult::ok)
{
	blockStates[0].SetPlainBlock(0);
}

// Copy constructor. This chains the new one to the previous one.
GCodeMachineState::GCodeMachineState(GCodeMachineState& prev, bool withinSameFile) noexcept
	: feedRate(prev.feedRate),
#if HAS_MASS_STORAGE
	  fileState(prev.fileState),
#endif
#if HAS_LINUX_INTERFACE
	  fileId(prev.fileId),
#endif
	  lockedResources(prev.lockedResources),
	  lineNumber(0),
	  selectedPlane(prev.selectedPlane), drivesRelative(prev.drivesRelative), axesRelative(prev.axesRelative),
	  doingFileMacro(prev.doingFileMacro), waitWhileCooling(prev.waitWhileCooling), runningM501(prev.runningM501), runningM502(prev.runningM502),
	  volumetricExtrusion(false), g53Active(false), runningSystemMacro(prev.runningSystemMacro), usingInches(prev.usingInches),
	  waitingForAcknowledgement(false), messageAcknowledged(false), localPush(withinSameFile),
#if HAS_LINUX_INTERFACE
	  lastCodeFromSbc(prev.lastCodeFromSbc), macroStartedByCode(prev.macroStartedByCode), fileFinished(prev.fileFinished),
#endif
	  compatibility(prev.compatibility),
	  previous(&prev), errorMessage(nullptr),
	  blockNesting((withinSameFile) ? prev.blockNesting : 0), state(GCodeState::normal), stateMachineResult(GCodeResult::ok)
{
	if (withinSameFile)
	{
		for (size_t i = 0; i <= blockNesting; ++i)
		{
			blockStates[i] = prev.blockStates[i];
		}
	}
	else
	{
		blockStates[0].SetPlainBlock(0);
	}
}

GCodeMachineState::~GCodeMachineState() noexcept
{
#if HAS_MASS_STORAGE
	fileState.Close();
#endif
}

#if HAS_LINUX_INTERFACE

// Set the state to indicate a file is being processed
void GCodeMachineState::SetFileExecuting() noexcept
{
#if HAS_MASS_STORAGE
	if (!fileState.IsLive())
#endif
	{
		fileId = (GetPrevious() != nullptr ? GetPrevious()->fileId : NoFileId) + 1;
		if (fileId == NoFileId)
		{
			// In case the ID overlapped increase it once more (should never happen)
			fileId++;
		}
		fileFinished = false;
	}
}

#endif

// Return true if we are reading GCode commands from a file or macro
bool GCodeMachineState::DoingFile() const noexcept
{
#if HAS_LINUX_INTERFACE
	if (reprap.UsingLinuxInterface() && fileId != NoFileId)
	{
		return true;
	}
#endif
#if HAS_MASS_STORAGE
	return fileState.IsLive();
#else
	return false;
#endif
}

// Close the currently executing file
void GCodeMachineState::CloseFile() noexcept
{
#if HAS_LINUX_INTERFACE
	if (reprap.UsingLinuxInterface())
	{
		if (fileId != NoFileId)
		{
			const FileId lastFileId = fileId;
			for (GCodeMachineState *ms = this; ms != nullptr; ms = ms->GetPrevious())
			{
				if (ms->fileId == lastFileId)
				{
					ms->fileId = NoFileId;
					ms->fileFinished = false;
				}
			}
		}
	}
	else
#endif
	{
#if HAS_MASS_STORAGE
		fileState.Close();
#endif
	}
}

void GCodeMachineState::WaitForAcknowledgement() noexcept
{
	waitingForAcknowledgement = true;
#if HAS_MASS_STORAGE
	if (fileState.IsLive())
	{
		// Stop reading from the current file
		CloseFile();
	}
#endif
}

void GCodeMachineState::CopyStateFrom(const GCodeMachineState& other) noexcept
{
	selectedPlane = other.selectedPlane;
	drivesRelative = other.drivesRelative;
	axesRelative = other.axesRelative;
	feedRate = other.feedRate;
	volumetricExtrusion = other.volumetricExtrusion;
	usingInches = other.usingInches;
}

// Set the error message and associated state
void GCodeMachineState::SetError(const char *msg) noexcept
{
	if (stateMachineResult != GCodeResult::error)
	{
		errorMessage = msg;
		stateMachineResult = GCodeResult::error;
	}
}

void GCodeMachineState::SetWarning(const char *msg) noexcept
{
	if (stateMachineResult == GCodeResult::ok)
	{
		errorMessage = msg;
		stateMachineResult = GCodeResult::warning;
	}
}

// Retrieve the result and error message if it is worse than the one we already have
void GCodeMachineState::RetrieveStateMachineResult(GCodeResult& rslt, const StringRef& reply) const noexcept
{
	if (stateMachineResult >= rslt)
	{
		rslt = stateMachineResult;
		if (errorMessage != nullptr)
		{
			reply.copy(errorMessage);
		}
	}
}

GCodeMachineState *GCodeMachineState::Pop() const noexcept
{
	GCodeMachineState * const rslt = GetPrevious();
	if (errorMessage != nullptr)
	{
		rslt->errorMessage = errorMessage;
		rslt->stateMachineResult = stateMachineResult;
	}
	return rslt;
}

void GCodeMachineState::SetState(GCodeState newState) noexcept
{
	if (state == GCodeState::normal && newState != GCodeState::normal)
	{
		stateMachineResult = GCodeResult::ok;
		errorMessage = nullptr;
	}
	state = newState;
}

GCodeMachineState::BlockState& GCodeMachineState::CurrentBlockState() noexcept
{
	return blockStates[blockNesting];
}

const GCodeMachineState::BlockState& GCodeMachineState::CurrentBlockState() const noexcept
{
	return blockStates[blockNesting];
}

// Get the number of iterations of the closest enclosing loop in the current file, or -1 if there is on enclosing loop
int32_t GCodeMachineState::GetIterations() const noexcept
{
	uint8_t i = blockNesting;
	while (true)
	{
		if (blockStates[i].GetType() == BlockType::loop)
		{
			return blockStates[i].GetIterations();
		}
		if (i == 0)
		{
			return -1;
		}
		--i;
	}
}

// Create a new block returning true if successful, false if maximum indent level exceeded
bool GCodeMachineState::CreateBlock(uint16_t indentLevel) noexcept
{
	if (blockNesting + 1 == ARRAY_SIZE(blockStates))
	{
		return false;
	}

	++blockNesting;
	CurrentBlockState().SetPlainBlock(indentLevel);
	return true;
}

void GCodeMachineState::EndBlock() noexcept
{
	if (blockNesting != 0)
	{
		--blockNesting;
		variables.EndScope(blockNesting);
	}
}

// End