From 43ec7cf8ad6e2a70011a2c38197ffbe97c9d75ee Mon Sep 17 00:00:00 2001 From: Ryan Blue Date: Wed, 16 Oct 2024 09:39:00 -0400 Subject: [PATCH] SPI & I2C: Close port in destructor This was overlooked by mistake in #7016 --- hal/src/main/native/include/hal/I2CTypes.h | 12 ------------ hal/src/main/native/include/hal/SPITypes.h | 12 ------------ wpilibc/src/main/native/cpp/SPI.cpp | 6 +++++- wpilibc/src/main/native/include/frc/I2C.h | 3 ++- wpilibc/src/main/native/include/frc/SPI.h | 3 ++- 5 files changed, 9 insertions(+), 27 deletions(-) diff --git a/hal/src/main/native/include/hal/I2CTypes.h b/hal/src/main/native/include/hal/I2CTypes.h index e1339f0140a..075820f31fc 100644 --- a/hal/src/main/native/include/hal/I2CTypes.h +++ b/hal/src/main/native/include/hal/I2CTypes.h @@ -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; - -} // namespace hal -#endif /** @} */ diff --git a/hal/src/main/native/include/hal/SPITypes.h b/hal/src/main/native/include/hal/SPITypes.h index b27c38828e9..8427bf339f1 100644 --- a/hal/src/main/native/include/hal/SPITypes.h +++ b/hal/src/main/native/include/hal/SPITypes.h @@ -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; - -} // namespace hal -#endif /** @} */ diff --git a/wpilibc/src/main/native/cpp/SPI.cpp b/wpilibc/src/main/native/cpp/SPI.cpp index 88f8f1a9fde..035a7df71fb 100644 --- a/wpilibc/src/main/native/cpp/SPI.cpp +++ b/wpilibc/src/main/native/cpp/SPI.cpp @@ -165,7 +165,11 @@ SPI::SPI(Port port) : m_port(static_cast(port)) { static_cast(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(static_cast(m_port)); diff --git a/wpilibc/src/main/native/include/frc/I2C.h b/wpilibc/src/main/native/include/frc/I2C.h index f51b1f25f04..a8424579886 100644 --- a/wpilibc/src/main/native/include/frc/I2C.h +++ b/wpilibc/src/main/native/include/frc/I2C.h @@ -6,6 +6,7 @@ #include +#include #include namespace frc { @@ -156,7 +157,7 @@ class I2C { bool VerifySensor(int registerAddress, int count, const uint8_t* expected); private: - hal::I2CPort m_port; + hal::Handle m_port; int m_deviceAddress; }; diff --git a/wpilibc/src/main/native/include/frc/SPI.h b/wpilibc/src/main/native/include/frc/SPI.h index 6004b261398..8f7f87349b4 100644 --- a/wpilibc/src/main/native/include/frc/SPI.h +++ b/wpilibc/src/main/native/include/frc/SPI.h @@ -9,6 +9,7 @@ #include #include +#include #include #include @@ -356,7 +357,7 @@ class SPI { double GetAccumulatorIntegratedAverage() const; protected: - hal::SPIPort m_port; + hal::Handle m_port; HAL_SPIMode m_mode = HAL_SPIMode::HAL_SPI_kMode0; private: