From 261a0ebbd7f2f03787deed36367d3ab9e76d96f9 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Sat, 14 Mar 2026 11:51:53 -0700 Subject: [PATCH] [hal,wpilib] Rename I2C constants to all caps --- .../main/native/include/wpi/hal/I2CTypes.h | 6 ++--- .../test/native/cpp/mockdata/I2CDataTest.cpp | 2 +- robotpyExamples/I2CCommunication/robot.py | 2 +- .../hardware/accelerometer/ADXL345_I2C.cpp | 4 +-- .../main/native/cpp/simulation/ADXL345Sim.cpp | 3 ++- .../native/include/wpi/hardware/bus/I2C.hpp | 8 +++--- .../snippets/ADXLAccelerometers/cpp/Robot.cpp | 2 +- .../I2CCommunication/include/Robot.hpp | 2 +- .../hardware/accelerometer/ADXL345_I2C.java | 2 +- .../java/org/wpilib/hardware/bus/I2C.java | 26 +++++++++---------- .../org/wpilib/simulation/ADXL345Sim.java | 7 ++++- .../org/wpilib/simulation/ADXL345SimTest.java | 2 +- .../snippets/adxlaccelerometers/Robot.java | 2 +- .../snippets/i2ccommunication/Robot.java | 2 +- 14 files changed, 38 insertions(+), 32 deletions(-) diff --git a/hal/src/main/native/include/wpi/hal/I2CTypes.h b/hal/src/main/native/include/wpi/hal/I2CTypes.h index 62d865e949..49073c5451 100644 --- a/hal/src/main/native/include/wpi/hal/I2CTypes.h +++ b/hal/src/main/native/include/wpi/hal/I2CTypes.h @@ -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 }; /** @} */ diff --git a/hal/src/test/native/cpp/mockdata/I2CDataTest.cpp b/hal/src/test/native/cpp/mockdata/I2CDataTest.cpp index af45acb02b..be5390ddd0 100644 --- a/hal/src/test/native/cpp/mockdata/I2CDataTest.cpp +++ b/hal/src/test/native/cpp/mockdata/I2CDataTest.cpp @@ -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()); diff --git a/robotpyExamples/I2CCommunication/robot.py b/robotpyExamples/I2CCommunication/robot.py index ceaef0034d..11406dbd28 100755 --- a/robotpyExamples/I2CCommunication/robot.py +++ b/robotpyExamples/I2CCommunication/robot.py @@ -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): diff --git a/wpilibc/src/main/native/cpp/hardware/accelerometer/ADXL345_I2C.cpp b/wpilibc/src/main/native/cpp/hardware/accelerometer/ADXL345_I2C.cpp index cab4b92e92..d83bffa2a3 100644 --- a/wpilibc/src/main/native/cpp/hardware/accelerometer/ADXL345_I2C.cpp +++ b/wpilibc/src/main/native/cpp/hardware/accelerometer/ADXL345_I2C.cpp @@ -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(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(port), deviceAddress), "ADXL345"); - wpi::util::SendableRegistry::Add(this, "ADXL345_I2C", port); + wpi::util::SendableRegistry::Add(this, "ADXL345_I2C", static_cast(port)); } I2C::Port ADXL345_I2C::GetI2CPort() const { diff --git a/wpilibc/src/main/native/cpp/simulation/ADXL345Sim.cpp b/wpilibc/src/main/native/cpp/simulation/ADXL345Sim.cpp index c09feed64d..8f7c514da7 100644 --- a/wpilibc/src/main/native/cpp/simulation/ADXL345Sim.cpp +++ b/wpilibc/src/main/native/cpp/simulation/ADXL345Sim.cpp @@ -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(accel.GetI2CPort()), accel.GetI2CDeviceAddress()}; m_simX = deviceSim.GetDouble("x"); m_simY = deviceSim.GetDouble("y"); diff --git a/wpilibc/src/main/native/include/wpi/hardware/bus/I2C.hpp b/wpilibc/src/main/native/include/wpi/hardware/bus/I2C.hpp index 7826cc39df..bf43e8c6b6 100644 --- a/wpilibc/src/main/native/include/wpi/hardware/bus/I2C.hpp +++ b/wpilibc/src/main/native/include/wpi/hardware/bus/I2C.hpp @@ -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 m_port; + wpi::hal::Handle m_port; int m_deviceAddress; }; diff --git a/wpilibcExamples/src/main/cpp/snippets/ADXLAccelerometers/cpp/Robot.cpp b/wpilibcExamples/src/main/cpp/snippets/ADXLAccelerometers/cpp/Robot.cpp index b77e91de58..6d29f4554d 100644 --- a/wpilibcExamples/src/main/cpp/snippets/ADXLAccelerometers/cpp/Robot.cpp +++ b/wpilibcExamples/src/main/cpp/snippets/ADXLAccelerometers/cpp/Robot.cpp @@ -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}; }; diff --git a/wpilibcExamples/src/main/cpp/snippets/I2CCommunication/include/Robot.hpp b/wpilibcExamples/src/main/cpp/snippets/I2CCommunication/include/Robot.hpp index 2f8877da8f..abfbe31a73 100644 --- a/wpilibcExamples/src/main/cpp/snippets/I2CCommunication/include/Robot.hpp +++ b/wpilibcExamples/src/main/cpp/snippets/I2CCommunication/include/Robot.hpp @@ -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; diff --git a/wpilibj/src/main/java/org/wpilib/hardware/accelerometer/ADXL345_I2C.java b/wpilibj/src/main/java/org/wpilib/hardware/accelerometer/ADXL345_I2C.java index 3b2759dc2f..ae8f06b24b 100644 --- a/wpilibj/src/main/java/org/wpilib/hardware/accelerometer/ADXL345_I2C.java +++ b/wpilibj/src/main/java/org/wpilib/hardware/accelerometer/ADXL345_I2C.java @@ -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(); } diff --git a/wpilibj/src/main/java/org/wpilib/hardware/bus/I2C.java b/wpilibj/src/main/java/org/wpilib/hardware/bus/I2C.java index 9f88de06a4..4e7cb88e71 100644 --- a/wpilibj/src/main/java/org/wpilib/hardware/bus/I2C.java +++ b/wpilibj/src/main/java/org/wpilib/hardware/bus/I2C.java @@ -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; } /* diff --git a/wpilibj/src/main/java/org/wpilib/simulation/ADXL345Sim.java b/wpilibj/src/main/java/org/wpilib/simulation/ADXL345Sim.java index 2eca04cb86..4f2d8278f3 100644 --- a/wpilibj/src/main/java/org/wpilib/simulation/ADXL345Sim.java +++ b/wpilibj/src/main/java/org/wpilib/simulation/ADXL345Sim.java @@ -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); } diff --git a/wpilibj/src/test/java/org/wpilib/simulation/ADXL345SimTest.java b/wpilibj/src/test/java/org/wpilib/simulation/ADXL345SimTest.java index afd7d6a960..d10bbe716f 100644 --- a/wpilibj/src/test/java/org/wpilib/simulation/ADXL345SimTest.java +++ b/wpilibj/src/test/java/org/wpilib/simulation/ADXL345SimTest.java @@ -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); diff --git a/wpilibjExamples/src/main/java/org/wpilib/snippets/adxlaccelerometers/Robot.java b/wpilibjExamples/src/main/java/org/wpilib/snippets/adxlaccelerometers/Robot.java index 3333d99f86..3439b54e59 100644 --- a/wpilibjExamples/src/main/java/org/wpilib/snippets/adxlaccelerometers/Robot.java +++ b/wpilibjExamples/src/main/java/org/wpilib/snippets/adxlaccelerometers/Robot.java @@ -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() {} diff --git a/wpilibjExamples/src/main/java/org/wpilib/snippets/i2ccommunication/Robot.java b/wpilibjExamples/src/main/java/org/wpilib/snippets/i2ccommunication/Robot.java index ae2618fdcc..fc64c7de54 100644 --- a/wpilibjExamples/src/main/java/org/wpilib/snippets/i2ccommunication/Robot.java +++ b/wpilibjExamples/src/main/java/org/wpilib/snippets/i2ccommunication/Robot.java @@ -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);