SCRIPT Move cc files

This commit is contained in:
PJ Reiniger
2025-11-07 19:55:39 -05:00
committed by Peter Johnson
parent 10b4a0c971
commit 7ca1be9bae
1197 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,187 @@
// 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.
#include "frc/Alert.h"
#include <set>
#include <string>
#include <utility>
#include <vector>
#include <fmt/format.h>
#include <networktables/NTSendable.h>
#include <networktables/NTSendableBuilder.h>
#include <wpi/StringMap.h>
#include <wpi/sendable/SendableHelper.h>
#include <wpi/sendable/SendableRegistry.h>
#include "frc/Errors.h"
#include "frc/RobotController.h"
#include "frc/smartdashboard/SmartDashboard.h"
using namespace frc;
class Alert::PublishedAlert {
public:
PublishedAlert(uint64_t timestamp, std::string_view text)
: timestamp{timestamp}, text{text} {}
uint64_t timestamp;
std::string text;
auto operator<=>(const PublishedAlert& other) const {
if (timestamp != other.timestamp) {
return other.timestamp <=> timestamp;
} else {
return text <=> other.text;
}
}
};
class Alert::SendableAlerts : public nt::NTSendable,
public wpi::SendableHelper<SendableAlerts> {
public:
SendableAlerts() { m_alerts.fill({}); }
void InitSendable(nt::NTSendableBuilder& builder) override {
builder.SetSmartDashboardType("Alerts");
builder.AddStringArrayProperty(
"errors", [this]() { return GetStrings(AlertType::kError); }, nullptr);
builder.AddStringArrayProperty(
"warnings", [this]() { return GetStrings(AlertType::kWarning); },
nullptr);
builder.AddStringArrayProperty(
"infos", [this]() { return GetStrings(AlertType::kInfo); }, nullptr);
}
/**
* Returns a reference to the set of active alerts for the given type.
* @param type the type
* @return reference to the set of active alerts for the type
*/
std::set<PublishedAlert>& GetActiveAlertsStorage(AlertType type) {
return const_cast<std::set<Alert::PublishedAlert>&>(
std::as_const(*this).GetActiveAlertsStorage(type));
}
const std::set<PublishedAlert>& GetActiveAlertsStorage(AlertType type) const {
switch (type) {
case AlertType::kInfo:
case AlertType::kWarning:
case AlertType::kError:
return m_alerts[static_cast<int32_t>(type)];
default:
throw FRC_MakeError(frc::err::InvalidParameter,
"Invalid Alert Type: {}", type);
}
}
/**
* Returns the SendableAlerts for a given group, initializing and publishing
* if it does not already exist.
* @param group the group name
* @return the SendableAlerts for the group
*/
static SendableAlerts& ForGroup(std::string_view group) {
SendableAlerts* salert = nullptr;
try {
auto* sendable = frc::SmartDashboard::GetData(group);
salert = dynamic_cast<SendableAlerts*>(sendable);
} catch (frc::RuntimeError&) {
}
if (!salert) {
// this leaks if ResetSmartDashboardInstance is called, but that's fine
salert = new Alert::SendableAlerts;
frc::SmartDashboard::PutData(group, salert);
}
return *salert;
}
private:
std::vector<std::string> GetStrings(AlertType type) const {
auto& set = GetActiveAlertsStorage(type);
std::vector<std::string> output;
output.reserve(set.size());
for (auto& alert : set) {
output.emplace_back(alert.text);
}
return output;
}
std::array<std::set<PublishedAlert>, 3> m_alerts;
};
Alert::Alert(std::string_view text, AlertType type)
: Alert("Alerts", text, type) {}
Alert::Alert(std::string_view group, std::string_view text, AlertType type)
: m_type(type),
m_text(text),
m_activeAlerts{
&SendableAlerts::ForGroup(group).GetActiveAlertsStorage(m_type)} {}
Alert::Alert(Alert&& other)
: m_type{other.m_type},
m_text{std::move(other.m_text)},
m_activeAlerts{std::exchange(other.m_activeAlerts, nullptr)},
m_active{std::exchange(other.m_active, false)},
m_activeStartTime{other.m_activeStartTime} {}
Alert& Alert::operator=(Alert&& other) {
if (&other != this) {
// We want to destroy current state after the move is done
Alert tmp{std::move(*this)};
// Now, swap moved-from state with other state
std::swap(m_type, other.m_type);
std::swap(m_text, other.m_text);
std::swap(m_activeAlerts, other.m_activeAlerts);
std::swap(m_active, other.m_active);
std::swap(m_activeStartTime, other.m_activeStartTime);
}
return *this;
}
Alert::~Alert() {
Set(false);
}
void Alert::Set(bool active) {
if (active == m_active) {
return;
}
if (active) {
m_activeStartTime = frc::RobotController::GetTime();
m_activeAlerts->emplace(m_activeStartTime, m_text);
} else {
m_activeAlerts->erase({m_activeStartTime, m_text});
}
m_active = active;
}
void Alert::SetText(std::string_view text) {
if (text == m_text) {
return;
}
std::string oldText = std::move(m_text);
m_text = text;
if (m_active) {
auto iter = m_activeAlerts->find({m_activeStartTime, oldText});
auto hint = m_activeAlerts->erase(iter);
m_activeAlerts->emplace_hint(hint, m_activeStartTime, m_text);
}
}
std::string frc::format_as(Alert::AlertType type) {
switch (type) {
case Alert::AlertType::kInfo:
return "kInfo";
case Alert::AlertType::kWarning:
return "kWarning";
case Alert::AlertType::kError:
return "kError";
default:
return std::to_string(static_cast<int>(type));
}
}

View File

@@ -0,0 +1,183 @@
// 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.
#include "frc/Preferences.h"
#include <memory>
#include <string>
#include <vector>
#include <fmt/format.h>
#include <hal/UsageReporting.h>
#include <networktables/MultiSubscriber.h>
#include <networktables/NetworkTable.h>
#include <networktables/NetworkTableInstance.h>
#include <networktables/NetworkTableListener.h>
#include <networktables/StringTopic.h>
#include <wpi/json.h>
using namespace frc;
// The Preferences table name
static constexpr std::string_view kTableName{"Preferences"};
static constexpr std::string_view kSmartDashboardType = "RobotPreferences";
namespace {
struct Instance {
Instance();
std::shared_ptr<nt::NetworkTable> table{
nt::NetworkTableInstance::GetDefault().GetTable(kTableName)};
nt::StringPublisher typePublisher{table->GetStringTopic(".type").PublishEx(
nt::StringTopic::kTypeString, {{"SmartDashboard", kSmartDashboardType}})};
nt::MultiSubscriber tableSubscriber{nt::NetworkTableInstance::GetDefault(),
{{fmt::format("{}/", table->GetPath())}}};
nt::NetworkTableListener listener;
};
} // namespace
static Instance& GetInstance() {
static Instance instance;
return instance;
}
#ifndef __FRC_SYSTEMCORE__
namespace frc::impl {
void ResetPreferencesInstance() {
GetInstance() = Instance();
}
} // namespace frc::impl
#endif
std::vector<std::string> Preferences::GetKeys() {
return ::GetInstance().table->GetKeys();
}
std::string Preferences::GetString(std::string_view key,
std::string_view defaultValue) {
return ::GetInstance().table->GetEntry(key).GetString(defaultValue);
}
int Preferences::GetInt(std::string_view key, int defaultValue) {
return ::GetInstance().table->GetEntry(key).GetInteger(defaultValue);
}
double Preferences::GetDouble(std::string_view key, double defaultValue) {
return ::GetInstance().table->GetEntry(key).GetDouble(defaultValue);
}
float Preferences::GetFloat(std::string_view key, float defaultValue) {
return ::GetInstance().table->GetEntry(key).GetFloat(defaultValue);
}
bool Preferences::GetBoolean(std::string_view key, bool defaultValue) {
return ::GetInstance().table->GetEntry(key).GetBoolean(defaultValue);
}
int64_t Preferences::GetLong(std::string_view key, int64_t defaultValue) {
return ::GetInstance().table->GetEntry(key).GetInteger(defaultValue);
}
void Preferences::SetString(std::string_view key, std::string_view value) {
auto entry = ::GetInstance().table->GetEntry(key);
entry.SetString(value);
entry.SetPersistent();
}
void Preferences::InitString(std::string_view key, std::string_view value) {
auto entry = ::GetInstance().table->GetEntry(key);
entry.SetDefaultString(value);
entry.SetPersistent();
}
void Preferences::SetInt(std::string_view key, int value) {
auto entry = ::GetInstance().table->GetEntry(key);
entry.SetInteger(value);
entry.SetPersistent();
}
void Preferences::InitInt(std::string_view key, int value) {
auto entry = ::GetInstance().table->GetEntry(key);
entry.SetDefaultInteger(value);
entry.SetPersistent();
}
void Preferences::SetDouble(std::string_view key, double value) {
auto entry = ::GetInstance().table->GetEntry(key);
entry.SetDouble(value);
entry.SetPersistent();
}
void Preferences::InitDouble(std::string_view key, double value) {
auto entry = ::GetInstance().table->GetEntry(key);
entry.SetDefaultDouble(value);
entry.SetPersistent();
}
void Preferences::SetFloat(std::string_view key, float value) {
auto entry = ::GetInstance().table->GetEntry(key);
entry.SetFloat(value);
entry.SetPersistent();
}
void Preferences::InitFloat(std::string_view key, float value) {
auto entry = ::GetInstance().table->GetEntry(key);
entry.SetDefaultFloat(value);
entry.SetPersistent();
}
void Preferences::SetBoolean(std::string_view key, bool value) {
auto entry = ::GetInstance().table->GetEntry(key);
entry.SetBoolean(value);
entry.SetPersistent();
}
void Preferences::InitBoolean(std::string_view key, bool value) {
auto entry = ::GetInstance().table->GetEntry(key);
entry.SetDefaultBoolean(value);
entry.SetPersistent();
}
void Preferences::SetLong(std::string_view key, int64_t value) {
auto entry = ::GetInstance().table->GetEntry(key);
entry.SetInteger(value);
entry.SetPersistent();
}
void Preferences::InitLong(std::string_view key, int64_t value) {
auto entry = ::GetInstance().table->GetEntry(key);
entry.SetDefaultInteger(value);
entry.SetPersistent();
}
bool Preferences::ContainsKey(std::string_view key) {
return ::GetInstance().table->ContainsKey(key);
}
void Preferences::Remove(std::string_view key) {
auto entry = ::GetInstance().table->GetEntry(key);
entry.ClearPersistent();
entry.Unpublish();
}
void Preferences::RemoveAll() {
for (auto preference : GetKeys()) {
if (preference != ".type") {
Remove(preference);
}
}
}
Instance::Instance() {
typePublisher.Set(kSmartDashboardType);
listener = nt::NetworkTableListener::CreateListener(
tableSubscriber, NT_EVENT_PUBLISH | NT_EVENT_IMMEDIATE,
[typeTopic = typePublisher.GetTopic().GetHandle()](auto& event) {
if (auto topicInfo = event.GetTopicInfo()) {
if (topicInfo->topic != typeTopic) {
nt::SetTopicPersistent(topicInfo->topic, true);
}
}
});
HAL_ReportUsage("Preferences", "");
}

View File

@@ -0,0 +1,44 @@
// 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.
#include "frc/SensorUtil.h"
#include <hal/AnalogInput.h>
#include <hal/DIO.h>
#include <hal/PWM.h>
#include <hal/Ports.h>
using namespace frc;
int SensorUtil::GetDefaultCTREPCMModule() {
return 0;
}
int SensorUtil::GetDefaultREVPHModule() {
return 1;
}
bool SensorUtil::CheckDigitalChannel(int channel) {
return HAL_CheckDIOChannel(channel);
}
bool SensorUtil::CheckPWMChannel(int channel) {
return HAL_CheckPWMChannel(channel);
}
bool SensorUtil::CheckAnalogInputChannel(int channel) {
return HAL_CheckAnalogInputChannel(channel);
}
int SensorUtil::GetNumDigitalChannels() {
return HAL_GetNumDigitalChannels();
}
int SensorUtil::GetNumAnalogInputs() {
return HAL_GetNumAnalogInputs();
}
int SensorUtil::GetNumPwmChannels() {
return HAL_GetNumPWMChannels();
}