Skip to content

Commit b814b0e

Browse files
committed
Added ParseLine(char* gCode)
1 parent 2c4fbdd commit b814b0e

5 files changed

Lines changed: 69 additions & 2 deletions

File tree

GCodeParserVs/GCodeParserUnitTests/GCodeParserUnitTests.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,14 @@ namespace GCodeParserUnitTests
373373
pointer++;
374374
Assert::AreEqual(GCode.comments[pointer], resultComments8[pointer]);
375375
} while (GCode.comments[pointer] != '\0');
376+
377+
GCode.ParseLine("G00 X0 Y3200;Comment");
378+
379+
pointer = -1;
380+
do {
381+
pointer++;
382+
Assert::AreEqual(GCode.comments[pointer], resultComments9[pointer]);
383+
} while (GCode.comments[pointer] != '\0');
376384
}
377385

378386
TEST_METHOD(ParseLine_ConfirmLastComment)
@@ -487,6 +495,14 @@ namespace GCodeParserUnitTests
487495
pointer++;
488496
Assert::AreEqual(GCode.lastComment[pointer], resultLastComment8[pointer]);
489497
} while (GCode.lastComment[pointer] != '\0');
498+
499+
GCode.ParseLine("/G21 (Block ;Delete) G90 ;MSG,123(?)");
500+
501+
pointer = -1;
502+
do {
503+
pointer++;
504+
Assert::AreEqual(GCode.lastComment[pointer], resultLastComment4[pointer]);
505+
} while (GCode.lastComment[pointer] != '\0');
490506
}
491507

492508
TEST_METHOD(ParseLine_ConfirmBlockDelete)
@@ -534,11 +550,20 @@ namespace GCodeParserUnitTests
534550
GCode.ParseLine();
535551

536552
Assert::AreEqual(GCode.blockDelete, false);
553+
537554
startAt = AddLine(&GCode, startAt);
538555

539556
GCode.ParseLine();
540557

541558
Assert::AreEqual(GCode.blockDelete, false);
559+
560+
GCode.ParseLine("/M300 ;Block Delete");
561+
562+
Assert::AreEqual(GCode.blockDelete, true);
563+
564+
GCode.ParseLine("M300 ;Block Delete");
565+
566+
Assert::AreEqual(GCode.blockDelete, false);
542567
}
543568

544569
TEST_METHOD(ParseLine_ConfirmBeginEnd)
@@ -598,6 +623,14 @@ namespace GCodeParserUnitTests
598623
GCode.ParseLine();
599624

600625
Assert::AreEqual(GCode.beginEnd, true);
626+
627+
GCode.ParseLine("%");
628+
629+
Assert::AreEqual(GCode.beginEnd, true);
630+
631+
GCode.ParseLine("G00 X20 Y20");
632+
633+
Assert::AreEqual(GCode.beginEnd, false);
601634
}
602635

603636
TEST_METHOD(ParseLine_ConfirmNoWords)
@@ -657,6 +690,14 @@ namespace GCodeParserUnitTests
657690
GCode.ParseLine();
658691

659692
Assert::AreEqual(GCode.NoWords(), true);
693+
694+
GCode.ParseLine("zf00^dsa&f%df; Comment");
695+
696+
Assert::AreEqual(GCode.NoWords(), true);
697+
698+
GCode.ParseLine("M300 ;Comment");
699+
700+
Assert::AreEqual(GCode.NoWords(), false);
660701
}
661702

662703
TEST_METHOD(RemoveCommentSeparators_ConfirmComments)

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,20 @@ The GetWordValue returns the value that follows the word character provided. If
6767
### `HasWord(char letter)`
6868
The HasWord method returns a Boolean true if the word (letter followed by value). The method does test to confirm the character provided is a valid G-Code word.
6969

70+
### `Initialize()`
71+
The Initialize method is call when the class in initialized and each time a new line is detected in `AddCharToLine(char c)`. If using the same instance of the GCodeParser with multiple files you may need to manually execute the method.
72+
7073
### `IsWord(char letter)`
7174
The IsWord method returns a Boolean true if the character provided represents a valid G-Code word.
7275

7376
### `NoWords()`
7477
The NoWords method returns a Boolean the if the line is blank and/or has no G-Code words.
7578

7679
### `ParseLine()`
77-
The ParseLine method parses the command line removing whitespace and comments. The method should be used after the AddCharToLine method returns true.
80+
The ParseLine method parses the command line removing whitespace and comments. The method should be used after the `AddCharToLine` method returns true.
81+
82+
### `ParseLine(char* gCode)`
83+
The ParseLine method when passed g-code parses the command line passed removing whitespace and comments. The method is an alternative to first using the `AddCharToLine` method to build a line.
7884

7985
### `RemoveCommentSeparators()`
8086
The RemoveCommentSeparators removes the comment separators (parenthesis or semicolon) from of the comments. The method should be used after the command line is parsed.

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=GCodeParser
2-
version=1.2.1
2+
version=1.3.0
33
author=Terence F. Golla tfg@terencegolla.com
44
maintainer=Terence F. Golla tfg@terencegolla.com
55
sentence=The GCodeParser library is a lightweight G-Code parser for the Arduino using only a single character buffer to first collect a line of code (also called a 'block') from a serial or file input and then parse that line into a code block and comments.

src/GCodeParser.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,25 @@ bool GCodeParser::AddCharToLine(char c)
8888
return completeLineIsAvailableToParse;
8989
}
9090

91+
/// <summary>
92+
/// Parses the line passed removing spaces, tabs and comments. Comments are shifted to the end of the line buffer.
93+
/// </summary>
94+
void GCodeParser::ParseLine(char* gCode)
95+
{
96+
Initialize();
97+
98+
int pointer = 0;
99+
while (gCode[pointer] != '\0')
100+
{
101+
AddCharToLine(gCode[pointer]);
102+
pointer = pointer + 1;
103+
}
104+
105+
AddCharToLine('\n');
106+
ParseLine();
107+
}
108+
109+
91110
/// <summary>
92111
/// Parses the line removing spaces, tabs and comments. Comments are shifted to the end of the line buffer.
93112
/// </summary>

src/GCodeParser.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ class GCodeParser
6464
GCodeParser();
6565
bool AddCharToLine(char c);
6666
void ParseLine();
67+
void ParseLine(char* gCode);
6768
void RemoveCommentSeparators();
6869

6970
int FindWord(char letter);

0 commit comments

Comments
 (0)