WPILibC++ 2025.1.1
Loading...
Searching...
No Matches
Alert.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <stdint.h>
8
9#include <set>
10#include <string>
11
12namespace frc {
13
14/**
15 * Persistent alert to be sent via NetworkTables. Alerts are tagged with a type
16 * of kError, kWarning, or kInfo to denote urgency. See Alert::AlertType for
17 * suggested usage of each type. Alerts can be displayed on supported
18 * dashboards, and are shown in a priority order based on type and recency of
19 * activation, with newly activated alerts first.
20 *
21 * Alerts should be created once and stored persistently, then updated to
22 * "active" or "inactive" as necessary. Set(bool) can be safely called
23 * periodically.
24 *
25 * This API is new for 2025, but is likely to change in future seasons to
26 * facilitate deeper integration with the robot control system.
27 *
28 * <pre>
29 * class Robot {
30 * frc::Alert alert{"Something went wrong", frc::Alert::AlertType::kWarning};
31 * }
32 *
33 * Robot::periodic() {
34 * alert.Set(...);
35 * }
36 * </pre>
37 */
38class Alert {
39 public:
40 /**
41 * Represents an alert's level of urgency.
42 */
43 enum class AlertType {
44 /**
45 * High priority alert - displayed first on the dashboard with a red "X"
46 * symbol. Use this type for problems which will seriously affect the
47 * robot's functionality and thus require immediate attention.
48 */
49 kError,
50
51 /**
52 * Medium priority alert - displayed second on the dashboard with a yellow
53 * "!" symbol. Use this type for problems which could affect the robot's
54 * functionality but do not necessarily require immediate attention.
55 */
57
58 /**
59 * Low priority alert - displayed last on the dashboard with a green "i"
60 * symbol. Use this type for problems which are unlikely to affect the
61 * robot's functionality, or any other alerts which do not fall under the
62 * other categories.
63 */
64 kInfo
65 };
66
67 /**
68 * Creates a new alert in the default group - "Alerts". If this is the first
69 * to be instantiated, the appropriate entries will be added to NetworkTables.
70 *
71 * @param text Text to be displayed when the alert is active.
72 * @param type Alert urgency level.
73 */
74 Alert(std::string_view text, AlertType type);
75
76 /**
77 * Creates a new alert. If this is the first to be instantiated in its group,
78 * the appropriate entries will be added to NetworkTables.
79 *
80 * @param group Group identifier, used as the entry name in NetworkTables.
81 * @param text Text to be displayed when the alert is active.
82 * @param type Alert urgency level.
83 */
84 Alert(std::string_view group, std::string_view text, AlertType type);
85
88
89 Alert(const Alert&) = default;
90 Alert& operator=(const Alert&) = default;
91
93
94 /**
95 * Sets whether the alert should currently be displayed. This method can be
96 * safely called periodically.
97 *
98 * @param active Whether to display the alert.
99 */
100 void Set(bool active);
101
102 /**
103 * Gets whether the alert is active.
104 * @return whether the alert is active.
105 */
106 bool Get() const { return m_active; }
107
108 /**
109 * Updates current alert text. Use this method to dynamically change the
110 * displayed alert, such as including more details about the detected problem.
111 *
112 * @param text Text to be displayed when the alert is active.
113 */
114 void SetText(std::string_view text);
115
116 /**
117 * Gets the current alert text.
118 * @return the current text.
119 */
120 std::string GetText() const { return m_text; }
121
122 /**
123 * Get the type of this alert.
124 * @return the type
125 */
126 AlertType GetType() const { return m_type; }
127
128 private:
129 class PublishedAlert;
130 class SendableAlerts;
131
132 AlertType m_type;
133 std::string m_text;
134 std::set<PublishedAlert>* m_activeAlerts;
135 bool m_active = false;
136 uint64_t m_activeStartTime;
137};
138
139std::string format_as(Alert::AlertType type);
140
141} // namespace frc
Persistent alert to be sent via NetworkTables.
Definition Alert.h:38
void SetText(std::string_view text)
Updates current alert text.
Alert & operator=(const Alert &)=default
Alert & operator=(Alert &&)
Alert(Alert &&)
Alert(const Alert &)=default
bool Get() const
Gets whether the alert is active.
Definition Alert.h:106
void Set(bool active)
Sets whether the alert should currently be displayed.
AlertType
Represents an alert's level of urgency.
Definition Alert.h:43
@ kInfo
Low priority alert - displayed last on the dashboard with a green "i" symbol.
@ kError
High priority alert - displayed first on the dashboard with a red "X" symbol.
@ kWarning
Medium priority alert - displayed second on the dashboard with a yellow "!" symbol.
Alert(std::string_view group, std::string_view text, AlertType type)
Creates a new alert.
std::string GetText() const
Gets the current alert text.
Definition Alert.h:120
AlertType GetType() const
Get the type of this alert.
Definition Alert.h:126
Alert(std::string_view text, AlertType type)
Creates a new alert in the default group - "Alerts".
Definition CAN.h:11
std::string format_as(Alert::AlertType type)