Skip to content

Commit fa5655c

Browse files
committed
ADXL and ADXRS new Sendable implementations (Java)
1 parent a9f4438 commit fa5655c

File tree

4 files changed

+235
-83
lines changed

4 files changed

+235
-83
lines changed

wpilibj/src/main/java/edu/wpi/first/wpilibj/ADXL345_I2C.java

+71-25
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@
1010
import edu.wpi.first.hal.SimDevice;
1111
import edu.wpi.first.hal.SimDouble;
1212
import edu.wpi.first.hal.SimEnum;
13-
import edu.wpi.first.networktables.DoublePublisher;
14-
import edu.wpi.first.networktables.DoubleTopic;
15-
import edu.wpi.first.networktables.NTSendable;
16-
import edu.wpi.first.networktables.NTSendableBuilder;
17-
import edu.wpi.first.util.sendable.SendableRegistry;
13+
import edu.wpi.first.util.sendable2.Sendable;
14+
import edu.wpi.first.util.sendable2.SendableSerializable;
15+
import edu.wpi.first.util.sendable2.SendableTable;
16+
import edu.wpi.first.util.struct.Struct;
17+
import edu.wpi.first.util.struct.StructSerializable;
18+
1819
import java.nio.ByteBuffer;
1920
import java.nio.ByteOrder;
2021

@@ -26,7 +27,7 @@
2627
* WPILib Known Issues</a> page for details.
2728
*/
2829
@SuppressWarnings("TypeName")
29-
public class ADXL345_I2C implements NTSendable, AutoCloseable {
30+
public class ADXL345_I2C implements SendableSerializable, AutoCloseable {
3031
/** Default I2C device address. */
3132
public static final byte kAddress = 0x1D;
3233

@@ -77,7 +78,7 @@ public enum Axes {
7778

7879
/** Container type for accelerations from all axes. */
7980
@SuppressWarnings("MemberName")
80-
public static class AllAxes {
81+
public static class AllAxes implements StructSerializable {
8182
/** Acceleration along the X axis in g-forces. */
8283
public double XAxis;
8384

@@ -89,6 +90,46 @@ public static class AllAxes {
8990

9091
/** Default constructor. */
9192
public AllAxes() {}
93+
94+
public static class AllAxesStruct implements Struct<AllAxes> {
95+
@Override
96+
public Class<AllAxes> getTypeClass() {
97+
return AllAxes.class;
98+
}
99+
100+
@Override
101+
public String getTypeString() {
102+
return "ADXL345_I2C.AllAxes";
103+
}
104+
105+
@Override
106+
public int getSize() {
107+
return 24;
108+
}
109+
110+
@Override
111+
public String getSchema() {
112+
return "double XAxis;double YAxis;double ZAxis";
113+
}
114+
115+
@Override
116+
public AllAxes unpack(ByteBuffer bb) {
117+
AllAxes val = new AllAxes();
118+
val.XAxis = bb.getDouble();
119+
val.YAxis = bb.getDouble();
120+
val.ZAxis = bb.getDouble();
121+
return val;
122+
}
123+
124+
@Override
125+
public void pack(ByteBuffer bb, AllAxes value) {
126+
bb.putDouble(value.XAxis);
127+
bb.putDouble(value.YAxis);
128+
bb.putDouble(value.ZAxis);
129+
}
130+
}
131+
132+
public static final AllAxesStruct struct = new AllAxesStruct();
92133
}
93134

94135
private I2C m_i2c;
@@ -141,7 +182,6 @@ public ADXL345_I2C(I2C.Port port, Range range, int deviceAddress) {
141182
setRange(range);
142183

143184
HAL.report(tResourceType.kResourceType_ADXL345, tInstances.kADXL345_I2C);
144-
SendableRegistry.addLW(this, "ADXL345_I2C", port.value);
145185
}
146186

147187
/**
@@ -164,7 +204,6 @@ public int getDeviceAddress() {
164204

165205
@Override
166206
public void close() {
167-
SendableRegistry.remove(this);
168207
if (m_i2c != null) {
169208
m_i2c.close();
170209
m_i2c = null;
@@ -283,21 +322,28 @@ public AllAxes getAccelerations() {
283322
return data;
284323
}
285324

286-
@Override
287-
public void initSendable(NTSendableBuilder builder) {
288-
builder.setSmartDashboardType("3AxisAccelerometer");
289-
DoublePublisher pubX = new DoubleTopic(builder.getTopic("X")).publish();
290-
DoublePublisher pubY = new DoubleTopic(builder.getTopic("Y")).publish();
291-
DoublePublisher pubZ = new DoubleTopic(builder.getTopic("Z")).publish();
292-
builder.addCloseable(pubX);
293-
builder.addCloseable(pubY);
294-
builder.addCloseable(pubZ);
295-
builder.setUpdateTable(
296-
() -> {
297-
AllAxes data = getAccelerations();
298-
pubX.set(data.XAxis);
299-
pubY.set(data.YAxis);
300-
pubZ.set(data.ZAxis);
301-
});
325+
public static class ADXL345I2CSendable implements Sendable<ADXL345_I2C> {
326+
@Override
327+
public Class<ADXL345_I2C> getTypeClass() {
328+
return ADXL345_I2C.class;
329+
}
330+
331+
@Override
332+
public String getTypeString() {
333+
return "3AxisAccelerometer";
334+
}
335+
336+
@Override
337+
public boolean isClosed(ADXL345_I2C obj) {
338+
return obj.m_i2c == null;
339+
}
340+
341+
@Override
342+
public void initSendable(ADXL345_I2C obj, SendableTable table) {
343+
table.publishStruct("Value", AllAxes.struct, obj::getAccelerations);
344+
}
302345
}
346+
347+
/** Sendable for serialization. */
348+
public static final ADXL345I2CSendable sendable = new ADXL345I2CSendable();
303349
}

wpilibj/src/main/java/edu/wpi/first/wpilibj/ADXL345_SPI.java

+68-24
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,16 @@
1010
import edu.wpi.first.hal.SimDevice;
1111
import edu.wpi.first.hal.SimDouble;
1212
import edu.wpi.first.hal.SimEnum;
13-
import edu.wpi.first.networktables.DoublePublisher;
14-
import edu.wpi.first.networktables.DoubleTopic;
15-
import edu.wpi.first.networktables.NTSendable;
16-
import edu.wpi.first.networktables.NTSendableBuilder;
17-
import edu.wpi.first.util.sendable.SendableRegistry;
13+
import edu.wpi.first.util.sendable2.Sendable;
14+
import edu.wpi.first.util.sendable2.SendableSerializable;
15+
import edu.wpi.first.util.sendable2.SendableTable;
16+
import edu.wpi.first.util.struct.Struct;
1817
import java.nio.ByteBuffer;
1918
import java.nio.ByteOrder;
2019

2120
/** ADXL345 SPI Accelerometer. */
2221
@SuppressWarnings("TypeName")
23-
public class ADXL345_SPI implements NTSendable, AutoCloseable {
22+
public class ADXL345_SPI implements SendableSerializable, AutoCloseable {
2423
private static final int kPowerCtlRegister = 0x2D;
2524
private static final int kDataFormatRegister = 0x31;
2625
private static final int kDataRegister = 0x32;
@@ -84,6 +83,46 @@ public static class AllAxes {
8483

8584
/** Default constructor. */
8685
public AllAxes() {}
86+
87+
public static class AllAxesStruct implements Struct<AllAxes> {
88+
@Override
89+
public Class<AllAxes> getTypeClass() {
90+
return AllAxes.class;
91+
}
92+
93+
@Override
94+
public String getTypeString() {
95+
return "ADXL345_SPI.AllAxes";
96+
}
97+
98+
@Override
99+
public int getSize() {
100+
return 24;
101+
}
102+
103+
@Override
104+
public String getSchema() {
105+
return "double XAxis;double YAxis;double ZAxis";
106+
}
107+
108+
@Override
109+
public AllAxes unpack(ByteBuffer bb) {
110+
AllAxes val = new AllAxes();
111+
val.XAxis = bb.getDouble();
112+
val.YAxis = bb.getDouble();
113+
val.ZAxis = bb.getDouble();
114+
return val;
115+
}
116+
117+
@Override
118+
public void pack(ByteBuffer bb, AllAxes value) {
119+
bb.putDouble(value.XAxis);
120+
bb.putDouble(value.YAxis);
121+
bb.putDouble(value.ZAxis);
122+
}
123+
}
124+
125+
public static final AllAxesStruct struct = new AllAxesStruct();
87126
}
88127

89128
private SPI m_spi;
@@ -118,7 +157,6 @@ public ADXL345_SPI(SPI.Port port, Range range) {
118157
m_simZ = m_simDevice.createDouble("z", SimDevice.Direction.kInput, 0.0);
119158
}
120159
init(range);
121-
SendableRegistry.addLW(this, "ADXL345_SPI", port.value);
122160
}
123161

124162
/**
@@ -132,7 +170,6 @@ public int getPort() {
132170

133171
@Override
134172
public void close() {
135-
SendableRegistry.remove(this);
136173
if (m_spi != null) {
137174
m_spi.close();
138175
m_spi = null;
@@ -279,21 +316,28 @@ public ADXL345_SPI.AllAxes getAccelerations() {
279316
return data;
280317
}
281318

282-
@Override
283-
public void initSendable(NTSendableBuilder builder) {
284-
builder.setSmartDashboardType("3AxisAccelerometer");
285-
DoublePublisher pubX = new DoubleTopic(builder.getTopic("X")).publish();
286-
DoublePublisher pubY = new DoubleTopic(builder.getTopic("Y")).publish();
287-
DoublePublisher pubZ = new DoubleTopic(builder.getTopic("Z")).publish();
288-
builder.addCloseable(pubX);
289-
builder.addCloseable(pubY);
290-
builder.addCloseable(pubZ);
291-
builder.setUpdateTable(
292-
() -> {
293-
AllAxes data = getAccelerations();
294-
pubX.set(data.XAxis);
295-
pubY.set(data.YAxis);
296-
pubZ.set(data.ZAxis);
297-
});
319+
public static class ADXL345SPISendable implements Sendable<ADXL345_SPI> {
320+
@Override
321+
public Class<ADXL345_SPI> getTypeClass() {
322+
return ADXL345_SPI.class;
323+
}
324+
325+
@Override
326+
public String getTypeString() {
327+
return "3AxisAccelerometer";
328+
}
329+
330+
@Override
331+
public boolean isClosed(ADXL345_SPI obj) {
332+
return obj.m_spi == null;
333+
}
334+
335+
@Override
336+
public void initSendable(ADXL345_SPI obj, SendableTable table) {
337+
table.publishStruct("Value", AllAxes.struct, obj::getAccelerations);
338+
}
298339
}
340+
341+
/** Sendable for serialization. */
342+
public static final ADXL345SPISendable sendable = new ADXL345SPISendable();
299343
}

0 commit comments

Comments
 (0)