[hal,wpilib] Rename I2C constants to all caps

This commit is contained in:
Peter Johnson
2026-03-14 11:51:53 -07:00
parent b68fbb1adc
commit 261a0ebbd7
14 changed files with 38 additions and 32 deletions

View File

@@ -15,8 +15,8 @@
*/
HAL_ENUM(HAL_I2CPort) {
HAL_I2C_kInvalid = -1,
HAL_I2C_kPort0,
HAL_I2C_kPort1
HAL_I2C_PORT_INVALID = -1,
HAL_I2C_PORT_0,
HAL_I2C_PORT_1
};
/** @} */

View File

@@ -32,7 +32,7 @@ TEST(I2CSimTest, I2CInitialization) {
INDEX_TO_TEST, &TestI2CInitializationCallback, &callbackParam, false);
ASSERT_TRUE(0 != callbackId);
port = HAL_I2C_kPort1;
port = HAL_I2C_PORT_1;
gTestI2CCallbackName = "Unset";
HAL_InitializeI2C(port, &status);
EXPECT_STREQ("Initialized", gTestI2CCallbackName.c_str());

View File

@@ -14,7 +14,7 @@ class MyRobot(wpilib.TimedRobot):
code using the roboRIO's I2C port.
"""
PORT = wpilib.I2C.Port.kPort0
PORT = wpilib.I2C.Port.PORT_0
DEVICE_ADDRESS = 4
def __init__(self):

View File

@@ -13,7 +13,7 @@ using namespace wpi;
ADXL345_I2C::ADXL345_I2C(I2C::Port port, Range range, int deviceAddress)
: m_i2c(port, deviceAddress),
m_simDevice("Accel:ADXL345_I2C", port, deviceAddress) {
m_simDevice("Accel:ADXL345_I2C", static_cast<int>(port), deviceAddress) {
if (m_simDevice) {
m_simRange = m_simDevice.CreateEnumDouble(
"range", wpi::hal::SimDevice::kOutput, {"2G", "4G", "8G", "16G"},
@@ -31,7 +31,7 @@ ADXL345_I2C::ADXL345_I2C(I2C::Port port, Range range, int deviceAddress)
fmt::format("I2C[{}][{}]", static_cast<int>(port), deviceAddress),
"ADXL345");
wpi::util::SendableRegistry::Add(this, "ADXL345_I2C", port);
wpi::util::SendableRegistry::Add(this, "ADXL345_I2C", static_cast<int>(port));
}
I2C::Port ADXL345_I2C::GetI2CPort() const {

View File

@@ -10,7 +10,8 @@
using namespace wpi::sim;
ADXL345Sim::ADXL345Sim(const wpi::ADXL345_I2C& accel) {
wpi::sim::SimDeviceSim deviceSim{"Accel:ADXL345_I2C", accel.GetI2CPort(),
wpi::sim::SimDeviceSim deviceSim{"Accel:ADXL345_I2C",
static_cast<int>(accel.GetI2CPort()),
accel.GetI2CDeviceAddress()};
m_simX = deviceSim.GetDouble("x");
m_simY = deviceSim.GetDouble("y");

View File

@@ -23,11 +23,11 @@ class I2C {
/**
* I2C connection ports.
*/
enum Port {
enum class Port {
/// I2C Port 0.
kPort0 = 0,
PORT_0 = 0,
/// I2C Port 1.
kPort1
PORT_1
};
/**
@@ -154,7 +154,7 @@ class I2C {
bool VerifySensor(int registerAddress, int count, const uint8_t* expected);
private:
wpi::hal::Handle<HAL_I2CPort, HAL_CloseI2C, HAL_I2C_kInvalid> m_port;
wpi::hal::Handle<HAL_I2CPort, HAL_CloseI2C, HAL_I2C_PORT_INVALID> m_port;
int m_deviceAddress;
};

View File

@@ -27,7 +27,7 @@ class Robot : public wpi::TimedRobot {
private:
// Creates an ADXL345 accelerometer object on the MXP I2C port
// with a measurement range from -8 to 8 G's
wpi::ADXL345_I2C m_accelerometer{wpi::I2C::Port::kPort0,
wpi::ADXL345_I2C m_accelerometer{wpi::I2C::Port::PORT_0,
wpi::ADXL345_I2C::Range::kRange_8G};
};

View File

@@ -17,7 +17,7 @@ class Robot : public wpi::TimedRobot {
public:
void RobotPeriodic() override;
static constexpr wpi::I2C::Port kPort = wpi::I2C::Port::kPort0;
static constexpr wpi::I2C::Port kPort = wpi::I2C::Port::PORT_0;
private:
static constexpr int deviceAddress = 4;

View File

@@ -142,7 +142,7 @@ public class ADXL345_I2C implements NTSendable, AutoCloseable {
*
* @return The I2C port.
*/
public int getPort() {
public I2C.Port getPort() {
return m_i2c.getPort();
}

View File

@@ -21,9 +21,9 @@ public class I2C implements AutoCloseable {
/** I2C connection ports. */
public enum Port {
/** I2C Port 0. */
kPort0(0),
PORT_0(0),
/** I2C Port 1. */
kPort1(1);
PORT_1(1);
/** Port value. */
public final int value;
@@ -33,7 +33,7 @@ public class I2C implements AutoCloseable {
}
}
private final int m_port;
private final Port m_port;
private final int m_deviceAddress;
private ByteBuffer m_readDataToSendBuffer;
@@ -44,7 +44,7 @@ public class I2C implements AutoCloseable {
* @param deviceAddress The address of the device on the I2C bus.
*/
public I2C(Port port, int deviceAddress) {
m_port = port.value;
m_port = port;
m_deviceAddress = deviceAddress;
I2CJNI.i2CInitialize((byte) port.value);
@@ -57,7 +57,7 @@ public class I2C implements AutoCloseable {
*
* @return I2C port.
*/
public int getPort() {
public Port getPort() {
return m_port;
}
@@ -72,7 +72,7 @@ public class I2C implements AutoCloseable {
@Override
public void close() {
I2CJNI.i2CClose(m_port);
I2CJNI.i2CClose(m_port.value);
}
/**
@@ -99,7 +99,7 @@ public class I2C implements AutoCloseable {
"dataReceived is too small, must be at least " + receiveSize);
}
return I2CJNI.i2CTransactionB(
m_port,
m_port.value,
(byte) m_deviceAddress,
dataToSend,
(byte) sendSize,
@@ -140,7 +140,7 @@ public class I2C implements AutoCloseable {
}
return I2CJNI.i2CTransaction(
m_port,
m_port.value,
(byte) m_deviceAddress,
dataToSend,
(byte) sendSize,
@@ -174,7 +174,7 @@ public class I2C implements AutoCloseable {
byte[] buffer = new byte[2];
buffer[0] = (byte) registerAddress;
buffer[1] = (byte) data;
return I2CJNI.i2CWriteB(m_port, (byte) m_deviceAddress, buffer, (byte) buffer.length) < 0;
return I2CJNI.i2CWriteB(m_port.value, (byte) m_deviceAddress, buffer, (byte) buffer.length) < 0;
}
/**
@@ -202,7 +202,7 @@ public class I2C implements AutoCloseable {
if (data.length < size) {
throw new IllegalArgumentException("buffer is too small, must be at least " + size);
}
return I2CJNI.i2CWriteB(m_port, (byte) m_deviceAddress, data, (byte) size) < 0;
return I2CJNI.i2CWriteB(m_port.value, (byte) m_deviceAddress, data, (byte) size) < 0;
}
/**
@@ -225,7 +225,7 @@ public class I2C implements AutoCloseable {
throw new IllegalArgumentException("buffer is too small, must be at least " + size);
}
return I2CJNI.i2CWrite(m_port, (byte) m_deviceAddress, data, (byte) size) < 0;
return I2CJNI.i2CWrite(m_port.value, (byte) m_deviceAddress, data, (byte) size) < 0;
}
/**
@@ -310,7 +310,7 @@ public class I2C implements AutoCloseable {
throw new IllegalArgumentException("buffer is too small, must be at least " + count);
}
return I2CJNI.i2CReadB(m_port, (byte) m_deviceAddress, buffer, (byte) count) < 0;
return I2CJNI.i2CReadB(m_port.value, (byte) m_deviceAddress, buffer, (byte) count) < 0;
}
/**
@@ -338,7 +338,7 @@ public class I2C implements AutoCloseable {
throw new IllegalArgumentException("buffer is too small, must be at least " + count);
}
return I2CJNI.i2CRead(m_port, (byte) m_deviceAddress, buffer, (byte) count) < 0;
return I2CJNI.i2CRead(m_port.value, (byte) m_deviceAddress, buffer, (byte) count) < 0;
}
/*

View File

@@ -22,7 +22,12 @@ public class ADXL345Sim {
public ADXL345Sim(ADXL345_I2C device) {
SimDeviceSim simDevice =
new SimDeviceSim(
"Accel:ADXL345_I2C" + "[" + device.getPort() + "," + device.getDeviceAddress() + "]");
"Accel:ADXL345_I2C"
+ "["
+ device.getPort().value
+ ","
+ device.getDeviceAddress()
+ "]");
initSim(simDevice);
}

View File

@@ -18,7 +18,7 @@ class ADXL345SimTest {
void testInitI2C(ADXL345_I2C.Range range) {
HAL.initialize(500, 0);
try (ADXL345_I2C accel = new ADXL345_I2C(I2C.Port.kPort0, range)) {
try (ADXL345_I2C accel = new ADXL345_I2C(I2C.Port.PORT_0, range)) {
ADXL345Sim sim = new ADXL345Sim(accel);
sim.setX(1.91);

View File

@@ -15,7 +15,7 @@ import org.wpilib.hardware.bus.I2C;
public class Robot extends TimedRobot {
// Creates an ADXL345 accelerometer object on the MXP I2C port
// with a measurement range from -8 to 8 G's
ADXL345_I2C m_accelerometer345I2C = new ADXL345_I2C(I2C.Port.kPort0, ADXL345_I2C.Range.k8G);
ADXL345_I2C m_accelerometer345I2C = new ADXL345_I2C(I2C.Port.PORT_0, ADXL345_I2C.Range.k8G);
/** Called once at the beginning of the robot program. */
public Robot() {}

View File

@@ -15,7 +15,7 @@ import org.wpilib.hardware.bus.I2C.Port;
* code using the roboRIO's I2C port.
*/
public class Robot extends TimedRobot {
static final Port kPort = Port.kPort0;
static final Port kPort = Port.PORT_0;
private static final int kDeviceAddress = 4;
private final I2C m_arduino = new I2C(kPort, kDeviceAddress);