I actually wrote a little extension for my team that replaces Label and can read values from a NetworkTable, inserting them into a string. I don't know if C++ has a NetworkTable implementation, but if so this might be helpful.
Most of the properties are self-explanatory, but two things you want to keep in mind:
- Network Table is the name of the chosen network table. To access it on the robot side, use NetworkTable.getTable("My table name"). The robot-end and SmartDashboard-end names must be the same for this to work.
- The format string acts like a normal label string, except that everything within % symbols is treated as a key, and gets replaced with the contents of that NetworkTable entry. For example, a format string "Shooter speed: %shooter.rpm%rpm" will display as "Shooter speed: 50000rpm" when your motor is about to explode.
And now, the code:
Code:
package storm.smartdashboard;
import edu.wpi.first.smartdashboard.gui.StaticWidget;
import edu.wpi.first.smartdashboard.properties.*;
import edu.wpi.first.wpilibj.networking.NetworkTable;
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JLabel;
/**
* @author Joe Doyle (Ginto8)
*/
public class SmartLabel extends StaticWidget {
public static final String NAME = "Smart Label";
private static NetworkTable SMARTDASHBOARD_FEEDBACK;
private static String format(String s, boolean plusPositive) {
String ret = "";
int start = 0, end = 0;
while (true) {
start = s.indexOf("%", end);
if (start < 0) {
break;
}
ret += s.substring(end, start);
end = s.indexOf("%", start + 1);
if (end < 0) {
end = start;
break;
}
String key = s.substring(start + 1, end);
Object value = SMARTDASHBOARD_FEEDBACK.getValue(key, null);
if (value == null) {
++end;
ret += s.substring(start, end);
continue;
}
String out = value.toString();
if (plusPositive) {
Double asDouble = (Double) value;
Integer asInteger = (Integer) value;
if (value instanceof Double && asDouble.doubleValue() >= 0) {
out = "+" + out;
} else if (value instanceof Integer && asInteger.intValue() >= 0) {
out = "+" + out;
}
}
ret += out;
++end;
start = end;
}
ret += s.substring(end);
return ret;
}
public final StringProperty netTable = new StringProperty(this, "Network Table","Feedback");
public final StringProperty text = new StringProperty(this, "Format String", "%DEFAULT%");
public final DoubleProperty fontSize = new DoubleProperty(this,"Font Size",12);
public final BooleanProperty plusPositive = new BooleanProperty(this,"Always show sign",false);
public final MultiProperty horizontal = new MultiProperty(this, "Horizontal Alignment");
public final MultiProperty vertical = new MultiProperty(this, "Vertical Alignment");
public final ColorProperty textColor = new ColorProperty(this,"Text Color",Color.black);
private volatile JLabel label;
Thread updateThread;
public SmartLabel() {
this.horizontal.add("Left", Integer.valueOf(2));
this.horizontal.add("Center", Integer.valueOf(0));
this.horizontal.add("Right", Integer.valueOf(4));
this.horizontal.setDefault("Center");
this.vertical.add("Up", Integer.valueOf(1));
this.vertical.add("Center", Integer.valueOf(0));
this.vertical.add("Down", Integer.valueOf(3));
this.vertical.setDefault("Center");
updateThread = new Thread() {
@Override
public void run() {
while(true) {
update();
try {
Thread.sleep(1000 / 5);
} catch (InterruptedException ex) {
}
}
}
};
}
private synchronized void update() {
this.label.setText(format(this.text.getValue(),plusPositive.getValue()));
}
@Override
public void init() {
setLayout(new BorderLayout());
SMARTDASHBOARD_FEEDBACK = NetworkTable.getTable(netTable.getValue());
this.label = new JLabel(format(this.text.getValue(),plusPositive.getValue()));
this.label.setHorizontalAlignment(((Integer) this.horizontal.getValue()).intValue());
this.label.setVerticalAlignment(((Integer) this.vertical.getValue()).intValue());
this.label.setFont(label.getFont().deriveFont(fontSize.getValue().floatValue()));
this.label.setForeground(this.textColor.getValue());
add(this.label, "Center");
updateThread.start();
}
@Override
public void propertyChanged(Property property) {
if (property == this.text || property == plusPositive) {
update();
} else if(property == this.fontSize) {
this.label.setFont(label.getFont().deriveFont(fontSize.getValue().floatValue()));
} else if (property == this.horizontal) {
this.label.setHorizontalAlignment(((Integer) this.horizontal.getValue()).intValue());
} else if (property == this.vertical) {
this.label.setVerticalAlignment(((Integer) this.vertical.getValue()).intValue());
} else if(property == this.textColor) {
this.label.setForeground(this.textColor.getValue());
} else if(property == this.netTable) {
SMARTDASHBOARD_FEEDBACK = NetworkTable.getTable(netTable.getValue());
}
}
}