Skip to content

Commit

Permalink
Merge branch 'wpilibsuite:main' into ScheduledCommand-decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
narmstro2020 authored Oct 11, 2024
2 parents bc3595b + 679892e commit 8c15f99
Show file tree
Hide file tree
Showing 85 changed files with 1,554 additions and 1,089 deletions.
2 changes: 1 addition & 1 deletion hal/src/main/native/athena/CTREPCM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ HAL_CTREPCMHandle HAL_InitializeCTREPCM(int32_t module,
pcm->previousAllocation);
} else {
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for CTRE PCM", 0,
kNumCTREPCMModules, module);
kNumCTREPCMModules - 1, module);
}
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
}
Expand Down
7 changes: 4 additions & 3 deletions hal/src/main/native/athena/CTREPDP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,9 @@ HAL_PDPHandle HAL_InitializePDP(int32_t module, const char* allocationLocation,
int32_t* status) {
hal::init::CheckInit();
if (!HAL_CheckPDPModule(module)) {
*status = PARAMETER_OUT_OF_RANGE;
hal::SetLastError(status, fmt::format("Invalid pdp module {}", module));
*status = RESOURCE_OUT_OF_RANGE;
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for CTRE PDP", 0,
kNumCTREPDPModules - 1, module);
return HAL_kInvalidHandle;
}

Expand All @@ -147,7 +148,7 @@ HAL_PDPHandle HAL_InitializePDP(int32_t module, const char* allocationLocation,
pdp->previousAllocation);
} else {
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for CTRE PDP", 0,
kNumCTREPDPModules, module);
kNumCTREPDPModules - 1, module);
}
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
}
Expand Down
59 changes: 41 additions & 18 deletions hal/src/main/native/athena/LEDs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@

#include <unistd.h>

#include <cstring>
#include <fstream>

#include <fmt/format.h>
#include <fmt/std.h>
#include <wpi/fs.h>

#include "HALInternal.h"
#include "hal/Errors.h"

namespace hal::init {
Expand All @@ -28,19 +32,56 @@ static const fs::path radioLEDRedFilePath =
static const char* onStr = "1";
static const char* offStr = "0";

static bool ReadStateFromFile(fs::path path, int32_t* status) {
std::error_code ec;
fs::file_t file = fs::OpenFileForRead(path, ec, fs::OF_Text);
if (ec) {
hal::SetLastError(status, fmt::format("Could not open '{}' for read: {}",
path, ec.message()));
*status = INCOMPATIBLE_STATE;
return false;
}
// We only need to read one byte because the file won't have leading zeros.
char buf[1]{};
ssize_t count = read(file, buf, 1);
// save errno, always close file.
int err = errno;
fs::CloseFile(file);
if (count <= 0) {
*status = INCOMPATIBLE_STATE;
if (count == 0) {
hal::SetLastError(status,
fmt::format("Read from '{}' returned no data.", path));
} else {
hal::SetLastError(status, fmt::format("Failed to read from '{}': {}",
path, std::strerror(err)));
}
return false;
}
// If the brightness is not zero, the LED is on.
return buf[0] != '0';
}

extern "C" {
void HAL_SetRadioLEDState(HAL_RadioLEDState state, int32_t* status) {
std::error_code ec;
fs::file_t greenFile = fs::OpenFileForWrite(radioLEDGreenFilePath, ec,
fs::CD_OpenExisting, fs::OF_Text);
if (ec) {
// not opened, nothing to clean up
*status = INCOMPATIBLE_STATE;
hal::SetLastError(status, fmt::format("Could not open '{}' for write: {}",
greenFile, ec.message()));
return;
}
fs::file_t redFile = fs::OpenFileForWrite(radioLEDRedFilePath, ec,
fs::CD_OpenExisting, fs::OF_Text);
if (ec) {
// green file opened successfully, need to close it
fs::CloseFile(greenFile);
*status = INCOMPATIBLE_STATE;
hal::SetLastError(status, fmt::format("Could not open '{}' for write: {}",
greenFile, ec.message()));
return;
}

Expand All @@ -51,24 +92,6 @@ void HAL_SetRadioLEDState(HAL_RadioLEDState state, int32_t* status) {
fs::CloseFile(redFile);
}

bool ReadStateFromFile(fs::path path, int32_t* status) {
std::error_code ec;
fs::file_t file = fs::OpenFileForRead(path, ec, fs::OF_Text);
if (ec) {
*status = INCOMPATIBLE_STATE;
return false;
}
// We only need to read one byte because the file won't have leading zeros.
char buf[1]{};
size_t count = read(file, buf, 1);
if (count == 0) {
*status = INCOMPATIBLE_STATE;
return false;
}
// If the brightness is not zero, the LED is on.
return buf[0] != '0';
}

HAL_RadioLEDState HAL_GetRadioLEDState(int32_t* status) {
bool green = ReadStateFromFile(radioLEDGreenFilePath, status);
bool red = ReadStateFromFile(radioLEDRedFilePath, status);
Expand Down
9 changes: 6 additions & 3 deletions hal/src/main/native/athena/REVPDH.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,17 +205,20 @@ HAL_REVPDHHandle HAL_InitializeREVPDH(int32_t module,
hal::init::CheckInit();
if (!HAL_CheckREVPDHModuleNumber(module)) {
*status = RESOURCE_OUT_OF_RANGE;
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for REV PDH", 1,
kNumREVPDHModules, module);
return HAL_kInvalidHandle;
}

HAL_REVPDHHandle handle;
auto hpdh = REVPDHHandles->Allocate(module, &handle, status);
// Module starts at 1
auto hpdh = REVPDHHandles->Allocate(module - 1, &handle, status);
if (*status != 0) {
if (hpdh) {
hal::SetLastErrorPreviouslyAllocated(status, "REV PDH", module,
hpdh->previousAllocation);
} else {
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for REV PDH", 0,
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for REV PDH", 1,
kNumREVPDHModules, module);
}
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
Expand Down Expand Up @@ -253,7 +256,7 @@ int32_t HAL_GetREVPDHModuleNumber(HAL_REVPDHHandle handle, int32_t* status) {
}

HAL_Bool HAL_CheckREVPDHModuleNumber(int32_t module) {
return ((module >= 1) && (module < kNumREVPDHModules)) ? 1 : 0;
return ((module >= 1) && (module <= kNumREVPDHModules)) ? 1 : 0;
}

HAL_Bool HAL_CheckREVPDHChannelNumber(int32_t channel) {
Expand Down
6 changes: 4 additions & 2 deletions hal/src/main/native/athena/REVPH.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,15 @@ HAL_REVPHHandle HAL_InitializeREVPH(int32_t module,
int32_t* status) {
hal::init::CheckInit();
if (!HAL_CheckREVPHModuleNumber(module)) {
*status = RESOURCE_OUT_OF_RANGE;
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for REV PH", 1,
kNumREVPHModules, module);
return HAL_kInvalidHandle;
}

HAL_REVPHHandle handle;
auto hph = REVPHHandles->Allocate(module, &handle, status);
// Module starts at 1
auto hph = REVPHHandles->Allocate(module - 1, &handle, status);
if (*status != 0) {
if (hph) {
hal::SetLastErrorPreviouslyAllocated(status, "REV PH", module,
Expand Down Expand Up @@ -247,7 +249,7 @@ void HAL_FreeREVPH(HAL_REVPHHandle handle) {
}

HAL_Bool HAL_CheckREVPHModuleNumber(int32_t module) {
return module >= 1 && module < kNumREVPDHModules;
return module >= 1 && module <= kNumREVPHModules;
}

HAL_Bool HAL_CheckREVPHSolenoidChannel(int32_t channel) {
Expand Down
2 changes: 1 addition & 1 deletion hal/src/main/native/sim/CTREPCM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ HAL_CTREPCMHandle HAL_InitializeCTREPCM(int32_t module,
pcm->previousAllocation);
} else {
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for CTRE PCM", 0,
kNumCTREPCMModules, module);
kNumCTREPCMModules - 1, module);
}
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
}
Expand Down
12 changes: 9 additions & 3 deletions hal/src/main/native/sim/PowerDistribution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,14 @@ HAL_PowerDistributionHandle HAL_InitializePowerDistribution(
}

if (!HAL_CheckPowerDistributionModule(module, type)) {
*status = PARAMETER_OUT_OF_RANGE;
hal::SetLastError(status, fmt::format("Invalid pdp module {}", module));
*status = RESOURCE_OUT_OF_RANGE;
if (type == HAL_PowerDistributionType::HAL_PowerDistributionType_kCTRE) {
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for CTRE PDP", 0,
kNumCTREPDPModules - 1, module);
} else {
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for REV PDH", 1,
kNumREVPDHModules, module);
}
return HAL_kInvalidHandle;
}
hal::init::CheckInit();
Expand Down Expand Up @@ -74,7 +80,7 @@ HAL_Bool HAL_CheckPowerDistributionModule(int32_t module,
if (type == HAL_PowerDistributionType::HAL_PowerDistributionType_kCTRE) {
return module < kNumCTREPDPModules && module >= 0;
} else {
return module < kNumREVPDHModules && module >= 1;
return module <= kNumREVPDHModules && module >= 1;
}
}

Expand Down
8 changes: 5 additions & 3 deletions hal/src/main/native/sim/REVPH.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,16 @@ HAL_REVPHHandle HAL_InitializeREVPH(int32_t module,
int32_t* status) {
hal::init::CheckInit();

if (module == 0) {
if (!HAL_CheckREVPHModuleNumber(module)) {
*status = RESOURCE_OUT_OF_RANGE;
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for REV PH", 1,
kNumREVPHModules, module);
return HAL_kInvalidHandle;
}

HAL_REVPHHandle handle;
auto pcm = pcmHandles->Allocate(module, &handle, status);
// Module starts at 1
auto pcm = pcmHandles->Allocate(module - 1, &handle, status);

if (*status != 0) {
if (pcm) {
Expand Down Expand Up @@ -82,7 +84,7 @@ void HAL_FreeREVPH(HAL_REVPHHandle handle) {
}

HAL_Bool HAL_CheckREVPHModuleNumber(int32_t module) {
return module >= 1 && module < kNumREVPDHModules;
return module >= 1 && module <= kNumREVPHModules;
}

HAL_Bool HAL_CheckREVPHSolenoidChannel(int32_t channel) {
Expand Down
4 changes: 3 additions & 1 deletion ntcore/src/main/native/cpp/NetworkServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ void NetworkServer::ServerConnection::UpdateOutgoingTimer(uint32_t repeatMs) {
void NetworkServer::ServerConnection::ConnectionClosed() {
// don't call back into m_server if it's being destroyed
if (!m_outgoingTimer->IsLoopClosing()) {
m_server.m_serverImpl.RemoveClient(m_clientId);
uv::Timer::SingleShot(m_outgoingTimer->GetLoopRef(), uv::Timer::Time{0},
[client = m_server.m_serverImpl.RemoveClient(
m_clientId)]() mutable { client.reset(); });
m_server.RemoveConnection(this);
}
m_outgoingTimer->Close();
Expand Down
5 changes: 2 additions & 3 deletions ntcore/src/main/native/cpp/net/ServerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1252,7 +1252,7 @@ int ServerImpl::AddClient3(std::string_view connInfo, bool local,
return index;
}

void ServerImpl::RemoveClient(int clientId) {
std::shared_ptr<void> ServerImpl::RemoveClient(int clientId) {
DEBUG3("RemoveClient({})", clientId);
auto& client = m_clients[clientId];

Expand Down Expand Up @@ -1288,8 +1288,7 @@ void ServerImpl::RemoveClient(int clientId) {
DeleteTopic(client->m_metaPub);
DeleteTopic(client->m_metaSub);

// delete the client
client.reset();
return std::move(client);
}

bool ServerImpl::PersistentChanged() {
Expand Down
2 changes: 1 addition & 1 deletion ntcore/src/main/native/cpp/net/ServerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class ServerImpl final {
int AddClient3(std::string_view connInfo, bool local,
net3::WireConnection3& wire, Connected3Func connected,
SetPeriodicFunc setPeriodic);
void RemoveClient(int clientId);
std::shared_ptr<void> RemoveClient(int clientId);

void ConnectionsChanged(const std::vector<ConnectionInfo>& conns);

Expand Down
3 changes: 1 addition & 2 deletions shared/config.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ nativeUtils {
wpi {
configureDependencies {
opencvYear = "frc2024"
googleTestYear = "frc2024"
niLibVersion = "2024.2.1"
niLibVersion = "2025.0.0"
opencvVersion = "4.8.0-4"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public class CommandGenericHID {
new HashMap<>();
private final Map<EventLoop, Map<Pair<Integer, Double>, Trigger>> m_axisGreaterThanCache =
new HashMap<>();
private final Map<EventLoop, Map<Pair<Integer, Double>, Trigger>>
m_axisMagnitudeGreaterThanCache = new HashMap<>();
private final Map<EventLoop, Map<Integer, Trigger>> m_povCache = new HashMap<>();

/**
Expand Down Expand Up @@ -260,6 +262,37 @@ public Trigger axisGreaterThan(int axis, double threshold, EventLoop loop) {
Pair.of(axis, threshold), k -> new Trigger(loop, () -> getRawAxis(axis) > threshold));
}

/**
* Constructs a Trigger instance that is true when the axis magnitude value is greater than {@code
* threshold}, attached to the given loop.
*
* @param axis The axis to read, starting at 0
* @param threshold The value above which this trigger should return true.
* @param loop the event loop instance to attach the trigger to.
* @return a Trigger instance that is true when the axis magnitude value is greater than the
* provided threshold.
*/
public Trigger axisMagnitudeGreaterThan(int axis, double threshold, EventLoop loop) {
var cache = m_axisMagnitudeGreaterThanCache.computeIfAbsent(loop, k -> new HashMap<>());
return cache.computeIfAbsent(
Pair.of(axis, threshold),
k -> new Trigger(loop, () -> Math.abs(getRawAxis(axis)) > threshold));
}

/**
* Constructs a Trigger instance that is true when the axis magnitude value is greater than {@code
* threshold}, attached to {@link CommandScheduler#getDefaultButtonLoop() the default command
* scheduler button loop}.
*
* @param axis The axis to read, starting at 0
* @param threshold The value above which this trigger should return true.
* @return a Trigger instance that is true when the deadbanded axis value is active (non-zero).
*/
public Trigger axisMagnitudeGreaterThan(int axis, double threshold) {
return axisMagnitudeGreaterThan(
axis, threshold, CommandScheduler.getInstance().getDefaultButtonLoop());
}

/**
* Get the value of the axis.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ Trigger CommandGenericHID::AxisGreaterThan(int axis, double threshold,
});
}

Trigger CommandGenericHID::AxisMagnitudeGreaterThan(
int axis, double threshold, frc::EventLoop* loop) const {
return Trigger(loop, [this, axis, threshold]() {
return std::abs(m_hid.GetRawAxis(axis)) > threshold;
});
}

void CommandGenericHID::SetRumble(frc::GenericHID::RumbleType type,
double value) {
m_hid.SetRumble(type, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,21 @@ class CommandGenericHID {
frc::EventLoop* loop =
CommandScheduler::GetInstance().GetDefaultButtonLoop()) const;

/**
* Constructs a Trigger instance that is true when the axis magnitude value is
* greater than {@code threshold}, attached to the given loop.
*
* @param axis The axis to read, starting at 0
* @param threshold The value above which this trigger should return true.
* @param loop the event loop instance to attach the trigger to.
* @return a Trigger instance that is true when the axis magnitude value is
* greater than the provided threshold.
*/
Trigger AxisMagnitudeGreaterThan(
int axis, double threshold,
frc::EventLoop* loop =
CommandScheduler::GetInstance().GetDefaultButtonLoop()) const;

/**
* Set the rumble output for the HID.
*
Expand Down
Loading

0 comments on commit 8c15f99

Please sign in to comment.