Skip to content

Commit edf720d

Browse files
authored
Optimize smart value implementation (#75)
* BOOM OPTIMIZED (smart number) * BOOM OPTIMIZED * BOOM OPTIMIZED
1 parent f14baf6 commit edf720d

File tree

4 files changed

+22
-260
lines changed

4 files changed

+22
-260
lines changed

src/com/stuypulse/stuylib/network/SmartAngle.java

-142
This file was deleted.

src/com/stuypulse/stuylib/network/SmartBoolean.java

+7-39
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66

77
import com.stuypulse.stuylib.streams.booleans.BStream;
88

9-
import edu.wpi.first.networktables.BooleanEntry;
10-
import edu.wpi.first.networktables.BooleanTopic;
11-
import edu.wpi.first.networktables.NetworkTableInstance;
9+
import edu.wpi.first.networktables.NetworkTablesJNI;
1210
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
1311

1412
/**
@@ -21,41 +19,11 @@
2119
public class SmartBoolean implements BStream {
2220

2321
/** The ID / Name for the value on {@link SmartDashboard}. */
24-
private final BooleanEntry mEntry;
22+
private final int mHandle;
2523

2624
/** The default value that the {@link SmartDashboard} value was set too. */
2725
private final boolean mDefaultValue;
2826

29-
/**
30-
* Creates a {@link SmartBoolean} with a BooleanEntry instead of a value for {@link
31-
* SmartDashboard}. This allows you to put items on things like {@link
32-
* edu.wpi.first.wpilibj.shuffleboard.Shuffleboard}, without having to use a raw {@link
33-
* BooleanEntry}.
34-
*
35-
* @param entry the {@link BooleanEntry} the {@link SmartBoolean} should be set to.
36-
* @param value the default value of the {@link SmartBoolean}
37-
*/
38-
public SmartBoolean(BooleanEntry entry, boolean value) {
39-
mEntry = entry;
40-
mDefaultValue = value;
41-
entry.setDefault(value);
42-
}
43-
44-
/**
45-
* Creates a {@link SmartBoolean} with a BooleanTopic instead of a value for {@link
46-
* SmartDashboard}. This allows you to put items on things like {@link
47-
* edu.wpi.first.wpilibj.shuffleboard.Shuffleboard}, without having to use a raw {@link
48-
* BooleanTopic}.
49-
*
50-
* @param topic the {@link BooleanTopic} the {@link SmartBoolean} should be set to.
51-
* @param value the default value of the {@link SmartBoolean}
52-
*/
53-
public SmartBoolean(BooleanTopic topic, boolean value) {
54-
mEntry = topic.getEntry(value);
55-
mDefaultValue = value;
56-
mEntry.setDefault(value);
57-
}
58-
5927
/**
6028
* Creates a {@link SmartBoolean} with the element name and a default value. The value on {@link
6129
* SmartDashboard} will be reset to the default value on initialization.
@@ -64,14 +32,14 @@ public SmartBoolean(BooleanTopic topic, boolean value) {
6432
* @param value the default / initialization value for the value
6533
*/
6634
public SmartBoolean(String id, boolean value) {
67-
this(
68-
NetworkTableInstance.getDefault().getTable("SmartDashboard").getBooleanTopic(id),
69-
value);
35+
mHandle = NetworkTablesJNI.getEntry(NetworkTablesJNI.getDefaultInstance(), "SmartDashboard/" + id);
36+
mDefaultValue = value;
37+
reset();
7038
}
7139

7240
/** @return the value of the boolean from {@link SmartDashboard} */
7341
public boolean get() {
74-
return mEntry.get(mDefaultValue);
42+
return NetworkTablesJNI.getBoolean(mHandle, mDefaultValue);
7543
}
7644

7745
/** @return the default value of the boolean */
@@ -81,7 +49,7 @@ public boolean getDefault() {
8149

8250
/** @param value what the value on {@link SmartDashboard} will be set to */
8351
public void set(boolean value) {
84-
mEntry.set(value);
52+
NetworkTablesJNI.setBoolean(mHandle, 0, value);
8553
}
8654

8755
/** Resets the value on {@link SmartDashboard} to the default value */

src/com/stuypulse/stuylib/network/SmartNumber.java

+8-40
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66

77
import com.stuypulse.stuylib.streams.IStream;
88

9-
import edu.wpi.first.networktables.DoubleEntry;
10-
import edu.wpi.first.networktables.DoubleTopic;
11-
import edu.wpi.first.networktables.NetworkTableInstance;
9+
import edu.wpi.first.networktables.NetworkTablesJNI;
1210
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
1311

1412
/**
@@ -18,46 +16,16 @@
1816
*
1917
* @author Sam (sam.belliveau@gmail.com)
2018
*/
21-
public class SmartNumber extends Number implements IStream {
19+
public final class SmartNumber extends Number implements IStream {
2220

2321
private static final long serialVersionUID = 1L;
2422

2523
/** The ID / Name for the value on {@link SmartDashboard}. */
26-
private final DoubleEntry mEntry;
24+
private final int mHandle;
2725

2826
/** The default value that the {@link SmartDashboard} value was set too. */
2927
private final double mDefaultValue;
3028

31-
/**
32-
* Creates a {@link SmartNumber} with a DoubleEntry instead of a value for {@link
33-
* SmartDashboard}. This allows you to put items on things like {@link
34-
* edu.wpi.first.wpilibj.shuffleboard.Shuffleboard}, without having to use a raw {@link
35-
* DoubleEntry}.
36-
*
37-
* @param entry the {@link DoubleEntry} the {@link SmartNumber} should be set to.
38-
* @param value the default value of the {@link SmartNumber}
39-
*/
40-
public SmartNumber(DoubleEntry entry, double value) {
41-
mEntry = entry;
42-
mDefaultValue = value;
43-
mEntry.setDefault(value);
44-
}
45-
46-
/**
47-
* Creates a {@link SmartNumber} with a DoubleTopic instead of a value for {@link
48-
* SmartDashboard}. This allows you to put items on things like {@link
49-
* edu.wpi.first.wpilibj.shuffleboard.Shuffleboard}, without having to use a raw {@link
50-
* DoubleTopic}.
51-
*
52-
* @param topic the {@link DoubleTopic} the {@link SmartNumber} should be set to.
53-
* @param value the default value of the {@link SmartNumber}
54-
*/
55-
public SmartNumber(DoubleTopic topic, double value) {
56-
mEntry = topic.getEntry(value);
57-
mDefaultValue = value;
58-
mEntry.setDefault(value);
59-
}
60-
6129
/**
6230
* Creates a SmartNumber with the element name and a default value. The value on {@link
6331
* SmartDashboard} will be reset to the default value on initialization.
@@ -66,14 +34,14 @@ public SmartNumber(DoubleTopic topic, double value) {
6634
* @param value the default / initialization value for the value
6735
*/
6836
public SmartNumber(String id, double value) {
69-
this(
70-
NetworkTableInstance.getDefault().getTable("SmartDashboard").getDoubleTopic(id),
71-
value);
37+
mHandle = NetworkTablesJNI.getEntry(NetworkTablesJNI.getDefaultInstance(), "SmartDashboard/" + id);
38+
mDefaultValue = value;
39+
reset();
7240
}
7341

7442
/** @return the value of the number from SmartDashboard */
7543
public double get() {
76-
return mEntry.get();
44+
return NetworkTablesJNI.getDouble(mHandle, mDefaultValue);
7745
}
7846

7947
/** @return the default value of the number */
@@ -83,7 +51,7 @@ public double getDefault() {
8351

8452
/** @param value what the value on {@link SmartDashboard} will be set to */
8553
public void set(Number value) {
86-
mEntry.set(value.doubleValue());
54+
NetworkTablesJNI.setDouble(mHandle, 0, value.doubleValue());
8755
}
8856

8957
/** Resets the value on {@link SmartDashboard} to the default value */

src/com/stuypulse/stuylib/network/SmartString.java

+7-39
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44

55
package com.stuypulse.stuylib.network;
66

7-
import edu.wpi.first.networktables.NetworkTableInstance;
8-
import edu.wpi.first.networktables.StringEntry;
9-
import edu.wpi.first.networktables.StringTopic;
7+
import edu.wpi.first.networktables.NetworkTablesJNI;
108
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
119
import java.util.function.Supplier;
1210

@@ -20,41 +18,11 @@
2018
public class SmartString implements Supplier<String> {
2119

2220
/** The ID / Name for the value on {@link SmartDashboard}. */
23-
private final StringEntry mEntry;
21+
private final int mHandle;
2422

2523
/** The default value that the {@link SmartDashboard} value was set too. */
2624
private final String mDefaultValue;
2725

28-
/**
29-
* Creates a {@link SmartString} with a StringEntry instead of a value for {@link
30-
* SmartDashboard}. This allows you to put items on things like {@link
31-
* edu.wpi.first.wpilibj.shuffleboard.Shuffleboard}, without having to use a raw {@link
32-
* StringEntry}.
33-
*
34-
* @param entry the {@link StringEntry} the {@link SmartString} should be set to.
35-
* @param value the default value of the {@link SmartString}
36-
*/
37-
public SmartString(StringEntry entry, String value) {
38-
mEntry = entry;
39-
mDefaultValue = value;
40-
mEntry.setDefault(value);
41-
}
42-
43-
/**
44-
* Creates a {@link SmartString} with a StringTopic instead of a value for {@link
45-
* SmartDashboard}. This allows you to put items on things like {@link
46-
* edu.wpi.first.wpilibj.shuffleboard.Shuffleboard}, without having to use a raw {@link
47-
* StringTopic}.
48-
*
49-
* @param topic the {@link StringTopic} the {@link SmartString} should be set to.
50-
* @param value the default value of the {@link SmartString}
51-
*/
52-
public SmartString(StringTopic topic, String value) {
53-
mEntry = topic.getEntry(value);
54-
mDefaultValue = value;
55-
mEntry.setDefault(value);
56-
}
57-
5826
/**
5927
* Creates a SmartString with the element name and a default value. The value on {@link
6028
* SmartDashboard} will be reset to the default value on initialization.
@@ -63,14 +31,14 @@ public SmartString(StringTopic topic, String value) {
6331
* @param value the default / initialization value for the value
6432
*/
6533
public SmartString(String id, String value) {
66-
this(
67-
NetworkTableInstance.getDefault().getTable("SmartDashboard").getStringTopic(id),
68-
value);
34+
mHandle = NetworkTablesJNI.getEntry(NetworkTablesJNI.getDefaultInstance(), "SmartDashboard/" + value);
35+
mDefaultValue = value;
36+
reset();
6937
}
7038

7139
/** @return the value of the String from SmartDashboard */
7240
public String get() {
73-
return mEntry.get(mDefaultValue);
41+
return NetworkTablesJNI.getString(mHandle, mDefaultValue);
7442
}
7543

7644
/** @return the default value of the String */
@@ -80,7 +48,7 @@ public String getDefault() {
8048

8149
/** @param value what the value on {@link SmartDashboard} will be set to */
8250
public void set(String value) {
83-
mEntry.set(value);
51+
NetworkTablesJNI.setString(mHandle, 0, value);
8452
}
8553

8654
/** Resets the value on {@link SmartDashboard} to the default value */

0 commit comments

Comments
 (0)