ArPiRobot-CoreLib C++
C++ library for ArPiRobot robots
BaseArduinoInterface.hpp
1 /*
2  * Copyright 2021 Marcus Behel
3  *
4  * This file is part of ArPiRobot-CoreLib.
5  *
6  * ArPiRobot-CoreLib is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * ArPiRobot-CoreLib is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with ArPiRobot-CoreLib. If not, see <https://www.gnu.org/licenses/>.
18  */
19 
20 #pragma once
21 
22 #include <thread>
23 #include <vector>
24 #include <string>
25 #include <functional>
26 #include <memory>
27 #include <unordered_map>
28 
29 namespace arpirobot{
30 
31  // Forward declare
32  class ArduinoDevice;
33 
42  public:
43  virtual ~BaseArduinoInterface();
44 
45  BaseArduinoInterface() = default;
46 
47  BaseArduinoInterface(const BaseArduinoInterface &other) = delete;
48  BaseArduinoInterface &operator=(const BaseArduinoInterface &other) = delete;
49 
54  bool begin();
55 
61  void addDevice(ArduinoDevice &device);
62 
68  void addDevice(std::shared_ptr<ArduinoDevice> device);
69 
75  bool isReady();
76 
84  void sendFromDevice(uint8_t deviceId, std::vector<uint8_t> data);
85 
86  protected:
87 
88  // Communication functions
89 
90  void run();
91 
92  uint16_t calcCCittFalse(const std::vector<uint8_t> &data, size_t len);
93 
98  void writeData(const std::vector<uint8_t> &data);
99 
109  bool readData();
110 
115  bool checkData();
116 
117  // This function will throw exceptions from lower level I/O functoins
118  std::vector<uint8_t> waitForMessage(const std::vector<uint8_t> &prefix, int timeoutMs);
119 
120  // Implementation specific (these can and should throw exceptions on errors)
121  // Exceptions thrown should be from arpirobot/core/io/exceptions.hpp
122  virtual void open() = 0;
123  virtual void close() = 0;
124  virtual bool isOpen() = 0;
125  virtual int available() = 0;
126  virtual uint8_t readOne() = 0;
127  virtual std::vector<uint8_t> readAll() = 0;
128  virtual void write(const uint8_t &b) = 0;
129  virtual std::string getDeviceName() = 0;
130 
131  private:
132 
133  static bool msgStartsWith(const std::vector<uint8_t> &msg, const std::vector<uint8_t> &prefix);
134  static bool msgEquals(const std::vector<uint8_t> &msg1, const std::vector<uint8_t> &msg2);
135 
136  std::vector<uint8_t> workingBuffer;
137  std::vector<uint8_t> readDataset;
138  bool parseStarted = false;
139  bool parseEscaped = false;
140  std::thread *processThread = nullptr;
141  std::vector<std::shared_ptr<ArduinoDevice>> devices;
142  bool initialized = false;
143  bool arduinoReady = false;
144 
145  const static uint8_t START_BYTE;
146  const static uint8_t END_BYTE;
147  const static uint8_t ESCAPE_BYTE;
148 
149  const static std::vector<uint8_t> MSG_START;
150  const static std::vector<uint8_t> MSG_ADD;
151  const static std::vector<uint8_t> MSG_ADDSUCCESS;
152 
153  const static std::vector<uint8_t> CMD_RESET;
154  const static std::vector<uint8_t> CMD_END;
155 
156  friend class ArduinoDevice;
157  };
158 }
Definition: ArduinoDevice.hpp:35
Definition: BaseArduinoInterface.hpp:41
void writeData(const std::vector< uint8_t > &data)
void sendFromDevice(uint8_t deviceId, std::vector< uint8_t > data)
void addDevice(std::shared_ptr< ArduinoDevice > device)
void addDevice(ArduinoDevice &device)
Definition: ArduinoDevice.hpp:27