MotionLib  1.0.0
SamBuCa motion library
MGrblParser.cpp
1 /*
2 ** Copyright (C) 2021 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-04-29T15:44:12+02:00
21 ** Author: Sylvain Fargier <sfargier> <sylvain.fargier@cern.ch>
22 **
23 */
24 
25 #include "MGrblParser.hpp"
26 
27 #include <ccut/utils.hpp>
28 #include <logger/Logger.hpp>
29 
30 #include "Exception.hpp"
31 
32 using namespace smc;
33 using namespace logger;
34 
35 namespace mgrbl {
36 
37 namespace gen {
38 const std::string Control::Layout = "$layout";
39 const std::string Control::GrblCount = "$grblCount";
40 const std::string Control::TrigArm = "$trigArm";
41 const std::string Control::TrigSoft = "$trigSoft";
42 const std::string Control::BuildInfo = "$I";
43 } // namespace gen
44 
45 static const std::string s_loggerCat{"mgrbl:parser"};
46 
47 bool MGrblParser::parseLayout(const std::string &line, Layout &layout)
48 {
49  if (!ccut::startsWith(line, gen::Control::Layout) ||
50  line[gen::Control::Layout.size()] != '=')
51  {
52  error(s_loggerCat) << "invalid layout";
53  return false;
54  }
55 
56  std::size_t end(line.find_first_of("\r\n", 8));
57  const std::string value(
58  line.substr(8, (end == std::string::npos) ? end : (end - 8)));
59  layout.reserve(value.size());
60  for (const char c : value)
61  layout.emplace_back(c - '0');
62  return true;
63 }
64 
65 bool MGrblParser::parseIsArmed(const std::string &line, bool &isArmed)
66 {
67  if (!ccut::startsWith(line, gen::Control::TrigArm) ||
68  line[gen::Control::TrigArm.size()] != '=')
69  {
70  return false;
71  }
72 
73  const std::string value(line.substr(9, 1));
74  if (value == "1")
75  isArmed = true;
76  else if (value == "0")
77  isArmed = false;
78  else
79  throw Exception(smc::ErrorCode::InvalidArguments,
80  "invalid isArmed response value");
81 
82  return true;
83 }
84 
85 } // namespace mgrbl
Exception thrown by MotionController in case of issues with command.
Definition: Exception.hpp:61
main motion-lib namespace
Definition: Client.cpp:30