MotionLib  1.0.0
SamBuCa motion library
GrblTester.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-05-06
21  * Author: Michal Mysior <mmysior> <michal.mysior@cern.ch>
22  *
23  */
24 
25 #ifndef GRBL_TESTER_HPP__
26 #define GRBL_TESTER_HPP__
27 
28 #include <chrono>
29 #include <cstdint>
30 #include <memory>
31 #include <string>
32 #include <vector>
33 
34 #include <ccut/Regex.hpp>
35 
36 #include "util/grbl/GrblDeviceBase.hpp"
37 
38 namespace grbl {
39 
41 {
42 public:
43  explicit GrblTester(std::unique_ptr<GrblDeviceBase> grblDevice);
44 
45  inline grbl::ErrorCode send(const std::string &cmd)
46  {
47  return m_grblDevice->send(cmd);
48  }
49  inline void write(const std::string &data) { m_grblDevice->write(data); };
50  inline bool wait(const std::chrono::milliseconds &timeout)
51  {
52  return m_grblDevice->wait(timeout);
53  }
54  inline void wake() { m_grblDevice->wake(); }
55 
56  bool expect(const std::string &regex,
57  std::chrono::milliseconds timeout = defaultExpectTimeout);
58  bool expect(const ccut::Regex &regex,
59  std::chrono::milliseconds timeout = defaultExpectTimeout);
60  bool expect(const ccut::Regex &regex,
61  std::vector<std::string> &matches,
62  std::chrono::milliseconds timeout = defaultExpectTimeout);
63 
64  static const std::chrono::milliseconds defaultExpectTimeout;
65 
66 protected:
67  void sendCmd(const std::string &command);
68 
69  std::unique_ptr<GrblDeviceBase> m_grblDevice;
70 };
71 
72 } // namespace grbl
73 
74 #endif