Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize smart value implementation #75

Merged
merged 3 commits into from
Feb 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 0 additions & 142 deletions src/com/stuypulse/stuylib/network/SmartAngle.java

This file was deleted.

46 changes: 7 additions & 39 deletions src/com/stuypulse/stuylib/network/SmartBoolean.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@

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

import edu.wpi.first.networktables.BooleanEntry;
import edu.wpi.first.networktables.BooleanTopic;
import edu.wpi.first.networktables.NetworkTableInstance;
import edu.wpi.first.networktables.NetworkTablesJNI;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;

/**
Expand All @@ -21,41 +19,11 @@
public class SmartBoolean implements BStream {

/** The ID / Name for the value on {@link SmartDashboard}. */
private final BooleanEntry mEntry;
private final int mHandle;

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

/**
* Creates a {@link SmartBoolean} with a BooleanEntry instead of a value for {@link
* SmartDashboard}. This allows you to put items on things like {@link
* edu.wpi.first.wpilibj.shuffleboard.Shuffleboard}, without having to use a raw {@link
* BooleanEntry}.
*
* @param entry the {@link BooleanEntry} the {@link SmartBoolean} should be set to.
* @param value the default value of the {@link SmartBoolean}
*/
public SmartBoolean(BooleanEntry entry, boolean value) {
mEntry = entry;
mDefaultValue = value;
entry.setDefault(value);
}

/**
* Creates a {@link SmartBoolean} with a BooleanTopic instead of a value for {@link
* SmartDashboard}. This allows you to put items on things like {@link
* edu.wpi.first.wpilibj.shuffleboard.Shuffleboard}, without having to use a raw {@link
* BooleanTopic}.
*
* @param topic the {@link BooleanTopic} the {@link SmartBoolean} should be set to.
* @param value the default value of the {@link SmartBoolean}
*/
public SmartBoolean(BooleanTopic topic, boolean value) {
mEntry = topic.getEntry(value);
mDefaultValue = value;
mEntry.setDefault(value);
}

/**
* Creates a {@link SmartBoolean} with the element name and a default value. The value on {@link
* SmartDashboard} will be reset to the default value on initialization.
Expand All @@ -64,14 +32,14 @@ public SmartBoolean(BooleanTopic topic, boolean value) {
* @param value the default / initialization value for the value
*/
public SmartBoolean(String id, boolean value) {
this(
NetworkTableInstance.getDefault().getTable("SmartDashboard").getBooleanTopic(id),
value);
mHandle = NetworkTablesJNI.getEntry(NetworkTablesJNI.getDefaultInstance(), "SmartDashboard/" + id);
mDefaultValue = value;
reset();
}

/** @return the value of the boolean from {@link SmartDashboard} */
public boolean get() {
return mEntry.get(mDefaultValue);
return NetworkTablesJNI.getBoolean(mHandle, mDefaultValue);
}

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

/** @param value what the value on {@link SmartDashboard} will be set to */
public void set(boolean value) {
mEntry.set(value);
NetworkTablesJNI.setBoolean(mHandle, 0, value);
}

/** Resets the value on {@link SmartDashboard} to the default value */
Expand Down
48 changes: 8 additions & 40 deletions src/com/stuypulse/stuylib/network/SmartNumber.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@

import com.stuypulse.stuylib.streams.IStream;

import edu.wpi.first.networktables.DoubleEntry;
import edu.wpi.first.networktables.DoubleTopic;
import edu.wpi.first.networktables.NetworkTableInstance;
import edu.wpi.first.networktables.NetworkTablesJNI;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;

/**
Expand All @@ -18,46 +16,16 @@
*
* @author Sam ([email protected])
*/
public class SmartNumber extends Number implements IStream {
public final class SmartNumber extends Number implements IStream {

private static final long serialVersionUID = 1L;

/** The ID / Name for the value on {@link SmartDashboard}. */
private final DoubleEntry mEntry;
private final int mHandle;

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

/**
* Creates a {@link SmartNumber} with a DoubleEntry instead of a value for {@link
* SmartDashboard}. This allows you to put items on things like {@link
* edu.wpi.first.wpilibj.shuffleboard.Shuffleboard}, without having to use a raw {@link
* DoubleEntry}.
*
* @param entry the {@link DoubleEntry} the {@link SmartNumber} should be set to.
* @param value the default value of the {@link SmartNumber}
*/
public SmartNumber(DoubleEntry entry, double value) {
mEntry = entry;
mDefaultValue = value;
mEntry.setDefault(value);
}

/**
* Creates a {@link SmartNumber} with a DoubleTopic instead of a value for {@link
* SmartDashboard}. This allows you to put items on things like {@link
* edu.wpi.first.wpilibj.shuffleboard.Shuffleboard}, without having to use a raw {@link
* DoubleTopic}.
*
* @param topic the {@link DoubleTopic} the {@link SmartNumber} should be set to.
* @param value the default value of the {@link SmartNumber}
*/
public SmartNumber(DoubleTopic topic, double value) {
mEntry = topic.getEntry(value);
mDefaultValue = value;
mEntry.setDefault(value);
}

/**
* Creates a SmartNumber with the element name and a default value. The value on {@link
* SmartDashboard} will be reset to the default value on initialization.
Expand All @@ -66,14 +34,14 @@ public SmartNumber(DoubleTopic topic, double value) {
* @param value the default / initialization value for the value
*/
public SmartNumber(String id, double value) {
this(
NetworkTableInstance.getDefault().getTable("SmartDashboard").getDoubleTopic(id),
value);
mHandle = NetworkTablesJNI.getEntry(NetworkTablesJNI.getDefaultInstance(), "SmartDashboard/" + id);
mDefaultValue = value;
reset();
}

/** @return the value of the number from SmartDashboard */
public double get() {
return mEntry.get();
return NetworkTablesJNI.getDouble(mHandle, mDefaultValue);
}

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

/** @param value what the value on {@link SmartDashboard} will be set to */
public void set(Number value) {
mEntry.set(value.doubleValue());
NetworkTablesJNI.setDouble(mHandle, 0, value.doubleValue());
}

/** Resets the value on {@link SmartDashboard} to the default value */
Expand Down
46 changes: 7 additions & 39 deletions src/com/stuypulse/stuylib/network/SmartString.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

package com.stuypulse.stuylib.network;

import edu.wpi.first.networktables.NetworkTableInstance;
import edu.wpi.first.networktables.StringEntry;
import edu.wpi.first.networktables.StringTopic;
import edu.wpi.first.networktables.NetworkTablesJNI;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import java.util.function.Supplier;

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

/** The ID / Name for the value on {@link SmartDashboard}. */
private final StringEntry mEntry;
private final int mHandle;

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

/**
* Creates a {@link SmartString} with a StringEntry instead of a value for {@link
* SmartDashboard}. This allows you to put items on things like {@link
* edu.wpi.first.wpilibj.shuffleboard.Shuffleboard}, without having to use a raw {@link
* StringEntry}.
*
* @param entry the {@link StringEntry} the {@link SmartString} should be set to.
* @param value the default value of the {@link SmartString}
*/
public SmartString(StringEntry entry, String value) {
mEntry = entry;
mDefaultValue = value;
mEntry.setDefault(value);
}

/**
* Creates a {@link SmartString} with a StringTopic instead of a value for {@link
* SmartDashboard}. This allows you to put items on things like {@link
* edu.wpi.first.wpilibj.shuffleboard.Shuffleboard}, without having to use a raw {@link
* StringTopic}.
*
* @param topic the {@link StringTopic} the {@link SmartString} should be set to.
* @param value the default value of the {@link SmartString}
*/
public SmartString(StringTopic topic, String value) {
mEntry = topic.getEntry(value);
mDefaultValue = value;
mEntry.setDefault(value);
}

/**
* Creates a SmartString with the element name and a default value. The value on {@link
* SmartDashboard} will be reset to the default value on initialization.
Expand All @@ -63,14 +31,14 @@ public SmartString(StringTopic topic, String value) {
* @param value the default / initialization value for the value
*/
public SmartString(String id, String value) {
this(
NetworkTableInstance.getDefault().getTable("SmartDashboard").getStringTopic(id),
value);
mHandle = NetworkTablesJNI.getEntry(NetworkTablesJNI.getDefaultInstance(), "SmartDashboard/" + value);
mDefaultValue = value;
reset();
}

/** @return the value of the String from SmartDashboard */
public String get() {
return mEntry.get(mDefaultValue);
return NetworkTablesJNI.getString(mHandle, mDefaultValue);
}

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

/** @param value what the value on {@link SmartDashboard} will be set to */
public void set(String value) {
mEntry.set(value);
NetworkTablesJNI.setString(mHandle, 0, value);
}

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