Add kInvalid value to HAL_I2CPort and HAL_SPIPort (#1329)

This allows HAL_CloseI2C() and HAL_CloseSPI() to be noops, which makes
enabling move semantics in the I2C and SPI wpilibc classes easier and
cleaner.

Fixes #1328.
This commit is contained in:
Tyler Veness
2018-09-23 21:14:04 -07:00
committed by Peter Johnson
parent b505bbefd1
commit 467c9fd686
6 changed files with 23 additions and 23 deletions

View File

@@ -46,12 +46,12 @@ void HAL_InitializeI2C(HAL_I2CPort port, int32_t* status) {
initializeDigital(status);
if (*status != 0) return;
if (port > 1) {
if (port < 0 || port > 1) {
// Set port out of range error here
return;
}
if (port == 0) {
if (port == HAL_I2C_kOnboard) {
std::lock_guard<wpi::mutex> lock(digitalI2COnBoardMutex);
i2COnboardObjCount++;
if (i2COnboardObjCount > 1) return;
@@ -88,7 +88,7 @@ void HAL_InitializeI2C(HAL_I2CPort port, int32_t* status) {
int32_t HAL_TransactionI2C(HAL_I2CPort port, int32_t deviceAddress,
const uint8_t* dataToSend, int32_t sendSize,
uint8_t* dataReceived, int32_t receiveSize) {
if (port > 1) {
if (port < 0 || port > 1) {
// Set port out of range error here
return -1;
}
@@ -107,7 +107,7 @@ int32_t HAL_TransactionI2C(HAL_I2CPort port, int32_t deviceAddress,
rdwr.msgs = msgs;
rdwr.nmsgs = 2;
if (port == 0) {
if (port == HAL_I2C_kOnboard) {
std::lock_guard<wpi::mutex> lock(digitalI2COnBoardMutex);
return ioctl(i2COnBoardHandle, I2C_RDWR, &rdwr);
} else {
@@ -118,7 +118,7 @@ int32_t HAL_TransactionI2C(HAL_I2CPort port, int32_t deviceAddress,
int32_t HAL_WriteI2C(HAL_I2CPort port, int32_t deviceAddress,
const uint8_t* dataToSend, int32_t sendSize) {
if (port > 1) {
if (port < 0 || port > 1) {
// Set port out of range error here
return -1;
}
@@ -133,7 +133,7 @@ int32_t HAL_WriteI2C(HAL_I2CPort port, int32_t deviceAddress,
rdwr.msgs = &msg;
rdwr.nmsgs = 1;
if (port == 0) {
if (port == HAL_I2C_kOnboard) {
std::lock_guard<wpi::mutex> lock(digitalI2COnBoardMutex);
return ioctl(i2COnBoardHandle, I2C_RDWR, &rdwr);
} else {
@@ -144,7 +144,7 @@ int32_t HAL_WriteI2C(HAL_I2CPort port, int32_t deviceAddress,
int32_t HAL_ReadI2C(HAL_I2CPort port, int32_t deviceAddress, uint8_t* buffer,
int32_t count) {
if (port > 1) {
if (port < 0 || port > 1) {
// Set port out of range error here
return -1;
}
@@ -159,7 +159,7 @@ int32_t HAL_ReadI2C(HAL_I2CPort port, int32_t deviceAddress, uint8_t* buffer,
rdwr.msgs = &msg;
rdwr.nmsgs = 1;
if (port == 0) {
if (port == HAL_I2C_kOnboard) {
std::lock_guard<wpi::mutex> lock(digitalI2COnBoardMutex);
return ioctl(i2COnBoardHandle, I2C_RDWR, &rdwr);
} else {
@@ -169,12 +169,12 @@ int32_t HAL_ReadI2C(HAL_I2CPort port, int32_t deviceAddress, uint8_t* buffer,
}
void HAL_CloseI2C(HAL_I2CPort port) {
if (port > 1) {
if (port < 0 || port > 1) {
// Set port out of range error here
return;
}
if (port == 0) {
if (port == HAL_I2C_kOnboard) {
std::lock_guard<wpi::mutex> lock(digitalI2COnBoardMutex);
if (i2COnboardObjCount-- == 0) {
close(i2COnBoardHandle);

View File

@@ -110,7 +110,7 @@ void HAL_InitializeSPI(HAL_SPIPort port, int32_t* status) {
int handle;
if (HAL_GetSPIHandle(port) != 0) return;
switch (port) {
case 0:
case HAL_SPI_kOnboardCS0:
CommonSPIPortInit(status);
if (*status != 0) return;
// CS0 is not a DIO port, so nothing to allocate
@@ -123,7 +123,7 @@ void HAL_InitializeSPI(HAL_SPIPort port, int32_t* status) {
}
HAL_SetSPIHandle(HAL_SPI_kOnboardCS0, handle);
break;
case 1:
case HAL_SPI_kOnboardCS1:
CommonSPIPortInit(status);
if (*status != 0) return;
// CS1, Allocate
@@ -144,7 +144,7 @@ void HAL_InitializeSPI(HAL_SPIPort port, int32_t* status) {
}
HAL_SetSPIHandle(HAL_SPI_kOnboardCS1, handle);
break;
case 2:
case HAL_SPI_kOnboardCS2:
CommonSPIPortInit(status);
if (*status != 0) return;
// CS2, Allocate
@@ -165,7 +165,7 @@ void HAL_InitializeSPI(HAL_SPIPort port, int32_t* status) {
}
HAL_SetSPIHandle(HAL_SPI_kOnboardCS2, handle);
break;
case 3:
case HAL_SPI_kOnboardCS3:
CommonSPIPortInit(status);
if (*status != 0) return;
// CS3, Allocate
@@ -186,7 +186,7 @@ void HAL_InitializeSPI(HAL_SPIPort port, int32_t* status) {
}
HAL_SetSPIHandle(HAL_SPI_kOnboardCS3, handle);
break;
case 4:
case HAL_SPI_kMXP:
initializeDigital(status);
if (*status != 0) return;
if ((digitalHandles[5] = HAL_InitializeDIOPort(createPortHandleForSPI(14),

View File

@@ -18,7 +18,7 @@
*/
// clang-format off
HAL_ENUM(HAL_I2CPort) { HAL_I2C_kOnboard = 0, HAL_I2C_kMXP };
HAL_ENUM(HAL_I2CPort) { HAL_I2C_kInvalid = -1, HAL_I2C_kOnboard, HAL_I2C_kMXP };
// clang-format on
#ifdef __cplusplus

View File

@@ -20,7 +20,8 @@
// clang-format off
HAL_ENUM(HAL_SPIPort) {
HAL_SPI_kOnboardCS0 = 0,
HAL_SPI_kInvalid = -1,
HAL_SPI_kOnboardCS0,
HAL_SPI_kOnboardCS1,
HAL_SPI_kOnboardCS2,
HAL_SPI_kOnboardCS3,