mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-07-01 02:41:48 +00:00
Use std::string_view and fmtlib across all libraries (#3402)
- Twine, StringRef, Format, and NativeFormatting have been removed - Logging now uses fmtlib style formatting - Nearly all uses of wpi::outs/errs have been replaced with fmt::print() or std::puts()/std::fputs() (for unformatted strings). - A wpi/fmt/raw_ostream.h header has been added to enable fmt::print() with wpi::raw_ostream
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
#include <cstring>
|
||||
|
||||
#include <FRC_FPGA_ChipObject/fpgainterfacecapi/NiFpga_HMB.h>
|
||||
#include <fmt/format.h>
|
||||
|
||||
#include "ConstantsInternal.h"
|
||||
#include "DigitalInternal.h"
|
||||
@@ -145,9 +146,11 @@ void HAL_SetAddressableLEDLength(HAL_AddressableLEDHandle handle,
|
||||
|
||||
if (length > HAL_kAddressableLEDMaxLength || length < 0) {
|
||||
*status = PARAMETER_OUT_OF_RANGE;
|
||||
hal::SetLastError(status, "LED length must be less than or equal to " +
|
||||
wpi::Twine(HAL_kAddressableLEDMaxLength) +
|
||||
". " + wpi::Twine(length) + " was requested");
|
||||
hal::SetLastError(
|
||||
status,
|
||||
fmt::format(
|
||||
"LED length must be less than or equal to {}. {} was requested",
|
||||
HAL_kAddressableLEDMaxLength, length));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -179,9 +182,11 @@ void HAL_WriteAddressableLEDData(HAL_AddressableLEDHandle handle,
|
||||
|
||||
if (length > led->stringLength || length < 0) {
|
||||
*status = PARAMETER_OUT_OF_RANGE;
|
||||
hal::SetLastError(status, "Data length must be less than or equal to " +
|
||||
wpi::Twine(led->stringLength) + ". " +
|
||||
wpi::Twine(length) + " was requested");
|
||||
hal::SetLastError(
|
||||
status,
|
||||
fmt::format(
|
||||
"Data length must be less than or equal to {}. {} was requested",
|
||||
led->stringLength, length));
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
#include <string>
|
||||
#include <thread>
|
||||
|
||||
#include <wpi/raw_ostream.h>
|
||||
#include <fmt/format.h>
|
||||
|
||||
#include "AnalogInternal.h"
|
||||
#include "HALInitializer.h"
|
||||
@@ -193,8 +193,8 @@ void HAL_CalibrateAnalogGyro(HAL_GyroHandle handle, int32_t* status) {
|
||||
if (*status != 0) {
|
||||
return;
|
||||
}
|
||||
wpi::outs() << "Calibrating analog gyro for " << kCalibrationSampleTime
|
||||
<< " seconds." << '\n';
|
||||
fmt::print("Calibrating analog gyro for {} seconds.\n",
|
||||
kCalibrationSampleTime);
|
||||
Wait(kCalibrationSampleTime);
|
||||
|
||||
int64_t value;
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
|
||||
#include "hal/CTREPCM.h"
|
||||
|
||||
#include <fmt/format.h>
|
||||
|
||||
#include "HALInitializer.h"
|
||||
#include "HALInternal.h"
|
||||
#include "PortsInternal.h"
|
||||
@@ -342,8 +344,9 @@ void HAL_FireCTREPCMOneShot(HAL_CTREPCMHandle handle, int32_t index,
|
||||
int32_t* status) {
|
||||
if (index > 7 || index < 0) {
|
||||
*status = PARAMETER_OUT_OF_RANGE;
|
||||
hal::SetLastError(status, "Only [0-7] are valid index values. Requested " +
|
||||
wpi::Twine(index));
|
||||
hal::SetLastError(
|
||||
status,
|
||||
fmt::format("Only [0-7] are valid index values. Requested {}", index));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -373,8 +376,9 @@ void HAL_SetCTREPCMOneShotDuration(HAL_CTREPCMHandle handle, int32_t index,
|
||||
int32_t durMs, int32_t* status) {
|
||||
if (index > 7 || index < 0) {
|
||||
*status = PARAMETER_OUT_OF_RANGE;
|
||||
hal::SetLastError(status, "Only [0-7] are valid index values. Requested " +
|
||||
wpi::Twine(index));
|
||||
hal::SetLastError(
|
||||
status,
|
||||
fmt::format("Only [0-7] are valid index values. Requested {}", index));
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
|
||||
#include "hal/Counter.h"
|
||||
|
||||
#include <fmt/format.h>
|
||||
|
||||
#include "ConstantsInternal.h"
|
||||
#include "DigitalInternal.h"
|
||||
#include "HALInitializer.h"
|
||||
@@ -264,10 +266,9 @@ void HAL_SetCounterSamplesToAverage(HAL_CounterHandle counterHandle,
|
||||
}
|
||||
if (samplesToAverage < 1 || samplesToAverage > 127) {
|
||||
*status = PARAMETER_OUT_OF_RANGE;
|
||||
hal::SetLastError(
|
||||
status,
|
||||
"Samples to average must be between 1 and 127 inclusive. Requested " +
|
||||
wpi::Twine(samplesToAverage));
|
||||
hal::SetLastError(status, fmt::format("Samples to average must be between "
|
||||
"1 and 127 inclusive. Requested {}",
|
||||
samplesToAverage));
|
||||
return;
|
||||
}
|
||||
counter->counter->writeTimerConfig_AverageSize(samplesToAverage, status);
|
||||
|
||||
@@ -5,10 +5,9 @@
|
||||
#include "hal/DIO.h"
|
||||
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <thread>
|
||||
|
||||
#include <wpi/raw_ostream.h>
|
||||
|
||||
#include "DigitalInternal.h"
|
||||
#include "HALInitializer.h"
|
||||
#include "HALInternal.h"
|
||||
@@ -143,8 +142,8 @@ void HAL_FreeDIOPort(HAL_DigitalHandle dioPortHandle) {
|
||||
while (port.use_count() != 1) {
|
||||
auto current = hal::fpga_clock::now();
|
||||
if (start + std::chrono::seconds(1) < current) {
|
||||
wpi::outs() << "DIO handle free timeout\n";
|
||||
wpi::outs().flush();
|
||||
std::puts("DIO handle free timeout");
|
||||
std::fflush(stdout);
|
||||
break;
|
||||
}
|
||||
std::this_thread::yield();
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
|
||||
#include "hal/Encoder.h"
|
||||
|
||||
#include <fmt/format.h>
|
||||
|
||||
#include "EncoderInternal.h"
|
||||
#include "FPGAEncoder.h"
|
||||
#include "HALInitializer.h"
|
||||
@@ -47,9 +49,8 @@ Encoder::Encoder(HAL_Handle digitalSourceHandleA,
|
||||
}
|
||||
default:
|
||||
*status = PARAMETER_OUT_OF_RANGE;
|
||||
hal::SetLastError(status, "Encoding type " +
|
||||
wpi::Twine(static_cast<int>(encodingType)) +
|
||||
" invalid.");
|
||||
hal::SetLastError(status, fmt::format("Encoding type {} invalid.",
|
||||
static_cast<int>(encodingType)));
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -185,10 +186,9 @@ void Encoder::SetReverseDirection(bool reverseDirection, int32_t* status) {
|
||||
void Encoder::SetSamplesToAverage(int32_t samplesToAverage, int32_t* status) {
|
||||
if (samplesToAverage < 1 || samplesToAverage > 127) {
|
||||
*status = PARAMETER_OUT_OF_RANGE;
|
||||
hal::SetLastError(
|
||||
status,
|
||||
"Samples to average must be between 1 and 127 inclusive. Requested " +
|
||||
wpi::Twine(samplesToAverage));
|
||||
hal::SetLastError(status, fmt::format("Samples to average must be between "
|
||||
"1 and 127 inclusive. Requested {}",
|
||||
samplesToAverage));
|
||||
return;
|
||||
}
|
||||
if (m_counter) {
|
||||
|
||||
@@ -6,6 +6,8 @@
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include <fmt/format.h>
|
||||
|
||||
#include "DigitalInternal.h"
|
||||
#include "HALInitializer.h"
|
||||
#include "HALInternal.h"
|
||||
@@ -194,10 +196,9 @@ void HAL_SetFPGAEncoderSamplesToAverage(HAL_FPGAEncoderHandle fpgaEncoderHandle,
|
||||
}
|
||||
if (samplesToAverage < 1 || samplesToAverage > 127) {
|
||||
*status = PARAMETER_OUT_OF_RANGE;
|
||||
hal::SetLastError(
|
||||
status,
|
||||
"Samples to average must be between 1 and 127 inclusive. Requested " +
|
||||
wpi::Twine(samplesToAverage));
|
||||
hal::SetLastError(status, fmt::format("Samples to average must be between "
|
||||
"1 and 127 inclusive. Requested {}",
|
||||
samplesToAverage));
|
||||
return;
|
||||
}
|
||||
encoder->encoder->writeTimerConfig_AverageSize(samplesToAverage, status);
|
||||
|
||||
@@ -7,13 +7,15 @@
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <limits>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
#include <FRC_NetworkCommunication/FRCComm.h>
|
||||
#include <FRC_NetworkCommunication/NetCommRPCProxy_Occur.h>
|
||||
#include <fmt/format.h>
|
||||
#include <wpi/SafeThread.h>
|
||||
#include <wpi/condition_variable.h>
|
||||
#include <wpi/mutex.h>
|
||||
#include <wpi/raw_ostream.h>
|
||||
|
||||
#include "hal/DriverStation.h"
|
||||
|
||||
@@ -161,9 +163,9 @@ int32_t HAL_SendError(HAL_Bool isError, int32_t errorCode, HAL_Bool isLVCode,
|
||||
}
|
||||
int retval = 0;
|
||||
if (i == KEEP_MSGS || (curTime - prevMsgTime[i]) >= std::chrono::seconds(1)) {
|
||||
wpi::StringRef detailsRef{details};
|
||||
wpi::StringRef locationRef{location};
|
||||
wpi::StringRef callStackRef{callStack};
|
||||
std::string_view detailsRef{details};
|
||||
std::string_view locationRef{location};
|
||||
std::string_view callStackRef{callStack};
|
||||
|
||||
// 1 tag, 4 timestamp, 2 seqnum
|
||||
// 2 numOccur, 4 error code, 1 flags, 6 strlen
|
||||
@@ -199,14 +201,16 @@ int32_t HAL_SendError(HAL_Bool isError, int32_t errorCode, HAL_Bool isLVCode,
|
||||
newCallStack.c_str());
|
||||
}
|
||||
if (printMsg) {
|
||||
fmt::memory_buffer buf;
|
||||
if (location && location[0] != '\0') {
|
||||
wpi::errs() << (isError ? "Error" : "Warning") << " at " << location
|
||||
<< ": ";
|
||||
fmt::format_to(buf, "{} at {}: ", isError ? "Error" : "Warning",
|
||||
location);
|
||||
}
|
||||
wpi::errs() << details << "\n";
|
||||
fmt::format_to(buf, "{}\n", details);
|
||||
if (callStack && callStack[0] != '\0') {
|
||||
wpi::errs() << callStack << "\n";
|
||||
fmt::format_to(buf, "{}\n", callStack);
|
||||
}
|
||||
std::fwrite(buf.data(), buf.size(), 1, stderr);
|
||||
}
|
||||
if (i == KEEP_MSGS) {
|
||||
// replace the oldest one
|
||||
@@ -226,7 +230,7 @@ int32_t HAL_SendError(HAL_Bool isError, int32_t errorCode, HAL_Bool isLVCode,
|
||||
}
|
||||
|
||||
int32_t HAL_SendConsoleLine(const char* line) {
|
||||
wpi::StringRef lineRef{line};
|
||||
std::string_view lineRef{line};
|
||||
if (lineRef.size() <= 65535) {
|
||||
// Send directly
|
||||
return FRC_NetworkCommunication_sendConsoleLine(line);
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include <unistd.h>
|
||||
|
||||
#include <atomic>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <fstream>
|
||||
#include <thread>
|
||||
@@ -16,8 +17,8 @@
|
||||
#include <FRC_NetworkCommunication/FRCComm.h>
|
||||
#include <FRC_NetworkCommunication/LoadOut.h>
|
||||
#include <FRC_NetworkCommunication/UsageReporting.h>
|
||||
#include <fmt/format.h>
|
||||
#include <wpi/mutex.h>
|
||||
#include <wpi/raw_ostream.h>
|
||||
#include <wpi/timestamp.h>
|
||||
|
||||
#include "HALInitializer.h"
|
||||
@@ -330,18 +331,19 @@ static bool killExistingProgram(int timeout, int mode) {
|
||||
// see if the pid is around, but we don't want to mess with init id=1, or
|
||||
// ourselves
|
||||
if (pid >= 2 && kill(pid, 0) == 0 && pid != getpid()) {
|
||||
wpi::outs() << "Killing previously running FRC program...\n";
|
||||
std::puts("Killing previously running FRC program...");
|
||||
kill(pid, SIGTERM); // try to kill it
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(timeout));
|
||||
if (kill(pid, 0) == 0) {
|
||||
// still not successful
|
||||
wpi::outs() << "FRC pid " << pid << " did not die within " << timeout
|
||||
<< "ms. Force killing with kill -9\n";
|
||||
fmt::print(
|
||||
"FRC pid {} did not die within {} ms. Force killing with kill -9\n",
|
||||
pid, timeout);
|
||||
// Force kill -9
|
||||
auto forceKill = kill(pid, SIGKILL);
|
||||
if (forceKill != 0) {
|
||||
auto errorMsg = std::strerror(forceKill);
|
||||
wpi::outs() << "Kill -9 error: " << errorMsg << "\n";
|
||||
fmt::print("Kill -9 error: {}\n", errorMsg);
|
||||
}
|
||||
// Give a bit of time for the kill to take place
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(250));
|
||||
@@ -378,7 +380,6 @@ HAL_Bool HAL_Initialize(int32_t timeout, int32_t mode) {
|
||||
|
||||
setlinebuf(stdin);
|
||||
setlinebuf(stdout);
|
||||
wpi::outs().SetUnbuffered();
|
||||
|
||||
prctl(PR_SET_PDEATHSIG, SIGTERM);
|
||||
|
||||
@@ -412,11 +413,11 @@ HAL_Bool HAL_Initialize(int32_t timeout, int32_t mode) {
|
||||
int32_t status = 0;
|
||||
uint64_t rv = HAL_GetFPGATime(&status);
|
||||
if (status != 0) {
|
||||
wpi::errs()
|
||||
<< "Call to HAL_GetFPGATime failed in wpi::Now() with status "
|
||||
<< status
|
||||
<< ". Initialization might have failed. Time will not be correct\n";
|
||||
wpi::errs().flush();
|
||||
fmt::print(stderr,
|
||||
"Call to HAL_GetFPGATime failed in wpi::Now() with status {}. "
|
||||
"Initialization might have failed. Time will not be correct\n",
|
||||
status);
|
||||
std::fflush(stderr);
|
||||
return 0u;
|
||||
}
|
||||
return rv;
|
||||
|
||||
@@ -6,15 +6,15 @@
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <wpi/Twine.h>
|
||||
#include <string_view>
|
||||
|
||||
namespace hal {
|
||||
void ReleaseFPGAInterrupt(int32_t interruptNumber);
|
||||
void SetLastError(int32_t* status, const wpi::Twine& value);
|
||||
void SetLastErrorIndexOutOfRange(int32_t* status, const wpi::Twine& message,
|
||||
void SetLastError(int32_t* status, std::string_view value);
|
||||
void SetLastErrorIndexOutOfRange(int32_t* status, std::string_view message,
|
||||
int32_t minimum, int32_t maximum,
|
||||
int32_t channel);
|
||||
void SetLastErrorPreviouslyAllocated(int32_t* status, const wpi::Twine& message,
|
||||
void SetLastErrorPreviouslyAllocated(int32_t* status, std::string_view message,
|
||||
int32_t channel,
|
||||
const wpi::Twine& previousAllocation);
|
||||
std::string_view previousAllocation);
|
||||
} // namespace hal
|
||||
|
||||
@@ -12,6 +12,8 @@
|
||||
|
||||
#include <cstring>
|
||||
|
||||
#include <fmt/format.h>
|
||||
|
||||
#include "DigitalInternal.h"
|
||||
#include "HALInitializer.h"
|
||||
#include "hal/DIO.h"
|
||||
@@ -56,7 +58,7 @@ void HAL_InitializeI2C(HAL_I2CPort port, int32_t* status) {
|
||||
}
|
||||
int handle = open("/dev/i2c-2", O_RDWR);
|
||||
if (handle < 0) {
|
||||
std::printf("Failed to open onboard i2c bus: %s\n", std::strerror(errno));
|
||||
fmt::print("Failed to open onboard i2c bus: {}\n", std::strerror(errno));
|
||||
return;
|
||||
}
|
||||
i2COnBoardHandle = handle;
|
||||
@@ -79,7 +81,7 @@ void HAL_InitializeI2C(HAL_I2CPort port, int32_t* status) {
|
||||
digitalSystem->readEnableMXPSpecialFunction(status) | 0xC000, status);
|
||||
int handle = open("/dev/i2c-1", O_RDWR);
|
||||
if (handle < 0) {
|
||||
std::printf("Failed to open MXP i2c bus: %s\n", std::strerror(errno));
|
||||
fmt::print("Failed to open MXP i2c bus: {}\n", std::strerror(errno));
|
||||
return;
|
||||
}
|
||||
i2CMXPHandle = handle;
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
#include "hal/PDP.h"
|
||||
|
||||
#include <fmt/format.h>
|
||||
#include <wpi/mutex.h>
|
||||
|
||||
#include "HALInitializer.h"
|
||||
@@ -118,7 +119,7 @@ HAL_PDPHandle HAL_InitializePDP(int32_t module, int32_t* status) {
|
||||
hal::init::CheckInit();
|
||||
if (!HAL_CheckPDPModule(module)) {
|
||||
*status = PARAMETER_OUT_OF_RANGE;
|
||||
hal::SetLastError(status, "Invalid pdp module " + wpi::Twine(module));
|
||||
hal::SetLastError(status, fmt::format("Invalid pdp module {}", module));
|
||||
return HAL_kInvalidHandle;
|
||||
}
|
||||
|
||||
@@ -194,7 +195,7 @@ double HAL_GetPDPChannelCurrent(HAL_PDPHandle handle, int32_t channel,
|
||||
int32_t* status) {
|
||||
if (!HAL_CheckPDPChannel(channel)) {
|
||||
*status = PARAMETER_OUT_OF_RANGE;
|
||||
hal::SetLastError(status, "Invalid pdp channel " + wpi::Twine(channel));
|
||||
hal::SetLastError(status, fmt::format("Invalid pdp channel {}", channel));
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -4,11 +4,11 @@
|
||||
|
||||
#include "hal/PWM.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <thread>
|
||||
|
||||
#include <wpi/raw_ostream.h>
|
||||
|
||||
#include "ConstantsInternal.h"
|
||||
#include "DigitalInternal.h"
|
||||
#include "HALInitializer.h"
|
||||
@@ -138,8 +138,8 @@ void HAL_FreePWMPort(HAL_DigitalHandle pwmPortHandle, int32_t* status) {
|
||||
while (port.use_count() != 1) {
|
||||
auto current = hal::fpga_clock::now();
|
||||
if (start + std::chrono::seconds(1) < current) {
|
||||
wpi::outs() << "PWM handle free timeout\n";
|
||||
wpi::outs().flush();
|
||||
std::puts("PWM handle free timeout");
|
||||
std::fflush(stdout);
|
||||
break;
|
||||
}
|
||||
std::this_thread::yield();
|
||||
|
||||
@@ -11,10 +11,11 @@
|
||||
|
||||
#include <array>
|
||||
#include <atomic>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
|
||||
#include <fmt/format.h>
|
||||
#include <wpi/mutex.h>
|
||||
#include <wpi/raw_ostream.h>
|
||||
|
||||
#include "DigitalInternal.h"
|
||||
#include "HALInitializer.h"
|
||||
@@ -78,14 +79,14 @@ static void CommonSPIPortInit(int32_t* status) {
|
||||
if ((digitalHandles[3] = HAL_InitializeDIOPort(createPortHandleForSPI(29),
|
||||
false, nullptr, status)) ==
|
||||
HAL_kInvalidHandle) {
|
||||
std::printf("Failed to allocate DIO 29 (MISO)\n");
|
||||
std::puts("Failed to allocate DIO 29 (MISO)");
|
||||
return;
|
||||
}
|
||||
// MOSI
|
||||
if ((digitalHandles[4] = HAL_InitializeDIOPort(createPortHandleForSPI(30),
|
||||
false, nullptr, status)) ==
|
||||
HAL_kInvalidHandle) {
|
||||
std::printf("Failed to allocate DIO 30 (MOSI)\n");
|
||||
std::puts("Failed to allocate DIO 30 (MOSI)");
|
||||
HAL_FreeDIOPort(digitalHandles[3]); // free the first port allocated
|
||||
return;
|
||||
}
|
||||
@@ -104,9 +105,10 @@ void HAL_InitializeSPI(HAL_SPIPort port, int32_t* status) {
|
||||
hal::init::CheckInit();
|
||||
if (port < 0 || port >= kSpiMaxHandles) {
|
||||
*status = PARAMETER_OUT_OF_RANGE;
|
||||
hal::SetLastError(status, "Serial port must be between 0 and " +
|
||||
wpi::Twine(kSpiMaxHandles) + ". Requested " +
|
||||
wpi::Twine(static_cast<int>(port)));
|
||||
hal::SetLastError(
|
||||
status,
|
||||
fmt::format("Serial port must be between 0 and {}. Requested {}",
|
||||
kSpiMaxHandles, static_cast<int>(port)));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -123,8 +125,8 @@ void HAL_InitializeSPI(HAL_SPIPort port, int32_t* status) {
|
||||
// CS0 is not a DIO port, so nothing to allocate
|
||||
handle = open("/dev/spidev0.0", O_RDWR);
|
||||
if (handle < 0) {
|
||||
std::printf("Failed to open SPI port %d: %s\n", port,
|
||||
std::strerror(errno));
|
||||
fmt::print("Failed to open SPI port {}: {}\n", port,
|
||||
std::strerror(errno));
|
||||
CommonSPIPortFree();
|
||||
return;
|
||||
}
|
||||
@@ -139,14 +141,14 @@ void HAL_InitializeSPI(HAL_SPIPort port, int32_t* status) {
|
||||
if ((digitalHandles[0] = HAL_InitializeDIOPort(createPortHandleForSPI(26),
|
||||
false, nullptr, status)) ==
|
||||
HAL_kInvalidHandle) {
|
||||
std::printf("Failed to allocate DIO 26 (CS1)\n");
|
||||
std::puts("Failed to allocate DIO 26 (CS1)");
|
||||
CommonSPIPortFree();
|
||||
return;
|
||||
}
|
||||
handle = open("/dev/spidev0.1", O_RDWR);
|
||||
if (handle < 0) {
|
||||
std::printf("Failed to open SPI port %d: %s\n", port,
|
||||
std::strerror(errno));
|
||||
fmt::print("Failed to open SPI port {}: {}\n", port,
|
||||
std::strerror(errno));
|
||||
CommonSPIPortFree();
|
||||
HAL_FreeDIOPort(digitalHandles[0]);
|
||||
return;
|
||||
@@ -162,14 +164,14 @@ void HAL_InitializeSPI(HAL_SPIPort port, int32_t* status) {
|
||||
if ((digitalHandles[1] = HAL_InitializeDIOPort(createPortHandleForSPI(27),
|
||||
false, nullptr, status)) ==
|
||||
HAL_kInvalidHandle) {
|
||||
std::printf("Failed to allocate DIO 27 (CS2)\n");
|
||||
std::puts("Failed to allocate DIO 27 (CS2)");
|
||||
CommonSPIPortFree();
|
||||
return;
|
||||
}
|
||||
handle = open("/dev/spidev0.2", O_RDWR);
|
||||
if (handle < 0) {
|
||||
std::printf("Failed to open SPI port %d: %s\n", port,
|
||||
std::strerror(errno));
|
||||
fmt::print("Failed to open SPI port {}: {}\n", port,
|
||||
std::strerror(errno));
|
||||
CommonSPIPortFree();
|
||||
HAL_FreeDIOPort(digitalHandles[1]);
|
||||
return;
|
||||
@@ -185,14 +187,14 @@ void HAL_InitializeSPI(HAL_SPIPort port, int32_t* status) {
|
||||
if ((digitalHandles[2] = HAL_InitializeDIOPort(createPortHandleForSPI(28),
|
||||
false, nullptr, status)) ==
|
||||
HAL_kInvalidHandle) {
|
||||
std::printf("Failed to allocate DIO 28 (CS3)\n");
|
||||
std::puts("Failed to allocate DIO 28 (CS3)");
|
||||
CommonSPIPortFree();
|
||||
return;
|
||||
}
|
||||
handle = open("/dev/spidev0.3", O_RDWR);
|
||||
if (handle < 0) {
|
||||
std::printf("Failed to open SPI port %d: %s\n", port,
|
||||
std::strerror(errno));
|
||||
fmt::print("Failed to open SPI port {}: {}\n", port,
|
||||
std::strerror(errno));
|
||||
CommonSPIPortFree();
|
||||
HAL_FreeDIOPort(digitalHandles[2]);
|
||||
return;
|
||||
@@ -207,20 +209,20 @@ void HAL_InitializeSPI(HAL_SPIPort port, int32_t* status) {
|
||||
if ((digitalHandles[5] = HAL_InitializeDIOPort(createPortHandleForSPI(14),
|
||||
false, nullptr, status)) ==
|
||||
HAL_kInvalidHandle) {
|
||||
wpi::outs() << "Failed to allocate DIO 14\n";
|
||||
std::puts("Failed to allocate DIO 14");
|
||||
return;
|
||||
}
|
||||
if ((digitalHandles[6] = HAL_InitializeDIOPort(createPortHandleForSPI(15),
|
||||
false, nullptr, status)) ==
|
||||
HAL_kInvalidHandle) {
|
||||
wpi::outs() << "Failed to allocate DIO 15\n";
|
||||
std::puts("Failed to allocate DIO 15");
|
||||
HAL_FreeDIOPort(digitalHandles[5]); // free the first port allocated
|
||||
return;
|
||||
}
|
||||
if ((digitalHandles[7] = HAL_InitializeDIOPort(createPortHandleForSPI(16),
|
||||
false, nullptr, status)) ==
|
||||
HAL_kInvalidHandle) {
|
||||
wpi::outs() << "Failed to allocate DIO 16\n";
|
||||
std::puts("Failed to allocate DIO 16");
|
||||
HAL_FreeDIOPort(digitalHandles[5]); // free the first port allocated
|
||||
HAL_FreeDIOPort(digitalHandles[6]); // free the second port allocated
|
||||
return;
|
||||
@@ -228,7 +230,7 @@ void HAL_InitializeSPI(HAL_SPIPort port, int32_t* status) {
|
||||
if ((digitalHandles[8] = HAL_InitializeDIOPort(createPortHandleForSPI(17),
|
||||
false, nullptr, status)) ==
|
||||
HAL_kInvalidHandle) {
|
||||
wpi::outs() << "Failed to allocate DIO 17\n";
|
||||
std::puts("Failed to allocate DIO 17");
|
||||
HAL_FreeDIOPort(digitalHandles[5]); // free the first port allocated
|
||||
HAL_FreeDIOPort(digitalHandles[6]); // free the second port allocated
|
||||
HAL_FreeDIOPort(digitalHandles[7]); // free the third port allocated
|
||||
@@ -238,8 +240,8 @@ void HAL_InitializeSPI(HAL_SPIPort port, int32_t* status) {
|
||||
digitalSystem->readEnableMXPSpecialFunction(status) | 0x00F0, status);
|
||||
handle = open("/dev/spidev1.0", O_RDWR);
|
||||
if (handle < 0) {
|
||||
std::printf("Failed to open SPI port %d: %s\n", port,
|
||||
std::strerror(errno));
|
||||
fmt::print("Failed to open SPI port {}: {}\n", port,
|
||||
std::strerror(errno));
|
||||
HAL_FreeDIOPort(digitalHandles[5]); // free the first port allocated
|
||||
HAL_FreeDIOPort(digitalHandles[6]); // free the second port allocated
|
||||
HAL_FreeDIOPort(digitalHandles[7]); // free the third port allocated
|
||||
@@ -251,7 +253,7 @@ void HAL_InitializeSPI(HAL_SPIPort port, int32_t* status) {
|
||||
default:
|
||||
*status = PARAMETER_OUT_OF_RANGE;
|
||||
hal::SetLastError(
|
||||
status, "Invalid SPI port " + wpi::Twine(static_cast<int>(port)));
|
||||
status, fmt::format("Invalid SPI port {}", static_cast<int>(port)));
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -380,9 +382,10 @@ void HAL_SetSPIOpts(HAL_SPIPort port, HAL_Bool msbFirst,
|
||||
void HAL_SetSPIChipSelectActiveHigh(HAL_SPIPort port, int32_t* status) {
|
||||
if (port < 0 || port >= kSpiMaxHandles) {
|
||||
*status = PARAMETER_OUT_OF_RANGE;
|
||||
hal::SetLastError(status, "Serial port must be between 0 and " +
|
||||
wpi::Twine(kSpiMaxHandles) + ". Requested " +
|
||||
wpi::Twine(static_cast<int>(port)));
|
||||
hal::SetLastError(
|
||||
status,
|
||||
fmt::format("Serial port must be between 0 and {}. Requested {}",
|
||||
kSpiMaxHandles, static_cast<int>(port)));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -398,9 +401,10 @@ void HAL_SetSPIChipSelectActiveHigh(HAL_SPIPort port, int32_t* status) {
|
||||
void HAL_SetSPIChipSelectActiveLow(HAL_SPIPort port, int32_t* status) {
|
||||
if (port < 0 || port >= kSpiMaxHandles) {
|
||||
*status = PARAMETER_OUT_OF_RANGE;
|
||||
hal::SetLastError(status, "Serial port must be between 0 and " +
|
||||
wpi::Twine(kSpiMaxHandles) + ". Requested " +
|
||||
wpi::Twine(static_cast<int>(port)));
|
||||
hal::SetLastError(
|
||||
status,
|
||||
fmt::format("Serial port must be between 0 and {}. Requested {}",
|
||||
kSpiMaxHandles, static_cast<int>(port)));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -465,9 +469,10 @@ void HAL_SetSPIHandle(HAL_SPIPort port, int32_t handle) {
|
||||
void HAL_InitSPIAuto(HAL_SPIPort port, int32_t bufferSize, int32_t* status) {
|
||||
if (port < 0 || port >= kSpiMaxHandles) {
|
||||
*status = PARAMETER_OUT_OF_RANGE;
|
||||
hal::SetLastError(status, "Serial port must be between 0 and " +
|
||||
wpi::Twine(kSpiMaxHandles) + ". Requested " +
|
||||
wpi::Twine(static_cast<int>(port)));
|
||||
hal::SetLastError(
|
||||
status,
|
||||
fmt::format("Serial port must be between 0 and {}. Requested {}",
|
||||
kSpiMaxHandles, static_cast<int>(port)));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -499,9 +504,10 @@ void HAL_InitSPIAuto(HAL_SPIPort port, int32_t bufferSize, int32_t* status) {
|
||||
void HAL_FreeSPIAuto(HAL_SPIPort port, int32_t* status) {
|
||||
if (port < 0 || port >= kSpiMaxHandles) {
|
||||
*status = PARAMETER_OUT_OF_RANGE;
|
||||
hal::SetLastError(status, "Serial port must be between 0 and " +
|
||||
wpi::Twine(kSpiMaxHandles) + ". Requested " +
|
||||
wpi::Twine(static_cast<int>(port)));
|
||||
hal::SetLastError(
|
||||
status,
|
||||
fmt::format("Serial port must be between 0 and {}. Requested {}",
|
||||
kSpiMaxHandles, static_cast<int>(port)));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -605,16 +611,20 @@ void HAL_SetSPIAutoTransmitData(HAL_SPIPort port, const uint8_t* dataToSend,
|
||||
if (dataSize < 0 || dataSize > 32) {
|
||||
*status = PARAMETER_OUT_OF_RANGE;
|
||||
hal::SetLastError(
|
||||
status, "Data size must be between 0 and 32 inclusive. Requested " +
|
||||
wpi::Twine(dataSize));
|
||||
status,
|
||||
fmt::format(
|
||||
"Data size must be between 0 and 32 inclusive. Requested {}",
|
||||
dataSize));
|
||||
return;
|
||||
}
|
||||
|
||||
if (zeroSize < 0 || zeroSize > 127) {
|
||||
*status = PARAMETER_OUT_OF_RANGE;
|
||||
hal::SetLastError(
|
||||
status, "Zero size must be between 0 and 127 inclusive. Requested " +
|
||||
wpi::Twine(zeroSize));
|
||||
status,
|
||||
fmt::format(
|
||||
"Zero size must be between 0 and 127 inclusive. Requested {}",
|
||||
zeroSize));
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -19,6 +19,8 @@
|
||||
#include <string>
|
||||
#include <thread>
|
||||
|
||||
#include <fmt/format.h>
|
||||
|
||||
#include "HALInternal.h"
|
||||
#include "hal/cpp/SerialHelper.h"
|
||||
#include "hal/handles/HandlesInternal.h"
|
||||
@@ -184,7 +186,7 @@ void HAL_SetSerialBaudRate(HAL_SerialPortHandle handle, int32_t baud,
|
||||
BAUDCASE(4000000)
|
||||
default:
|
||||
*status = PARAMETER_OUT_OF_RANGE;
|
||||
hal::SetLastError(status, "Invalid BaudRate: " + wpi::Twine(baud));
|
||||
hal::SetLastError(status, fmt::format("Invalid BaudRate: {}", baud));
|
||||
return;
|
||||
}
|
||||
int err = cfsetospeed(&port->tty, static_cast<speed_t>(port->baudRate));
|
||||
@@ -227,7 +229,7 @@ void HAL_SetSerialDataBits(HAL_SerialPortHandle handle, int32_t bits,
|
||||
break;
|
||||
default:
|
||||
*status = PARAMETER_OUT_OF_RANGE;
|
||||
hal::SetLastError(status, "Invalid data bits: " + wpi::Twine(bits));
|
||||
hal::SetLastError(status, fmt::format("Invalid data bits: {}", bits));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -275,7 +277,7 @@ void HAL_SetSerialParity(HAL_SerialPortHandle handle, int32_t parity,
|
||||
break;
|
||||
default:
|
||||
*status = PARAMETER_OUT_OF_RANGE;
|
||||
hal::SetLastError(status, "Invalid parity bits: " + wpi::Twine(parity));
|
||||
hal::SetLastError(status, fmt::format("Invalid parity bits: {}", parity));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -303,7 +305,7 @@ void HAL_SetSerialStopBits(HAL_SerialPortHandle handle, int32_t stopBits,
|
||||
break;
|
||||
default:
|
||||
*status = PARAMETER_OUT_OF_RANGE;
|
||||
hal::SetLastError(status, "Invalid stop bits: " + wpi::Twine(stopBits));
|
||||
hal::SetLastError(status, fmt::format("Invalid stop bits: {}", stopBits));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -339,7 +341,7 @@ void HAL_SetSerialFlowControl(HAL_SerialPortHandle handle, int32_t flow,
|
||||
break;
|
||||
default:
|
||||
*status = PARAMETER_OUT_OF_RANGE;
|
||||
hal::SetLastError(status, "Invalid fc bits: " + wpi::Twine(flow));
|
||||
hal::SetLastError(status, fmt::format("Invalid fc bits: {}", flow));
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -7,8 +7,9 @@
|
||||
#include <algorithm>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <string_view>
|
||||
|
||||
#include <wpi/StringRef.h>
|
||||
#include <wpi/StringExtras.h>
|
||||
#include <wpi/fs.h>
|
||||
|
||||
#include "hal/Errors.h"
|
||||
@@ -53,7 +54,7 @@ std::string SerialHelper::GetVISASerialPortName(HAL_SerialPort port,
|
||||
return "";
|
||||
// Error
|
||||
} else {
|
||||
return m_visaResource[visaIndex].str();
|
||||
return std::string{m_visaResource[visaIndex].str()};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,7 +81,7 @@ std::string SerialHelper::GetOSSerialPortName(HAL_SerialPort port,
|
||||
return "";
|
||||
// Error
|
||||
} else {
|
||||
return m_osResource[osIndex].str();
|
||||
return std::string{m_osResource[osIndex].str()};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -136,8 +137,8 @@ void SerialHelper::SortHubPathVector() {
|
||||
std::sort(m_sortedHubPath.begin(), m_sortedHubPath.end(),
|
||||
[](const wpi::SmallVectorImpl<char>& lhs,
|
||||
const wpi::SmallVectorImpl<char>& rhs) -> int {
|
||||
wpi::StringRef lhsRef(lhs.begin(), lhs.size());
|
||||
wpi::StringRef rhsRef(rhs.begin(), rhs.size());
|
||||
std::string_view lhsRef(lhs.begin(), lhs.size());
|
||||
std::string_view rhsRef(rhs.begin(), rhs.size());
|
||||
return lhsRef.compare(rhsRef);
|
||||
});
|
||||
}
|
||||
@@ -147,9 +148,9 @@ void SerialHelper::CoiteratedSort(
|
||||
wpi::SmallVector<wpi::SmallString<16>, 4> sortedVec;
|
||||
for (auto& str : m_sortedHubPath) {
|
||||
for (size_t i = 0; i < m_unsortedHubPath.size(); i++) {
|
||||
if (wpi::StringRef{m_unsortedHubPath[i].begin(),
|
||||
m_unsortedHubPath[i].size()}
|
||||
.equals(wpi::StringRef{str.begin(), str.size()})) {
|
||||
if (wpi::equals(std::string_view{m_unsortedHubPath[i].begin(),
|
||||
m_unsortedHubPath[i].size()},
|
||||
std::string_view{str.begin(), str.size()})) {
|
||||
sortedVec.push_back(vec[i]);
|
||||
break;
|
||||
}
|
||||
@@ -206,14 +207,14 @@ void SerialHelper::QueryHubPaths(int32_t* status) {
|
||||
*status = 0;
|
||||
|
||||
// split until (/dev/
|
||||
wpi::StringRef devNameRef = wpi::StringRef{osName}.split("(/dev/").second;
|
||||
std::string_view devNameRef = wpi::split(osName, "(/dev/").second;
|
||||
// String not found, continue
|
||||
if (devNameRef.equals(""))
|
||||
if (wpi::equals(devNameRef, ""))
|
||||
continue;
|
||||
|
||||
// Split at )
|
||||
wpi::StringRef matchString = devNameRef.split(')').first;
|
||||
if (matchString.equals(devNameRef))
|
||||
std::string_view matchString = wpi::split(devNameRef, ')').first;
|
||||
if (wpi::equals(matchString, devNameRef))
|
||||
continue;
|
||||
|
||||
// Search directories to get a list of system accessors
|
||||
@@ -223,7 +224,7 @@ void SerialHelper::QueryHubPaths(int32_t* status) {
|
||||
for (auto& p : fs::recursive_directory_iterator("/sys/devices/soc0", ec)) {
|
||||
if (ec)
|
||||
break;
|
||||
std::string path = p.path().string();
|
||||
std::string path = p.path();
|
||||
if (path.find("amba") == std::string::npos)
|
||||
continue;
|
||||
if (path.find("usb") == std::string::npos)
|
||||
@@ -231,22 +232,22 @@ void SerialHelper::QueryHubPaths(int32_t* status) {
|
||||
if (path.find(matchString) == std::string::npos)
|
||||
continue;
|
||||
|
||||
wpi::SmallVector<wpi::StringRef, 16> pathSplitVec;
|
||||
wpi::SmallVector<std::string_view, 16> pathSplitVec;
|
||||
// Split path into individual directories
|
||||
wpi::StringRef{path}.split(pathSplitVec, '/', -1, false);
|
||||
wpi::split(path, pathSplitVec, '/', -1, false);
|
||||
|
||||
// Find each individual item index
|
||||
int findusb = -1;
|
||||
int findtty = -1;
|
||||
int findregex = -1;
|
||||
for (size_t i = 0; i < pathSplitVec.size(); i++) {
|
||||
if (findusb == -1 && pathSplitVec[i].equals("usb1")) {
|
||||
if (findusb == -1 && wpi::equals(pathSplitVec[i], "usb1")) {
|
||||
findusb = i;
|
||||
}
|
||||
if (findtty == -1 && pathSplitVec[i].equals("tty")) {
|
||||
if (findtty == -1 && wpi::equals(pathSplitVec[i], "tty")) {
|
||||
findtty = i;
|
||||
}
|
||||
if (findregex == -1 && pathSplitVec[i].equals(matchString)) {
|
||||
if (findregex == -1 && wpi::equals(pathSplitVec[i], matchString)) {
|
||||
findregex = i;
|
||||
}
|
||||
}
|
||||
@@ -263,10 +264,10 @@ void SerialHelper::QueryHubPaths(int32_t* status) {
|
||||
|
||||
// Add our devices to our list
|
||||
m_unsortedHubPath.emplace_back(
|
||||
wpi::StringRef{pathSplitVec[hubIndex - 2]});
|
||||
std::string_view{pathSplitVec[hubIndex - 2]});
|
||||
m_visaResource.emplace_back(desc);
|
||||
m_osResource.emplace_back(
|
||||
wpi::StringRef{osName}.split("(").second.split(")").first);
|
||||
wpi::split(wpi::split(osName, "(").second, ")").first);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -291,8 +292,9 @@ int32_t SerialHelper::GetIndexForPort(HAL_SerialPort port, int32_t* status) {
|
||||
if (portString.empty()) {
|
||||
for (size_t i = 0; i < 2; i++) {
|
||||
// Remove all used ports
|
||||
auto idx = std::find(m_sortedHubPath.begin(), m_sortedHubPath.end(),
|
||||
m_usbNames[i]);
|
||||
auto idx = std::find_if(
|
||||
m_sortedHubPath.begin(), m_sortedHubPath.end(),
|
||||
[&](const auto& s) { return wpi::equals(s, m_usbNames[i]); });
|
||||
if (idx != m_sortedHubPath.end()) {
|
||||
// found
|
||||
m_sortedHubPath.erase(idx);
|
||||
@@ -327,7 +329,7 @@ int32_t SerialHelper::GetIndexForPort(HAL_SerialPort port, int32_t* status) {
|
||||
int retIndex = -1;
|
||||
|
||||
for (size_t i = 0; i < m_sortedHubPath.size(); i++) {
|
||||
if (m_sortedHubPath[i].equals(portString)) {
|
||||
if (wpi::equals(m_sortedHubPath[i], portString)) {
|
||||
retIndex = i;
|
||||
break;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user