ArPiRobot-CoreLib C++
C++ library for ArPiRobot robots
BaseDevice.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 <string>
23 #include <mutex>
24 #include <memory>
25 
26 namespace arpirobot{
27 
28  class Action; // Forward declare b/c this header is included by action.hpp
29 
35  class BaseDevice{
36  public:
37 
38  virtual ~BaseDevice();
39 
44  virtual std::string getDeviceName();
45 
55  bool isLockedByAction(std::shared_ptr<Action> action = nullptr);
56 
65  bool isLockedByAction(Action &action);
66 
67  protected:
68  virtual void begin() = 0;
69  virtual bool isEnabled() = 0;
70  virtual bool shouldMatchRobotState() = 0;
71  virtual bool shouldDisableWithWatchdog() = 0;
72  virtual void enable() = 0;
73  virtual void disable() = 0;
74 
75  bool initialized = false;
76  std::string deviceName;
77 
78  private:
79 
80  void lockDevice(Action *action);
81 
82  void releaseDevice(Action *action);
83 
84  void doBegin();
85 
86  std::mutex actionLock;
87 
88  // Not shared_ptr because should not keep Action in scope
89  // Action will set when locking
90  // Action will unset on unlock or on finish
91  Action *lockingAction = nullptr;
92 
93  friend class Action; // Needs to call lockDevice
94  friend class BaseRobot; // Needs doBegin, enable, disable
95  };
96 }
Definition: Action.hpp:41
Definition: BaseDevice.hpp:35
bool isLockedByAction(std::shared_ptr< Action > action=nullptr)
Check if a given action locks the device.
virtual std::string getDeviceName()
bool isLockedByAction(Action &action)
Check if a given action locks the device.
Definition: BaseRobot.hpp:40
Definition: ArduinoDevice.hpp:27