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

GCodeInput.h « GCodes « src - github.com/Duet3D/RepRapFirmware.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 06c96fa281fb1d581b1cae375f9ae2edc24047a2 (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
/*
 * GCodeInput.h
 *
 *  Created on: 16 Sep 2016
 *      Author: Christian
 */

#ifndef GCODEINPUT_H
#define GCODEINPUT_H

#include "RepRapFirmware.h"

#include "GCodeBuffer.h"
#include "Storage/FileStore.h"


const size_t GCodeInputBufferSize = 256;				// How many bytes can we cache per input source?
const size_t GCodeInputFileReadThreshold = 128;			// How many free bytes must be available before data is read from the SD card?


// This base class is intended to provide incoming G-codes for the GCodeBuffer class
class GCodeInput
{
public:
	virtual void Reset() = 0;							// Clean all the cached data from this input
	virtual bool FillBuffer(GCodeBuffer *gb) = 0;		// Fill a GCodeBuffer with the last available G-code
	virtual size_t BytesCached() const = 0;				// How many bytes have been cached?
};


// This class wraps around an existing Stream device which lets us avoid double buffering.
// The only downside is that we cannot (yet) look through the hardware buffer and check for requested emergency stops.
// TODO: This will require some more work in the Arduino core.
class StreamGCodeInput : GCodeInput
{
public:
	StreamGCodeInput(Stream &dev) : device(dev) { }

	void Reset() override;
	bool FillBuffer(GCodeBuffer *gb) override;			// Fill a GCodeBuffer with the last available G-code
	size_t BytesCached() const override;				// How many bytes have been cached?

private:
	Stream &device;
};


// When characters from input sources are received, they should be checked consequently for M112 (Emergency Stop).
// This allows us to react faster to an incoming emergency stop since other codes may be blocking the associated
// GCodeBuffer instance.
enum class GCodeInputState
{
	idle,
	doingCode,
	inComment,
	doingMCode,
	doingMCode1,
	doingMCode11,
	doingMCode12,
	doingMCode112,
	doingMCode122
};

// This class allows caching of dynamic content (from web-based sources) and implements a simple ring buffer.
// In addition, incoming codes are checked for M112 (emergency stop) to execute perform emergency stops as quickly
// as possible. Comments can be optionally stripped from sources where comments are not needed (e.g. HTTP).
class RegularGCodeInput : GCodeInput
{
public:
	RegularGCodeInput(bool removeComments);

	void Reset() override;
	bool FillBuffer(GCodeBuffer *gb) override;			// Fill a GCodeBuffer with the last available G-code
	size_t BytesCached() const override;				// How many bytes have been cached?

	void Put(MessageType mtype, const char c);			// Append a single character
	void Put(MessageType mtype, const char *buf);		// Append a null-terminated string to the buffer
	void Put(MessageType mtype, const char *buf, size_t len);	// Append a generic string to the buffer

	size_t BufferSpaceLeft() const;						// How much space do we have left?

private:
	bool stripComments;
	GCodeInputState state;

protected:
	uint32_t buf32[(GCodeInputBufferSize + 3) / 4];
	char * const buffer;
	size_t writingPointer, readingPointer;
};

// This class is an expansion of the RegularGCodeInput class to buffer G-codes and to rewind file positions when
// nested G-code files are started. However buffered codes are not explicitly checked for M112.
class FileGCodeInput : public RegularGCodeInput
{
public:
	FileGCodeInput() : RegularGCodeInput(false), lastFile(nullptr) { }

	void Reset() override;								// This should be called when the associated file is being closed
	void Reset(const FileData &file);					// Should be called when a specific G-code or macro file is closed outside the reading context

	bool ReadFromFile(FileData &file);					// Read another chunk of G-codes from the file and return true if more data is available

private:
	FileStore *lastFile;
};

#endif