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: 8412b4c35737e0b0845386cbae524677c80d3ba2 (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
/*
 * GCodeMachineState.cpp
 *
 *  Created on: 15 Nov 2016
 *      Author: David
 */

#include "GCodeMachineState.h"
#include "RepRap.h"

#include <limits>

#if HAS_LINUX_INTERFACE
static unsigned int LastFileId = 1;
#endif

// Create a default initialised GCodeMachineState
GCodeMachineState::GCodeMachineState() noexcept
	: previous(nullptr), feedRate(DefaultFeedRate * SecondsToMinutes),
#if HAS_LINUX_INTERFACE
	  fileId(0),
#endif
	  lockedResources(0), errorMessage(nullptr),
	  lineNumber(0),
	  compatibility(Compatibility::reprapFirmware), drivesRelative(false), axesRelative(false),
#if HAS_LINUX_INTERFACE
	  isFileFinished(false), fileError(false),
#endif
	  doingFileMacro(false), waitWhileCooling(false), runningM501(false), runningM502(false),
	  volumetricExtrusion(false), g53Active(false), runningSystemMacro(false), usingInches(false),
	  waitingForAcknowledgement(false), messageAcknowledged(false), blockNesting(0), state(GCodeState::normal)
{
	blockStates[0].SetPlainBlock(0);
}

// Copy constructor. This chains the new one to the previous one.
GCodeMachineState::GCodeMachineState(GCodeMachineState& prev, bool withinSameFile) noexcept
	: previous(&prev), feedRate(prev.feedRate),
#if HAS_MASS_STORAGE
	  fileState(prev.fileState),
#endif
#if HAS_LINUX_INTERFACE
	  fileId(prev.fileId),
#endif
	  lockedResources(prev.lockedResources), errorMessage(nullptr),
	  lineNumber((withinSameFile) ? prev.lineNumber : 0),
	  compatibility(prev.compatibility), drivesRelative(prev.drivesRelative), axesRelative(prev.axesRelative),
#if HAS_LINUX_INTERFACE
	  isFileFinished(prev.isFileFinished), fileError(false),
#endif
	  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), blockNesting((withinSameFile) ? prev.blockNesting : 0), state(GCodeState::normal)
{
	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
# if HAS_LINUX_INTERFACE
	if (!reprap.UsingLinuxInterface())
# endif
	{
		fileState.Close();
	}
#endif
}

#if HAS_LINUX_INTERFACE

// Set the state to indicate a file is being processed
void GCodeMachineState::SetFileExecuting() noexcept
{
	fileId = LastFileId++;
	isFileFinished = fileError = false;
}

// Mark the currently executing file as finished
void GCodeMachineState::SetFileFinished(bool error) noexcept
{
	isFileFinished = true;
	fileError = error;
}

#endif

bool GCodeMachineState::DoingFile() const noexcept
{
#if HAS_LINUX_INTERFACE
	if (reprap.UsingLinuxInterface())
	{
		return fileId != 0;
	}
#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())
	{
		for (GCodeMachineState *ms = this; ms != nullptr; ms = ms->previous)
		{
			if (ms->fileId == fileId)
			{
				ms->isFileFinished = false;
				ms->fileId = 0;
			}
		}
	}
	else
#endif
	{
#if HAS_MASS_STORAGE
		fileState.Close();
#endif
	}
}

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

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;
	}
}

// End