ArPiRobot-CoreLib C++
C++ library for ArPiRobot robots
Action.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 <arpirobot/core/device/BaseDevice.hpp>
23 #include <arpirobot/core/scheduler.hpp>
24 
25 #include <vector>
26 #include <mutex>
27 #include <memory>
28 #include <functional>
29 
30 namespace arpirobot{
31 
32 
33  typedef std::vector<std::reference_wrapper<BaseDevice>> LockedDeviceList;
34 
35 
41  class Action{
42  public:
43 
50  Action(int32_t processRateMs = -1);
51 
52  virtual ~Action();
53 
57  bool isRunning();
58 
64  int32_t getProcessPeriodMs();
65 
72  void setProcessPeriodMs(int32_t processPeriodMs);
73 
74  protected:
75 
76  /*
77  * Returns a list of devices this action should lock
78  */
79  virtual LockedDeviceList lockedDevices();
80 
84  virtual void begin() = 0;
85 
89  virtual void process() = 0;
90 
95  virtual void finish(bool wasInterrupted) = 0;
96 
101  virtual bool shouldContinue() = 0;
102 
103  private:
104 
105  static bool predDevice(std::reference_wrapper<BaseDevice> a, std::reference_wrapper<BaseDevice> b);
106 
107  static bool compDevice(std::reference_wrapper<BaseDevice> a, std::reference_wrapper<BaseDevice> b);
108 
109  static void makeDeviceListUnique(std::vector<std::reference_wrapper<BaseDevice>> &list);
110 
111  void actionStart(bool skipLock);
112 
113  void actionStop(bool interrupted);
114 
115  void actionProcess();
116 
117  bool isStarted();
118 
119  bool isFinished();
120 
121  std::shared_ptr<Task> _schedulerTask = nullptr;
122  int32_t processRateMs = -1;
123 
124  std::vector<std::reference_wrapper<BaseDevice>> currentlyLocked;
125 
126  std::mutex stateLock;
127  bool started = false;
128  bool finished = false;
129 
130  friend class ActionManager;
131  friend class ActionSeries;
132  };
133 
134 }
Definition: ActionManager.hpp:37
Definition: ActionSeries.hpp:34
Definition: Action.hpp:41
virtual bool shouldContinue()=0
virtual void begin()=0
int32_t getProcessPeriodMs()
virtual void finish(bool wasInterrupted)=0
Action(int32_t processRateMs=-1)
void setProcessPeriodMs(int32_t processPeriodMs)
virtual void process()=0
Definition: ArduinoDevice.hpp:27