mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-07-05 03:21:42 +00:00
SCRIPT Move cc files
This commit is contained in:
committed by
Peter Johnson
parent
10b4a0c971
commit
7ca1be9bae
141
wpilibc/src/main/native/include/wpi/util/Alert.hpp
Normal file
141
wpilibc/src/main/native/include/wpi/util/Alert.hpp
Normal file
@@ -0,0 +1,141 @@
|
||||
// Copyright (c) FIRST and other WPILib contributors.
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <set>
|
||||
#include <string>
|
||||
|
||||
namespace frc {
|
||||
|
||||
/**
|
||||
* Persistent alert to be sent via NetworkTables. Alerts are tagged with a type
|
||||
* of kError, kWarning, or kInfo to denote urgency. See Alert::AlertType for
|
||||
* suggested usage of each type. Alerts can be displayed on supported
|
||||
* dashboards, and are shown in a priority order based on type and recency of
|
||||
* activation, with newly activated alerts first.
|
||||
*
|
||||
* Alerts should be created once and stored persistently, then updated to
|
||||
* "active" or "inactive" as necessary. Set(bool) can be safely called
|
||||
* periodically.
|
||||
*
|
||||
* This API is new for 2025, but is likely to change in future seasons to
|
||||
* facilitate deeper integration with the robot control system.
|
||||
*
|
||||
* <pre>
|
||||
* class Robot {
|
||||
* frc::Alert alert{"Something went wrong", frc::Alert::AlertType::kWarning};
|
||||
* }
|
||||
*
|
||||
* Robot::periodic() {
|
||||
* alert.Set(...);
|
||||
* }
|
||||
* </pre>
|
||||
*/
|
||||
class Alert {
|
||||
public:
|
||||
/**
|
||||
* Represents an alert's level of urgency.
|
||||
*/
|
||||
enum class AlertType {
|
||||
/**
|
||||
* High priority alert - displayed first on the dashboard with a red "X"
|
||||
* symbol. Use this type for problems which will seriously affect the
|
||||
* robot's functionality and thus require immediate attention.
|
||||
*/
|
||||
kError,
|
||||
|
||||
/**
|
||||
* Medium priority alert - displayed second on the dashboard with a yellow
|
||||
* "!" symbol. Use this type for problems which could affect the robot's
|
||||
* functionality but do not necessarily require immediate attention.
|
||||
*/
|
||||
kWarning,
|
||||
|
||||
/**
|
||||
* Low priority alert - displayed last on the dashboard with a green "i"
|
||||
* symbol. Use this type for problems which are unlikely to affect the
|
||||
* robot's functionality, or any other alerts which do not fall under the
|
||||
* other categories.
|
||||
*/
|
||||
kInfo
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a new alert in the default group - "Alerts". If this is the first
|
||||
* to be instantiated, the appropriate entries will be added to NetworkTables.
|
||||
*
|
||||
* @param text Text to be displayed when the alert is active.
|
||||
* @param type Alert urgency level.
|
||||
*/
|
||||
Alert(std::string_view text, AlertType type);
|
||||
|
||||
/**
|
||||
* Creates a new alert. If this is the first to be instantiated in its group,
|
||||
* the appropriate entries will be added to NetworkTables.
|
||||
*
|
||||
* @param group Group identifier, used as the entry name in NetworkTables.
|
||||
* @param text Text to be displayed when the alert is active.
|
||||
* @param type Alert urgency level.
|
||||
*/
|
||||
Alert(std::string_view group, std::string_view text, AlertType type);
|
||||
|
||||
Alert(Alert&&);
|
||||
Alert& operator=(Alert&&);
|
||||
|
||||
Alert(const Alert&) = default;
|
||||
Alert& operator=(const Alert&) = default;
|
||||
|
||||
~Alert();
|
||||
|
||||
/**
|
||||
* Sets whether the alert should currently be displayed. This method can be
|
||||
* safely called periodically.
|
||||
*
|
||||
* @param active Whether to display the alert.
|
||||
*/
|
||||
void Set(bool active);
|
||||
|
||||
/**
|
||||
* Gets whether the alert is active.
|
||||
* @return whether the alert is active.
|
||||
*/
|
||||
bool Get() const { return m_active; }
|
||||
|
||||
/**
|
||||
* Updates current alert text. Use this method to dynamically change the
|
||||
* displayed alert, such as including more details about the detected problem.
|
||||
*
|
||||
* @param text Text to be displayed when the alert is active.
|
||||
*/
|
||||
void SetText(std::string_view text);
|
||||
|
||||
/**
|
||||
* Gets the current alert text.
|
||||
* @return the current text.
|
||||
*/
|
||||
std::string GetText() const { return m_text; }
|
||||
|
||||
/**
|
||||
* Get the type of this alert.
|
||||
* @return the type
|
||||
*/
|
||||
AlertType GetType() const { return m_type; }
|
||||
|
||||
private:
|
||||
class PublishedAlert;
|
||||
class SendableAlerts;
|
||||
|
||||
AlertType m_type;
|
||||
std::string m_text;
|
||||
std::set<PublishedAlert>* m_activeAlerts;
|
||||
bool m_active = false;
|
||||
uint64_t m_activeStartTime;
|
||||
};
|
||||
|
||||
std::string format_as(Alert::AlertType type);
|
||||
|
||||
} // namespace frc
|
||||
1055
wpilibc/src/main/native/include/wpi/util/Color.hpp
Normal file
1055
wpilibc/src/main/native/include/wpi/util/Color.hpp
Normal file
File diff suppressed because it is too large
Load Diff
128
wpilibc/src/main/native/include/wpi/util/Color8Bit.hpp
Normal file
128
wpilibc/src/main/native/include/wpi/util/Color8Bit.hpp
Normal file
@@ -0,0 +1,128 @@
|
||||
// Copyright (c) FIRST and other WPILib contributors.
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
#include <fmt/format.h>
|
||||
#include <wpi/StringExtras.h>
|
||||
#include <wpi/ct_string.h>
|
||||
|
||||
#include "Color.h"
|
||||
|
||||
namespace frc {
|
||||
|
||||
/**
|
||||
* Represents colors that can be used with Addressable LEDs.
|
||||
*/
|
||||
class Color8Bit {
|
||||
public:
|
||||
/**
|
||||
* Constructs a default color (black).
|
||||
*/
|
||||
constexpr Color8Bit() = default;
|
||||
|
||||
/**
|
||||
* Constructs a Color8Bit.
|
||||
*
|
||||
* @param r Red value (0-255)
|
||||
* @param g Green value (0-255)
|
||||
* @param b Blue value (0-255)
|
||||
*/
|
||||
constexpr Color8Bit(int r, int g, int b)
|
||||
: red(std::clamp(r, 0, 255)),
|
||||
green(std::clamp(g, 0, 255)),
|
||||
blue(std::clamp(b, 0, 255)) {}
|
||||
|
||||
/**
|
||||
* Constructs a Color8Bit from a Color.
|
||||
*
|
||||
* @param color The color
|
||||
*/
|
||||
constexpr Color8Bit(const Color& color) // NOLINT
|
||||
: red(color.red * 255),
|
||||
green(color.green * 255),
|
||||
blue(color.blue * 255) {}
|
||||
|
||||
/**
|
||||
* Constructs a Color8Bit from a hex string.
|
||||
*
|
||||
* @param hexString a string of the format <tt>\#RRGGBB</tt>
|
||||
* @throws std::invalid_argument if the hex string is invalid.
|
||||
*/
|
||||
explicit constexpr Color8Bit(std::string_view hexString) {
|
||||
if (hexString.length() != 7 || !hexString.starts_with("#") ||
|
||||
!wpi::isHexDigit(hexString[1]) || !wpi::isHexDigit(hexString[2]) ||
|
||||
!wpi::isHexDigit(hexString[3]) || !wpi::isHexDigit(hexString[4]) ||
|
||||
!wpi::isHexDigit(hexString[5]) || !wpi::isHexDigit(hexString[6])) {
|
||||
throw std::invalid_argument(
|
||||
fmt::format("Invalid hex string for Color \"{}\"", hexString));
|
||||
}
|
||||
|
||||
red = wpi::hexDigitValue(hexString[1]) * 16 +
|
||||
wpi::hexDigitValue(hexString[2]);
|
||||
green = wpi::hexDigitValue(hexString[3]) * 16 +
|
||||
wpi::hexDigitValue(hexString[4]);
|
||||
blue = wpi::hexDigitValue(hexString[5]) * 16 +
|
||||
wpi::hexDigitValue(hexString[6]);
|
||||
}
|
||||
|
||||
constexpr bool operator==(const Color8Bit&) const = default;
|
||||
|
||||
constexpr operator Color() const { // NOLINT
|
||||
return Color(red / 255.0, green / 255.0, blue / 255.0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a Color8Bit from a hex string.
|
||||
*
|
||||
* @param hexString a string of the format <tt>\#RRGGBB</tt>
|
||||
* @return Color8Bit object from hex string.
|
||||
* @throws std::invalid_argument if the hex string is invalid.
|
||||
*/
|
||||
static constexpr Color8Bit FromHexString(std::string_view hexString) {
|
||||
if (hexString.length() != 7 || !hexString.starts_with("#") ||
|
||||
!wpi::isHexDigit(hexString[1]) || !wpi::isHexDigit(hexString[2]) ||
|
||||
!wpi::isHexDigit(hexString[3]) || !wpi::isHexDigit(hexString[4]) ||
|
||||
!wpi::isHexDigit(hexString[5]) || !wpi::isHexDigit(hexString[6])) {
|
||||
throw std::invalid_argument(
|
||||
fmt::format("Invalid hex string for Color \"{}\"", hexString));
|
||||
}
|
||||
|
||||
int r = wpi::hexDigitValue(hexString[0]) * 16 +
|
||||
wpi::hexDigitValue(hexString[1]);
|
||||
int g = wpi::hexDigitValue(hexString[2]) * 16 +
|
||||
wpi::hexDigitValue(hexString[3]);
|
||||
int b = wpi::hexDigitValue(hexString[4]) * 16 +
|
||||
wpi::hexDigitValue(hexString[5]);
|
||||
return Color8Bit{r, g, b};
|
||||
}
|
||||
|
||||
/**
|
||||
* Return this color represented as a hex string.
|
||||
*
|
||||
* @return a string of the format <tt>\#RRGGBB</tt>
|
||||
*/
|
||||
constexpr auto HexString() const {
|
||||
return wpi::ct_string<char, std::char_traits<char>, 7>{
|
||||
{'#', wpi::hexdigit(red / 16), wpi::hexdigit(red % 16),
|
||||
wpi::hexdigit(green / 16), wpi::hexdigit(green % 16),
|
||||
wpi::hexdigit(blue / 16), wpi::hexdigit(blue % 16)}};
|
||||
}
|
||||
|
||||
/// Red component (0-255).
|
||||
int red = 0;
|
||||
|
||||
/// Green component (0-255).
|
||||
int green = 0;
|
||||
|
||||
/// Blue component (0-255).
|
||||
int blue = 0;
|
||||
};
|
||||
|
||||
} // namespace frc
|
||||
221
wpilibc/src/main/native/include/wpi/util/Preferences.hpp
Normal file
221
wpilibc/src/main/native/include/wpi/util/Preferences.hpp
Normal file
@@ -0,0 +1,221 @@
|
||||
// Copyright (c) FIRST and other WPILib contributors.
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
namespace frc {
|
||||
|
||||
/**
|
||||
* The preferences class provides a relatively simple way to save important
|
||||
* values to the roboRIO to access the next time the roboRIO is booted.
|
||||
*
|
||||
* This class loads and saves from a file inside the roboRIO. The user cannot
|
||||
* access the file directly, but may modify values at specific fields which will
|
||||
* then be automatically periodically saved to the file by the NetworkTable
|
||||
* server.
|
||||
*
|
||||
* This class is thread safe.
|
||||
*
|
||||
* This will also interact with NetworkTable by creating a table called
|
||||
* "Preferences" with all the key-value pairs.
|
||||
*/
|
||||
class Preferences {
|
||||
public:
|
||||
/**
|
||||
* Returns a vector of all the keys.
|
||||
*
|
||||
* @return a vector of the keys
|
||||
*/
|
||||
static std::vector<std::string> GetKeys();
|
||||
|
||||
/**
|
||||
* Returns the string at the given key. If this table does not have a value
|
||||
* for that position, then the given defaultValue will be returned.
|
||||
*
|
||||
* @param key the key
|
||||
* @param defaultValue the value to return if none exists in the table
|
||||
* @return either the value in the table, or the defaultValue
|
||||
*/
|
||||
static std::string GetString(std::string_view key,
|
||||
std::string_view defaultValue = "");
|
||||
|
||||
/**
|
||||
* Returns the int at the given key. If this table does not have a value for
|
||||
* that position, then the given defaultValue value will be returned.
|
||||
*
|
||||
* @param key the key
|
||||
* @param defaultValue the value to return if none exists in the table
|
||||
* @return either the value in the table, or the defaultValue
|
||||
*/
|
||||
static int GetInt(std::string_view key, int defaultValue = 0);
|
||||
|
||||
/**
|
||||
* Returns the double at the given key. If this table does not have a value
|
||||
* for that position, then the given defaultValue value will be returned.
|
||||
*
|
||||
* @param key the key
|
||||
* @param defaultValue the value to return if none exists in the table
|
||||
* @return either the value in the table, or the defaultValue
|
||||
*/
|
||||
static double GetDouble(std::string_view key, double defaultValue = 0.0);
|
||||
|
||||
/**
|
||||
* Returns the float at the given key. If this table does not have a value
|
||||
* for that position, then the given defaultValue value will be returned.
|
||||
*
|
||||
* @param key the key
|
||||
* @param defaultValue the value to return if none exists in the table
|
||||
* @return either the value in the table, or the defaultValue
|
||||
*/
|
||||
static float GetFloat(std::string_view key, float defaultValue = 0.0);
|
||||
|
||||
/**
|
||||
* Returns the boolean at the given key. If this table does not have a value
|
||||
* for that position, then the given defaultValue value will be returned.
|
||||
*
|
||||
* @param key the key
|
||||
* @param defaultValue the value to return if none exists in the table
|
||||
* @return either the value in the table, or the defaultValue
|
||||
*/
|
||||
static bool GetBoolean(std::string_view key, bool defaultValue = false);
|
||||
|
||||
/**
|
||||
* Returns the long (int64_t) at the given key. If this table does not have a
|
||||
* value for that position, then the given defaultValue value will be
|
||||
* returned.
|
||||
*
|
||||
* @param key the key
|
||||
* @param defaultValue the value to return if none exists in the table
|
||||
* @return either the value in the table, or the defaultValue
|
||||
*/
|
||||
static int64_t GetLong(std::string_view key, int64_t defaultValue = 0);
|
||||
|
||||
/**
|
||||
* Puts the given string into the preferences table.
|
||||
*
|
||||
* The value may not have quotation marks, nor may the key have any whitespace
|
||||
* nor an equals sign.
|
||||
*
|
||||
* @param key the key
|
||||
* @param value the value
|
||||
*/
|
||||
static void SetString(std::string_view key, std::string_view value);
|
||||
|
||||
/**
|
||||
* Puts the given string into the preferences table if it doesn't
|
||||
* already exist.
|
||||
*/
|
||||
static void InitString(std::string_view key, std::string_view value);
|
||||
|
||||
/**
|
||||
* Puts the given int into the preferences table.
|
||||
*
|
||||
* The key may not have any whitespace nor an equals sign.
|
||||
*
|
||||
* @param key the key
|
||||
* @param value the value
|
||||
*/
|
||||
static void SetInt(std::string_view key, int value);
|
||||
|
||||
/**
|
||||
* Puts the given int into the preferences table if it doesn't
|
||||
* already exist.
|
||||
*/
|
||||
static void InitInt(std::string_view key, int value);
|
||||
|
||||
/**
|
||||
* Puts the given double into the preferences table.
|
||||
*
|
||||
* The key may not have any whitespace nor an equals sign.
|
||||
*
|
||||
* @param key the key
|
||||
* @param value the value
|
||||
*/
|
||||
static void SetDouble(std::string_view key, double value);
|
||||
|
||||
/**
|
||||
* Puts the given double into the preferences table if it doesn't
|
||||
* already exist.
|
||||
*/
|
||||
static void InitDouble(std::string_view key, double value);
|
||||
|
||||
/**
|
||||
* Puts the given float into the preferences table.
|
||||
*
|
||||
* The key may not have any whitespace nor an equals sign.
|
||||
*
|
||||
* @param key the key
|
||||
* @param value the value
|
||||
*/
|
||||
static void SetFloat(std::string_view key, float value);
|
||||
|
||||
/**
|
||||
* Puts the given float into the preferences table if it doesn't
|
||||
* already exist.
|
||||
*/
|
||||
static void InitFloat(std::string_view key, float value);
|
||||
|
||||
/**
|
||||
* Puts the given boolean into the preferences table.
|
||||
*
|
||||
* The key may not have any whitespace nor an equals sign.
|
||||
*
|
||||
* @param key the key
|
||||
* @param value the value
|
||||
*/
|
||||
static void SetBoolean(std::string_view key, bool value);
|
||||
|
||||
/**
|
||||
* Puts the given boolean into the preferences table if it doesn't
|
||||
* already exist.
|
||||
*/
|
||||
static void InitBoolean(std::string_view key, bool value);
|
||||
|
||||
/**
|
||||
* Puts the given long (int64_t) into the preferences table.
|
||||
*
|
||||
* The key may not have any whitespace nor an equals sign.
|
||||
*
|
||||
* @param key the key
|
||||
* @param value the value
|
||||
*/
|
||||
static void SetLong(std::string_view key, int64_t value);
|
||||
|
||||
/**
|
||||
* Puts the given long into the preferences table if it doesn't
|
||||
* already exist.
|
||||
*/
|
||||
static void InitLong(std::string_view key, int64_t value);
|
||||
|
||||
/**
|
||||
* Returns whether or not there is a key with the given name.
|
||||
*
|
||||
* @param key the key
|
||||
* @return if there is a value at the given key
|
||||
*/
|
||||
static bool ContainsKey(std::string_view key);
|
||||
|
||||
/**
|
||||
* Remove a preference.
|
||||
*
|
||||
* @param key the key
|
||||
*/
|
||||
static void Remove(std::string_view key);
|
||||
|
||||
/**
|
||||
* Remove all preferences.
|
||||
*/
|
||||
static void RemoveAll();
|
||||
|
||||
private:
|
||||
Preferences() = default;
|
||||
};
|
||||
|
||||
} // namespace frc
|
||||
66
wpilibc/src/main/native/include/wpi/util/SensorUtil.hpp
Normal file
66
wpilibc/src/main/native/include/wpi/util/SensorUtil.hpp
Normal file
@@ -0,0 +1,66 @@
|
||||
// Copyright (c) FIRST and other WPILib contributors.
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace frc {
|
||||
|
||||
/**
|
||||
* Stores most recent status information as well as containing utility functions
|
||||
* for checking channels and error processing.
|
||||
*/
|
||||
class SensorUtil final {
|
||||
public:
|
||||
SensorUtil() = delete;
|
||||
|
||||
/**
|
||||
* Get the number of the default solenoid module.
|
||||
*
|
||||
* @return The number of the default solenoid module.
|
||||
*/
|
||||
static int GetDefaultCTREPCMModule();
|
||||
|
||||
/**
|
||||
* Get the number of the default solenoid module.
|
||||
*
|
||||
* @return The number of the default solenoid module.
|
||||
*/
|
||||
static int GetDefaultREVPHModule();
|
||||
|
||||
/**
|
||||
* Check that the digital channel number is valid.
|
||||
*
|
||||
* Verify that the channel number is one of the legal channel numbers. Channel
|
||||
* numbers are 0-based.
|
||||
*
|
||||
* @return Digital channel is valid
|
||||
*/
|
||||
static bool CheckDigitalChannel(int channel);
|
||||
|
||||
/**
|
||||
* Check that the PWM channel number is valid.
|
||||
*
|
||||
* Verify that the channel number is one of the legal channel numbers. Channel
|
||||
* numbers are 0-based.
|
||||
*
|
||||
* @return PWM channel is valid
|
||||
*/
|
||||
static bool CheckPWMChannel(int channel);
|
||||
|
||||
/**
|
||||
* Check that the analog input number is value.
|
||||
*
|
||||
* Verify that the analog input number is one of the legal channel numbers.
|
||||
* Channel numbers are 0-based.
|
||||
*
|
||||
* @return Analog channel is valid
|
||||
*/
|
||||
static bool CheckAnalogInputChannel(int channel);
|
||||
|
||||
static int GetNumDigitalChannels();
|
||||
static int GetNumAnalogInputs();
|
||||
static int GetNumPwmChannels();
|
||||
};
|
||||
|
||||
} // namespace frc
|
||||
Reference in New Issue
Block a user