CTRE Phoenix 6 C++ 26.70.0-alpha-2
Loading...
Searching...
No Matches
Alertable.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) Cross The Road Electronics.  All rights reserved.
3 * License information can be found in CTRE_LICENSE.txt
4 * For support and suggestions contact support@ctr-electronics.com or file
5 * an issue tracker at https://github.com/CrossTheRoadElec/Phoenix-Releases
6 */
7#pragma once
8
10#include <functional>
11#include <vector>
12
13namespace ctre {
14namespace phoenix6 {
15
17
18 /**
19 * \brief Interface for all types that can report a Phoenix alert.
20 */
22 public:
23 /**
24 * \brief Levels of urgency of a registered alert.
25 */
27
28 /**
29 * \brief Registers all alerts for this type to the provided collection.
30 *
31 * When registering alerts, the provided alert IDs must be unique within the
32 * AlertableCollection. As a result, the IDs should generally contain information
33 * unique to this specific instance, such as device and signal name.
34 *
35 * \param collection Alertable collection into which the alerts are registered
36 */
37 virtual void RegisterAlerts(AlertableCollection &collection) = 0;
38 };
39
40 /**
41 * \brief Information about an alert.
42 */
43 struct AlertInfo final {
44 /** \brief Whether the alert is active. */
45 bool isActive{};
46 /** \brief The new text to report in the alert, or nullopt to leave the text unchanged. */
47 std::optional<std::string> text{};
48
49 /**
50 * Creates new information for an inactive alert.
51 *
52 * \returns Information for an inactive alert
53 */
54 static AlertInfo Inactive() { return {}; }
55
56 /**
57 * Creates new information for an active alert.
58 *
59 * \tparam S Type of the text, convertible to std::optional<std::string>
60 * \param text New text to report in the alert, or nullopt to leave the text unchanged
61 * \return Information for an active alert
62 */
63 template <std::convertible_to<std::optional<std::string>> S = std::optional<std::string>>
64 static AlertInfo Active(S &&text = std::nullopt)
65 {
66 return {true, std::forward<S>(text)};
67 }
68 };
69
70 /**
71 * \brief Information about a multi-level alert.
72 */
73 struct MultiAlertInfo final {
74 /** \brief Whether the multi-level alert is active. */
75 bool isActive{};
76 /** \brief The new alert urgency level. */
78 /** \brief The new text to report in the alert, or nullopt to leave the text unchanged. */
79 std::optional<std::string> text{};
80
81 /**
82 * Creates new information for an inactive multi-level alert.
83 *
84 * \returns Information for an inactive multi-level alert
85 */
86 static MultiAlertInfo Inactive() { return {}; }
87
88 /**
89 * Creates new information for an active multi-level alert.
90 *
91 * \tparam S Type of the text, convertible to std::optional<std::string>
92 * \param level New alert urgency level
93 * \param text New text to report in the alert, or nullopt to leave the text unchanged
94 * \return Information for an active multi-level alert
95 */
96 template <std::convertible_to<std::optional<std::string>> S = std::optional<std::string>>
98 {
99 return {true, level, std::forward<S>(text)};
100 }
101 };
102
103 /**
104 * \brief Collection of objects that can report alerts.
105 *
106 * This collection must outlive any PhoenixAlertable objects and lambda
107 * captures registered to the collection.
108 */
110 std::string group;
111 std::vector<std::function<void()>> alerts;
112 std::vector<std::function<void()>> beforeAlerts;
113
114 public:
115 /**
116 * \brief Constructs a new collection of alertable objects.
117 *
118 * \param group Alert group identifier
119 */
120 AlertableCollection(std::string group) :
121 group{group}
122 {}
123
126
127 /**
128 * \brief Reports all alerts in this collection.
129 */
130 void Report() const
131 {
132 for (auto const &func : beforeAlerts) {
133 func();
134 }
135 for (auto const &alert : alerts) {
136 alert();
137 }
138 }
139
140 /**
141 * \brief Adds a PhoenixAlertable to this collection.
142 *
143 * \param alertable PhoenixAlertable to register
144 * \returns This collection
145 */
147 {
148 alertable.RegisterAlerts(*this);
149 return *this;
150 }
151
152 /**
153 * \brief Adds a PhoenixAlertable to this collection.
154 *
155 * \param alertable PhoenixAlertable to register
156 * \returns This collection
157 */
159 {
160 return std::move(WithAlertable(alertable));
161 }
162
163 /**
164 * \brief Adds an alertable to this collection.
165 *
166 * \tparam F Type of the function registering the alerts
167 * \param alertable Function that registers the alerts
168 * \returns This collection
169 */
170 template <std::invocable<AlertableCollection &> F>
171 AlertableCollection &WithAlertable(F const &alertable) &
172 {
173 alertable(*this);
174 return *this;
175 }
176
177 /**
178 * \brief Adds an alertable to this collection.
179 *
180 * \tparam F Type of the function registering the alerts
181 * \param alertable Function that registers the alerts
182 * \returns This collection
183 */
184 template <std::invocable<AlertableCollection &> F>
185 AlertableCollection &&WithAlertable(F const &alertable) &&
186 {
187 return std::move(WithAlertable(alertable));
188 }
189
190 /**
191 * \brief Adds a PhoenixAlert to this collection.
192 *
193 * \tparam F Type of the function returning live information to report in the alert
194 * \param id Alert identifier, unique within the group
195 * \param level Alert urgency level
196 * \param infoGetter Function returning the #AlertInfo to report
197 * \param text Default alert text to display when the alert is active
198 * \param debounce Debounce time of the alert, or negative to use the default debounce time (1 second)
199 * \returns This collection
200 */
201 template <std::invocable F>
202 requires std::convertible_to<std::invoke_result_t<F>, std::optional<AlertInfo>>
203 AlertableCollection &WithAlert(std::string id, PhoenixAlert::Level level, F &&infoGetter, std::string const &text = "", wpi::units::second_t debounce = -1_s) &
204 {
205 PhoenixAlert alert{group, std::move(id), level, text};
206 if (debounce >= 0_s) {
207 alert.WithDebounce(debounce);
208 }
209 alerts.emplace_back([alert=std::move(alert), infoGetter=std::forward<F>(infoGetter)]() mutable {
210 std::optional<AlertInfo> const result = infoGetter();
211 if (result) {
212 if (result->isActive) {
213 alert.SetActive(result->text);
214 } else {
215 alert.SetInactive();
216 }
217 }
218 });
219 return *this;
220 }
221
222 /**
223 * \brief Adds a PhoenixAlert to this collection.
224 *
225 * \tparam F Type of the function returning live information to report in the alert
226 * \param id Alert identifier, unique within the group
227 * \param level Alert urgency level
228 * \param infoGetter Function returning the #AlertInfo to report
229 * \param text Default alert text to display when the alert is active
230 * \param debounce Debounce time of the alert, or negative to use the default debounce time (1 second)
231 * \returns This collection
232 */
233 template <std::invocable F>
234 requires std::convertible_to<std::invoke_result_t<F>, std::optional<AlertInfo>>
235 AlertableCollection &&WithAlert(std::string id, PhoenixAlert::Level level, F &&infoGetter, std::string const &text = "", wpi::units::second_t debounce = -1_s) &&
236 {
237 return std::move(WithAlert(std::move(id), level, std::forward<F>(infoGetter), text, debounce));
238 }
239
240 /**
241 * \brief Adds a PhoenixMultiAlert to this collection.
242 *
243 * \tparam F Type of the function returning live information to report in the alert
244 * \param id Alert identifier, unique within the group
245 * \param levelFlags Alert urgency levels to include
246 * \param infoGetter Function returning the #MultiAlertInfo to report
247 * \param text Default alert text to display when the alert is active
248 * \param debounce Debounce time of the alert, or negative to use the default debounce time (1 second)
249 * \returns This collection
250 */
251 template <std::invocable F>
252 requires std::convertible_to<std::invoke_result_t<F>, std::optional<MultiAlertInfo>>
253 AlertableCollection &WithMultiAlert(std::string id, uint32_t levelFlags, F &&infoGetter, std::string const &text = "", wpi::units::second_t debounce = -1_s) &
254 {
255 PhoenixMultiAlert alert{group, std::move(id), levelFlags, text};
256 if (debounce >= 0_s) {
257 alert.WithDebounce(debounce);
258 }
259 alerts.emplace_back([alert=std::move(alert), infoGetter=std::forward<F>(infoGetter)]() mutable {
260 std::optional<MultiAlertInfo> const result = infoGetter();
261 if (result) {
262 if (result->isActive) {
263 alert.SetActive(result->level, result->text);
264 } else {
265 alert.SetInactive();
266 }
267 }
268 });
269 return *this;
270 }
271
272 /**
273 * \brief Adds a PhoenixMultiAlert to this collection.
274 *
275 * \tparam F Type of the function returning live information to report in the alert
276 * \param id Alert identifier, unique within the group
277 * \param levelFlags Alert urgency levels to include
278 * \param infoGetter Function returning the #MultiAlertInfo to report
279 * \param text Default alert text to display when the alert is active
280 * \param debounce Debounce time of the alert, or negative to use the default debounce time (1 second)
281 * \returns This collection
282 */
283 template <std::invocable F>
284 requires std::convertible_to<std::invoke_result_t<F>, std::optional<MultiAlertInfo>>
285 AlertableCollection &&WithMultiAlert(std::string id, uint32_t levelFlags, F &&infoGetter, std::string const &text = "", wpi::units::second_t debounce = -1_s) &&
286 {
287 return std::move(WithMultiAlert(std::move(id), levelFlags, std::forward<F>(infoGetter), text, debounce));
288 }
289
290 /**
291 * \brief Adds a function to run before updating any alerts. This can be
292 * used to perform actions such as a bulk signal refresh so individual
293 * alerts do not have to perform refreshes.
294 *
295 * \tparam F Type of the function to register
296 * \param func Function to run before any alerts
297 * \returns This collection
298 */
299 template <std::invocable F>
301 {
302 beforeAlerts.emplace_back(std::forward<F>(func));
303 return *this;
304 }
305
306 /**
307 * \brief Adds a function to run before updating any alerts. This can be
308 * used to perform actions such as a bulk signal refresh so individual
309 * alerts do not have to perform refreshes.
310 *
311 * \tparam F Type of the function to register
312 * \param func Function to run before any alerts
313 * \returns This collection
314 */
315 template <std::invocable F>
317 {
318 return std::move(WithBeforeAlerts(std::forward<F>(func)));
319 }
320 };
321
322}
323}
Cross the Road Electronics End User License Agreement This Software License or “Customer” and Cross The Road Electronics a Michigan based company with its principal place of business located at MI Terms Agreement this End User License Agreement that forms the entire agreement between You and the Company regarding the use of the Software CAN BUS a communication standard designed to allow hardware devices to communicate with each other Leone USA Content refers to content such as text
Definition CTRE_LICENSE.txt:15
Collection of objects that can report alerts.
Definition Alertable.hpp:109
AlertableCollection(std::string group)
Constructs a new collection of alertable objects.
Definition Alertable.hpp:120
AlertableCollection && WithAlertable(F const &alertable) &&
Adds an alertable to this collection.
Definition Alertable.hpp:185
AlertableCollection & WithBeforeAlerts(F &&func) &
Adds a function to run before updating any alerts.
Definition Alertable.hpp:300
AlertableCollection && WithMultiAlert(std::string id, uint32_t levelFlags, F &&infoGetter, std::string const &text="", wpi::units::second_t debounce=-1_s) &&
Adds a PhoenixMultiAlert to this collection.
Definition Alertable.hpp:285
AlertableCollection && WithAlert(std::string id, PhoenixAlert::Level level, F &&infoGetter, std::string const &text="", wpi::units::second_t debounce=-1_s) &&
Adds a PhoenixAlert to this collection.
Definition Alertable.hpp:235
AlertableCollection && WithAlertable(PhoenixAlertable &alertable) &&
Adds a PhoenixAlertable to this collection.
Definition Alertable.hpp:158
AlertableCollection & WithAlert(std::string id, PhoenixAlert::Level level, F &&infoGetter, std::string const &text="", wpi::units::second_t debounce=-1_s) &
Adds a PhoenixAlert to this collection.
Definition Alertable.hpp:203
AlertableCollection && WithBeforeAlerts(F &&func) &&
Adds a function to run before updating any alerts.
Definition Alertable.hpp:316
AlertableCollection & operator=(AlertableCollection &&)=default
AlertableCollection(AlertableCollection &&)=default
AlertableCollection & WithAlertable(F const &alertable) &
Adds an alertable to this collection.
Definition Alertable.hpp:171
AlertableCollection & WithMultiAlert(std::string id, uint32_t levelFlags, F &&infoGetter, std::string const &text="", wpi::units::second_t debounce=-1_s) &
Adds a PhoenixMultiAlert to this collection.
Definition Alertable.hpp:253
void Report() const
Reports all alerts in this collection.
Definition Alertable.hpp:130
AlertableCollection & WithAlertable(PhoenixAlertable &alertable) &
Adds a PhoenixAlertable to this collection.
Definition Alertable.hpp:146
Persistent alert with debouncing.
Definition Alerts.hpp:24
Level
Levels of urgency of the alert.
Definition Alerts.hpp:32
void SetInactive()
Sets the alert state to inactive.
PhoenixAlert & WithDebounce(wpi::units::second_t debounce)
Sets the time debounce of this alert, default 1 second.
void SetActive(char const *text=nullptr)
Sets the alert state to active and optionally changes the text.
Interface for all types that can report a Phoenix alert.
Definition Alertable.hpp:21
virtual void RegisterAlerts(AlertableCollection &collection)=0
Registers all alerts for this type to the provided collection.
PhoenixAlert::Level AlertLevel
Levels of urgency of a registered alert.
Definition Alertable.hpp:26
Persistent multi-level alert with debouncing.
Definition Alerts.hpp:120
PhoenixAlert::Level Level
Definition Alerts.hpp:125
PhoenixMultiAlert & WithDebounce(wpi::units::second_t debounce)
Sets the time debounce of this alert, default 1 second.
void SetActive(Level level, char const *text=nullptr)
Sets the alert state to active at the given level and optionally changes the text.
void SetInactive()
Sets the alert state to inactive.
Definition ExternalFeedbackConfigs.hpp:16
Definition motor_constants.h:14
Information about an alert.
Definition Alertable.hpp:43
bool isActive
Whether the alert is active.
Definition Alertable.hpp:45
std::optional< std::string > text
The new text to report in the alert, or nullopt to leave the text unchanged.
Definition Alertable.hpp:47
static AlertInfo Inactive()
Creates new information for an inactive alert.
Definition Alertable.hpp:54
static AlertInfo Active(S &&text=std::nullopt)
Creates new information for an active alert.
Definition Alertable.hpp:64
Information about a multi-level alert.
Definition Alertable.hpp:73
PhoenixMultiAlert::Level level
The new alert urgency level.
Definition Alertable.hpp:77
static MultiAlertInfo Active(PhoenixMultiAlert::Level level, S &&text=std::nullopt)
Creates new information for an active multi-level alert.
Definition Alertable.hpp:97
static MultiAlertInfo Inactive()
Creates new information for an inactive multi-level alert.
Definition Alertable.hpp:86
std::optional< std::string > text
The new text to report in the alert, or nullopt to leave the text unchanged.
Definition Alertable.hpp:79
bool isActive
Whether the multi-level alert is active.
Definition Alertable.hpp:75