$darkmode
actuation_level.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2020 Robert Bosch GmbH
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * SPDX-License-Identifier: Apache-2.0
17  */
39 #pragma once
40 #ifndef CLOE_UTILITY_ACTUATION_LEVEL_HPP_
41 #define CLOE_UTILITY_ACTUATION_LEVEL_HPP_
42 
43 #include <string> // for string
44 
45 #include <fable/json.hpp> // for Json
46 
47 namespace cloe {
48 namespace utility {
49 
51  public:
52  enum Enum {
53  None = 0,
54  Long = 1,
55  Lat = 2,
56  LatLong = 3,
57  Standby = 4,
58  };
59 
60  ActuationLevel() : enum_(None) {}
61  ActuationLevel(Enum e) : enum_(e) {} // NOLINT
62  ActuationLevel(bool lat, bool lng) : enum_(None) {
63  if (lat) {
64  this->set_lat();
65  }
66  if (lng) {
67  this->set_long();
68  }
69  }
70 
71  Enum get_raw() const { return enum_; }
72  void set_raw(Enum e) {
73  enum_ = e;
74  assert(this->is_valid());
75  }
76 
77  bool is_valid() const {
78  // This looks complex, but it isn't that much.
79  // Currently, the following states are allowed:
80  //
81  // 0b100 0b011 0b010 0b001
82  // 4 3 2 1
83  //
84  // In particular, any other combination and any setting of higher bits
85  // would push the value of the enum to be greater than 4.
86  return enum_ <= 4;
87  }
88 
89  bool has_lat() const { return (enum_ & Lat) != 0; }
90  bool has_long() const { return (enum_ & Long) != 0; }
91  bool has_both() const { return enum_ == LatLong; }
92  bool has_control() const { return (enum_ & LatLong) != 0; }
93  bool is_standby() const { return enum_ == Standby; }
94  bool is_none() const { return enum_ == None; }
95 
96  void set_none() { enum_ = None; }
97  void set_standby() { enum_ = Standby; }
98  void set_latlong() { enum_ = LatLong; }
99  void set_lat() { enum_ = static_cast<Enum>((enum_ & Long) | Lat); }
100  void set_long() { enum_ = static_cast<Enum>((enum_ & Lat) | Long); }
101 
102  bool operator==(const ActuationLevel& other) const { return enum_ == other.enum_; }
103  bool operator!=(const ActuationLevel& other) const { return !operator==(other); }
104 
105  const char* to_human_cstr() const {
106  switch (enum_) {
107  case None:
108  return "none";
109  case Long:
110  return "longitudinal";
111  case Lat:
112  return "lateral";
113  case LatLong:
114  return "longitudinal and lateral";
115  case Standby:
116  return "standby";
117  default:
118  return "unknown";
119  }
120  }
121 
122  const char* to_symbol_cstr() const {
123  switch (enum_) {
124  case None:
125  return "*";
126  case Long:
127  return "/ \\";
128  case Lat:
129  return "<->";
130  case LatLong:
131  return "/+\\";
132  case Standby:
133  return "...";
134  default:
135  return "!";
136  }
137  }
138 
139  const char* to_loud_cstr() const {
140  switch (enum_) {
141  case None:
142  return "N/A";
143  case Long:
144  return "LONG";
145  case Lat:
146  return "LAT";
147  case LatLong:
148  return "LONG+LAT";
149  case Standby:
150  return "STANDBY";
151  default:
152  return "ERROR";
153  }
154  }
155 
156  const char* to_unicode_cstr() const {
157  switch (enum_) {
158  case None:
159  return "✖";
160  case Long:
161  return "∥";
162  case Lat:
163  return "≈";
164  case LatLong:
165  return "∆";
166  case Standby:
167  return "♤";
168  default:
169  return "↯";
170  }
171  }
172 
173  const char* to_cstr() const { return this->to_human_cstr(); }
174 
175  friend void to_json(fable::Json& j, const ActuationLevel& l) { j = l.to_cstr(); }
176 
177  private:
178  Enum enum_;
179 };
180 
181 // Required for cloe::utility::Pie to work.
182 inline std::string to_string(const ActuationLevel::Enum level) {
183  return std::string(ActuationLevel(level).to_cstr());
184 }
185 
186 } // namespace utility
187 } // namespace cloe
188 
189 #endif // CLOE_UTILITY_ACTUATION_LEVEL_HPP_
nlohmann::json Json
Definition: json.hpp:62
Definition: coordinator.hpp:36
Definition: actuation_level.hpp:50