vulp  2.3.0
StateMachine.h
Go to the documentation of this file.
1 // SPDX-License-Identifier: Apache-2.0
2 // Copyright 2022 Stéphane Caron
3 
4 #pragma once
5 
7 
8 namespace vulp::spine {
9 
11 constexpr unsigned kNbStopCycles = 5;
12 
14 enum class State : uint32_t {
15  kSendStops = 0,
16  kReset = 1,
17  kIdle = 2,
18  kObserve = 3,
19  kAct = 4,
20  kShutdown = 5,
21  kOver = 6
22 };
23 
25 enum class Event : uint32_t {
26  kCycleBeginning = 0,
27  kCycleEnd = 1,
28  kInterrupt = 2,
29 };
30 
35 constexpr const char* state_name(const State& state) noexcept {
36  switch (state) {
37  case State::kSendStops:
38  return "State::kSendStops";
39  case State::kReset:
40  return "State::kReset";
41  case State::kIdle:
42  return "State::kIdle";
43  case State::kObserve:
44  return "State::kObserve";
45  case State::kAct:
46  return "State::kAct";
47  case State::kShutdown:
48  return "State::kShutdown";
49  case State::kOver:
50  return "State::kOver";
51  default:
52  break;
53  }
54  return "?";
55 }
56 
88 class StateMachine {
89  public:
94  explicit StateMachine(AgentInterface& interface) noexcept;
95 
100  void process_event(const Event& event) noexcept;
101 
103  const State& state() const noexcept { return state_; }
104 
106  bool is_over_after_this_cycle() const noexcept {
107  return (state_ == State::kShutdown && stop_cycles_ + 1u == kNbStopCycles);
108  }
109 
110  private:
115  void enter_state(const State& next_state) noexcept;
116 
118  void process_cycle_beginning();
119 
121  void process_cycle_end();
122 
123  private:
125  AgentInterface& interface_;
126 
128  State state_;
129 
131  unsigned stop_cycles_;
132 };
133 
134 } // namespace vulp::spine
Memory map to shared memory.
Spine state machine.
Definition: StateMachine.h:88
void process_event(const Event &event) noexcept
Process a new event.
StateMachine(AgentInterface &interface) noexcept
Initialize state machine.
const State & state() const noexcept
Get current state.
Definition: StateMachine.h:103
bool is_over_after_this_cycle() const noexcept
Whether we transition to the terminal state at the next end-cycle event.
Definition: StateMachine.h:106
Inter-process communication protocol with the spine.
Definition: __init__.py:1
constexpr unsigned kNbStopCycles
When sending stop cycles, send at least that many.
Definition: StateMachine.h:11
Event
Events that may trigger transitions between states.
Definition: StateMachine.h:25
State
States of the state machine.
Definition: StateMachine.h:14
constexpr const char * state_name(const State &state) noexcept
Name of a state.
Definition: StateMachine.h:35