MotionLib  1.0.0
SamBuCa motion library
GrblParser.hpp
1 /*
2  * Copyright (C) 2022 CERN
3  *
4  * This software is provided 'as-is', without any express or implied
5  * warranty. In no event will the authors be held liable for any damages
6  * arising from the use of this software.
7  *
8  * Permission is granted to anyone to use this software for any purpose,
9  * including commercial applications, and to alter it and redistribute it
10  * freely, subject to the following restrictions:
11  *
12  * 1. The origin of this software must not be misrepresented; you must not
13  * claim that you wrote the original software. If you use this software
14  * in a product, an acknowledgment in the product documentation would be
15  * appreciated but is not required.
16  * 2. Altered source versions must be plainly marked as such, and must not be
17  * misrepresented as being the original software.
18  * 3. This notice may not be removed or altered from any source distribution.
19  *
20  * Created on: 2022-01-17
21  * Author: Michal Mysior <mmysior> <michal.mysior@cern.ch>
22  *
23  * @todo Make command generation stream-oriented, expand commands to allow for
24  * multi-motor movements.
25  *
26  */
27 
28 #ifndef GRBL_PARSER_HPP__
29 #define GRBL_PARSER_HPP__
30 
31 #include <cstdint>
32 #include <map>
33 #include <string>
34 #include <vector>
35 
36 #include "../../Units.hpp"
37 #include "GrblTypes.hpp"
38 
39 namespace grbl {
49 {
50 public:
51  typedef grbl::LineType LineType;
52  typedef std::pair<std::string, std::string> Message;
53  typedef std::pair<int, std::string> Setting;
54 
60  static LineType getLineType(const std::string &line);
61 
69  static bool parseMessage(const std::string &line, Message &message);
70 
77  static bool parseSetting(const std::string &line, Setting &setting);
78 
85  static bool parseErrorReply(const std::string &line,
86  grbl::ErrorCode &errorCode);
87 
95  struct State
96  {
97  grbl::RunState runState = RunState::Unknown;
98 
99  std::map<Axis, grbl_float_t> motorPositions;
100 
101  size_t runStateArg = 0;
102  size_t motionBufferFree = 0;
103  size_t charBufferFree = 0;
104  size_t line = 0;
105 
106  State() = default;
107  State(grbl::RunState rs,
108  const std::map<Axis, grbl_float_t> &mp,
109  size_t rsa,
110  size_t mbf,
111  size_t cbf,
112  size_t l) :
113  runState(rs),
114  motorPositions(mp),
115  runStateArg(rsa),
116  motionBufferFree(mbf),
117  charBufferFree(cbf),
118  line(l)
119  {}
120  };
121 
128  static bool parseState(const std::string &line, State &state);
129 
133  struct BuildInfo
134  {
135  std::string version;
136  std::string options; // FIXME: parse options and newopt
137  std::string firmware;
138  std::string driver;
139  std::string driverVersion;
140 
141  size_t motionBufferSize;
142  size_t charBufferSize;
143  size_t axisCount;
144  size_t toolsCount;
145  };
146 
153  static bool parseBuildInfo(const std::string &line, BuildInfo &info);
154 
159  {
160  int id;
161  int parentId;
162  std::string name;
163  };
164 
171  static bool parseSettingGroup(const std::string &line, SettingGroup &group);
172 
176  struct SettingDesc
177  {
178  SettingId id;
179  int groupId;
180  std::string name;
181  std::string unit;
182  int datatype;
183  std::string format;
184  float min;
185  float max;
186  };
187 
194  static bool parseSettingDesc(const std::string &line, SettingDesc &desc);
195 
199  struct PinInfo
200  {
201  uint8_t pin;
202  std::string function;
203  std::string description;
204  std::string port;
205  };
206 
216  static bool parsePinInfo(const std::string &line, PinInfo &info);
217 
218  template<typename C>
219  static bool parsePinInfo(const std::string &line, C &c)
220  {
221  PinInfo p;
222  if (parsePinInfo(line, p))
223  {
224  c.push_back(p);
225  return true;
226  }
227  return false;
228  }
229 };
230 
234 } // namespace grbl
235 
236 #endif /* GRBL_PARSER_HPP__ */
static bool parseState(const std::string &line, State &state)
parse a state message
Definition: GrblParser.cpp:117
static bool parseMessage(const std::string &line, Message &message)
parse message
Definition: GrblParser.cpp:75
static LineType getLineType(const std::string &line)
detect line type
Definition: GrblParser.cpp:51
static bool parseSettingGroup(const std::string &line, SettingGroup &group)
incremental parsing of settingGroup
Definition: GrblParser.cpp:261
static bool parseSetting(const std::string &line, Setting &setting)
parse a setting message
Definition: GrblParser.cpp:89
static bool parseBuildInfo(const std::string &line, BuildInfo &info)
incremental parsing of build-info
Definition: GrblParser.cpp:209
static bool parseSettingDesc(const std::string &line, SettingDesc &desc)
incremental parsing of setting description
Definition: GrblParser.cpp:295
static bool parsePinInfo(const std::string &line, PinInfo &info)
incremental parsing of pins description
Definition: GrblParser.cpp:335
static bool parseErrorReply(const std::string &line, grbl::ErrorCode &errorCode)
parse an error message
Definition: GrblParser.cpp:103
structure holding Grbl build-info ($I)
Definition: GrblParser.hpp:134
structure holding Grbl pin description ($PINS)
Definition: GrblParser.hpp:200
structure holding Grbl setting description ($ES)
Definition: GrblParser.hpp:177
structure holding Grbl settingroup info ($EG)
Definition: GrblParser.hpp:159
Struct holding state of Grbl as returned by "?".
Definition: GrblParser.hpp:96