10
10
import edu .wpi .first .hal .SimDevice ;
11
11
import edu .wpi .first .hal .SimDouble ;
12
12
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
+
18
19
import java .nio .ByteBuffer ;
19
20
import java .nio .ByteOrder ;
20
21
26
27
* WPILib Known Issues</a> page for details.
27
28
*/
28
29
@ SuppressWarnings ("TypeName" )
29
- public class ADXL345_I2C implements NTSendable , AutoCloseable {
30
+ public class ADXL345_I2C implements SendableSerializable , AutoCloseable {
30
31
/** Default I2C device address. */
31
32
public static final byte kAddress = 0x1D ;
32
33
@@ -77,7 +78,7 @@ public enum Axes {
77
78
78
79
/** Container type for accelerations from all axes. */
79
80
@ SuppressWarnings ("MemberName" )
80
- public static class AllAxes {
81
+ public static class AllAxes implements StructSerializable {
81
82
/** Acceleration along the X axis in g-forces. */
82
83
public double XAxis ;
83
84
@@ -89,6 +90,46 @@ public static class AllAxes {
89
90
90
91
/** Default constructor. */
91
92
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 ();
92
133
}
93
134
94
135
private I2C m_i2c ;
@@ -141,7 +182,6 @@ public ADXL345_I2C(I2C.Port port, Range range, int deviceAddress) {
141
182
setRange (range );
142
183
143
184
HAL .report (tResourceType .kResourceType_ADXL345 , tInstances .kADXL345_I2C );
144
- SendableRegistry .addLW (this , "ADXL345_I2C" , port .value );
145
185
}
146
186
147
187
/**
@@ -164,7 +204,6 @@ public int getDeviceAddress() {
164
204
165
205
@ Override
166
206
public void close () {
167
- SendableRegistry .remove (this );
168
207
if (m_i2c != null ) {
169
208
m_i2c .close ();
170
209
m_i2c = null ;
@@ -283,21 +322,28 @@ public AllAxes getAccelerations() {
283
322
return data ;
284
323
}
285
324
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
+ }
302
345
}
346
+
347
+ /** Sendable for serialization. */
348
+ public static final ADXL345I2CSendable sendable = new ADXL345I2CSendable ();
303
349
}
0 commit comments