MotionLib  1.0.0
SamBuCa motion library
GrblDeviceBase.hpp
1 /*
2 ** Copyright (C) 2020 Sylvain Fargier
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-02-13T21:09:57+01:00
21 ** Author: Sylvain Fargier <fargie_s> <sylvain.fargier@cern.ch>
22 **
23 */
24 
25 #ifndef GRBLDEVICEBASE_HPP__
26 #define GRBLDEVICEBASE_HPP__
27 
28 #include <chrono>
29 #include <functional>
30 #include <string>
31 
32 #include <ccut/LineBuffer.hpp>
33 #include <ccut/Signal.hpp>
34 
35 #include "GrblTypes.hpp"
36 
37 namespace grbl {
44 {
45 public:
46  virtual ~GrblDeviceBase() = default;
47 
54  void fetch(const std::chrono::milliseconds &timeout);
55 
60  virtual void write(const std::string &data) = 0;
61 
73  virtual grbl::ErrorCode send(
74  const std::string &cmd,
75  std::function<bool(const std::string &)> msg = {},
76  const std::chrono::milliseconds &timeout = defaultSendTimeout);
77 
91  virtual grbl::ErrorCode sendRealtime(
92  const std::string &cmd,
93  std::function<bool(const std::string &)> msg = {},
94  const std::chrono::milliseconds &timeout = defaultSendTimeout);
95 
99  virtual void wake() = 0;
100 
104  virtual void read() = 0;
105 
112  virtual bool wait(const std::chrono::milliseconds &timeout) = 0;
113 
119  virtual bool takeLine(std::string &line);
120 
121  ccut::Signal<std::string> dataSignal;
122 
123  static const std::chrono::milliseconds defaultSendTimeout;
124 
125 protected:
126  ccut::LineBuffer m_buffer;
127 };
128 
131 } // namespace grbl
132 
133 #endif
virtual grbl::ErrorCode sendRealtime(const std::string &cmd, std::function< bool(const std::string &)> msg={}, const std::chrono::milliseconds &timeout=defaultSendTimeout)
send a realtime-command wait for reply
virtual bool takeLine(std::string &line)
consume line from buffer
virtual void wake()=0
wake thread waiting for input
virtual void read()=0
fill LineBuffer with incoming bytes
virtual void write(const std::string &data)=0
raw write some bytes
virtual grbl::ErrorCode send(const std::string &cmd, std::function< bool(const std::string &)> msg={}, const std::chrono::milliseconds &timeout=defaultSendTimeout)
send a command wait for reply
void fetch(const std::chrono::milliseconds &timeout)
wait for data and process it
virtual bool wait(const std::chrono::milliseconds &timeout)=0
wait for input