Add braces to C++ single-line loops and conditionals (NFC) (#2973)

This makes code easier to read and more consistent between C++ and Java.
Also update clang-format settings to always add a line break (even if no braces are used).
This commit is contained in:
Peter Johnson
2020-12-28 12:58:06 -08:00
committed by GitHub
parent 0291a3ff56
commit 2aed432b4b
634 changed files with 10716 additions and 3938 deletions

View File

@@ -123,7 +123,8 @@ static void writeRegister(Register reg, uint8_t data) {
// Execute and wait until it's done (up to a millisecond)
initialTime = HAL_GetFPGATime(&status);
while (accel->readSTAT(&status) & 1) {
if (HAL_GetFPGATime(&status) > initialTime + 1000) break;
if (HAL_GetFPGATime(&status) > initialTime + 1000)
break;
}
// Send a stop transmit/receive message with the data
@@ -134,7 +135,8 @@ static void writeRegister(Register reg, uint8_t data) {
// Execute and wait until it's done (up to a millisecond)
initialTime = HAL_GetFPGATime(&status);
while (accel->readSTAT(&status) & 1) {
if (HAL_GetFPGATime(&status) > initialTime + 1000) break;
if (HAL_GetFPGATime(&status) > initialTime + 1000)
break;
}
}
@@ -151,7 +153,8 @@ static uint8_t readRegister(Register reg) {
// Execute and wait until it's done (up to a millisecond)
initialTime = HAL_GetFPGATime(&status);
while (accel->readSTAT(&status) & 1) {
if (HAL_GetFPGATime(&status) > initialTime + 1000) break;
if (HAL_GetFPGATime(&status) > initialTime + 1000)
break;
}
// Receive a message with the data and stop
@@ -162,7 +165,8 @@ static uint8_t readRegister(Register reg) {
// Execute and wait until it's done (up to a millisecond)
initialTime = HAL_GetFPGATime(&status);
while (accel->readSTAT(&status) & 1) {
if (HAL_GetFPGATime(&status) > initialTime + 1000) break;
if (HAL_GetFPGATime(&status) > initialTime + 1000)
break;
}
return accel->readDATI(&status);

View File

@@ -25,7 +25,8 @@ HAL_Bool HAL_IsAccumulatorChannel(HAL_AnalogInputHandle analogPortHandle,
return false;
}
for (int32_t i = 0; i < kNumAccumulators; i++) {
if (port->channel == kAccumulatorChannels[i]) return true;
if (port->channel == kAccumulatorChannels[i])
return true;
}
return false;
}

View File

@@ -48,7 +48,9 @@ void InitializeAnalogGyro() {
} // namespace hal
static void Wait(double seconds) {
if (seconds < 0.0) return;
if (seconds < 0.0) {
return;
}
std::this_thread::sleep_for(std::chrono::duration<double>(seconds));
}
@@ -69,8 +71,9 @@ HAL_GyroHandle HAL_InitializeAnalogGyro(HAL_AnalogInputHandle analogHandle,
auto handle = analogGyroHandles->Allocate(channel, status);
if (*status != 0)
if (*status != 0) {
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
}
// Initialize port structure
auto gyro = analogGyroHandles->Get(handle);
@@ -97,17 +100,25 @@ void HAL_SetupAnalogGyro(HAL_GyroHandle handle, int32_t* status) {
gyro->voltsPerDegreePerSecond = kDefaultVoltsPerDegreePerSecond;
HAL_SetAnalogAverageBits(gyro->handle, kAverageBits, status);
if (*status != 0) return;
if (*status != 0) {
return;
}
HAL_SetAnalogOversampleBits(gyro->handle, kOversampleBits, status);
if (*status != 0) return;
if (*status != 0) {
return;
}
double sampleRate =
kSamplesPerSecond * (1 << (kAverageBits + kOversampleBits));
HAL_SetAnalogSampleRate(sampleRate, status);
if (*status != 0) return;
if (*status != 0) {
return;
}
Wait(0.1);
HAL_SetAnalogGyroDeadband(handle, 0.0, status);
if (*status != 0) return;
if (*status != 0) {
return;
}
}
void HAL_FreeAnalogGyro(HAL_GyroHandle handle) {
@@ -148,14 +159,18 @@ void HAL_ResetAnalogGyro(HAL_GyroHandle handle, int32_t* status) {
return;
}
HAL_ResetAccumulator(gyro->handle, status);
if (*status != 0) return;
if (*status != 0) {
return;
}
const double sampleTime = 1.0 / HAL_GetAnalogSampleRate(status);
const double overSamples =
1 << HAL_GetAnalogOversampleBits(gyro->handle, status);
const double averageSamples =
1 << HAL_GetAnalogAverageBits(gyro->handle, status);
if (*status != 0) return;
if (*status != 0) {
return;
}
Wait(sampleTime * overSamples * averageSamples);
}
@@ -167,7 +182,9 @@ void HAL_CalibrateAnalogGyro(HAL_GyroHandle handle, int32_t* status) {
}
HAL_InitAccumulator(gyro->handle, status);
if (*status != 0) return;
if (*status != 0) {
return;
}
wpi::outs() << "Calibrating analog gyro for " << kCalibrationSampleTime
<< " seconds." << '\n';
Wait(kCalibrationSampleTime);
@@ -175,7 +192,9 @@ void HAL_CalibrateAnalogGyro(HAL_GyroHandle handle, int32_t* status) {
int64_t value;
int64_t count;
HAL_GetAccumulatorOutput(gyro->handle, &value, &count, status);
if (*status != 0) return;
if (*status != 0) {
return;
}
gyro->center = static_cast<int32_t>(
static_cast<double>(value) / static_cast<double>(count) + 0.5);
@@ -183,7 +202,9 @@ void HAL_CalibrateAnalogGyro(HAL_GyroHandle handle, int32_t* status) {
gyro->offset = static_cast<double>(value) / static_cast<double>(count) -
static_cast<double>(gyro->center);
HAL_SetAccumulatorCenter(gyro->handle, gyro->center, status);
if (*status != 0) return;
if (*status != 0) {
return;
}
HAL_ResetAnalogGyro(handle, status);
}
@@ -197,7 +218,9 @@ void HAL_SetAnalogGyroDeadband(HAL_GyroHandle handle, double volts,
int32_t deadband = static_cast<int32_t>(
volts * 1e9 / HAL_GetAnalogLSBWeight(gyro->handle, status) *
(1 << HAL_GetAnalogOversampleBits(gyro->handle, status)));
if (*status != 0) return;
if (*status != 0) {
return;
}
HAL_SetAccumulatorDeadband(gyro->handle, deadband, status);
}

View File

@@ -28,7 +28,9 @@ HAL_AnalogInputHandle HAL_InitializeAnalogInputPort(HAL_PortHandle portHandle,
hal::init::CheckInit();
initializeAnalog(status);
if (*status != 0) return HAL_kInvalidHandle;
if (*status != 0) {
return HAL_kInvalidHandle;
}
int16_t channel = getPortHandleChannel(portHandle);
if (channel == InvalidHandleIndex) {
@@ -38,8 +40,9 @@ HAL_AnalogInputHandle HAL_InitializeAnalogInputPort(HAL_PortHandle portHandle,
HAL_AnalogInputHandle handle = analogInputHandles->Allocate(channel, status);
if (*status != 0)
if (*status != 0) {
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
}
// Initialize port structure
auto analog_port = analogInputHandles->Get(handle);
@@ -66,7 +69,9 @@ void HAL_FreeAnalogInputPort(HAL_AnalogInputHandle analogPortHandle) {
analogInputHandles->Free(analogPortHandle);
}
HAL_Bool HAL_CheckAnalogModule(int32_t module) { return module == 1; }
HAL_Bool HAL_CheckAnalogModule(int32_t module) {
return module == 1;
}
HAL_Bool HAL_CheckAnalogInputChannel(int32_t channel) {
return channel < kNumAnalogInputs && channel >= 0;
@@ -80,13 +85,17 @@ void HAL_SetAnalogSampleRate(double samplesPerSecond, int32_t* status) {
// TODO: Need double comparison with epsilon.
// wpi_assert(!sampleRateSet || GetSampleRate() == samplesPerSecond);
initializeAnalog(status);
if (*status != 0) return;
if (*status != 0) {
return;
}
setAnalogSampleRate(samplesPerSecond, status);
}
double HAL_GetAnalogSampleRate(int32_t* status) {
initializeAnalog(status);
if (*status != 0) return 0;
if (*status != 0) {
return 0;
}
uint32_t ticksPerConversion = analogInputSystem->readLoopTiming(status);
uint32_t ticksPerSample =
ticksPerConversion * getAnalogNumActiveChannels(status);

View File

@@ -40,9 +40,13 @@ void InitializeAnalogInternal() {
void initializeAnalog(int32_t* status) {
hal::init::CheckInit();
if (analogSystemInitialized) return;
if (analogSystemInitialized) {
return;
}
std::scoped_lock lock(analogRegisterWindowMutex);
if (analogSystemInitialized) return;
if (analogSystemInitialized) {
return;
}
analogInputSystem.reset(tAI::create(status));
analogOutputSystem.reset(tAO::create(status));
setAnalogNumChannelsToActivate(kNumAnalogInputs);
@@ -52,7 +56,9 @@ void initializeAnalog(int32_t* status) {
int32_t getAnalogNumActiveChannels(int32_t* status) {
int32_t scanSize = analogInputSystem->readConfig_ScanSize(status);
if (scanSize == 0) return 8;
if (scanSize == 0) {
return 8;
}
return scanSize;
}
@@ -61,8 +67,9 @@ void setAnalogNumChannelsToActivate(int32_t channels) {
}
int32_t getAnalogNumChannelsToActivate(int32_t* status) {
if (analogNumChannelsToActivate == 0)
if (analogNumChannelsToActivate == 0) {
return getAnalogNumActiveChannels(status);
}
return analogNumChannelsToActivate;
}
@@ -79,7 +86,9 @@ void setAnalogSampleRate(double samplesPerSecond, int32_t* status) {
ticksPerSample / getAnalogNumChannelsToActivate(status);
// ticksPerConversion must be at least 80
if (ticksPerConversion < 80) {
if ((*status) >= 0) *status = SAMPLE_RATE_TOO_HIGH;
if ((*status) >= 0) {
*status = SAMPLE_RATE_TOO_HIGH;
}
ticksPerConversion = 80;
}

View File

@@ -43,7 +43,9 @@ HAL_AnalogOutputHandle HAL_InitializeAnalogOutputPort(HAL_PortHandle portHandle,
hal::init::CheckInit();
initializeAnalog(status);
if (*status != 0) return HAL_kInvalidHandle;
if (*status != 0) {
return HAL_kInvalidHandle;
}
int16_t channel = getPortHandleChannel(portHandle);
if (channel == InvalidHandleIndex) {
@@ -54,8 +56,9 @@ HAL_AnalogOutputHandle HAL_InitializeAnalogOutputPort(HAL_PortHandle portHandle,
HAL_AnalogOutputHandle handle =
analogOutputHandles->Allocate(channel, status);
if (*status != 0)
if (*status != 0) {
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
}
auto port = analogOutputHandles->Get(handle);
if (port == nullptr) { // would only error on thread issue
@@ -82,10 +85,11 @@ void HAL_SetAnalogOutput(HAL_AnalogOutputHandle analogOutputHandle,
uint16_t rawValue = static_cast<uint16_t>(voltage / 5.0 * 0x1000);
if (voltage < 0.0)
if (voltage < 0.0) {
rawValue = 0;
else if (voltage > 5.0)
} else if (voltage > 5.0) {
rawValue = 0x1000;
}
analogOutputSystem->writeMXP(port->channel, rawValue, status);
}

View File

@@ -359,10 +359,11 @@ void HAL_SetCounterReverseDirection(HAL_CounterHandle counterHandle,
}
if (counter->counter->readConfig_Mode(status) ==
HAL_Counter_kExternalDirection) {
if (reverseDirection)
if (reverseDirection) {
HAL_SetCounterDownSourceEdge(counterHandle, true, true, status);
else
} else {
HAL_SetCounterDownSourceEdge(counterHandle, false, true, status);
}
}
}

View File

@@ -44,7 +44,9 @@ HAL_DigitalHandle HAL_InitializeDIOPort(HAL_PortHandle portHandle,
hal::init::CheckInit();
initializeDigital(status);
if (*status != 0) return HAL_kInvalidHandle;
if (*status != 0) {
return HAL_kInvalidHandle;
}
int16_t channel = getPortHandleChannel(portHandle);
if (channel == InvalidHandleIndex || channel >= kNumDigitalChannels) {
@@ -55,8 +57,9 @@ HAL_DigitalHandle HAL_InitializeDIOPort(HAL_PortHandle portHandle,
auto handle =
digitalChannelHandles->Allocate(channel, HAL_HandleEnum::DIO, status);
if (*status != 0)
if (*status != 0) {
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
}
auto port = digitalChannelHandles->Get(handle, HAL_HandleEnum::DIO);
if (port == nullptr) { // would only occur on thread issue.
@@ -124,7 +127,8 @@ HAL_Bool HAL_CheckDIOChannel(int32_t channel) {
void HAL_FreeDIOPort(HAL_DigitalHandle dioPortHandle) {
auto port = digitalChannelHandles->Get(dioPortHandle, HAL_HandleEnum::DIO);
// no status, so no need to check for a proper free.
if (port == nullptr) return;
if (port == nullptr)
return;
digitalChannelHandles->Free(dioPortHandle, HAL_HandleEnum::DIO);
// Wait for no other object to hold this handle.
@@ -186,7 +190,9 @@ void HAL_SetDigitalPWMRate(double rate, int32_t* status) {
// higher freq.
// TODO: Round in the linear rate domain.
initializeDigital(status);
if (*status != 0) return;
if (*status != 0) {
return;
}
uint16_t pwmPeriodPower = static_cast<uint16_t>(
std::log(1.0 / (16 * 1.0E-6 * rate)) / std::log(2.0) + 0.5);
digitalSystem->writePWMPeriodPower(pwmPeriodPower, status);
@@ -200,10 +206,16 @@ void HAL_SetDigitalPWMDutyCycle(HAL_DigitalPWMHandle pwmGenerator,
return;
}
int32_t id = *port;
if (dutyCycle > 1.0) dutyCycle = 1.0;
if (dutyCycle < 0.0) dutyCycle = 0.0;
if (dutyCycle > 1.0) {
dutyCycle = 1.0;
}
if (dutyCycle < 0.0) {
dutyCycle = 0.0;
}
double rawDutyCycle = 256.0 * dutyCycle;
if (rawDutyCycle > 255.5) rawDutyCycle = 255.5;
if (rawDutyCycle > 255.5) {
rawDutyCycle = 255.5;
}
{
std::scoped_lock lock(digitalPwmMutex);
uint16_t pwmPeriodPower = digitalSystem->readPWMPeriodPower(status);
@@ -248,7 +260,9 @@ void HAL_SetDIO(HAL_DigitalHandle dioPortHandle, HAL_Bool value,
return;
}
if (value != 0 && value != 1) {
if (value != 0) value = 1;
if (value != 0) {
value = 1;
}
}
{
std::scoped_lock lock(digitalDIOMutex);
@@ -407,7 +421,9 @@ HAL_Bool HAL_IsPulsing(HAL_DigitalHandle dioPortHandle, int32_t* status) {
HAL_Bool HAL_IsAnyPulsing(int32_t* status) {
initializeDigital(status);
if (*status != 0) return false;
if (*status != 0) {
return false;
}
tDIO::tPulse pulseRegister = digitalSystem->readPulse(status);
return pulseRegister.Headers != 0 && pulseRegister.MXP != 0 &&
pulseRegister.SPIPort != 0;
@@ -456,7 +472,9 @@ int32_t HAL_GetFilterSelect(HAL_DigitalHandle dioPortHandle, int32_t* status) {
void HAL_SetFilterPeriod(int32_t filterIndex, int64_t value, int32_t* status) {
initializeDigital(status);
if (*status != 0) return;
if (*status != 0) {
return;
}
std::scoped_lock lock(digitalDIOMutex);
digitalSystem->writeFilterPeriodHdr(filterIndex, value, status);
if (*status == 0) {
@@ -466,7 +484,9 @@ void HAL_SetFilterPeriod(int32_t filterIndex, int64_t value, int32_t* status) {
int64_t HAL_GetFilterPeriod(int32_t filterIndex, int32_t* status) {
initializeDigital(status);
if (*status != 0) return 0;
if (*status != 0) {
return 0;
}
uint32_t hdrPeriod = 0;
uint32_t mxpPeriod = 0;
{

View File

@@ -124,7 +124,8 @@ void HAL_FreeDMA(HAL_DMAHandle handle) {
auto dma = dmaHandles->Get(handle);
dmaHandles->Free(handle);
if (!dma) return;
if (!dma)
return;
int32_t status = 0;
if (dma->manager) {
@@ -514,7 +515,9 @@ void HAL_SetDMAExternalTrigger(HAL_DMAHandle handle,
auto isExternalClock = dma->aDMA->readConfig_ExternalClock(status);
if (*status == 0 && !isExternalClock) {
dma->aDMA->writeConfig_ExternalClock(true, status);
if (*status != 0) return;
if (*status != 0) {
return;
}
} else if (*status != 0) {
return;
}
@@ -554,7 +557,9 @@ void HAL_StartDMA(HAL_DMAHandle handle, int32_t queueDepth, int32_t* status) {
}
tDMA::tConfig config = dma->aDMA->readConfig(status);
if (*status != 0) return;
if (*status != 0) {
return;
}
{
size_t accum_size = 0;

View File

@@ -43,8 +43,12 @@ void InitializeDigitalInternal() {
} // namespace init
namespace detail {
wpi::mutex& UnsafeGetDIOMutex() { return digitalDIOMutex; }
tDIO* UnsafeGetDigialSystem() { return digitalSystem.get(); }
wpi::mutex& UnsafeGetDIOMutex() {
return digitalDIOMutex;
}
tDIO* UnsafeGetDigialSystem() {
return digitalSystem.get();
}
int32_t ComputeDigitalMask(HAL_DigitalHandle handle, int32_t* status) {
auto port = digitalChannelHandles->Get(handle, HAL_HandleEnum::DIO);
if (port == nullptr) {
@@ -69,11 +73,15 @@ void initializeDigital(int32_t* status) {
static std::atomic_bool initialized{false};
static wpi::mutex initializeMutex;
// Initial check, as if it's true initialization has finished
if (initialized) return;
if (initialized) {
return;
}
std::scoped_lock lock(initializeMutex);
// Second check in case another thread was waiting
if (initialized) return;
if (initialized) {
return;
}
digitalSystem.reset(tDIO::create(status));
@@ -89,7 +97,8 @@ void initializeDigital(int32_t* status) {
// Make sure that the 9403 IONode has had a chance to initialize before
// continuing.
while (pwmSystem->readLoopTiming(status) == 0) std::this_thread::yield();
while (pwmSystem->readLoopTiming(status) == 0)
std::this_thread::yield();
if (pwmSystem->readLoopTiming(status) != kExpectedLoopTiming) {
*status = LOOP_TIMING_ERROR; // NOTE: Doesn't display the error
@@ -160,7 +169,9 @@ bool remapDigitalSource(HAL_Handle digitalSourceHandle,
}
}
int32_t remapMXPChannel(int32_t channel) { return channel - 10; }
int32_t remapMXPChannel(int32_t channel) {
return channel - 10;
}
int32_t remapMXPPWMChannel(int32_t channel) {
if (channel < 14) {
@@ -170,7 +181,9 @@ int32_t remapMXPPWMChannel(int32_t channel) {
}
}
int32_t remapSPIChannel(int32_t channel) { return channel - 26; }
int32_t remapSPIChannel(int32_t channel) {
return channel - 26;
}
} // namespace hal

View File

@@ -98,13 +98,15 @@ bool remapDigitalSource(HAL_Handle digitalSourceHandle,
*/
constexpr int32_t remapDigitalChannelToBitfieldChannel(int32_t channel) {
// First 10 are headers
if (channel < kNumDigitalHeaders) return channel;
// 2nd group of 16 are mxp. So if mxp port, add 6, since they start at 10
else if (channel < kNumDigitalMXPChannels)
if (channel < kNumDigitalHeaders) {
return channel;
// 2nd group of 16 are mxp. So if mxp port, add 6, since they start at 10
} else if (channel < kNumDigitalMXPChannels) {
return channel + 6;
// Assume SPI, so remove MXP channels
else
// Assume SPI, so remove MXP channels
} else {
return channel - kNumDigitalMXPChannels;
}
}
/**

View File

@@ -60,15 +60,23 @@ void Encoder::SetupCounter(HAL_Handle digitalSourceHandleA,
m_encodingScale = encodingType == HAL_Encoder_k1X ? 1 : 2;
m_counter =
HAL_InitializeCounter(HAL_Counter_kExternalDirection, &m_index, status);
if (*status != 0) return;
if (*status != 0) {
return;
}
HAL_SetCounterMaxPeriod(m_counter, 0.5, status);
if (*status != 0) return;
if (*status != 0) {
return;
}
HAL_SetCounterUpSource(m_counter, digitalSourceHandleA, analogTriggerTypeA,
status);
if (*status != 0) return;
if (*status != 0) {
return;
}
HAL_SetCounterDownSource(m_counter, digitalSourceHandleB, analogTriggerTypeB,
status);
if (*status != 0) return;
if (*status != 0) {
return;
}
if (encodingType == HAL_Encoder_k1X) {
HAL_SetCounterUpSourceEdge(m_counter, true, false, status);
HAL_SetCounterAverageSize(m_counter, 1, status);
@@ -240,7 +248,9 @@ bool GetEncoderBaseHandle(HAL_EncoderHandle handle,
HAL_FPGAEncoderHandle* fpgaHandle,
HAL_CounterHandle* counterHandle) {
auto encoder = encoderHandles->Get(handle);
if (!handle) return false;
if (!handle) {
return false;
}
*fpgaHandle = encoder->m_encoder;
*counterHandle = encoder->m_counter;
@@ -258,7 +268,9 @@ HAL_EncoderHandle HAL_InitializeEncoder(
auto encoder = std::make_shared<Encoder>(
digitalSourceHandleA, analogTriggerTypeA, digitalSourceHandleB,
analogTriggerTypeB, reverseDirection, encodingType, status);
if (*status != 0) return HAL_kInvalidHandle; // return in creation error
if (*status != 0) {
return HAL_kInvalidHandle; // return in creation error
}
auto handle = encoderHandles->Allocate(encoder);
if (handle == HAL_kInvalidHandle) {
*status = NO_AVAILABLE_RESOURCES;

View File

@@ -158,7 +158,8 @@ int32_t HAL_SendError(HAL_Bool isError, int32_t errorCode, HAL_Bool isLVCode,
auto curTime = std::chrono::steady_clock::now();
int i;
for (i = 0; i < KEEP_MSGS; ++i) {
if (prevMsg[i] == details) break;
if (prevMsg[i] == details)
break;
}
int retval = 0;
if (i == KEEP_MSGS || (curTime - prevMsgTime[i]) >= std::chrono::seconds(1)) {
@@ -304,7 +305,9 @@ char* HAL_GetJoystickName(int32_t joystickNum) {
}
}
void HAL_FreeJoystickName(char* name) { std::free(name); }
void HAL_FreeJoystickName(char* name) {
std::free(name);
}
int32_t HAL_GetJoystickAxisType(int32_t joystickNum, int32_t axis) {
HAL_JoystickDescriptor joystickDesc;
@@ -360,7 +363,9 @@ HAL_Bool HAL_IsNewControlData(void) {
std::scoped_lock lock{*newDSDataAvailableMutex};
int& lastCount = GetThreadLocalLastCount();
int currentCount = newDSDataAvailableCounter;
if (lastCount == currentCount) return false;
if (lastCount == currentCount) {
return false;
}
lastCount = currentCount;
return true;
}
@@ -368,7 +373,9 @@ HAL_Bool HAL_IsNewControlData(void) {
/**
* Waits for the newest DS packet to arrive. Note that this is a blocking call.
*/
void HAL_WaitForDSData(void) { HAL_WaitForDSDataTimeout(0); }
void HAL_WaitForDSData(void) {
HAL_WaitForDSDataTimeout(0);
}
/**
* Waits for the newest DS packet to arrive. If timeout is <= 0, this will wait
@@ -406,7 +413,9 @@ constexpr int32_t refNumber = 42;
static void newDataOccur(uint32_t refNum) {
// Since we could get other values, require our specific handle
// to signal our threads
if (refNum != refNumber) return;
if (refNum != refNumber) {
return;
}
std::scoped_lock lock{*newDSDataAvailableMutex};
// Notify all threads
++newDSDataAvailableCounter;
@@ -422,11 +431,15 @@ void HAL_InitializeDriverStation(void) {
static std::atomic_bool initialized{false};
static wpi::mutex initializeMutex;
// Initial check, as if it's true initialization has finished
if (initialized) return;
if (initialized) {
return;
}
std::scoped_lock lock(initializeMutex);
// Second check in case another thread was waiting
if (initialized) return;
if (initialized) {
return;
}
// Set up the occur function internally with NetComm
NetCommRPCProxy_SetOccurFuncPointer(newDataOccur);
@@ -440,6 +453,8 @@ void HAL_InitializeDriverStation(void) {
* Releases the DS Mutex to allow proper shutdown of any threads that are
* waiting on it.
*/
void HAL_ReleaseDSMutex(void) { newDataOccur(refNumber); }
void HAL_ReleaseDSMutex(void) {
newDataOccur(refNumber);
}
} // extern "C"

View File

@@ -92,14 +92,20 @@ extern "C" {
HAL_PortHandle HAL_GetPort(int32_t channel) {
// Dont allow a number that wouldn't fit in a uint8_t
if (channel < 0 || channel >= 255) return HAL_kInvalidHandle;
if (channel < 0 || channel >= 255) {
return HAL_kInvalidHandle;
}
return createPortHandle(channel, 1);
}
HAL_PortHandle HAL_GetPortWithModule(int32_t module, int32_t channel) {
// Dont allow a number that wouldn't fit in a uint8_t
if (channel < 0 || channel >= 255) return HAL_kInvalidHandle;
if (module < 0 || module >= 255) return HAL_kInvalidHandle;
if (channel < 0 || channel >= 255) {
return HAL_kInvalidHandle;
}
if (module < 0 || module >= 255) {
return HAL_kInvalidHandle;
}
return createPortHandle(channel, module);
}
@@ -232,7 +238,9 @@ const char* HAL_GetErrorMessage(int32_t code) {
}
}
HAL_RuntimeType HAL_GetRuntimeType(void) { return HAL_Athena; }
HAL_RuntimeType HAL_GetRuntimeType(void) {
return HAL_Athena;
}
int32_t HAL_GetFPGAVersion(int32_t* status) {
if (!global) {
@@ -259,11 +267,15 @@ uint64_t HAL_GetFPGATime(int32_t* status) {
uint64_t upper1 = global->readLocalTimeUpper(status);
uint32_t lower = global->readLocalTime(status);
uint64_t upper2 = global->readLocalTimeUpper(status);
if (*status != 0) return 0;
if (*status != 0) {
return 0;
}
if (upper1 != upper2) {
// Rolled over between the lower call, reread lower
lower = global->readLocalTime(status);
if (*status != 0) return 0;
if (*status != 0) {
return 0;
}
}
return (upper2 << 32) + lower;
}
@@ -272,7 +284,9 @@ uint64_t HAL_ExpandFPGATime(uint32_t unexpanded_lower, int32_t* status) {
// Capture the current FPGA time. This will give us the upper half of the
// clock.
uint64_t fpga_time = HAL_GetFPGATime(status);
if (*status != 0) return 0;
if (*status != 0) {
return 0;
}
// Now, we need to detect the case where the lower bits rolled over after we
// sampled. In that case, the upper bits will be 1 bigger than they should
@@ -319,7 +333,9 @@ static bool killExistingProgram(int timeout, int mode) {
std::fstream fs;
// By making this both in/out, it won't give us an error if it doesnt exist
fs.open("/var/lock/frc.pid", std::fstream::in | std::fstream::out);
if (fs.bad()) return false;
if (fs.bad()) {
return false;
}
pid_t pid = 0;
if (!fs.eof() && !fs.fail()) {
@@ -359,11 +375,15 @@ HAL_Bool HAL_Initialize(int32_t timeout, int32_t mode) {
static std::atomic_bool initialized{false};
static wpi::mutex initializeMutex;
// Initial check, as if it's true initialization has finished
if (initialized) return true;
if (initialized) {
return true;
}
std::scoped_lock lock(initializeMutex);
// Second check in case another thread was waiting
if (initialized) return true;
if (initialized) {
return true;
}
hal::init::InitializeHAL();
@@ -394,7 +414,9 @@ HAL_Bool HAL_Initialize(int32_t timeout, int32_t mode) {
global.reset(tGlobal::create(&status));
watchdog.reset(tSysWatchdog::create(&status));
if (status != 0) return false;
if (status != 0) {
return false;
}
HAL_InitializeDriverStation();

View File

@@ -9,6 +9,8 @@
namespace hal {
namespace init {
std::atomic_bool HAL_IsInitialized{false};
void RunInitialize() { HAL_Initialize(500, 0); }
void RunInitialize() {
HAL_Initialize(500, 0);
}
} // namespace init
} // namespace hal

View File

@@ -11,7 +11,9 @@ namespace init {
extern std::atomic_bool HAL_IsInitialized;
extern void RunInitialize();
static inline void CheckInit() {
if (HAL_IsInitialized.load(std::memory_order_relaxed)) return;
if (HAL_IsInitialized.load(std::memory_order_relaxed)) {
return;
}
RunInitialize();
}

View File

@@ -41,7 +41,9 @@ extern "C" {
void HAL_InitializeI2C(HAL_I2CPort port, int32_t* status) {
hal::init::CheckInit();
initializeDigital(status);
if (*status != 0) return;
if (*status != 0) {
return;
}
if (port < 0 || port > 1) {
// Set port out of range error here
@@ -51,7 +53,9 @@ void HAL_InitializeI2C(HAL_I2CPort port, int32_t* status) {
if (port == HAL_I2C_kOnboard) {
std::scoped_lock lock(digitalI2COnBoardMutex);
i2COnboardObjCount++;
if (i2COnboardObjCount > 1) return;
if (i2COnboardObjCount > 1) {
return;
}
int handle = open("/dev/i2c-2", O_RDWR);
if (handle < 0) {
std::printf("Failed to open onboard i2c bus: %s\n", std::strerror(errno));
@@ -61,7 +65,9 @@ void HAL_InitializeI2C(HAL_I2CPort port, int32_t* status) {
} else {
std::scoped_lock lock(digitalI2CMXPMutex);
i2CMXPObjCount++;
if (i2CMXPObjCount > 1) return;
if (i2CMXPObjCount > 1) {
return;
}
if ((i2CMXPDigitalHandle1 = HAL_InitializeDIOPort(
HAL_GetPort(24), false, status)) == HAL_kInvalidHandle) {
return;

View File

@@ -28,7 +28,9 @@ class InterruptThread : public wpi::SafeThread {
std::unique_lock lock(m_mutex);
while (m_active) {
m_cond.wait(lock, [&] { return !m_active || m_notify; });
if (!m_active) break;
if (!m_active) {
break;
}
m_notify = false;
HAL_InterruptHandlerFunction handler = m_handler;
uint32_t mask = m_mask;
@@ -49,14 +51,16 @@ class InterruptThreadOwner : public wpi::SafeThreadOwner<InterruptThread> {
public:
void SetFunc(HAL_InterruptHandlerFunction handler, void* param) {
auto thr = GetThread();
if (!thr) return;
if (!thr)
return;
thr->m_handler = handler;
thr->m_param = param;
}
void Notify(uint32_t mask) {
auto thr = GetThread();
if (!thr) return;
if (!thr)
return;
thr->m_mask = mask;
thr->m_notify = true;
thr->m_cond.notify_one();

View File

@@ -75,8 +75,10 @@ static void alarmCallback() {
// process all notifiers
notifierHandles->ForEach([&](HAL_NotifierHandle handle, Notifier* notifier) {
if (notifier->triggerTime == UINT64_MAX) return;
if (currentTime == 0) currentTime = HAL_GetFPGATime(&status);
if (notifier->triggerTime == UINT64_MAX)
return;
if (currentTime == 0)
currentTime = HAL_GetFPGATime(&status);
std::unique_lock lock(notifier->mutex);
if (notifier->triggerTime < currentTime) {
notifier->triggerTime = UINT64_MAX;
@@ -102,19 +104,25 @@ static void notifierThreadMain() {
tInterruptManager manager{1 << kTimerInterruptNumber, true, &status};
while (notifierRunning) {
auto triggeredMask = manager.watch(10000, false, &status);
if (!notifierRunning) break;
if (triggeredMask == 0) continue;
if (!notifierRunning) {
break;
}
if (triggeredMask == 0)
continue;
alarmCallback();
}
}
static void cleanupNotifierAtExit() {
int32_t status = 0;
if (notifierAlarm) notifierAlarm->writeEnable(false, &status);
if (notifierAlarm)
notifierAlarm->writeEnable(false, &status);
notifierAlarm = nullptr;
notifierRunning = false;
hal::ReleaseFPGAInterrupt(kTimerInterruptNumber);
if (notifierThread.joinable()) notifierThread.join();
if (notifierThread.joinable()) {
notifierThread.join();
}
}
namespace hal {
@@ -130,8 +138,9 @@ extern "C" {
HAL_NotifierHandle HAL_InitializeNotifier(int32_t* status) {
hal::init::CheckInit();
if (!notifierAtexitRegistered.test_and_set())
if (!notifierAtexitRegistered.test_and_set()) {
std::atexit(cleanupNotifierAtExit);
}
if (notifierRefCount.fetch_add(1) == 0) {
std::scoped_lock lock(notifierMutex);
@@ -154,7 +163,8 @@ void HAL_SetNotifierName(HAL_NotifierHandle notifierHandle, const char* name,
void HAL_StopNotifier(HAL_NotifierHandle notifierHandle, int32_t* status) {
auto notifier = notifierHandles->Get(notifierHandle);
if (!notifier) return;
if (!notifier)
return;
{
std::scoped_lock lock(notifier->mutex);
@@ -167,7 +177,8 @@ void HAL_StopNotifier(HAL_NotifierHandle notifierHandle, int32_t* status) {
void HAL_CleanNotifier(HAL_NotifierHandle notifierHandle, int32_t* status) {
auto notifier = notifierHandles->Free(notifierHandle);
if (!notifier) return;
if (!notifier)
return;
// Just in case HAL_StopNotifier() wasn't called...
{
@@ -184,10 +195,13 @@ void HAL_CleanNotifier(HAL_NotifierHandle notifierHandle, int32_t* status) {
// here (the atomic fetch_sub will prevent multiple parallel entries
// into this function)
if (notifierAlarm) notifierAlarm->writeEnable(false, status);
if (notifierAlarm)
notifierAlarm->writeEnable(false, status);
notifierRunning = false;
hal::ReleaseFPGAInterrupt(kTimerInterruptNumber);
if (notifierThread.joinable()) notifierThread.join();
if (notifierThread.joinable()) {
notifierThread.join();
}
std::scoped_lock lock(notifierMutex);
notifierAlarm = nullptr;
@@ -198,7 +212,8 @@ void HAL_CleanNotifier(HAL_NotifierHandle notifierHandle, int32_t* status) {
void HAL_UpdateNotifierAlarm(HAL_NotifierHandle notifierHandle,
uint64_t triggerTime, int32_t* status) {
auto notifier = notifierHandles->Get(notifierHandle);
if (!notifier) return;
if (!notifier)
return;
{
std::scoped_lock lock(notifier->mutex);
@@ -215,14 +230,16 @@ void HAL_UpdateNotifierAlarm(HAL_NotifierHandle notifierHandle,
notifierAlarm->writeTriggerTime(static_cast<uint32_t>(closestTrigger),
status);
// Enable the alarm.
if (!wasActive) notifierAlarm->writeEnable(true, status);
if (!wasActive)
notifierAlarm->writeEnable(true, status);
}
}
void HAL_CancelNotifierAlarm(HAL_NotifierHandle notifierHandle,
int32_t* status) {
auto notifier = notifierHandles->Get(notifierHandle);
if (!notifier) return;
if (!notifier)
return;
{
std::scoped_lock lock(notifier->mutex);
@@ -233,7 +250,8 @@ void HAL_CancelNotifierAlarm(HAL_NotifierHandle notifierHandle,
uint64_t HAL_WaitForNotifierAlarm(HAL_NotifierHandle notifierHandle,
int32_t* status) {
auto notifier = notifierHandles->Get(notifierHandle);
if (!notifier) return 0;
if (!notifier)
return 0;
std::unique_lock lock(notifier->mutex);
notifier->cond.wait(lock, [&] {
return !notifier->active || notifier->triggeredTime != UINT64_MAX;

View File

@@ -306,15 +306,21 @@ void HAL_GetPDPAllChannelCurrents(HAL_PDPHandle handle, double* currents,
PdpStatus1 pdpStatus;
HAL_ReadCANPacketTimeout(handle, Status1, pdpStatus.data, &length,
&receivedTimestamp, TimeoutMs, status);
if (*status != 0) return;
if (*status != 0) {
return;
}
PdpStatus2 pdpStatus2;
HAL_ReadCANPacketTimeout(handle, Status2, pdpStatus2.data, &length,
&receivedTimestamp, TimeoutMs, status);
if (*status != 0) return;
if (*status != 0) {
return;
}
PdpStatus3 pdpStatus3;
HAL_ReadCANPacketTimeout(handle, Status3, pdpStatus3.data, &length,
&receivedTimestamp, TimeoutMs, status);
if (*status != 0) return;
if (*status != 0) {
return;
}
currents[0] = ((static_cast<uint32_t>(pdpStatus.bits.chan1_h8) << 2) |
pdpStatus.bits.chan1_l2) *

View File

@@ -71,7 +71,9 @@ HAL_DigitalHandle HAL_InitializePWMPort(HAL_PortHandle portHandle,
hal::init::CheckInit();
initializeDigital(status);
if (*status != 0) return HAL_kInvalidHandle;
if (*status != 0) {
return HAL_kInvalidHandle;
}
int16_t channel = getPortHandleChannel(portHandle);
if (channel == InvalidHandleIndex || channel >= kNumPWMChannels) {
@@ -90,8 +92,9 @@ HAL_DigitalHandle HAL_InitializePWMPort(HAL_PortHandle portHandle,
auto handle =
digitalChannelHandles->Allocate(channel, HAL_HandleEnum::PWM, status);
if (*status != 0)
if (*status != 0) {
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
}
auto port = digitalChannelHandles->Get(handle, HAL_HandleEnum::PWM);
if (port == nullptr) { // would only occur on thread issue.
@@ -160,7 +163,9 @@ void HAL_SetPWMConfig(HAL_DigitalHandle pwmPortHandle, double max,
// calculate the loop time in milliseconds
double loopTime =
HAL_GetPWMLoopTiming(status) / (kSystemClockTicksPerMicrosecond * 1e3);
if (*status != 0) return;
if (*status != 0) {
return;
}
int32_t maxPwm = static_cast<int32_t>((max - kDefaultPwmCenter) / loopTime +
kDefaultPwmStepsDown - 1);
@@ -356,7 +361,9 @@ double HAL_GetPWMSpeed(HAL_DigitalHandle pwmPortHandle, int32_t* status) {
}
int32_t value = HAL_GetPWMRaw(pwmPortHandle, status);
if (*status != 0) return 0;
if (*status != 0) {
return 0;
}
DigitalPort* dPort = port.get();
if (value == kPwmDisabled) {
@@ -388,7 +395,9 @@ double HAL_GetPWMPosition(HAL_DigitalHandle pwmPortHandle, int32_t* status) {
}
int32_t value = HAL_GetPWMRaw(pwmPortHandle, status);
if (*status != 0) return 0;
if (*status != 0) {
return 0;
}
DigitalPort* dPort = port.get();
if (value < GetMinNegativePwm(dPort)) {
@@ -430,22 +439,30 @@ void HAL_SetPWMPeriodScale(HAL_DigitalHandle pwmPortHandle, int32_t squelchMask,
int32_t HAL_GetPWMLoopTiming(int32_t* status) {
initializeDigital(status);
if (*status != 0) return 0;
if (*status != 0) {
return 0;
}
return pwmSystem->readLoopTiming(status);
}
uint64_t HAL_GetPWMCycleStartTime(int32_t* status) {
initializeDigital(status);
if (*status != 0) return 0;
if (*status != 0) {
return 0;
}
uint64_t upper1 = pwmSystem->readCycleStartTimeUpper(status);
uint32_t lower = pwmSystem->readCycleStartTime(status);
uint64_t upper2 = pwmSystem->readCycleStartTimeUpper(status);
if (*status != 0) return 0;
if (*status != 0) {
return 0;
}
if (upper1 != upper2) {
// Rolled over between the lower call, reread lower
lower = pwmSystem->readCycleStartTime(status);
if (*status != 0) return 0;
if (*status != 0) {
return 0;
}
}
return (upper2 << 32) + lower;
}

View File

@@ -16,25 +16,65 @@ void InitializePorts() {}
extern "C" {
int32_t HAL_GetNumAccumulators(void) { return kNumAccumulators; }
int32_t HAL_GetNumAnalogTriggers(void) { return kNumAnalogTriggers; }
int32_t HAL_GetNumAnalogInputs(void) { return kNumAnalogInputs; }
int32_t HAL_GetNumAnalogOutputs(void) { return kNumAnalogOutputs; }
int32_t HAL_GetNumCounters(void) { return kNumCounters; }
int32_t HAL_GetNumDigitalHeaders(void) { return kNumDigitalHeaders; }
int32_t HAL_GetNumPWMHeaders(void) { return kNumPWMHeaders; }
int32_t HAL_GetNumDigitalChannels(void) { return kNumDigitalChannels; }
int32_t HAL_GetNumPWMChannels(void) { return kNumPWMChannels; }
int32_t HAL_GetNumDigitalPWMOutputs(void) { return kNumDigitalPWMOutputs; }
int32_t HAL_GetNumEncoders(void) { return kNumEncoders; }
int32_t HAL_GetNumInterrupts(void) { return kNumInterrupts; }
int32_t HAL_GetNumRelayChannels(void) { return kNumRelayChannels; }
int32_t HAL_GetNumRelayHeaders(void) { return kNumRelayHeaders; }
int32_t HAL_GetNumPCMModules(void) { return kNumPCMModules; }
int32_t HAL_GetNumSolenoidChannels(void) { return kNumSolenoidChannels; }
int32_t HAL_GetNumPDPModules(void) { return kNumPDPModules; }
int32_t HAL_GetNumPDPChannels(void) { return kNumPDPChannels; }
int32_t HAL_GetNumDutyCycles(void) { return kNumDutyCycles; }
int32_t HAL_GetNumAddressableLEDs(void) { return kNumAddressableLEDs; }
int32_t HAL_GetNumAccumulators(void) {
return kNumAccumulators;
}
int32_t HAL_GetNumAnalogTriggers(void) {
return kNumAnalogTriggers;
}
int32_t HAL_GetNumAnalogInputs(void) {
return kNumAnalogInputs;
}
int32_t HAL_GetNumAnalogOutputs(void) {
return kNumAnalogOutputs;
}
int32_t HAL_GetNumCounters(void) {
return kNumCounters;
}
int32_t HAL_GetNumDigitalHeaders(void) {
return kNumDigitalHeaders;
}
int32_t HAL_GetNumPWMHeaders(void) {
return kNumPWMHeaders;
}
int32_t HAL_GetNumDigitalChannels(void) {
return kNumDigitalChannels;
}
int32_t HAL_GetNumPWMChannels(void) {
return kNumPWMChannels;
}
int32_t HAL_GetNumDigitalPWMOutputs(void) {
return kNumDigitalPWMOutputs;
}
int32_t HAL_GetNumEncoders(void) {
return kNumEncoders;
}
int32_t HAL_GetNumInterrupts(void) {
return kNumInterrupts;
}
int32_t HAL_GetNumRelayChannels(void) {
return kNumRelayChannels;
}
int32_t HAL_GetNumRelayHeaders(void) {
return kNumRelayHeaders;
}
int32_t HAL_GetNumPCMModules(void) {
return kNumPCMModules;
}
int32_t HAL_GetNumSolenoidChannels(void) {
return kNumSolenoidChannels;
}
int32_t HAL_GetNumPDPModules(void) {
return kNumPDPModules;
}
int32_t HAL_GetNumPDPChannels(void) {
return kNumPDPChannels;
}
int32_t HAL_GetNumDutyCycles(void) {
return kNumDutyCycles;
}
int32_t HAL_GetNumAddressableLEDs(void) {
return kNumAddressableLEDs;
}
} // extern "C"

View File

@@ -44,7 +44,9 @@ HAL_RelayHandle HAL_InitializeRelayPort(HAL_PortHandle portHandle, HAL_Bool fwd,
hal::init::CheckInit();
initializeDigital(status);
if (*status != 0) return HAL_kInvalidHandle;
if (*status != 0) {
return HAL_kInvalidHandle;
}
int16_t channel = getPortHandleChannel(portHandle);
if (channel == InvalidHandleIndex) {
@@ -52,12 +54,15 @@ HAL_RelayHandle HAL_InitializeRelayPort(HAL_PortHandle portHandle, HAL_Bool fwd,
return HAL_kInvalidHandle;
}
if (!fwd) channel += kNumRelayHeaders; // add 4 to reverse channels
if (!fwd) {
channel += kNumRelayHeaders; // add 4 to reverse channels
}
auto handle = relayHandles->Allocate(channel, status);
if (*status != 0)
if (*status != 0) {
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
}
auto port = relayHandles->Get(handle);
if (port == nullptr) { // would only occur on thread issue.
@@ -105,7 +110,9 @@ void HAL_SetRelay(HAL_RelayHandle relayPortHandle, HAL_Bool on,
relays = relaySystem->readValue_Reverse(status);
}
if (*status != 0) return; // bad status read
if (*status != 0) {
return; // bad status read
}
if (on) {
relays |= 1 << port->channel;

View File

@@ -51,7 +51,9 @@ static bool SPIInUseByAuto(HAL_SPIPort port) {
// SPI engine conflicts with any other chip selects on the same SPI device.
// There are two SPI devices: one for ports 0-3 (onboard), the other for port
// 4 (MXP).
if (!spiAutoRunning) return false;
if (!spiAutoRunning) {
return false;
}
std::scoped_lock lock(spiAutoMutex);
return (spiAutoPort >= 0 && spiAutoPort <= 3 && port >= 0 && port <= 3) ||
(spiAutoPort == 4 && port == 4);
@@ -70,7 +72,9 @@ static void CommonSPIPortInit(int32_t* status) {
if (spiPortCount.fetch_add(1) == 0) {
// Have not been initialized yet
initializeDigital(status);
if (*status != 0) return;
if (*status != 0) {
return;
}
// MISO
if ((digitalHandles[3] = HAL_InitializeDIOPort(createPortHandleForSPI(29),
false, status)) ==
@@ -105,11 +109,15 @@ void HAL_InitializeSPI(HAL_SPIPort port, int32_t* status) {
}
int handle;
if (HAL_GetSPIHandle(port) != 0) return;
if (HAL_GetSPIHandle(port) != 0) {
return;
}
switch (port) {
case HAL_SPI_kOnboardCS0:
CommonSPIPortInit(status);
if (*status != 0) return;
if (*status != 0) {
return;
}
// CS0 is not a DIO port, so nothing to allocate
handle = open("/dev/spidev0.0", O_RDWR);
if (handle < 0) {
@@ -122,7 +130,9 @@ void HAL_InitializeSPI(HAL_SPIPort port, int32_t* status) {
break;
case HAL_SPI_kOnboardCS1:
CommonSPIPortInit(status);
if (*status != 0) return;
if (*status != 0) {
return;
}
// CS1, Allocate
if ((digitalHandles[0] = HAL_InitializeDIOPort(createPortHandleForSPI(26),
false, status)) ==
@@ -143,7 +153,9 @@ void HAL_InitializeSPI(HAL_SPIPort port, int32_t* status) {
break;
case HAL_SPI_kOnboardCS2:
CommonSPIPortInit(status);
if (*status != 0) return;
if (*status != 0) {
return;
}
// CS2, Allocate
if ((digitalHandles[1] = HAL_InitializeDIOPort(createPortHandleForSPI(27),
false, status)) ==
@@ -164,7 +176,9 @@ void HAL_InitializeSPI(HAL_SPIPort port, int32_t* status) {
break;
case HAL_SPI_kOnboardCS3:
CommonSPIPortInit(status);
if (*status != 0) return;
if (*status != 0) {
return;
}
// CS3, Allocate
if ((digitalHandles[2] = HAL_InitializeDIOPort(createPortHandleForSPI(28),
false, status)) ==
@@ -185,7 +199,9 @@ void HAL_InitializeSPI(HAL_SPIPort port, int32_t* status) {
break;
case HAL_SPI_kMXP:
initializeDigital(status);
if (*status != 0) return;
if (*status != 0) {
return;
}
if ((digitalHandles[5] = HAL_InitializeDIOPort(createPortHandleForSPI(14),
false, status)) ==
HAL_kInvalidHandle) {
@@ -242,7 +258,9 @@ int32_t HAL_TransactionSPI(HAL_SPIPort port, const uint8_t* dataToSend,
return -1;
}
if (SPIInUseByAuto(port)) return -1;
if (SPIInUseByAuto(port)) {
return -1;
}
struct spi_ioc_transfer xfer;
std::memset(&xfer, 0, sizeof(xfer));
@@ -260,7 +278,9 @@ int32_t HAL_WriteSPI(HAL_SPIPort port, const uint8_t* dataToSend,
return -1;
}
if (SPIInUseByAuto(port)) return -1;
if (SPIInUseByAuto(port)) {
return -1;
}
struct spi_ioc_transfer xfer;
std::memset(&xfer, 0, sizeof(xfer));
@@ -276,7 +296,9 @@ int32_t HAL_ReadSPI(HAL_SPIPort port, uint8_t* buffer, int32_t count) {
return -1;
}
if (SPIInUseByAuto(port)) return -1;
if (SPIInUseByAuto(port)) {
return -1;
}
struct spi_ioc_transfer xfer;
std::memset(&xfer, 0, sizeof(xfer));
@@ -468,7 +490,9 @@ void HAL_FreeSPIAuto(HAL_SPIPort port, int32_t* status) {
}
std::scoped_lock lock(spiAutoMutex);
if (spiAutoPort != port) return;
if (spiAutoPort != port) {
return;
}
spiAutoPort = kSpiMaxHandles;
// disable by setting to internal clock and setting rate=0

View File

@@ -414,7 +414,9 @@ int32_t HAL_GetSerialBytesReceived(HAL_SerialPortHandle handle,
int32_t HAL_ReadSerial(HAL_SerialPortHandle handle, char* buffer, int32_t count,
int32_t* status) {
// Don't do anything if 0 bytes were requested
if (count == 0) return 0;
if (count == 0) {
return 0;
}
auto port = serialPortHandles->Get(handle);
if (!port) {

View File

@@ -6,7 +6,9 @@
extern "C" {
HAL_SimDeviceHandle HAL_CreateSimDevice(const char* name) { return 0; }
HAL_SimDeviceHandle HAL_CreateSimDevice(const char* name) {
return 0;
}
void HAL_FreeSimDevice(HAL_SimDeviceHandle handle) {}

View File

@@ -105,7 +105,9 @@ HAL_Bool HAL_GetSolenoid(HAL_SolenoidHandle solenoidPortHandle,
}
int32_t HAL_GetAllSolenoids(int32_t module, int32_t* status) {
if (!checkPCMInit(module, status)) return 0;
if (!checkPCMInit(module, status)) {
return 0;
}
uint8_t value;
*status = PCM_modules[module]->GetAllSolenoids(value);
@@ -125,13 +127,17 @@ void HAL_SetSolenoid(HAL_SolenoidHandle solenoidPortHandle, HAL_Bool value,
}
void HAL_SetAllSolenoids(int32_t module, int32_t state, int32_t* status) {
if (!checkPCMInit(module, status)) return;
if (!checkPCMInit(module, status)) {
return;
}
*status = PCM_modules[module]->SetAllSolenoids(state);
}
int32_t HAL_GetPCMSolenoidBlackList(int32_t module, int32_t* status) {
if (!checkPCMInit(module, status)) return 0;
if (!checkPCMInit(module, status)) {
return 0;
}
uint8_t value;
*status = PCM_modules[module]->GetSolenoidBlackList(value);
@@ -139,7 +145,9 @@ int32_t HAL_GetPCMSolenoidBlackList(int32_t module, int32_t* status) {
return value;
}
HAL_Bool HAL_GetPCMSolenoidVoltageStickyFault(int32_t module, int32_t* status) {
if (!checkPCMInit(module, status)) return 0;
if (!checkPCMInit(module, status)) {
return 0;
}
bool value;
*status = PCM_modules[module]->GetSolenoidStickyFault(value);
@@ -147,7 +155,9 @@ HAL_Bool HAL_GetPCMSolenoidVoltageStickyFault(int32_t module, int32_t* status) {
return value;
}
HAL_Bool HAL_GetPCMSolenoidVoltageFault(int32_t module, int32_t* status) {
if (!checkPCMInit(module, status)) return false;
if (!checkPCMInit(module, status)) {
return false;
}
bool value;
*status = PCM_modules[module]->GetSolenoidFault(value);
@@ -155,7 +165,9 @@ HAL_Bool HAL_GetPCMSolenoidVoltageFault(int32_t module, int32_t* status) {
return value;
}
void HAL_ClearAllPCMStickyFaults(int32_t module, int32_t* status) {
if (!checkPCMInit(module, status)) return;
if (!checkPCMInit(module, status)) {
return;
}
*status = PCM_modules[module]->ClearStickyFaults();
}

View File

@@ -66,11 +66,12 @@ HAL_Bool HAL_SetThreadPriority(NativeThreadHandle handle, HAL_Bool realTime,
int policy;
pthread_getschedparam(*reinterpret_cast<const pthread_t*>(handle), &policy,
&sch);
if (scheduler == SCHED_FIFO || scheduler == SCHED_RR)
if (scheduler == SCHED_FIFO || scheduler == SCHED_RR) {
sch.sched_priority = priority;
else
} else {
// Only need to set 0 priority for non RT thread
sch.sched_priority = 0;
}
if (pthread_setschedparam(*reinterpret_cast<const pthread_t*>(handle),
scheduler, &sch)) {
*status = HAL_THREAD_PRIORITY_ERROR;

View File

@@ -191,25 +191,30 @@ void SerialHelper::QueryHubPaths(int32_t* status) {
// Open the resource, grab its interface name, and close it.
ViSession vSession;
*status = viOpen(m_resourceHandle, desc, VI_NULL, VI_NULL, &vSession);
if (*status < 0) goto done;
if (*status < 0)
goto done;
*status = 0;
*status = viGetAttribute(vSession, VI_ATTR_INTF_INST_NAME, &osName);
// Ignore an error here, as we want to close the session on an error
// Use a separate close variable so we can check
ViStatus closeStatus = viClose(vSession);
if (*status < 0) goto done;
if (closeStatus < 0) goto done;
if (*status < 0)
goto done;
if (closeStatus < 0)
goto done;
*status = 0;
// split until (/dev/
wpi::StringRef devNameRef = wpi::StringRef{osName}.split("(/dev/").second;
// String not found, continue
if (devNameRef.equals("")) continue;
if (devNameRef.equals(""))
continue;
// Split at )
wpi::StringRef matchString = devNameRef.split(')').first;
if (matchString.equals(devNameRef)) continue;
if (matchString.equals(devNameRef))
continue;
// Search directories to get a list of system accessors
// The directories we need are not symbolic, so we can safely
@@ -218,11 +223,15 @@ void SerialHelper::QueryHubPaths(int32_t* status) {
for (auto p = wpi::sys::fs::recursive_directory_iterator(
"/sys/devices/soc0", ec, false);
p != wpi::sys::fs::recursive_directory_iterator(); p.increment(ec)) {
if (ec) break;
if (ec)
break;
wpi::StringRef path{p->path()};
if (path.find("amba") == wpi::StringRef::npos) continue;
if (path.find("usb") == wpi::StringRef::npos) continue;
if (path.find(matchString) == wpi::StringRef::npos) continue;
if (path.find("amba") == wpi::StringRef::npos)
continue;
if (path.find("usb") == wpi::StringRef::npos)
continue;
if (path.find(matchString) == wpi::StringRef::npos)
continue;
wpi::SmallVector<wpi::StringRef, 16> pathSplitVec;
// Split path into individual directories
@@ -246,11 +255,13 @@ void SerialHelper::QueryHubPaths(int32_t* status) {
// Get the index for our device
int hubIndex = findtty;
if (findtty == -1) hubIndex = findregex;
if (findtty == -1)
hubIndex = findregex;
int devStart = findusb + 1;
if (hubIndex < devStart) continue;
if (hubIndex < devStart)
continue;
// Add our devices to our list
m_unsortedHubPath.emplace_back(

View File

@@ -8,7 +8,9 @@
extern "C" {
int32_t HALSIM_FindAddressableLEDForChannel(int32_t channel) { return 0; }
int32_t HALSIM_FindAddressableLEDForChannel(int32_t channel) {
return 0;
}
void HALSIM_ResetAddressableLEDData(int32_t index) {}

View File

@@ -9,7 +9,9 @@
extern "C" {
void HALSIM_ResetAnalogInData(int32_t index) {}
HAL_SimDeviceHandle HALSIM_GetAnalogInSimDevice(int32_t index) { return 0; }
HAL_SimDeviceHandle HALSIM_GetAnalogInSimDevice(int32_t index) {
return 0;
}
#define DEFINE_CAPI(TYPE, CAPINAME, RETURN) \
HAL_SIMDATAVALUE_STUB_CAPI(TYPE, HALSIM, AnalogIn##CAPINAME, RETURN)

View File

@@ -8,7 +8,9 @@
extern "C" {
int32_t HALSIM_FindAnalogTriggerForChannel(int32_t channel) { return 0; }
int32_t HALSIM_FindAnalogTriggerForChannel(int32_t channel) {
return 0;
}
void HALSIM_ResetAnalogTriggerData(int32_t index) {}

View File

@@ -9,7 +9,9 @@
extern "C" {
void HALSIM_ResetDIOData(int32_t index) {}
HAL_SimDeviceHandle HALSIM_GetDIOSimDevice(int32_t index) { return 0; }
HAL_SimDeviceHandle HALSIM_GetDIOSimDevice(int32_t index) {
return 0;
}
#define DEFINE_CAPI(TYPE, CAPINAME, RETURN) \
HAL_SIMDATAVALUE_STUB_CAPI(TYPE, HALSIM, DIO##CAPINAME, RETURN)

View File

@@ -7,7 +7,9 @@
#include "hal/simulation/SimDataValue.h"
extern "C" {
int32_t HALSIM_FindDigitalPWMForChannel(int32_t channel) { return 0; }
int32_t HALSIM_FindDigitalPWMForChannel(int32_t channel) {
return 0;
}
void HALSIM_ResetDigitalPWMData(int32_t index) {}

View File

@@ -7,13 +7,19 @@
#include "hal/simulation/SimDataValue.h"
extern "C" {
int32_t HALSIM_FindDutyCycleForChannel(int32_t channel) { return 0; }
int32_t HALSIM_FindDutyCycleForChannel(int32_t channel) {
return 0;
}
void HALSIM_ResetDutyCycleData(int32_t index) {}
int32_t HALSIM_GetDutyCycleDigitalChannel(int32_t index) { return 0; }
int32_t HALSIM_GetDutyCycleDigitalChannel(int32_t index) {
return 0;
}
HAL_SimDeviceHandle HALSIM_GetDutyCycleSimDevice(int32_t index) { return 0; }
HAL_SimDeviceHandle HALSIM_GetDutyCycleSimDevice(int32_t index) {
return 0;
}
#define DEFINE_CAPI(TYPE, CAPINAME, RETURN) \
HAL_SIMDATAVALUE_STUB_CAPI(TYPE, HALSIM, DutyCycle##CAPINAME, RETURN)

View File

@@ -7,15 +7,23 @@
#include "hal/simulation/SimDataValue.h"
extern "C" {
int32_t HALSIM_FindEncoderForChannel(int32_t channel) { return 0; }
int32_t HALSIM_FindEncoderForChannel(int32_t channel) {
return 0;
}
void HALSIM_ResetEncoderData(int32_t index) {}
int32_t HALSIM_GetEncoderDigitalChannelA(int32_t index) { return 0; }
int32_t HALSIM_GetEncoderDigitalChannelA(int32_t index) {
return 0;
}
int32_t HALSIM_GetEncoderDigitalChannelB(int32_t index) { return 0; }
int32_t HALSIM_GetEncoderDigitalChannelB(int32_t index) {
return 0;
}
HAL_SimDeviceHandle HALSIM_GetEncoderSimDevice(int32_t index) { return 0; }
HAL_SimDeviceHandle HALSIM_GetEncoderSimDevice(int32_t index) {
return 0;
}
#define DEFINE_CAPI(TYPE, CAPINAME, RETURN) \
HAL_SIMDATAVALUE_STUB_CAPI(TYPE, HALSIM, Encoder##CAPINAME, RETURN)
@@ -32,11 +40,15 @@ DEFINE_CAPI(double, DistancePerPulse, 0)
void HALSIM_SetEncoderDistance(int32_t index, double distance) {}
double HALSIM_GetEncoderDistance(int32_t index) { return 0; }
double HALSIM_GetEncoderDistance(int32_t index) {
return 0;
}
void HALSIM_SetEncoderRate(int32_t index, double rate) {}
double HALSIM_GetEncoderRate(int32_t index) { return 0; }
double HALSIM_GetEncoderRate(int32_t index) {
return 0;
}
void HALSIM_RegisterEncoderAllCallbacks(int32_t index,
HAL_NotifyCallback callback,

View File

@@ -12,7 +12,9 @@ void HALSIM_WaitForProgramStart(void) {}
void HALSIM_SetProgramStarted(void) {}
HAL_Bool HALSIM_GetProgramStarted(void) { return false; }
HAL_Bool HALSIM_GetProgramStarted(void) {
return false;
}
void HALSIM_RestartTiming(void) {}
@@ -20,7 +22,9 @@ void HALSIM_PauseTiming(void) {}
void HALSIM_ResumeTiming(void) {}
HAL_Bool HALSIM_IsTimingPaused(void) { return false; }
HAL_Bool HALSIM_IsTimingPaused(void) {
return false;
}
void HALSIM_StepTiming(uint64_t delta) {}

View File

@@ -6,9 +6,13 @@
extern "C" {
uint64_t HALSIM_GetNextNotifierTimeout(void) { return 0; }
uint64_t HALSIM_GetNextNotifierTimeout(void) {
return 0;
}
int32_t HALSIM_GetNumNotifiers(void) { return 0; }
int32_t HALSIM_GetNumNotifiers(void) {
return 0;
}
int32_t HALSIM_GetNotifierInfo(struct HALSIM_NotifierInfo* arr, int32_t size) {
return 0;

View File

@@ -21,7 +21,9 @@ DEFINE_CAPI(HAL_Bool, ClosedLoopEnabled, false)
DEFINE_CAPI(HAL_Bool, PressureSwitch, false)
DEFINE_CAPI(double, CompressorCurrent, 0)
void HALSIM_GetPCMAllSolenoids(int32_t index, uint8_t* values) { *values = 0; }
void HALSIM_GetPCMAllSolenoids(int32_t index, uint8_t* values) {
*values = 0;
}
void HALSIM_SetPCMAllSolenoids(int32_t index, uint8_t values) {}

View File

@@ -19,7 +19,9 @@ DEFINE_CAPI(double, Voltage, 0)
HAL_SIMDATAVALUE_STUB_CAPI_CHANNEL(double, HALSIM, PDPCurrent, 0)
void HALSIM_GetPDPAllCurrents(int32_t index, double* currents) {
for (int i = 0; i < hal::kNumPDPChannels; i++) currents[i] = 0;
for (int i = 0; i < hal::kNumPDPChannels; i++) {
currents[i] = 0;
}
}
void HALSIM_SetPDPAllCurrents(int32_t index, const double* currents) {}

View File

@@ -10,7 +10,9 @@ extern "C" {
void HALSIM_SetSimDeviceEnabled(const char* prefix, HAL_Bool enabled) {}
HAL_Bool HALSIM_IsSimDeviceEnabled(const char* name) { return false; }
HAL_Bool HALSIM_IsSimDeviceEnabled(const char* name) {
return false;
}
int32_t HALSIM_RegisterSimDeviceCreatedCallback(
const char* prefix, void* param, HALSIM_SimDeviceCallback callback,
@@ -28,9 +30,13 @@ int32_t HALSIM_RegisterSimDeviceFreedCallback(const char* prefix, void* param,
void HALSIM_CancelSimDeviceFreedCallback(int32_t uid) {}
HAL_SimDeviceHandle HALSIM_GetSimDeviceHandle(const char* name) { return 0; }
HAL_SimDeviceHandle HALSIM_GetSimDeviceHandle(const char* name) {
return 0;
}
const char* HALSIM_GetSimDeviceName(HAL_SimDeviceHandle handle) { return ""; }
const char* HALSIM_GetSimDeviceName(HAL_SimDeviceHandle handle) {
return "";
}
HAL_SimDeviceHandle HALSIM_GetSimValueDeviceHandle(HAL_SimValueHandle handle) {
return 0;