$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 
41 #include <string> // for string
42 
43 #include <fable/json.hpp> // for Json
44 
45 namespace cloe {
46 namespace utility {
47 
49  public:
50  enum Enum {
51  None = 0,
52  Long = 1,
53  Lat = 2,
54  LatLong = 3,
55  Standby = 4,
56  };
57 
58  ActuationLevel() : enum_(None) {}
59  ActuationLevel(Enum e) : enum_(e) {} // NOLINT
60  ActuationLevel(bool lat, bool lng) : enum_(None) {
61  if (lat) {
62  this->set_lat();
63  }
64  if (lng) {
65  this->set_long();
66  }
67  }
68 
69  Enum get_raw() const { return enum_; }
70  void set_raw(Enum e) {
71  enum_ = e;
72  assert(this->is_valid());
73  }
74 
75  bool is_valid() const {
76  // This looks complex, but it isn't that much.
77  // Currently, the following states are allowed:
78  //
79  // 0b100 0b011 0b010 0b001
80  // 4 3 2 1
81  //
82  // In particular, any other combination and any setting of higher bits
83  // would push the value of the enum to be greater than 4.
84  return enum_ <= 4;
85  }
86 
87  bool has_lat() const { return (enum_ & Lat) != 0; }
88  bool has_long() const { return (enum_ & Long) != 0; }
89  bool has_both() const { return enum_ == LatLong; }
90  bool has_control() const { return (enum_ & LatLong) != 0; }
91  bool is_standby() const { return enum_ == Standby; }
92  bool is_none() const { return enum_ == None; }
93 
94  void set_none() { enum_ = None; }
95  void set_standby() { enum_ = Standby; }
96  void set_latlong() { enum_ = LatLong; }
97  void set_lat() { enum_ = static_cast<Enum>((enum_ & Long) | Lat); }
98  void set_long() { enum_ = static_cast<Enum>((enum_ & Lat) | Long); }
99 
100  bool operator==(const ActuationLevel& other) const { return enum_ == other.enum_; }
101  bool operator!=(const ActuationLevel& other) const { return !operator==(other); }
102 
103  const char* to_human_cstr() const {
104  switch (enum_) {
105  case None:
106  return "none";
107  case Long:
108  return "longitudinal";
109  case Lat:
110  return "lateral";
111  case LatLong:
112  return "longitudinal and lateral";
113  case Standby:
114  return "standby";
115  default:
116  return "unknown";
117  }
118  }
119 
120  const char* to_symbol_cstr() const {
121  switch (enum_) {
122  case None:
123  return "*";
124  case Long:
125  return "/ \\";
126  case Lat:
127  return "<->";
128  case LatLong:
129  return "/+\\";
130  case Standby:
131  return "...";
132  default:
133  return "!";
134  }
135  }
136 
137  const char* to_loud_cstr() const {
138  switch (enum_) {
139  case None:
140  return "N/A";
141  case Long:
142  return "LONG";
143  case Lat:
144  return "LAT";
145  case LatLong:
146  return "LONG+LAT";
147  case Standby:
148  return "STANDBY";
149  default:
150  return "ERROR";
151  }
152  }
153 
154  const char* to_unicode_cstr() const {
155  switch (enum_) {
156  case None:
157  return "✖";
158  case Long:
159  return "∥";
160  case Lat:
161  return "≈";
162  case LatLong:
163  return "∆";
164  case Standby:
165  return "♤";
166  default:
167  return "↯";
168  }
169  }
170 
171  const char* to_cstr() const { return this->to_human_cstr(); }
172 
173  friend void to_json(fable::Json& j, const ActuationLevel& l) { j = l.to_cstr(); }
174 
175  private:
176  Enum enum_;
177 };
178 
179 // Required for cloe::utility::Pie to work.
180 inline std::string to_string(const ActuationLevel::Enum level) {
181  return std::string(ActuationLevel(level).to_cstr());
182 }
183 
184 } // namespace utility
185 } // namespace cloe
Definition: actuation_level.hpp:48
nlohmann::json Json
Definition: fable_fwd.hpp:35