From 12f759860eea3127d3ff1ccabbf801a31ffef863 Mon Sep 17 00:00:00 2001 From: Tyler Veness Date: Fri, 17 Feb 2017 00:05:54 -0800 Subject: [PATCH] Corrects assumptions about return values from i2c-lib (#484) Fixes #478 --- hal/lib/athena/I2C.cpp | 6 +++--- wpilibc/athena/src/I2C.cpp | 10 ++++------ .../athena/java/edu/wpi/first/wpilibj/I2C.java | 17 ++++++++++------- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/hal/lib/athena/I2C.cpp b/hal/lib/athena/I2C.cpp index 8065d37700..7d22cd48c3 100644 --- a/hal/lib/athena/I2C.cpp +++ b/hal/lib/athena/I2C.cpp @@ -78,7 +78,7 @@ void HAL_InitializeI2C(int32_t port, int32_t* status) { * @param sendSize Number of bytes to send as part of the transaction. * @param dataReceived Buffer to read data into. * @param receiveSize Number of bytes to read from the device. - * @return The number of bytes read (>= 0) or -1 on transfer abort. + * @return >= 0 on success or -1 on transfer abort. */ int32_t HAL_TransactionI2C(int32_t port, int32_t deviceAddress, uint8_t* dataToSend, int32_t sendSize, @@ -110,7 +110,7 @@ int32_t HAL_TransactionI2C(int32_t port, int32_t deviceAddress, * @param registerAddress The address of the register on the device to be * written. * @param data The byte to write to the register on the device. - * @return The number of bytes written (>= 0) or -1 on transfer abort. + * @return >= 0 on success or -1 on transfer abort. */ int32_t HAL_WriteI2C(int32_t port, int32_t deviceAddress, uint8_t* dataToSend, int32_t sendSize) { @@ -140,7 +140,7 @@ int32_t HAL_WriteI2C(int32_t port, int32_t deviceAddress, uint8_t* dataToSend, * @param count The number of bytes to read in the transaction. * @param buffer A pointer to the array of bytes to store the data read from the * device. - * @return The number of bytes read (>= 0) or -1 on transfer abort. + * @return >= 0 on success or -1 on transfer abort. */ int32_t HAL_ReadI2C(int32_t port, int32_t deviceAddress, uint8_t* buffer, int32_t count) { diff --git a/wpilibc/athena/src/I2C.cpp b/wpilibc/athena/src/I2C.cpp index 5ea58b45ee..6c35e3b071 100644 --- a/wpilibc/athena/src/I2C.cpp +++ b/wpilibc/athena/src/I2C.cpp @@ -51,7 +51,7 @@ bool I2C::Transaction(uint8_t* dataToSend, int sendSize, uint8_t* dataReceived, status = HAL_TransactionI2C(m_port, m_deviceAddress, dataToSend, sendSize, dataReceived, receiveSize); // wpi_setErrorWithContext(status, HAL_GetErrorMessage(status)); - return status < receiveSize; + return status < 0; } /** @@ -81,7 +81,7 @@ bool I2C::Write(int registerAddress, uint8_t data) { buffer[1] = data; int32_t status = 0; status = HAL_WriteI2C(m_port, m_deviceAddress, buffer, sizeof(buffer)); - return status < static_cast(sizeof(buffer)); + return status < 0; } /** @@ -97,7 +97,7 @@ bool I2C::Write(int registerAddress, uint8_t data) { bool I2C::WriteBulk(uint8_t* data, int count) { int32_t status = 0; status = HAL_WriteI2C(m_port, m_deviceAddress, data, count); - return status < count; + return status < 0; } /** @@ -146,9 +146,7 @@ bool I2C::ReadOnly(int count, uint8_t* buffer) { wpi_setWPIErrorWithContext(NullParameter, "buffer"); return true; } - int32_t status = 0; - status = HAL_ReadI2C(m_port, m_deviceAddress, buffer, count); - return status < count; + return HAL_ReadI2C(m_port, m_deviceAddress, buffer, count) < 0; } /** diff --git a/wpilibj/src/athena/java/edu/wpi/first/wpilibj/I2C.java b/wpilibj/src/athena/java/edu/wpi/first/wpilibj/I2C.java index 5763bdcc6d..e3c264ebf4 100644 --- a/wpilibj/src/athena/java/edu/wpi/first/wpilibj/I2C.java +++ b/wpilibj/src/athena/java/edu/wpi/first/wpilibj/I2C.java @@ -83,7 +83,7 @@ public class I2C extends SensorBase { if (receiveSize > 0 && dataReceived != null) { dataReceivedBuffer.get(dataReceived); } - return status < receiveSize; + return status < 0; } /** @@ -117,7 +117,7 @@ public class I2C extends SensorBase { } return I2CJNI.i2CTransaction((byte) m_port.value, (byte) m_deviceAddress, dataToSend, - (byte) sendSize, dataReceived, (byte) receiveSize) < receiveSize; + (byte) sendSize, dataReceived, (byte) receiveSize) < 0; } /** @@ -139,6 +139,7 @@ public class I2C extends SensorBase { * * @param registerAddress The address of the register on the device to be written. * @param data The byte to write to the register on the device. + * @return Transfer Aborted... false for success, true for aborted. */ public synchronized boolean write(int registerAddress, int data) { byte[] buffer = new byte[2]; @@ -149,7 +150,7 @@ public class I2C extends SensorBase { dataToSendBuffer.put(buffer); return I2CJNI.i2CWrite((byte) m_port.value, (byte) m_deviceAddress, dataToSendBuffer, - (byte) buffer.length) < buffer.length; + (byte) buffer.length) < 0; } /** @@ -158,13 +159,14 @@ public class I2C extends SensorBase { *

Write multiple bytes to a register on a device and wait until the transaction is complete. * * @param data The data to write to the device. + * @return Transfer Aborted... false for success, true for aborted. */ public synchronized boolean writeBulk(byte[] data) { ByteBuffer dataToSendBuffer = ByteBuffer.allocateDirect(data.length); dataToSendBuffer.put(data); return I2CJNI.i2CWrite((byte) m_port.value, (byte) m_deviceAddress, dataToSendBuffer, - (byte) data.length) < data.length; + (byte) data.length) < 0; } /** @@ -173,6 +175,7 @@ public class I2C extends SensorBase { *

Write multiple bytes to a register on a device and wait until the transaction is complete. * * @param data The data to write to the device. Must be created using ByteBuffer.allocateDirect(). + * @return Transfer Aborted... false for success, true for aborted. */ public synchronized boolean writeBulk(ByteBuffer data, int size) { if (!data.isDirect()) { @@ -183,7 +186,7 @@ public class I2C extends SensorBase { "buffer is too small, must be at least " + size); } - return I2CJNI.i2CWrite((byte) m_port.value, (byte) m_deviceAddress, data, (byte) size) < size; + return I2CJNI.i2CWrite((byte) m_port.value, (byte) m_deviceAddress, data, (byte) size) < 0; } /** @@ -264,7 +267,7 @@ public class I2C extends SensorBase { int retVal = I2CJNI.i2CRead((byte) m_port.value, (byte) m_deviceAddress, dataReceivedBuffer, (byte) count); dataReceivedBuffer.get(buffer); - return retVal < count; + return retVal < 0; } /** @@ -291,7 +294,7 @@ public class I2C extends SensorBase { } return I2CJNI.i2CRead((byte) m_port.value, (byte) m_deviceAddress, buffer, (byte) count) - < count; + < 0; } /*