Skip to content

Commit

Permalink
SPI & I2C: Close port in destructor
Browse files Browse the repository at this point in the history
This was overlooked by mistake in wpilibsuite#7016
  • Loading branch information
rzblue committed Oct 16, 2024
1 parent fad06ae commit 43ec7cf
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 27 deletions.
12 changes: 0 additions & 12 deletions hal/src/main/native/include/hal/I2CTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,4 @@ HAL_ENUM(HAL_I2CPort) {
HAL_I2C_kOnboard,
HAL_I2C_kMXP
};

#ifdef __cplusplus
namespace hal {

/**
* A move-only C++ wrapper around HAL_I2CPort.
* Does not ensure destruction.
*/
using I2CPort = Handle<HAL_I2CPort, nullptr, HAL_I2C_kInvalid>;

} // namespace hal
#endif
/** @} */
12 changes: 0 additions & 12 deletions hal/src/main/native/include/hal/SPITypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,4 @@ HAL_ENUM(HAL_SPIMode) {
/** Clock idle high, data sampled on rising edge. */
HAL_SPI_kMode3 = 3,
};

#ifdef __cplusplus
namespace hal {

/**
* A move-only C++ wrapper around HAL_SPIPort.
* Does not ensure destruction.
*/
using SPIPort = Handle<HAL_SPIPort, nullptr, HAL_SPI_kInvalid>;

} // namespace hal
#endif
/** @} */
6 changes: 5 additions & 1 deletion wpilibc/src/main/native/cpp/SPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,11 @@ SPI::SPI(Port port) : m_port(static_cast<HAL_SPIPort>(port)) {
static_cast<uint8_t>(port) + 1);
}

SPI::~SPI() = default;
SPI::~SPI() {
if (m_port != HAL_SPI_kInvalid) {
HAL_CloseSPI(m_port);
}
};

SPI::Port SPI::GetPort() const {
return static_cast<Port>(static_cast<int>(m_port));
Expand Down
3 changes: 2 additions & 1 deletion wpilibc/src/main/native/include/frc/I2C.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <stdint.h>

#include <hal/I2C.h>
#include <hal/I2CTypes.h>

namespace frc {
Expand Down Expand Up @@ -156,7 +157,7 @@ class I2C {
bool VerifySensor(int registerAddress, int count, const uint8_t* expected);

private:
hal::I2CPort m_port;
hal::Handle<HAL_I2CPort, HAL_CloseI2C, HAL_I2C_kInvalid> m_port;
int m_deviceAddress;
};

Expand Down
3 changes: 2 additions & 1 deletion wpilibc/src/main/native/include/frc/SPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <memory>
#include <span>

#include <hal/SPI.h>
#include <hal/SPITypes.h>
#include <units/time.h>

Expand Down Expand Up @@ -356,7 +357,7 @@ class SPI {
double GetAccumulatorIntegratedAverage() const;

protected:
hal::SPIPort m_port;
hal::Handle<HAL_SPIPort, HAL_CloseSPI, HAL_SPI_kInvalid> m_port;
HAL_SPIMode m_mode = HAL_SPIMode::HAL_SPI_kMode0;

private:
Expand Down

0 comments on commit 43ec7cf

Please sign in to comment.